Input using scanf() function
We use the scanf() function each time we want the program to get hold of inputs from us. Thus, while this system receives Input from the user, it stores the input values into any variable.
Syntax
scanf("format-specifiers", var1-memory-address, ... , varN-memory-address);

You can also try this code with Online C++ Compiler
Run Code
Example
#include <stdio.h>
int main()
{
int input_from_user;
printf(“ Enter a digit here: “);
scanf(“%d”, &input_from_user);
printf(“Entered number is: %d”, input_from_user);
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Output
Enter a digit here: 20
The digit that you entered is: 20

You can also try this code with Online C++ Compiler
Run CodeInput a character using getchar()
In C, we use the getchar() function to study a single individual from the keyboard. The getchar() function returns an integer i.e. getchar() function waits for Input till the user presses any character. It accepts any individual as an input. For this reason, be cautious while using getchar(). As non-printable characters like blank area, new lines, tab, etc., are also considered Input to getchar().
Output a character using putchar()
We use C's putchar() function to show or output a character on the console (display). The putchar() function takes an integer argument and displays its character representation on the console.
Example of getchar() & putchar()
#include <stdio.h>
int main()
{
char grade;
printf("Enter student grade: ");
/* Input character using getchar() and store in grade */
grade = getchar();
/* Output grade using putchar() */
putchar(grade);
return 0;
}

You can also try this code with Online C++ Compiler
Run CodeFrequently Asked Questions
1. Can we specify a variable field width in a scanf() format string?
Ans: In scanf(), a * in a format string after a % sign is used for the suppression of an assignment. That's, The current input field is scanned but not STORED.
2. In a call to printf() function, the format specifier %b can be used to print the binary equivalent of an integer?
Ans: There is no format specifier named %b in c.
3. stderr, stdin, stdout are FILE pointers?
Ans: Yes, these will be declared like
The corresponding stdio.h variable is FILE* stdin;
The corresponding stdio.h variable is FILE* stdout;
The corresponding stdio.h variable is FILE* stderr;
Conclusion
We hope you have gained some insights on Input and Output Functions in C through this article. The scanf() function is to take Input from the user and the printf() function is used to display output to the user and learned about the getchar() & putchar() objects. You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.