C Printing Outputs

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("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

  1. %d for integers
  2. %f for floats
  3. %c for character
  4. %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);

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.


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,


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.


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

Will give following output.

10 20

But the with new line (\n) you get each output in its own line.


Will print following.


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");

     /* Result with + or - sign */
     printf("Result with plus sign\n"); printf("%+f\n",result2);

     /* Result justified */
     printf("Result 10 space right justified\n");

     /*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 with newline and tab */
     printf("result with tab\n");
     printf("result with tab \n");

return 0;

Output- printf()

Normal Result
Result with plus sign
Result 10 place right justified
Result 12 place justified and precision set to 3digits
result with tab
result with tab
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.


int x = '5';

The above statement is incorrect because the datatype is an integer.

char x = '5';

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';
    return 0;

Output – putchar()

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";

The above will result in following output.


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";
    return 0;


Press any key to continue . . . _


  • 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.


Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.