input output management in c programming
DESCRIPTION
This slide provides the introduction to the input and output process available in C Programming.TRANSCRIPT
Compiled By: Kamal Acharya
PRINTF(), SCANF()CONVERSION SPECIFIERSESCAPE SEQUENCES
Input Output Management
Compiled By: Kamal Acharya
INPUT and OUTPUT
Compiled By: Kamal Acharya
printf() will print formatted output to the screen.
To print a message: printf("This is a message\n");
How do we print the value of a variable? Answer: Use special format specifiers depending on the
type of the variable
this is a string literal
Printing Output: printf()
Compiled By: Kamal Acharya
Compiled By: Kamal Acharya
int degreesF = 68;printf("The temperature is %d degrees.", degreesF);
Specifier for“print an integer value”
“and the value of that number is read from this variable”
> The temperature is 68 degrees.
Output:
Compiled By: Kamal Acharya
int a = 1;int b = 2;int c = 3;
printf(“%d plus %d is equal to %d", a, b, c);
1 plus 2 is equal to 3
Output:
Compiled By: Kamal Acharya
Format specifiers: %c for single characters %d for integers %f for float/double (fractions): 1234.56 %g for float/double (scientific): 1.23456E+3 %s for phrases or ‘strings’
Compiled By: Kamal Acharya
Output:
A 65 41 101
Example:char a='A';printf("%c %d %x %0", a, a, a, a);
Compiled By: Kamal Acharya
Format specifier can be used with modifiers
Example :%-6d, %5d, %6.2f
Modifier
digit
Description
Allocate minimum width (in characters).
.digit Number of floating-points
- left justified
l Print the data as a long integer.
Compiled By: Kamal Acharya
Statement Output
printf("|%d|", 987); |987|
printf("|%2d|", 987); |987|
printf("|%8d|", 987); | 987|
printf("|%-8d|", 987); |987 |
printf("|%0.2f|", 9876.54); |9876.54|
printf("|%4.2f|", 9876.54); |9876.54|
printf("|%3.1f|", 9876.54); |9876.5|
printf("|%10.3f|", 9876.54); | 9876.540|
Compiled By: Kamal Acharya
printf(“Hello \n World!");
Hello World!
Output:
‘\n’ is not a data to be printed. Instead, it is a command that tells the monitor to move the cursor to the next line
Compiled By: Kamal Acharya
Control Characters (Escape Sequences)
Character
'\n''\t''\v''\r'
'\x41''\101''\0'
'\'''\"''\\''\b''\f''\a'
Description
newlinehorizontal tabvertical tabcarriage returnhexadecimal number, 0x41octal number 101null character - indicates the end of a string
single quatation mark (')double quatation mark (")backslash mark (\)backspaceformfeed - next page (used for printer)alert - produce a beep sound
Compiled By: Kamal Acharya
Figure: Output specification for inventory report
printf (“Part Number\tQty On Hand\tQty On Order\t\tPrice\n”);
\t \t \t\t \n
Compiled By: Kamal Acharya
Keyboard input: scanf()
scanf() will scan formatted input from the keyboard. It uses the same format specifiers as printf() To read an integer:
int num_students;scanf("%d", &num_students);
Specifier for“reading an integer value”
VERY IMPORTANTspecial symbol
“Place value into this variable”
Compiled By: Kamal Acharya
Compiled By: Kamal Acharya
Format specifiers for scanf()
Format specifiers: %c for single characters
scanf(" %c", &some_character);
%d for integers scanf ("%d", &some_integer);
%f for float scanf ("%f", &some_float);
%lf for double scanf ("%lf", &some_double);
always put a space between " and % when reading characters
Compiled By: Kamal Acharya
Escape Sequences