C Programming

Input Output Instructions in C

Input-output instruction is the basic concept of C programming language. We can take input from the user through input-output instruction and see some output on our output screen. Now we discuss the concept of input-output instruction in C language in detail.

Standard Input-Output Device

There are two types of the input-output device present in our computer. They are:

Keyboard: It is a standard Input Device.

Monitor: It is a standard Output Device.

In the C language, two predefined functions exist in the C library. They are printf () function and scanf () function.

printf ()

printf () is not a keyword.

printf () is a predefined function.

With the help pf printf () function, there are two types of messages printed on the screen:

1. Printing Text as it is.

2. Printing value of expression or value of the variable.

Programming Example 1

#include <stdio.h>

int main ()
{
printf(" Hello \n");
printf(" John ");
return 0;
}

Output

“ \n ” is a special type of character called newline character. The function of “\n” does not print itself on the screen but changes the cursor position to the new line after printing a statement. \n is anEscape Sequence.

How to Print the Value of a Variable?

Programming Example 2

#include<stdio.h>

int main ()
{
int a=4, b=5;
printf("%d", a);
return 0;
}

Output

Explanation

To print the value of a variable, we use %d or %f or %c. There are special types of Format Specifier, which does not print themselves but helps to print values of different types of variables like int, float, char, etc.

%d -> int, %f -> float, %c -> char, %lf -> double.

We want to see the value, a= 4, then we write:

printf (“ a = %d “, a) ;

We want to see the value of a is 4, then we write:

printf (“ value of a is %d ”, a) ;

We want to see if the value of a is 4 and b is 5; we write:

printf (“ value of a is %d and b is %d “, a, b)

Programming Example 3

#include <stdio.h>

int main ()
{
int a=4, b=5;
printf (" Sum of %d and %d is %d", a, b, a+b);
return 0;
}

Output

Explanation

Here we declare two variables, a and b. Initialization is done at the time of declaration. We assign values 4 to a variable and 5 to b variable. Then we sum these two values as a + b directly in the standard output function printf () function. Inside the printf () function, we use format specifier ( %d ), which does not print itself but helps to print the values of the integer type variable.

Programming Example 4

#include <stdio.h>

#include <stdlib.h>

void  gotoxy ( int  x, int  y )             //  gotoxy function
{
printf( " %c[%d;%df", 0x1B, y, x ) ;
}

int main ( void )
{
gotoxy( 20, 13 ) ;                  // reposition of the  cursor
printf( " Hello John!! " ) ;                // display  the text
return 0 ;
}

Output

Explanation

“gotoxy ()“ is a predefined function, which function is to move the cursor position anywhere in our output screen. But this particular function runs on turbo c IDE. But if we want to run this function on other IDE like Linux, it does not work. We have to use the gotoxy () function as a user-defined function. gotoxy () function is not an inbuilt function here. In the output screen, we write a maximum of 80 characters and 25 lines in a line. With the help of this function, we can move the cursor as we like on the screen.

scanf()

scanf () is not a keyword.

scanf () is a predefined function.

scanf ( “ format specifier “, variable address ) ;

printf () To print some value on the screen, we use printf ().

scanf () To take an input value from the keyboard, we use scanf ().

scanf () eco the value which we press on the keyboard. getch () gets only one character, but scanf () takes multiple digits until we press ENTER from the keyboard.

Programming Example 5

#include<stdio.h>

int main()
{
int number;
printf(" Enter a number: ");
scanf("%d",&number);                //take a input value from the user
printf(" The Square of number is: %d ",number*number);  //displaying output
return 0;
}

Output

Explanation

We want to show how the input instruction scanf () function works. We declare a variable named number. We want to take a value from the keyboard or the user. We use standard input instruction or inbuilt function scanf () function. It helps the programmer store the value that the user will get to a specific address where our variable exists. Scanf () function performs this action with the help of the address of the ( & ) operator.

Programming Example 6

#include < stdio.h>

int main()
{
int x, y, z;
printf("Addition of 2 numbers \n");
printf("Enter the first number: ");
scanf("%d", &x);
printf ("Enter the second number: ");
scanf("%d", &y);
z= x + y;
printf("%d + %d = %d\n" , x, y, z);
return 0;
}

Output

Explanation

This programming example declares three integer type variables x, y, and z. We want to sum up the two variables. We take two integer values from the user using the input instruction scanf () function. Then add these two values using add operator and keep the added value to the variable z. Now we print the value of z using the output instruction printf () function.

Conclusion

We have understood from the above example that input-output instructions are widely used in every C program. Without these, we cannot build any robust program. This helps to take input from the user and shows the output to the user. These are the base of any program like a C program.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com