Sunteți pe pagina 1din 17

Input Output Management

P R I N T F ( ) , S C A N F ( )
C O N V E R S I O N S P E C I F I E R S
E S C A P E S E Q U E N C E S

Compiled By: Kamal Acharya


INPUT and OUTPUT

Compiled By: Kamal Acharya


Printing Output: printf()

 printf() will print formatted output to the screen.

 To print a message:
printf("This is a message\n");

this is a string literal

 How do we print the value of a variable?


 Answer: Use special format specifiers depending on the type of
the variable

Compiled By: Kamal Acharya


Compiled By: Kamal Acharya
int degreesF = 68;
printf("The temperature is %d degrees.", degreesF);

Specifier for “and the value of


“print an integer value” that number is read
from this variable”
Output:

> The temperature is 68 degrees.

Compiled By: Kamal Acharya


int a = 1;
int b = 2;
int c = 3;

printf(“%d plus %d is equal to %d", a, b, c);

Output:

1 plus 2 is equal to 3

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


Example:
char a='A';
printf("%c %d %x %0", a, a, a, a);

Output:

A 65 41 101

Compiled By: Kamal Acharya


Format specifier can be used with modifiers

Example :
%-6d, %5d, %6.2f

Modifier Description

digit 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!");

‘\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

Output:

Hello
World!

Compiled By: Kamal Acharya


Control Characters (Escape Sequences)

Character Description

'\n' newline
'\t' horizontal tab
'\v' vertical tab
'\r' carriage return
'\x41' hexadecimal number, 0x41
'\101' octal number 101
'\0' null character - indicates the end of a string

'\'' single quatation mark (')


'\"' double quatation mark (")
'\\' backslash mark (\)
'\b' backspace
'\f' formfeed - next page (used for printer)
'\a' 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 VERY IMPORTANT “Place value into this


“reading an integer value” special symbol variable”

Compiled By: Kamal Acharya


Compiled By: Kamal Acharya
Format specifiers for scanf()

 Format specifiers:
 %c for single characters
 scanf(" %c", &some_character);
always put a space between " and % when reading characters

 %d for integers
 scanf ("%d", &some_integer);
 %f for float
 scanf ("%f", &some_float);
 %lf for double
 scanf ("%lf", &some_double);

Compiled By: Kamal Acharya


Escape Sequences

Compiled By: Kamal Acharya

S-ar putea să vă placă și