In this lesson, you will learn about the Output Function in C printf(), its usage, and examples to better understand the topic.
To send output in the C programming language, use the printf() function. This function allows you to send formatted output to the console. For example:
#include <stdio.h>
int main()
{
// Print the string inside quotations to the screen
printf("Welcome to Learnmodo!");
return 0;
}
Output
Welcome to Learnmodo!
To output an integer, you need to use the %d to tell the compiler that the output is an integer.
#include <stdio.h>
int main()
{
int number = 10;
// Print an integer to the screen
printf("Number = %d", number );
return 0;
}
OutputĀ
Number = 10
Similar to printing an integer and a string, we can output double and float by using the %f for float and %lf for double.
#include <stdio.h>
int main()
{
float floatVariable = 3.14;
double doubleVariable = 2.8;
printf("The float variable value is = %f\n", floatVariable);
printf("The double variable value is = %lf", doubleVariable);
return 0;
}
Output
The float variable value is = 3.14 The double variable value is = 2.8
Note: the \n tells the compiler to send the Output to the screen followed by a new line
Also, printing char in C is possible using the %c with the printf() function.
Example
#include <stdio.h>
int main()
{
char charVariable = 'a';
printf("The character is = %c", charVariable );
return 0;
}
Output
The character is = a
In the next lesson, you will learn about the Input function in C, scanf()