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 Courseinside quotations - For
printf()we need to includestdio.hheader 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
%dhere is the format specifier to printinttypes- Here, the
%dinside the quotations will be replaced by the value ofvarInt
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
%fformat specifier - To print double, we use
%lfakalong floatformat specifier - Both
%fand%lfprint 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
%cformat specifier.