C Input/Output

Single character input / output
Character input

The getchar function is used to accept one character from the standard input device i.e the keyboard.

Syntax: variablename = getchar; Example: void main {  char initial; //declaration of a variable named initial and assign it char data type initial = getchar; } Character output
 * 1) include

The putchar function outputs a single character to the standard output device, i.e the monitor.

Syntax: putchar(variablename); Example: void main {  char initial; initial = getchar; putchar(initial); }
 * 1) include

String input / output
String input

The gets function is used to input a string trailing with the newline character(inserts a carriage return).

Syntax: gets(variablename); Example: //program to capture a string void main {   char line[69]; //declaration of an array of 69 characters printf("\n Type in a line\n"); gets(line);//the variable name is line } String output
 * 1) include

The puts function is used to display a string

Syntax: puts(variablename); Example: //program to capture a string void main {   char line[69]; //declaration of an array of 69 characters printf("\n Type in a line\n"); gets(line);//the variable is "line" is inserted into the memory printf("\n The line entered is shown below\n\n"); puts(line); //ending of statements. }
 * 1) include

scanf
This function accepts all types of data from the standard input device, i.e keyboard.

Syntax: scanf("controlstring:,variablename); scanf("cs1 cs2 cs3 .. csn", variable1, variable2, variable3 .. variablen); The control string (cs) contains several groups of special characters which describe the data types of the variables; variable1,variable2,variable3,..,variablen. variable1, variable2, variable3, ..., variablen are the addresses of the variables that are to be read in.

The first character of each group in the control string is %. This is followed by a conversion character, which describes to what data type variable1, variable2, variable3, ..., variablen belong.

The groups (control strings) are separated by blank spaces. The addresses of the variables are obtained by operating the address fetching operator &on the variables.

Example: to read an integer x The first character in the control string for each group is %. This is followed by a conversion character, which describes the data types of variable1, variable2, ..., variablen. These groups of control characters are separated by blank spaces. The memory addresses for the variables are obtained by using the address fetching operator (&) to operate on the variables. Example 1: Reading an integer x scanf("%d", &i); Example 2: Reading a character named stdInitial and an integer named score

Inputting strings
Example 1: char studName[40]; // for the input "Joseph K. K", the data assigned to stdName is Joseph scanf("%s", stdName); // data input terminates at the space character Example 2: char studName[40]; scanf("%[ abcdefghijklmnopqrstuvwxyz]", stdName); //for the input "Joseph K K", the data assigned to stdName is Joseph K K Example 3: reading the string data until the newline character is encountered. char studName[ ]; scanf("%[^\n]", stdName); //for the input "Joseph K. K", stdName[ ] = "Joseph P. K"    // you can type any string of any length

Specifying field width
The width of the character can also be specified along with the conversion character. The width specified indicates the maximum field width. Example 1: int a, b, c; scanf("%3d  %3d  %3d", &a, &b, &c); // three decimal integers assigned to each variable /* for the input  123456789 a = 123 b = 456 c = 789 for the input     1  2   3   4 a = 1 b = 2 c = 3 for the input   1234  5678 a = 123 b = 4 c = 567 */ Example 2: scanf("%c  %c   %c", &c1, &c2, &c3); /* for the input abc c1 = a                        c2 = b                         c3 = c      for the input    a   b    c                          c1 = a                          c2 =    (blank) c3 = c

Data Output
The printf function is used to output any type of data. Syntax: printf("control string", var1, var2, ..., varn); The control string contains groups of characters which describe the data types of the variables var1, var2, ..., varn, which are to be output to the console. Each group of characters begins with a % sign. This sign is followed by a conversion character which tells the data type of the variable to be output.

Conversion characters used for output
Example 1: float a = 2.5; char item[ ] = "book"; int int = 253; printf("%d%s%f\n", i, item, a); //output : 253book2.500000 Example 2: float a = 2.5; char item[ ] = "book"; int int = 257; printf(" %d  %s  %f\n", i, item, a); //if blanks are present, they will appear in the result //output : 257 book  2.500000 Example 3: x = 0.0025; y = 233.0; printf("%f  %f\n", x, y); //output : 0.005000 233.000000 Example 4: x = 0.0025; y = 233.0; printf("%e  %e\n", x, y); //output : 5.000000e -003 2.330000e +002 Example 5: x = 2.35e23; y = 774.34; printf("%g  %g\n", x, y); //output : 2.35e +23 774.34

Handling Strings with s
char lineStr[ ] = "This is a string output demonstration"; printf("%s\n", lineStr);

Determining field width for the output
Minimum field width can be specified for the output. If the data to be displayed is equal to the field, It is displayed in the in the in the specified form but right justified. If the data to be displayed is less than the the specified field, blanks will be added to the its left side making the data to appear right aligned. If the data to be output requires more field width than specified, the the width specified is ignored and the data is printed out with the full required width. Example: float a = 25.742; int x = 123; printf("%2f %6f  %12f\n", a, a, a); printf("%2d %3d  %7d\n", n, n, n);

Precision
Refers to the number of digits in the fraction. It can be specified for both floating point and string data.

Precision for floating point values
Example 1: float x = 7325.247; printf("%6.3f %7.3f  %10.3f\n", x, x, x); Example 2: float m = 7325.247, n = 325.343; printf("%6.3f %7.3f\n",  m, n);

Precision for strings
char strLine[ ] = "This is a demonstration"; printf("%12s\n", strLine); printf("%10.7s\n",strLine);

Flag Characters
These characters are used to alter the output.