Output in C
In C, Output can be printed on user computer screen or written on file. To Output, a program needs input from user using standard input device(keyboard) or a file. To Output, c provide printf() function, to use <stdio.h> header file must be included in c program.
Syntax
printf("format_specifier", variable);
printf takes two arguments
format_specifier | The type of data accepted as input. |
variable | Only variable name. |
Example
#include<stdio.h>
int main()
{
int a;
printf("Enter a value: ");
scanf("%d", &a);
printf("\nValue is %d", a);
return 0;
}
Output:
Enter a value: 30
Value is 30
Multiple Output using printf()
Single printf() function can be used for multiple Outputs.
Example
#include<stdio.h>
int main()
{
int a , b;
float f;
printf("Enter a value separated by space: ");
scanf("%d %d %f", &a, &b, &f);
printf("\nValues are: ");
printf("%d %d %f", a, b, f);
return 0;
}
Output:
Enter a value separated by space: 50, 90 3.14
50 90 3.14
Integer Output using printf()
For integer Output %d format specifier is used.
#include<stdio.h>
int main()
{
int a;
printf("Enter a value: ");
scanf("%d", &a);
printf("\n%d", a);
return 0;
}
float Output using printf()
For float Output %f format specifier is used.
#include<stdio.h>
int main()
{
float flt;
printf("Enter a decimal value: ");
scanf("%f", &flt);
printf("\n%f", flt);
return 0;
}
Character Output using printf()
For character Output %c format specifier is used.
#include<stdio.h>
int main()
{
char chr;
printf("Enter a character: ");
scanf("%c", &chr);
printf("%c", chr);
return 0;
}
String Output using printf()
For string Output %s format specifier is used.
#include<stdio.h>
int main()
{
char *str;
printf("Enter a string: ");
scanf("%s", str);
printf("%s", str);
return 0;
}