Input and Output in C

Input - To get data from the user to the program.

Output - To display the data to the screen.

It is always need to get input from the user and display the output to the screen in any C program.

C language provides with some functions to read any given input and to display data to the screen.

The header file stdio.h provides with 2 functions scanf() and printf() function to get input from the user and display output to the screen.

Example

#include<stdio.h>
void main()
{
    // defining a variable
    int age;
	// Display a message to the screen to enter the age.
    printf("Please enter your age\n");
	// Read the input value.
    scanf("%d", &age);
	// Display the age to the screen
	printf( "\nYour age is: %d", age);
}

Output

Please enter your age
25
Your age is:25

When the above program is compiled and run, it will print a message ???Please enter your age??? with the help of printf() function. The /n denotes the new line.

Now to get the input as integer from the user and store it in a variable we use scanf() function.

In scanf() function, the %d access specifier is used to get integer as input and store it in the address of age variable. The & denotes the address. So &age stores the integer value to its address.

Next, to print the age to the screen we use the %d notation in printf() function.

The printf() function initially contains a string as first parameter(values inside the function) and the variables to be replaced in the string will be mentioned as remaining parameters.

Example to get 2 inputs using a single scanf() function we use

printf("Enter the value of a and b\n");
scanf("%d%d", &a, &b);

// Printing the value of a and b.
printf("The value of a is %d and b is %d",a,b);

Output

Enter the value of a and b
5
6
The value of a is 5 and b is 6

getchar() and putchar() functions

The getchar() function is used to read a character from the terminal. The function will read only one character.

The putchar() function is used to print the character to the terminal. It can print only the character.

Example for getchar() and putchar() function

#include <stdio.h>

void main( )
{
    int c;
    printf("Enter a character\n");
	// Get a character as input and store in the variable c
    c = getchar();
	// Display the character in the variable c
    putchar(c);
}

Output

Enter a character
x
x

In the above program we get a character from the terminal using getchar() function and store it in the c variable and instead of using printf() function we have used putchar() function to print the value in c variable.

gets() and puts() functions

The gets() function is used to read a line of text from the terminal which is terminated using new line.

The puts() function is used to write the string to the terminal.

Example program for gets and puts function

#include<stdio.h>

void main()
{
    /* character array of length 50 */
    char name[50];
    printf("Enter your name\n");
    gets(name);
    puts(name);
    getch();
}

Output

Enter your name
Andrew
Andrew

Here we have a name variable of size 50. Using gets() function we will get the string value and store it in name variable and using puts() function we will print the value in name variable.

The difference between scanf() funtion and gets() function is that, the scanf() stops reading characters when it encounters a space, but gets() accepts the space as character.


Most Read