Input and Output in C


C language provides many built-in functions to read any given input and to display output data on screen. All these functions are present in header file “stdio.h”, which is an abbreviation for “standard input-output” header file.
There are two method to assign the data to a variable:

  • First: Assign value to variable using assignment statment such as b=6, a=9.
  • Second: By using the inbuilt input function ‘scanf’.

The inbulit output function ‘printf’ is most commanly used to display the output or any message on screen/console.


Unformatted input and output functions


unformatted input function includes getchar(), getc(), gets() and output function includes puts(), putc(), putchar().


getchar():

The library function getchar() reads a single character from the keyboard and store it in a variable.

Syntax: variable=getchar();
Example: char ch=getchar();

In the above example the getchar() takes input from keyboard and store it in char type variable “ch”.


Example Illustrating use of getchar():

Output:

Will you marry me ??
Type Y for yes and N for No :
Y
Yes, I will marry you


putchar():

The library function putchar() display a single character on the screen/ console.
Syntax: putchar(<variable-name>);
Example: char ch=getchar();
In the above example the getchar() takes input from keyboard and store it in char type variable “ch”.


Example Illustrating use of putchar():

Output:

Yes


gets():

The gets() is used to read “string with space” & store it in a variable.
Example: char a[20]=gets(a);


puts():

The puts() is used to write string with space on the screen/console which is previously read by gets().
Example: puts("Print this");


Formated Input:


definition .........
Inbuilt function scanf is used to accept formated input.The syntax of scanf() function is given below:

scanf(“Format string”,arg1);
The format(control) string specifies the field format like float (%f), int (%d), char (%c) in which the data is to be entered and the arguments arg1 specify the address of locations where the data is stored.
Note:Control string and agruments are separated by Comma (,)

Control string contains field specification which is used to interpret the input data. It consists of following three things:

  • conversion character (%)
  • datatype character (%f, %d, %c)
  • whitespaces
  • field width (Optional)

List of format specifier

Format specifier in C

Getting Formatted Integer Input:

The format specifier %Wd is used to read an integer number.

Examples:
scanf(“%2d %5d”,&num1,&num2);
printf(“%d %d”,num1, num2);
For input: 50 31245
Output: 50 31245
Here the value of 50 is assigned to num1 and 31245 is assigned to num2.

scanf(“%2d %5d”,&num1,&num2);
printf(“%d %d”,num1, num2);
For input:31245 50
Output:31 245
Here 31 will be assigned to num1 (%2d) and 245(unread part of 31245) will be assigned to num2.

In the above example ,If we enter floating number instead of integer, than the fraction part is stripped away.

scanf(“%d %*d %d”,&num1,&num2);
printf(“%d %d”,num1, num2);
For input: 123 456 789
Output:123 789

An Input field can be skipped by specifying * in place of field width as in above example 456 is skipped.


Example Illustrating different Input formatting:

Output:

Enter three integer number
1
2
3
1 3 -451364624

Enter two 5-digit number
67890 34567
67 890

Enter two integer number
22 78
34567 22

Enter a 10-digit number
1234567890
78 12345 67

Enter two three digit numbers
12 45 89
890 12



Getting Formatted Integer Input:

The format specifier %Wd is used to read an integer number.

Examples:
scanf(“%2d %5d”,&num1,&num2);
printf(“%d %d”,num1, num2);
For input: 50 31245
Output: 50 31245
Here the value of 50 is assigned to num1 and 31245 is assigned to num2.

scanf(“%2d %5d”,&num1,&num2);
printf(“%d %d”,num1, num2);
For input:31245 50
Output:31 245
Here 31 will be assigned to num1 (%2d) and 245(unread part of 31245) will be assigned to num2.

In the above example ,If we enter floating number instead of integer, than the fraction part is stripped away.

scanf(“%d %*d %d”,&num1,&num2);
printf(“%d %d”,num1, num2);
For input: 123 456 789
Output:123 789

An Input field can be skipped by specifying * in place of field width as in above example 456 is skipped.


Getting Formatted Real Number as Input:

The format specifier %f and %lf are used to take real number as input.


Example Illustrating different Input formatting:

Output:

Enter values of x and y :12.3456
17.5e-5

x= 12.345600
y=0.000175

Enter values of p and q :4.142345678
19.6458693634643986
p= 4.142345678000
q= 1.964586936346e+01

Explanation:
  • %e is used to take input value in term of exp (e)
  • %.12lf is used to print 12 value after decimal(.)
  • %.12e is used to print value in term of power of e (e^+5) after decimal 12 values.

Formated Input:


Printf statement is used to print output and other messages on screen.The general syntax of printf() statment is:
printf(“Format string ”,arg1);
Format string contains field specification used for interpret the input data ,It consists of following three things:

  • conversion character (%)
  • datatype character (%f, %d, %c)
  • Escape sequence character such as \n,\t,and \b
  • field width (Optional)

Printing Formatted Integer output:

Example Illustrating formatted Integer output:

Output:

12345
      12345
0000012345
12345
    983467
-983467
000000000983467

Printing Formatted Real Input:

The Format specifiers %w.p f (float) and %w.p ld (double) are used to print formatted output of real number.
And %w.p e is used for printing numbers in exponential notation.
Example: y=98.7654

output:


Example of formatted real number output in C

Printing Character and strings:

The format specifier %wc(%c) is used to display a single character and %ws(%s) is used to display string, where w is the minimum field width.
Example: “NEW DELHI 110001” is a string, which contains 16 characters including 2 space character.

Output:


Example of formatted string output in C

isalpha() & isdigit()

The functions isalpha() & isdigit() are present in ctype.h header file. “isalpha() is used for check the character is letter or not” and “isdigit() is used for check the chracter is digit or not ” .


Example Illustrating use of isalpha() and isdigit():

Output:

Press a key
@
The character @ is neither letter nor digit.


islower(),toupper() & tolower()

The functions islower(),toupper() & tolower() are present in ctype.h header file.

  • The islower() function returns True if the argument is a lowercase alphabet and False if the argument is uppercase alphabet.
  • The tolower() converts the uppercase argument into a lowercase alphabet.
  • The toupper() function converts the lowercase argument into a uppercase alphabet.

Example Illustrating use of islower(),toupper() & tolower():

Output:

Enter an Alphabet
r
R


Do You Know?