Input Output

Input : data provided to a program.
Output : data provided by a program.

The basic input/output function are getcharputchargetsputsscanfprintfgetchar and putchar : Single Character getchar function : Single Character Input

The getchar function returns a single character. putchar function : Single Character Output Single characters can be displayed using the putchar function.

 

Example of getchar and putchar :

#include <stdio.h>
int main( ){
    int c;

    printf( "Enter a single character :");
    c = getchar( );

    printf( "\nEntered Character is : ");
    putchar( c );

    return 0;
}

Output :

Enter a single character : a
Entered Character is : a

Explanation :

When you enter a single character or string as input in getchar function, the putchar function will only going to display the first character from the entered input. Because getchar and putchar function are single character inputand output function.

gets and puts function

gets function

Reads characters from the standard input, until a newline character or the end-of-file is reached.

puts function

Puts function is used to output strings.

Example of gets and puts :

#include <stdio.h>
int main(){
    char str[100];

    printf( "Enter a string : ");
    gets( str );

    printf( "\nEntered String : ");
    puts( str );

    return 0;
}

Output :

Enter a string : Hello World
Entered String : Hello World

Explanation :

The gets function stores the input in str and puts function displays that string str.

scanf function :

The scanf() function is used to take input from user. This function can be used to enter any combination of numeric valuessingle characters and strings.

Syntax :

scanf ("format string", argument list);

format string : It contains the information for interpreting the entire data.

 

NOTE :

  • The format string must be a text enclosed in double quotes.

printf function

printf() is used to display output.

Syntax :

printf ("format string", argument list);

format string : collection of escape sequence or/and conversion specification.
Argument list : contains a list of variables.

Example of scanf and printf :

#include<stdio.h>
int main(){
    char str[10];
    char ch;
    int i;
    float f;
	
    printf("Enter Character : ");
    scanf("%c",&ch); // Accept Character
    printf("\nCharacter is : %c ",ch);

    printf("\nEnter String : ");
    scanf("%s",str); // Accept String
    printf("\nString is : %s ",str);

    printf("\nEnter Integer : ");
    scanf("%d",&i); // Accept Integer
    printf("\nInteger is : %d ",i);

    printf("\nEnter Float : ");
    scanf("%f",&f); // Accept Float
    printf("\nFloat is : %f ",f);

    return 0;
}

 

Explanation :

scanf will take the input as character (%c)string (%s)integer (%d)float (%f) and displays the output through printf statement by using conversion specification (%c, %s, %d, %f).

NOTE :

  • #include<stdio.h> is mandatory for printf function.
  • format specification of scanf and printf function must be enclosed in double quotes.
  • & ampersand is required in argument list of scanf function to be used with the list of variables.
  • The format specification and argument list must be separated by comma.