Standard Input and Output in C Language - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts

Standard Input and Output in C Language

Share This
Before writing programs to solve computational problems, we need to understand how to work with input and output devices. The main tasks of any program are reading the data from the input devices and displaying the results on the screen. C language has several input and output functions to perform interactive tasks.

Though many functions exist in stdio.h header library, here we will discuss about scanf and printf functions.

Standard input - using scanf() function

In C language, scanf function is used to take input (standard input) from terminal. Declaration of this function is available in stdio.h hader file and it is defined in library.

We can pass one or more arguments in this function. As a beginner, it will be difficult to understand the working principle of this function in details. We will discuss it later.

To take integer value as input, the function can be used as shown below. The value will be taken from user and will be stored in variable m.

#include <stdio.h< int main(void) { int m; printf("\nEnter an integer:"); scanf("%d", &m); return 0; }


If we want to take more than one value at a time, we can write the code as below:
#include <stdio.h> int main(void) { int m, n, p; printf("\nEnter three integer values:"); scanf("%d %d %d", &m, &n, &p); return 0; }


Alternatively, we can write:
#include <stdio.h> int main(void) { int m, n, p; printf("\nEnter three integer values:"); scanf("%d", &m); scanf("%d", &n); scanf("%d", &p); return 0; }

To take a character as input, instead of "%d" in scanf function, we have to use "%c".
#include <stdio.h> int main(void) { char m; printf("\nEnter a character:"); scanf("%c", &m); return 0; }


Standard output - using printf() function

In C language, printf function is used to print (standard output) on terminal. The function is declared in stdio.h hader file and is defined in library.

In this function, we can pass one or more than one arguments. As a beginer, it will be difficult to understand the working principle of this function in details. We will discuss it later.

Previously we have used this function many times in sample codes. Here we will discuss, how to use this function to print desired result.

To print a string, the printf function can be used as below:
#include <stdio.h> int main(void) { printf("Hi, how are you?"); return 0; }


To print integer value, the function can be used as shown below.
#include <stdio.h> int main(void) { int m = 10; printf("\nThe value is : %d", m); return 0; }



If we want to print more than one value at a time, we can write the code as below:
#include <stdio.h> int main(void) { int m = 10, n = 5, p =17; printf("\nThe values are: %d %d %d", m, n, p); return 0; }


To print the value of a character variable, instead of "%d" we have to use "%c".
#include <stdio.h> int main(void) { char m = 'x'; printf("\nThe value of m is :%c", m); return 0; }


In the above examples, we have used only integer data types. But most of the time you have to work with other data types. So, you should know how to work with other data types. In the following table, different format specifiers are shown along with their usage.

Format SpecifierUsage
%csingle character
%d (%i)signed integer
%e (%E)float or double exponential format
%ffloat or double signed decimal
%g (%G)float or double use %f or %e as required
%uunsigned integer
%ooctal value of unsigned integer
%ppointer address stored in pointer
%sarray of characters
%x (%X)hex value of unsigned integer

Happy Exploring!



No comments:

Post a Comment