input output management in c programming

17
PRINTF(), SCANF() CONVERSION SPECIFIERS ESCAPE SEQUENCES Input Output Management Compiled By: Kamal Acharya

Upload: kamal-acharya

Post on 06-May-2015

528 views

Category:

Education


2 download

DESCRIPTION

This slide provides the introduction to the input and output process available in C Programming.

TRANSCRIPT

Page 1: Input Output Management In C Programming

Compiled By: Kamal Acharya

PRINTF(), SCANF()CONVERSION SPECIFIERSESCAPE SEQUENCES

Input Output Management

Page 2: Input Output Management In C Programming

Compiled By: Kamal Acharya

INPUT and OUTPUT

Page 3: Input Output Management In C Programming

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()

Page 4: Input Output Management In C Programming

Compiled By: Kamal Acharya

Page 5: Input Output Management In C Programming

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:

Page 6: Input Output Management In C Programming

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:

Page 7: Input Output Management In C Programming

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’

Page 8: Input Output Management In C Programming

Compiled By: Kamal Acharya

Output:

A 65 41 101

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

Page 9: Input Output Management In C Programming

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.

Page 10: Input Output Management In C Programming

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|

Page 11: Input Output Management In C Programming

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

Page 12: Input Output Management In C Programming

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

Page 13: Input Output Management In C Programming

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

Page 14: Input Output Management In C Programming

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”

Page 15: Input Output Management In C Programming

Compiled By: Kamal Acharya

Page 16: Input Output Management In C Programming

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

Page 17: Input Output Management In C Programming

Compiled By: Kamal Acharya

Escape Sequences