-
Hajipur, Bihar, 844101
Displaying information on the screen is one of the most basic tasks in programming. In C, output refers to sending data from the program to the user or an external device, usually through the console or terminal. Understanding output in C is important because it allows you to communicate results, debug programs, and interact with users.
This chapter will guide you through different ways of producing output in C, including standard functions, format specifiers, and best practices for readable results.
Output in C is the process of displaying data generated by a program to the user. The most common form of output is text on the screen, but C can also handle other forms, like writing to files. Output is essential because programs are often meant to provide results or information, not just perform calculations silently.
The standard library in C, stdio.h, provides built-in functions to handle output efficiently.
printf() FunctionThe most commonly used function for output in C is printf(). It comes from the Standard Input/Output library (stdio.h) and allows you to display text, numbers, and other types of data on the console.
printf("format string", arguments);
format string: A string containing text and format specifiers.
arguments: Values or variables that will replace the format specifiers.
Example:
#include <stdio.h>
int main() {
int age = 20;
printf("I am %d years old.", age);
return 0;
}
Here, %d is a format specifier for an integer. The age variable replaces %d in the output.
Format specifiers tell printf() what type of data you want to display. Some commonly used ones include:
%d or %i — integer
%f — float or double
%c — character
%s — string
%u — unsigned integer
%x — hexadecimal integer
%% — to display the % symbol
Example:
#include <stdio.h>
int main() {
int a = 10;
float b = 5.5;
char c = 'A';
char name[] = "Lina";
printf("Integer: %d\n", a);
printf("Float: %.2f\n", b); // two decimal places
printf("Character: %c\n", c);
printf("Name: %s\n", name);
return 0;
}
The output will be:
Integer: 10
Float: 5.50
Character: A
Name: Lina
C provides escape sequences to control how the output appears. Some commonly used escape sequences:
\n — new line
\t — horizontal tab
\\ — backslash
\" — double quote
Example:
printf("Hello\nWorld");
Output:
Hello
World
You can print multiple variables in a single printf() statement by adding multiple format specifiers:
int a = 5, b = 10;
printf("a = %d, b = %d", a, b);
Output:
a = 5, b = 10
The order of the variables in the argument list must match the order of the format specifiers.
puts() FunctionAnother function used for output is puts(). It is simpler than printf() but has some limitations:
It automatically adds a newline at the end of the string.
It cannot print formatted text or variables; only strings.
Example:
#include <stdio.h>
int main() {
puts("Hello, world!");
return 0;
}
Output:
Hello, world!
puts() is useful when you just need to display text quickly without formatting.
putchar() FunctionThe putchar() function is used to print a single character:
#include <stdio.h>
int main() {
char ch = 'B';
putchar(ch);
return 0;
}
Output:
B
putchar() is often used in loops to display characters one by one, for example when printing arrays or patterns.
Use clear and descriptive messages:
When displaying results, make it easy for users to understand what the numbers or text represent.
printf("Total marks obtained: %d\n", totalMarks);
Format output properly:
Align numbers, use tabs or spaces, and control decimal places for floating-point numbers.
printf("Price: %.2f\n", price);
Keep code readable:
Break long output statements into multiple lines for readability.
Use comments:
Explain complex output logic with comments so future readers understand your intentions.
#include <stdio.h>
int main() {
char name[] = "Maya";
int age = 18;
float marks = 88.5;
// Using printf()
printf("Name: %s\n", name);
printf("Age: %d\n", age);
printf("Marks: %.2f\n", marks);
// Using puts()
puts("This is a simple message using puts.");
// Using putchar()
putchar('X');
putchar('\n');
return 0;
}
Output:
Name: Maya
Age: 18
Marks: 88.50
This is a simple message using puts.
X
This example demonstrates different ways to output information in C.
Output in C is primarily handled through printf(), puts(), and putchar(). Understanding format specifiers, escape sequences, and proper formatting is essential to produce readable and user-friendly results. Practicing output operations will help you become comfortable with displaying variables, text, and structured information on the screen.
What is the purpose of the printf() function in C?
Write a printf() statement to display the value of a float variable price with two decimal places.
Explain the difference between puts() and printf().
What does the %c format specifier represent in C?
Write a C program using putchar() to display each character of the string "Lina" on a separate line.
What will be the output of the following code?
printf("Hello\tWorld\nC Programming");
How does C handle escape sequences like \n and \t in output?
Write a printf() statement that displays: Score = 95 where 95 comes from an integer variable named score.
Why is it important to format output properly in a C program?
Explain the difference between printing multiple variables in a single printf() versus multiple printf() statements.