In this post you will learn how you can do Input and Output in C programming. For this we will read about printf(), scanf(), getchar() and putchar() functions as well as we will also read about format specifiers and escape sequence.
Note:
In this post you will hear some such terms about which you have not read yet such as string, function. We will soon read about them in our upcoming tutorials.
The main work in any computer program is input, process and output. Most of the input in C programs is taken from the user.
We process the input given by the user in our program and then give the output to the user with a user friendly message.
Whenever you (programmer) make any program, always keep in mind that you are making this program for someone (user) else who does not know anything about your code and will not show your code to him.
Computer program is like communication between programmer and user, so your program should be user friendly only then your program is of any use otherwise it is useless.
To communicate with users, we (programmers) take the help of input and output functions in our programs.
For data input and output, the stdio.h (Standard Input Output) header file has been given in C language, in which many built-in input and output functions have been given.
To use all these input and output functions, you have to include the stdio.h header file at the top of your program, whose syntax I have given below.
Contents
- 1 Input and Output in C Programming.
- 2 You can also read: Constants in C Programming
- 3 1. Formatted Input and Output Functions
- 4 2. Non-formatted Input and Output Functions
- 5 What are Format Specifiers in C Programming Language?
- 6 Output using printf() function
- 7 printf() Function Syntax and Example 1:
- 8 printf() Function Syntax 2:
- 9 printf() Function Example Program:
- 10 Output:
- 11 Explanation:
- 12 Input using scanf() function
- 13 getchar() and putchar() character input output functions
- 14 Escape Sequence in C Programming
- 14.1 Without \n Escape Sequence:
- 14.2 Output:
- 14.3 Explanation:
- 14.4 With \n Escape Sequence:
- 14.5 Output:
- 14.6 Without \t Escape Sequence:
- 14.7 Output:
- 14.8 Explanation:
- 14.9 With \t Escape Sequence:
- 14.10 Output:
- 14.11 Print ” Without \” Escape Sequence:
- 14.12 Output:
- 14.13 Print ” With \” Escape Sequence:
- 14.14 Output:
- 14.15 Explanation:
- 15 You can also visit my Hindi YouTube Channel.
Input and Output in C Programming.
#include <stdio.h>
You can also read: Constants in C Programming
In C programming, all input and output functions are divided into 2 categories.
1. Formatted Input and Output Functions
2. Non-formatted Input and Output Functions
Note:
You will read the use of gets and puts functions in String post.
What are Format Specifiers in C Programming Language?
In C programming, format specifiers are used while taking input into variables and outputting variables.
Format specifiers allow the compiler to specify the format of the variables at the time of input and output. Some common data types and their format specifiers are given in the table below.
Data Types | Format Specifiers |
---|---|
int | %d |
float | %f |
char | %c |
Format specifiers are used only in formatted input/output functions. Let us now learn the use of all formatted input/output functions one by one.
Output using printf() function
As I told you above that printf() and scanf() are built-in functions and they are defined in stdio.h header file so you have to include this header file to use these functions.
Whenever you want to print (output) any message, variables or both in your program on the user’s monitor (console) in a particular format, you can use printf() function for this.
Input and Output in C Programming
printf() Function Syntax and Example 1:
printf("You can write anything. It will print on output screen as it is.");
printf("Avinash Singh");
Explanation:Like in this example I have written my text message with the help of printf() function. Like in this example, I have printed my text message Avinash Singh with the help of printf() function, in the same way you can also print any text message.
printf() Function Syntax 2:
printf("<Format Specifiers><format-specifiers>", variables);
printf("<Format Specifiers</format-specifiers>><format-specifiers><format-specifiers> Text Message", variables);</format-specifiers></format-specifiers>
printf() Function Example Program:
#include <stdio.h><stdio.h>
int main()
{
int rollnumber=61;
int stu_age=15;
float stu_cgpa=9.6;
char stu_gen = 'F';
printf("%d\n", rollnumber);
printf("Your Age : %d\n", stu_age);
printf("Roll : %d, CGPA : %f and Gender : %c", rollnumber, stu_cgpa, stu_gen);
return 0;
}
</stdio.h>
Output:
61
Your Age : 15
Roll : 61, CGPA : 9.6 and Gender : F
Explanation:
Input and Output in C Programming
Input using scanf() function
scanf() is the main input function in C programming. Whenever you want to take input from any kind of data user through keyword, then you can use scanf() function.
scanf() Function Syntax:
scanf("<format-specifiers><Format Specifiers>",&variables);</format-specifiers>
scanf() Function Example Program:
#include <stdio.h><stdio.h>
int main()
{
int stu_roll;
float stu_cgpa;
char stu_gen;
printf("Enter Your Roll No. : ");
scanf("%d",&stu_roll);
printf("Enter Your CGPA and Gender : ");
scanf("%f %c",&stu_cgpa,&stu_gen);
printf("Roll : %d, CGPA : %f and Gender : %c",stu_roll,stu_cgpa,stu_gen);
return 0;
}</stdio.h>
Explanation
getchar() and putchar() character input output functions
If you want to input and output a single character from the user in your program in a very easy way, then for that you use getchar() and putchar() functions.
The getchar() function is used to take a single character input from the keyboard and the putchar() function is used to output a single character to the monitor.
As you know that putchar() is not-formatted output function, so you can make it simple to print single character and that too without any text message.
Input and Output in C Programming
getchar() function syntax:
char variable = getchar();
getchar() function example:
char ch;
ch= getchar();
putchar() function syntax:
putchar(variable);
putchar() function example:
char ch;
ch = getchar();
putchar(ch);
getchar() and putchar() function example program:
#include <stdio.h><stdio.h>
int main()
{
char ch;
printf("Enter Your Char : ");
ch = getchar();
printf("You Entered : ");
putchar(ch);
return 0;
}</stdio.h>
Escape Sequence in C Programming
Input and Output in C Programming
Escape sequences are used with the printf() function to format the output and are not shown on the output screen.
Escape sequences begin with a backslash ( \ ) and have 1 or 2 characters with a backslash, and their use and function are already fixed.
In the table below, I have given only useful escape sequences and have explained them all with the help of examples at the bottom of the table.
Escape Sequence | Description |
---|---|
\n | Create New Line |
\t | Set Horizontal Tab |
\’ | Print Single Quote |
\” | Print Double Quote |
\\ | Print Backslash |
Without \n Escape Sequence:
printf("Avinash ");
printf("Singh");
Output:
Avinash Singh
Explanation:
We have written Hello and World in different printf() function but you will see in the same line in the output.
Line break has to be done in C Programming i.e. you have to use \n escape sequence. Whatever words are after \n will come in the new line.
Input and Output in C Programming
With \n Escape Sequence:
printf("Avinash\n");
printf("Singh");
printf("\nAvinash\nSingh");
Output:
Avinash
Singh
Avinash
Singh
Without \t Escape Sequence:
printf("Blog of Avinash Singh");
Output:
Blog of Avinash Singh
Explanation:
We have written post in printf() function by giving more than 1 space between words but in the output all three words are printed with single space.
In C Programming, if you want to give more than one space between words, then you have to use \t escape sequence. Due to this, about 4-5 space (tab) comes in between the words.
Input and Output in C Programming
With \t Escape Sequence:
printf("Blog\tof\tAvinash Singh");
Output:
Blog of Avinash Singh
Print ” Without \” Escape Sequence:
printf("You are reading " blog of Avinash Singh."");
Output:
Error
Print ” With \” Escape Sequence:
printf("You are reading\" blog of Avinash Singh.\"");
Output:
You are reading " blog of Avinash Singh."
Explanation:
Similarly, if you want to print single quotation mark and backslash in the output then you have to use \’ and \ escape sequences.
Input and Output in C Programming
I hope that you have liked this post. If you liked the post, then please share it with your friends and relatives and you can send your suggestion in the comment box below or email me at pr***********@gm***.com .