printf() and scanf() in C

The printf() and scanf() functions are used for input and output in C language. Both functions are inbuilt library functions, defined in stdio.h (header file).

printf() function

The printf() function is used for output. It prints the given statement to the console.

The syntax of printf() function is given below:

printf("format string",argument_list);  

The format string can be %d (integer), %c (character), %s (string), %f (float) etc.

scanf() function

The scanf() function is used for input. It reads the input data from the console.

scanf("format string",argument_list);  

Program to print cube of given number

Let's see a simple example of c language that gets input from the user and prints the cube of the given number.

int main(){    
int number;    
printf("enter a number:");    
printf("cube of number is:%d ",number*number*number);    
return 0;  


enter a number:5

cube of number is:125

The scanf("%d",&number) statement reads integer number from the console and stores the given value in number variable.

The printf("cube of number is:%d ",number*number*number) statement prints the cube of number on the console.

Program to print sum of 2 numbers

Let's see a simple example of input and output in C language that prints addition of 2 numbers.

int main(){    
int x=0,y=0,result=0;  
printf("enter first number:");  
printf("enter second number:");  
printf("sum of 2 numbers:%d ",result);  
return 0;  


enter first number:9

enter second number:9

sum of 2 numbers:18

Header files contain the set of predefined standard library functions that we can include in our c programs. But to use these various library functions, we have to include the appropriate header files.

Let us see in detail how the compiler interprets the line :


Here, # is a pre-processor directive which tells us that this is the line which must be pre-processed by pre-processor.

include tells us that there is a filename ahead which must be included at the top of our program. Preprocessor simply copies contents of file stdio.h in our code.

<> – Angled brackets defines where to search for header files.

stdio.h – is the file to be included in our program so that we can use built-in functions in our program. These built-in functions are only declared in such header files and not defined.

Apart from method or class declarations, header files also contain predefined macros, data type definitions, etc.

When you call a built-in function, at compile time compiler compares your calling statement with function prototype(which is in the header file) and if the return type, function name, number of arguments, type of arguments are same then only the result of comparison is said to be satisfying otherwise compiler gives you errors.

We can create our own header files as well. But they are specified in between double quotes instead of angular brackets which will convene to make programming easier.

If you have a standard set of instructions that you want to insert in a lot of programs that you are writing then you can do it using the #include statement.

The # symbol at the start stipulate that this isn’t a C statement but one for the C pre-processor which looks at the text file before the compiler gets it. The #include tells the pre-processor to read in a text file and treat it as if it was part of the program’s text. For example:

#include “copy.txt”

could be used to include a copyright notice stored in the file copy.txt. However, the most common use of the #include is to define constants and macros. The C pre-processor is almost a language in its own right For example if you define the identifier NULL as:

#define NULL 0

then whenever you use NULL in your program the pre-processor substitutes 0. In most cases you want these definitions to be included in all your programs and so the obvious thing to do is to create a separate file that you can #include.

This idea of using standard include files has spiraled out of all proportions. Now such include files are called header files and they are distinguished by ending in the extension .h. A header file is generally used to define all of the functions, variables, and constants contained in any function library that you might want to use.

There are many header files in C programming language and there all header files have their own different functionalities…

List of all header file of c language as below.


1 stdio.h Input/Output Functions
2 conio.h console input/output
3 assert.h Diagnostics Functions
4 ctype.h Character Handling Functions
5 cocale.h Localization Functions
6 math.h Mathematics Functions
7 setjmp.h Nonlocal Jump Functions
8 signal.h Signal Handling Functions
9 stdarg.h Variable Argument List Functions
10 stdlib.h General Utility Functions
11 string.h String Functions
12 time.h Date and Time Functions
13 complex.h A set of function for manipulating complex numbers
14 stdalign.h For querying and specifying the alignment of objects
15 errno.h For testing error codes
16 locale.h Defines localization functions
17 stdatomic.h For atomic operations on data shared between threads
18 stdnoreturn.h For specifying non-returning functions
19 uchar.h Types and functions for manipulating Unicode characters
20 fenv.h A set of functions for controlling floating-point environment
21 wchar.h Defines wide string handling functions
22 tgmath.h Type-generic mathematical functions
23 stdarg.h Accessing a varying number of arguments passed to functions
24 stdbool.h Defines a boolean data type