Fifth Chapter Lesson-12: Input and output functions in C programming language

At the end of this lesson-

  • 1. You will be able to use input and output functions in c program. 
  • 2. You will be able to describe formatted and un-formatted input-output functions. 
  • 3. You will be able to explain printf() and scanf() functions details. 

 

Input and Output functions in C programming language: 

Input means to provide some data to be used in the program and Output means to display data on screen or write the data to a file. Input functions are used to read any given input and Output functions are used to display data on screen.

C programming language provides many built-in functions to read any given input and to display data on screen when there is a need to output the result.

Format specifier: Format specifiers defines the type of data to be printed on standard output. Whether to print formatted output or to take formatted input we need format specifiers. Format specifiers are also called as format string. Each format specifier begins with the percentage character (%) followed by a conversion character which indicates the type of the corresponding data item.

Here is a list of all format specifiers used in C programming language: 

Format specifiers Uses Example
%c  For ‘char’ type data scanf(“%c”,&a); printf(“%c”,a);
%d  For ‘int’ type data scanf(“%d”,&a); printf(“%d”,a);
%f  For ‘float’ type data scanf(“%f”,&a); printf(“%f”,a);
%lf  For ‘double’ type data scanf(“%lf”,&a); printf(“%lf”,a);
%ld  For ‘long int ‘ type data scanf(“%ld”,&a); printf(“%ld”,a);
%u  For ‘unsigned int ‘ type data scanf(“%u”,&a); printf(“%u”,a);
%o  For ‘Octal’ type data scanf(“%o”,&a); printf(“%o”,a);
%x  For ‘Hexadecimal’ type data scanf(“%x”,&a); printf(“%x”,a);

Uses of scanf() function:

Format of scanf() function for taking single input: 

scanf(“format_specifier “, &variable_name);

Example:

  • scanf() function for taking ‘char’ type data in variable a:  scanf(“%c”, &a);
  • scanf() function for taking ‘int’ type data in variable a:  scanf(“%d”, &a);
  • scanf() function for taking ‘float’ type data in variable a:  scanf(“%f”, &a);
  • scanf() function for taking ‘double’ type data in variable a:  scanf(“%lf”, &a);

Format of scanf() function for taking multiple inputs: 

scanf(” format_specifier1 format_specifier2….”, &variable_name1, &variable_name2…….);

Example:

Format of scanf() function for taking same type of multiple inputs: 

  • If variable a,b and c are int type then the format of scanf():
  • scanf(“%d %d %d”, &a, &b, &c);

Format of scanf() function for taking different type of multiple inputs: 

  • If variable a,b and c are int, float and double type respectively then the format of scanf():
  • scanf(“%d %f %lf”, &a, &b, &c);

 

Uses of printf() function:

printf() function can be used in two ways. First one is, to print a text that is not a value of a variable. Second one is, to print the value of variables.

Format of printf() function to print a text: The text should be in double quotation of the function.

printf(” Output text should be here “);

Format of printf() function to print the value of a single variable:

printf(“format_specifier”, variable_name);

Example:

  • printf() function for printing ‘char’ type data of variable a:  printf(“%c”, a);
  • printf() function for printing ‘int’ type data of variable a:  printf(“%d”, a);
  • printf() function for printing ‘float’ type data of variable a:  printf(“%f”, a);
  • printf() function for printing ‘double’ type data of variable a:  printf(“%lf”, a);

Format of printf() function to print the value of multiple variables:

printf(“format_specifier1, format_specifier2….”, variable_name1, variable_name2…);

Format of printf() function for printing same type of multiple variables: 

  • If variable a,b and c are int type then the format of printf():
  • printf(“%d %d %d”, a, b, c);

Format of printf() function for printing different type of multiple variables: 

  • If variable a,b and c are int, float and double type respectively then the format of printf():
  • printf(“%d %f %lf”, a, b, c);

 

Backslash Constant Characters: In C language a sequence of characters that doesn’t represent itself when used inside string literal or character. It is composed of two or more characters starting with backslash \. For example: \n represents new line.

Backslash Characters Uses Examples Output
\n Printing Newline printf(“Learning\n ICT”); Learning                          ICT
\t Printing Horizontal tab printf(“Learning\t ICT”); Learning      ICT
\v Printing Vertical tab printf(“Learning\v ICT”); Learning                                                                        ICT
\a Alarm or Beep printf(“\aLearning ICT”);  Alerm!
\” Printing Double quotation mark printf(“Learning \”ICT\” “); Learning “ICT”
\’ Printing Single quotation mark printf(“Learning \’ICT\’ “); Learning ‘ICT’
\\ Printing Backslash printf(“Learning \\ICT\\ “); Learning \ICT\
\? Printing Question mark printf(” Are you learning ICT\? “); Are you learning ICT?

 

Lesson Evaluation-

Knowledge Based Questions:

  • a. What is input function?
  • a. What is output function?
  • a. What is format specifier?
  • a. What is backslash character?

Comprehension Based Questions:

  • b. What is meant by output function?
  • b. Explain the statement scanf(“%d”, &a);
  • b. Explain the statement scanf(“%d %x”, &a, &b);
  • b. Write input and output functions used in c programming language.
  • b. Explain printf() and scanf() function with examples. 

Creative Questions:

Multiple Choice Questions:

 


Written by,

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *