Skip to main content

Displaying the Output of C Program

C Program Output

In C programming, printf() is one of the main output function. The function sends formatted output to the screen.

Example 1: Output a String

// C Output

#include <stdio.h>
int main()
{
// Displays the string inside quotations
printf("My C Programming Course \n");
return 0;
}
Output:

Output:

My C Programming Course

In this program, the printf() function is used to display the message "My C Programming Course" to the user. The \n at the end of the message is a newline character, which tells the computer to move to the next line after printing the message.

  • The printf() is a library function that sends formatted output to the screen
  • The function prints the string My C Programming Course inside quotations
  • For printf() we need to include stdio.h header file using #inclue <stdio.h> statement.

Example 2: Output an integer

//Integer Output

#include <stdio.h>
int main()
{
int varInt = 5;
printf("Number = %d \n", varInt);

return 0;
}
Output:

Output:

Number = 5
  • %d here is the format specifier to print int types
  • Here, the %d inside the quotations will be replaced by the value of varInt

Example 3: Output float and double

// float and double Output

#include <stdio.h>
int main()
{
float varFloat = 18.5;
double varDouble = 15.4;

printf("varFloat = %f \n", varFloat);
printf("varDouble = %lf \n", varDouble);

return 0;
}
Output:

Output:

varFloat = 18.500000
varDouble = 15.400000
  • To print float, we use %f format specifier
  • To print double, we use %lf aka long float format specifier
  • Both %f and %lf print six decimal places

Example 4: Output a character

// Print Character

#include <stdio.h>
int main()
{
char varChar = 'a';
printf("character = %c \n", varChar);

return 0;
}
Output:

Output:

character = a
  • To print char, we use %c format specifier.