In C language, you need to compute the results and then output the results to the console. In order to do that we need specific built-in functions from C library.
The most common library function is printf()
, putchar()
and puts()
. In this article we will discuss each one of them in details and see some examples. Note that the above three functions belong to the stdio.h
header file which is part of the standard C library.
Output with printf()
The printf() function is the most popular and common way to output the results of a C program to console. It is a built-in function from standard C library.
Syntax for printf():-
The syntax for printf() is very simple and given below. You can just printf a simple statement like the following.
For example,
printf("");
Or
printf("Hello World!");
Print a variable using the following syntax. For example,
printf("%d", result);
The second syntax example has %d as the format specifier that says we are going to print an integer value and the name result is an integer that has some integer value.
you can have more than one variables in the printf() statement, however, the format specifier given should match with the variable data type in the order they are given.
printf( "int specifier float specifier char specifier",
integer variable, float variable, char variable);
In the above example first in the format specifier is an integer type , then variable listing should have an integer first, the second format is float , then the variable listing should be float and so on.
Format specifiers
There are many format specifiers in C language, but most commonly used are
- %d for integers
- %f for floats
- %c for character
- %s for strings of character like a name.
Plus, or minus sign for outputs
You can use more details between % and ‘d’,’f’,’c’ or ‘s’ to describe how you want to display your output.
For example,
printf("%+d", result);
printf("%-d",result);
The result will contain a plus or a minus in front of integer result.
+2344 or – 2334
Right or Left justification of output
You can insert a digit in between % and ‘d’ and it will display result left or right justified to the console. For example, a positive value will right justify and a negative value will left justify.
printf("%10d",result);
The above will justify 10 space to the right.
The negative value above will justify 10 space to the left if space is available.
Precision control
When you are working with float, the output will be a real value with decimal places depending on your system – 32 bit or 64 bit. You can control the decimal places with precision control. For example,
printf("%.2f",result);
In the above example,
- .2 will limit the decimal places to 2 digits.
- f indicates that this is a float type variable
- result is the float type variable.
The output is 23.454566, the output will be 23.45 after the .2 is applied limiting the decimal to 2 digits.
printf("%10.2f",result);
The above will result in
- 10 place right justified
- .2 limit decimal place to 2 digits
- Variable is float type
- Result is the variable of float type
New Line and tab
New line (\n) and (\t) are frequently used with printf() statement. The newline will print the output and begin the cursor in a new line, ready to print the next output statement.
a = 10
b = 20
printf("%d\n",a);
printf("%d\n",b);
Will give following output.
10 20
But the with new line (\n) you get each output in its own line.
printf("%d\n",a);
printf("%d\n",b);
Will print following.
10
20
The tab(\t) will move the output to one or more tab space to right depending on how many time you have used it in the printf() statement.
printf("%d\t",result); /* one time tab */
printf("%d\t\t\t\t",result); /* 4 time tab */
Example Program
The example program below will demonstrate all the concepts we discussed above.
/* Program to demonstrate the printf statement with different format specifiers */
#include <stdio.h>
int main() {
int number1, number2 , result;
float number3,number4,result2;
/* initialize variables */
number1 = 200;
number2 = 300;
number3 = 2.5;
number4 = 5.2;
result = number1 + number2;
result2 = number3 + number4;
/* Normal output */
printf("Normal Result\n");
printf("%d\n",result);
/* Result with + or - sign */
printf("Result with plus sign\n"); printf("%+f\n",result2);
/* Result justified */
printf("Result 10 space right justified\n");
printf("%10d\n",result);
/*result justified to 12 place right and precision set to 3 digits */
printf("Result 12 place justified and precision set to 3 digits\n");
printf("%12.3f\n",result2);
/* printf with newline and tab */
printf("result with tab\n");
printf("\t%d",result);
printf("\n");
printf("result with tab \n");
printf("\t%f",result2);
printf("\n\n");
system("pause");
return 0;
}
Output- printf()
Normal Result
500
Result with plus sign
+7.700000
Result 10 place right justified
500
Result 12 place justified and precision set to 3digits
7.700
result with tab
500
result with tab
7.700000
Press any key to continue . . .
Output with putchar()
The putchar() is also a library function belongs to stdio which output a single character to the console.The putchar() need a single argument to print it on screen.
Syntax:-
int x = '5';
putchar(x);
The above statement is incorrect because the datatype is an integer.
char x = '5';
putchar(x);
The above statement will give output 5 which is not an integer but a character.Check the example program below.
/* program to print a single character using putchar() function */
#include <stdio.h>
#include <stdlib.h>
int main()
{
char x = '5';
putchar(x);
putchar('\n');
system("PAUSE");
return 0;
}
Output – putchar()
5
Press any key to continue . . . _
Output with puts()
The puts() is another library function that will print a string of characters until null character is reached or end of file (EOF).
Syntax: –
The syntax for puts() is as follows
char name[10] = "puppy";
puts(name);
The above will result in following output.
puppy
Example Program
You can check the example program that demonstrate the usage of puts().
#include <stdio.h>
#include <stdlib.h>
int main()
{
char name[10] = "PUPPY";
puts(name);
system("PAUSE");
return 0;
}
Output-puts()
PUPPY
Press any key to continue . . . _
References
- Balagurusamy, E. 2000. Programming in ANSI C. Tata McGraw-Hill Education,.
- Brian W. Kernighan, Dennis M. Ritchie. 1988. C Programming Language, 2nd Edition. Prentice Hall.
- Kanetkar, Yashavant. 20 November 2002. Let us C. Bpb Publications.