Chapter 3 IO Management printf and scanf function

  • Slides: 9
Download presentation
Chapter 3: I/O Management

Chapter 3: I/O Management

printf() and scanf() function § printf() and scanf() function are used as basic I/O

printf() and scanf() function § printf() and scanf() function are used as basic I/O function. § printf syntax: • printf(string, expression 1, expression 2, ……) § No limit on number of value can be that can be printed § Format string may contain ordinary character and conversion specification begin with % § Conversion specifier specifies compiler to convert into specific format. § Though compiler doesn’t check the conversion specification number should match.

Why following printf() is wrong § printf(“%d %d”, i); § printf(“ %d”, i, j);

Why following printf() is wrong § printf(“%d %d”, i); § printf(“ %d”, i, j); § int i; float x; printf(“%f %dn”, i, x);

Conversion Specifier § d deimal § e floating point is exponential § f Display

Conversion Specifier § d deimal § e floating point is exponential § f Display floating point number § c character § s string § %. 1 f display float value with one digit decimal § %4 d display with space in front (Right justification) § %-4 d display with space in back (Left Justification)

Example

Example

Escape Sequence § Non printing character having special meaning § Start with  §

Escape Sequence § Non printing character having special meaning § Start with § Some common escape sequence are

The scanf() function § Takes input from user, § Syntax • scanf(“string ”, &variable,

The scanf() function § Takes input from user, § Syntax • scanf(“string ”, &variable, ………. . ) § Never FORGET to use & before variable § Confusing printf() and scanf() …

Input fraction

Input fraction

Thank you

Thank you