C programming has several in-built functions to perform input and output tasks.
Two commonly used functions for I/O (Input/Output) are printf() and scanf().
printf() and scanf() functions are inbuilt functions in C programming environment which are available in C library by default. These functions are declared and related with “stdio.h” which is a header file in C language.
printf() method in C :
printf() function sends formatted output to the standard output(screen). printf() is used to show any message or value of any data type.
We use printf() function with %d format specifier to display the value of an integer variable. Similarly %c is used to display character, %f for float variable, %s for string variable, %lf for double and %x for hexadecimal variable.
To generate a newline,we use “\n” in C printf() statement.
Example 1 :
int main()
{
printf("C Programming"); //displays the content inside quotation
return 0;
}
Output :
C Programming
Example 2 :
#include<stdio.h>
int main()
{
int a = 5, b = 7;
printf("Value of a = %d \n, a);
printf("Value of b = %d, b);
return 0;
}
Output :
Value of a = 5
Value of b =7
scanf() method in C :
scanf() function is used to read character, string, numeric data from keyboard.
Syntax :
scanf ("control string", &variable_name);
Example :
#include <stdio.h>
int main()
{
char ch;
char str[100];
printf("Enter any character :\n");
scanf("%c", &ch);
printf("Entered character is %c \n", ch);
printf("Enter any string : \n");
scanf("%s", &str);
printf("Entered string is %s \n", str);
}
Output :
Enter any character :
k
Entered character is k
Enter any string :
hello
Entered string is hello
k
Entered character is k
Enter any string :
hello
Entered string is hello