Input Output in C
Input Output in C
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.
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);
Text string - text message to be displayed on screen. It is displayed as it is on screen.
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
|String Format||Data Type||String Format||Data Type|
|%d||signed decimal integer||%lf||double floating point|
|%i||signed integer||%e||exponential notation|
|%ld||signed long||%x||unsigned hexadecimal|
|%lu||unsigned long||%o||unsigned octal|
For Example :
int i ; printf( "%d", i ) ;
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():
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().
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.