A1363502154_23838_3_2018_for Unfor.ppt

  • Uploaded by: Vaibhav Pal
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View A1363502154_23838_3_2018_for Unfor.ppt as PDF for free.

More details

  • Words: 1,361
  • Pages: 29
Outline •



Formatted Input/Output functions –

printf() function



scanf() function

Conversion Specifiers

©LPU CSE101 C Programming

Introduction •

Presentation of output is very important.



Formatted functions scanf and printf :





these functions input data from standard input stream and



output data to standard output stream.

Include the header #include<stdio.h>

©LPU CSE101 C Programming

Streams •





Streams are sequence of bytes.

In input operations, the bytes flow from a device(e.g. keyboard, disk drive) to main memory. In output operations, bytes flow from main memory to device(e.g. display screen, printer).

©LPU CSE101 C Programming

Standard I/O Functions •



There are many library functions available for standard I/O. These functions categories:

are

–Unformatted functions –Formatted functions

©LPU CSE101 C Programming

divided

into

two

Formatted Functions •

With Formatted functions, input and output is formatted as per our requirement –



For example, if different values are to be displayed, how much field width i.e., how many columns on screen, is to be used, and how much space between two values is to be given. If a value to be displayed is of real type, then how many decimal places to output

Formatted functions are: –

printf()

©LPU CSE101 C Programming

Unformatted functions •





The unformatted functions work only with character data type. They do not require format conversion symbol for formatting of data types because they work only with character data type Unformatted functions are: –

getchar() and putchar()



getch() and putch()



gets() and puts()

©LPU CSE101 C Programming

Formatted output with printf function •

The printf() function: (Formatted output) printf() is an output function that takes text and values from within the program and sends it out onto the screen. Syntax

arg1,arg2,……….,argN); Inprintf(“format-control-string”, general terms, the printf function is written as:



The format-control-string can contain: –

Characters that are simply printed as they are



Conversion specifications that begin with a % sign

©LPU CSE101 C Programming

Example printf(“Area of circle is %f units \n”, area); In this :“Area of circle is %f units \n”control string. area

-

is a

is a variable whose value will be printed.

%f- is the conversion specifier indicating the type of corresponding value to be printed. ©LPU CSE101 C Programming

Printing Integers Integer values can be 0, 890, -328.



Conversion Specifier

Description

Example

d

Display as a signed/unsigned integer.

printf(“%d”, -890);

i

Display as a signed integer.

printf(“%i”, -890);

u

Display as an unsigned decimal integer.

printf(“%u”, 890);

h or l

Used before any integer conversion specifier to indicate that a short or long integer is displayed, respectively

printf(“%hd”, 890); printf(“%ld”, 800000000L)

©LPU CSE101 C Programming

#include <stdio.h> int main( void )

{ printf( "%d\n", 890); printf( "%i\n", 890); // i same as d in printf 890 890 890 -890 32000 200000000 890 3246435674

printf( "%d\n", +890 ); // plus sign does not //print printf( "%d\n", -890 ); //

©LPU CSE101 C Programming

Printing Floating-Point number Decimal point numbers like 0.01, 98.07 or -23.78



Conversion Specifier

Description

Example

e or E

Display a floating-point value in exponential notation.

printf(“%e”,-1234567.89);

f or F

Display floating-point values in fixedpoint notation

printf(“%f”,1234567.89);

g or G

Display a floating-point value in either the floating-point from f or the exponential form e based on the magnitude of the value

printf(“%g”, 1234567.89);

L

Used before any floating-point conversion specifier to indicate that a long double is displayed.

printf(“%lf”,1234567.89L);

©LPU CSE101 C Programming

#include <stdio.h> int main( void )

{ printf( "%e\n", 1234567.89 ); printf( "%e\n", -1234567.89 );//minus prints 1.234568e+006 -1.234568e+006 1.234568E+006 1234567.890000 1.234568e+006 1.234568E+006

printf( "%E\n", 1234567.89 );

printf( "%f\n", 1234567.89 ); printf( "%g\n", 1234567.89 );

©LPU CSE101 C Programming

Printing Strings and Characters Character = ‘A’ and String= “This is string” Conversion Specifier

Description

Example

c

Display a single character argument.

printf(“%c”, ‘A’);

s

Displays a string and requires a pointer to a character argument.

printf(“%s”, “This is String”);

©LPU CSE101 C Programming

#include <stdio.h> int main( void ) { char character = 'A'; // initialize char char string[] = "This is a string"; // initialize char array

printf( "%c\n", character ); A printf( "%s\n", This is string This is string

"This is a string" );

printf( "%s\n", string );

©LPU CSE101 C Programming

Other Conversion Specifier Pointer holds the address of another variable. Conversion Specifier

Description

Example

p

Display a pointer value

Int *ptr=&score; printf(“%p”, ptr); printf(“%p”, &score);

%

Displays the percent character.

printf(“a%%”);

©LPU CSE101 C Programming

#include <stdio.h> int main( void )

{ int *ptr; // define pointer to int int x = 12345; // initialize The value of ptr is 002ER443 The address int x of x is 002ER443 Printing a % in a format control string

ptr = &x; // assign address ©LPU CSE101 C Programming

How? •



Till now we have displayed numbers in left justified manner Consider the program that displays 1 12 123 1234

©LPU CSE101 C Programming

Printing with Field widths •





Field width: the exact size of field in which data is printed is specified by field width. The data is printed in the specified field and right justified. The integer representing the width size is inserted between percent sign(%) and the conversion specifier(e.g. %8d).

©LPU CSE101 C Programming

#include <stdio.h>

int main( void ) { printf( "%4d\n", 123 ); 123 1234 12345 -123 -1234 -12345

printf( "%4d\n", 1234 ); printf( "%4d\n\n", 12345 );

©LPU CSE101 C Programming

How? •

Dividing 7 by 3

Answer : 2.33333333333……

But the required output is 2.3333

©LPU CSE101 C Programming

Printing with Precision •







Specifies precision with which data is printed.

Precision with integer conversion specifier indicates the minimum number of digits to be printed. Precision with floating-point conversion specifier indicates the number of digits to appear after the decimal point. Precision with string specifier indicates the maximum number of characters to be written

©LPU CSE101 C Programming

#include <stdio.h> int main( void ) { int i = 873; // initialize int i

double f = 123.94536; // Using precision for integers 0873 initialize double f 000000873 Using precision for floating-point numbers 123.945 1.239e+002 124

char s[] = "Happy Birthday"; // initialize char array s Using precision for strings Happy Birth

©LPU CSE101 C Programming

How? Suppose the output required is First\one there “ There’s

©LPU CSE101 C Programming

Printing literals and escape sequences Escape sequence

Description

\’

Output the single quote(‘) character

\”

Output the double quote(“) character

\\

Output the backslash (\) character

\a

Cause an audible(bell)

\b

Move the cursor back one position on the current line

\n

Move the cursor to the beginning of the next line

\t

Move the cursor to the next horizontal tab position

©LPU CSE101 C Programming

Formatted Functions The scanf() function: (Formatted input) scanf() is a function that reads data from the keyboard. It interprets character input to the computer and stores the interpretation in specified variable(s). Syntax In general terms, the scanf function is written as: scanf (format-control-string, arg1, arg2,………., argN);



The format-control-string can contain: –

Describes the format of the input.

©LPU CSE101 C Programming

Example: scanf(“%s %d %f”, name, &age, &salary); In this :“%s %d %f”is a control string. name – is a string argument and it’s a array name and implicit memory address reference. age - is a decimal integer variable preceded by &. salary - is floating-point value preceded by &.

©LPU CSE101 C Programming

Reading data Conversion Specifier

Description

d

Read signed /unsigned decimal integer

i

Read a signed decimal integer

u

Read an unsigned decimal integer

h or l

Used before any integer conversion specifier to indicate that a short or long integer is to be input, respectively

e, E, f, g, G

Read a floating-point value

c

Read a character

s

Read a string

p

Read an address

©LPU CSE101 C Programming

#include <stdio.h> int main( void ) {

int a, c; float f; char day[10]; printf( "Enter integers: " );

Enter integers: -89 23 Enter floating-point numbers: 1.34256 Enter a string: Monday

scanf( "%d %u", &a, &c);

printf( "Enter floating-point

©LPU CSE101 C Programming

Next Class: Other Input/Output Functions

©LPU CSE101 C Programming

[email protected]

More Documents from "Vaibhav Pal"