What are input and output functions? Explain printf() and scanf() functions with examples.

4 years ago
C Programming

Input Functions: The functions which helps user to feed some data into program are called as input functions. When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line or from keyboard. C programming language provides a set of built-in functions to read given input and feed it to the program as per requirement.

Examples: scanf(), gets(), getchar(), fscanf(), etc

Output Functions: The functions which helps user to display or print output on screen or on paper using printer or in file are called as output functions. When we are saying Output that means to display some data on screen, printer or in any file. C programming language provides a set of built-in functions to output the data on the computer screen as well as you can save that data in text or binary files.

Examples: printf(), puts(), putchar(), fprintf(), etc.

printf(): print formatted

The printf() function is used for formatted output and uses a control string or format string which is made up of a series of format specifiers to govern how it prints out the values of the variables or constants required.

General Syntax:

int printf(const char *format, ...);

Printf function writes output to the standard output stream stdout(computer screen) and produces output according to a format provided.

Printf function returns an integer constant which is the number of character displayed on screen successfully. The return value can be ignored.

Two ways to use printf()

  1. N=printf(“Text string”);
  2. N=printf(“format string”,variables-list);

Where, screen.

Text string      - text message to be displayed on screen. It is displayed as it is on Format string - it is a sequence of one or more format specifiers depending on type of

data to be displayed.

Variable-list   - list of one or more variables, expressions, or constants whose data will be displayed on screen

N  - contains the return value of printf function. It is optional means that it can be omitted.

The more common format specifiers for displaying various types of data are given below

%c character                         %f  floating point

%d signed decimal integer    %lf double floating point

%i signed integer                  %e exponential notation

%u unsigned integer              %s string

%ld signed long                      %x unsigned hexadecimal

%lu unsigned long                 %o unsigned octal

%% prints a % sign For Example :-

int i ;

printf( "%d", i ) ;

In the above example, the format string is %d and the variable is i. The value of i is substituted for the format specifier %d which simply specifies how the value is to be displayed, in this case as a signed integer.

int N;

N = printf(“Hello\n”);

In this example, the printf() prints the text string “Hello” on screen and returns the

integer value 5 which will be stored in N. Some more examples :-

int i = 10, j = 20 ; char ch = 'a' ;

double f = 23421.2345 ;

printf( "%d + %d", i, j ) ; /* values of i and j are substituted from the variable list in order as required */

printf( "%c", ch ) ;

printf( "%s", "Hello World\n" ) ;

printf( "The value of f is : %lf", f ) ; /*Output as : The value of f is : 23421.2345 */ printf( "f in exponential form : %e", f ) ; /* Output as : f in exponential form :

2.34212345e+4

Scanf(): scan formatted

The scanf() function is used for formatted input and uses a control string or format string which is made up of a series of format specifiers to govern how to read out the values for the variables as required.

This function is similar to the printf function except that it is used for formatted input.

The space character or the newline character are normally used as delimiters between different inputs.

General Syntax of scanf():

where,

int scanf(“format string”,Address-list);

format string   - it is a sequence of   one or more format specifiers to read data as required.

Address-list    - it is a list of address of one or more variables depending on number of format specifiers.

The format specifiers have the same meaning as for printf().

The more common format specifiers for reading various types of data are given below

%c character                         %f  floating point

%d signed decimal integer    %lf double floating point

%i signed integer                  %e exponential notation

%u unsigned integer              %s string

%ld signed long                      %x unsigned hexadecimal

%lu unsigned long                 %o unsigned octal

For Example :-

int i, d ; char c ; float f ; scanf( "%d", &i ) ;

scanf( "%d%c%f", &d, &c, &f ) ; /* e.g. type "10 x 1.234" */ scanf( "%d:%c", &i, &c ) ;       /* e.g. type "10:x" */

The & character is the address of operator in C, it returns the address of the memory location of the variable.

Note that while the space and newline characters are normally used as delimiters between input fields the actual delimiters specified in the format string of the scanf statement must be reproduced at the keyboard faithfully as in the case of the last example. If this is not done the program can produce somewhat erratic results!

The scanf function has a return value which represents the number of fields it was able to convert or read successfully. It can be ignored.

For Example :- num = scanf( “%c %d”, &ch, &i );

This scanf function requires two fields, a character and an integer, so the value placed in num after the scanf() call will be 2 if successful.

0
Sanisha Maharjan
Jan 20, 2022
More related questions

Questions Bank

View all Questions