Input and Output in C language {Module-3}

Input and output in C language - Module 3
Input and output in C language - Module 3

Input and Output in C language {Module-3}

Any meaningful program written in any language has three sections:-

  1. In the first section is the program will accept the input from the device like keyboard and store the data in memory.
  2. In the second section the program will work with the data according to the required logic to generate some result and store the result in the memory.
  3. In the third section the program will display the result on the display device like screen.

Accepting the data or displaying the data is not a simple process. There are several complications like the format of the data etc. For example if you want to accept a number like 107, it can be interpreted in many ways i.e. this 107 number can be considered as octal number, decimal number or hexadecimal number. The system cannot know how to regard the data. There are several other problems like keyboard and display with ASCII codes which have to be converted to hexadecimal etc. All this stuff is beyond the reach of normal programmers. Therefore most of the language including the ‘C’ language provide support in the form of inbuilt functions to simplify the process.

Scanf and Printf

In ‘C’ language we have “scanf” function for accepting the data from keyboard and “printf” function for displaying the data on screen. Both these function require “format specifiers” which will take care of the problem that what kind of data does the ‘C’ language is going to accept or what kind of data is it going to display. For example if you have declared a variable ‘i’ as ‘int’ then you can accept the decimal data from the keyboard through the following way.

int i;                       From this instruction you have declared a variable ‘i’ as integer data type and 2                                                  bytes of memory will be allocated in the memory.

scanf(“%d”, &i);

Now ‘scanf’ is the function which we have used to accept the data from the user. Here ‘%d’ is the format specifier which is used to let the system know that the value which the system is going to accept is a decimal value. The format specifier is put in “” and comma is given after that. Now the “&” is used to tell the system the address allocated to ‘i’ in the system so that the value which the system is going to accept will be stored in ‘i.

While writing a ‘C’ program we are free to specify multiple variable of different widths and data types in 1 “scanf” function.

Some more examples of “scanf” function:-

int i;

char j;

float k

scanf(“%d%c%f”, &i,&j,&k);

In the above example we have declared three variables i,j,k as decimal, character and float data type. And in the next instruction in the scanf function we have declared three format specifiers accordingly. After that we have given the address of i, j,k to store the data accordingly.

Remember that the data type must be matched with the associated format specifier.

printf” is the function used for displaying the result on screen.

For example:  int i;

scanf(“%d”, & i);

printf(“%d”, i);

In the above example we declared a variable ‘i’ of int data type. Then we accepted the value from user and stored the value in ‘i’. After storing the value we displayed the value using ‘printf‘ function. The concept for using ‘printf‘ is same as ‘scanf‘. The only difference is that the ‘&’ sign is not used in the ‘printf‘ function. Some examples of ‘printf‘ is given below.

int i,j,k;

printf(“Enter the first number”);  Display “Enter the first number” on screen

scanf(“%d”, &i);                                Accept and store the value in i;

printf(“Enter the second number”);  Display “Enter the second number” on screen

scanf(“%d”, &j);                                               Accept and store the value in j;

printf(“Enter the third number”);    Display “Enter the third number” on screen

scanf(“%d”, &k);                              Accept and store the value in k;

printf(“The first number is %d”, i);   Display “The first number is (and the number you have stored in i)” on the place of %d the number will be displayed because the number you have stored in i is integer value.

printf(“The second number is %d”, j);   Display “The second number is (and the number you have stored in j)” on the place of %d the number will be displayed because the number you have stored in j is integer value.

printf(“The third number is %d”, k);   Display “The third number is (and the number you have stored in k)” on the place of %d the number will be displayed because the number you have stored in k is integer value.

if You have missed our Previous Modules

Read Here:

Module 1 – Basics of C Language

Module 2 – Data Types in C language

About Suraj Pandita 22 Articles
Studying Computer Engineering from University of Pune.

3 Trackbacks / Pingbacks

  1. Control Structures in C language – {Module-4}
  2. Introduction to Data Structures and Algorithms
  3. Different types of Data Structures

Leave a Reply

Your email address will not be published.


*