number input output statement2

Post on 20-Jul-2016

11 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

C++

TRANSCRIPT

printf() and scanf()

The Screen Output Function: printf()

The most common output function

Used to write information to the screen

– Format: printf(<format string>);

Where: <format string>- string that begins and ends with

double quotes

The <format string> may contain two types of items:

– String

– Escape sequence

The Screen Output Function: printf()

String– A sequence of characters that will be printed on the

screen

– Example: printf(“Think Positive!”);

Escape Sequence– Starts with the escape code “\”.

– Escape code is a character that indicates that a following character in a data stream is not to be processed in the ordinary way.

– Example: printf(“Think\t Positive!\n”);

Escape Sequences

\a audible bell

\n newline (line feed)

\t tab

\r carriage return

\f form feed

\v vertical tab

\” double quote

\\ backslash

The Screen Output Function: printf()

An extended printf() format is:

– Format: printf(<format string>, <argument list>);

Where: <format string>- string that begins and ends with

double quotes

<argument list> - list of variables, constants,

expressions, or function calls whose values will

be printed

The <format string> may contain two types of items:

– Strings and/or escape sequence

– Format specifiers

The Screen Output Function: printf()

Strings and/or escape sequence

Example:

#include <stdio.h>

main()

{

printf(“You are\n\n”);

printf(“\tmy \”SUNSHINE\”!”);

return 0;

}

You are

my “SUNSHINE” !_

The Screen Output Function: printf()

Format Specifiers– Define the way to display the arguments that follow the

<format string>

– Starts with a percent sign ‘%”

Example:– printf(“The product of 11 and 10 is %d.\n” 11*30);

The product of 11 and 10 is 330.

_

– printf(“The product of %i and %i is %i.\n”,11,30,11*30);

The product of 11 and 30 is 330.

_

printf Format Specifiers

%c a single character

%s string of characters

%i decimal integer

%e scientific notation

%f decimal floating point

%o octal (base 8)

%d decimal (base 10) integer

%u unsigned decimal

%x hexadecimal (base 16)

%% prints a % sign

Setting Field Width and Justification

Format Description

– Controls the number of

decimal places displayed

by printf() as well as the

width output.

– Display a double (a

decimal number with

double precision) data in

any format

– Examples: 123, 45, 6789

and Total:

COLUMNS

1 2 3 4 5 6 7

1 2 3

4 5

6 7 8 9

T o t a l :

Setting Field Width and Justification

To set the width of the output field in whichdata is printed out right justified, place aninteger (whole number) between the % andthe format specifier,

Format:

%<fieldwidth specifier> <format specifier>;

For example, to set a 7-character field width,type %7d

Setting Field Width and Justification

For example,

#include <stdio.h>

int main()

{ printf (“%10d\n”, 123);

printf (“%10d\n”, 4567);

printf (“%10d\n”, 89);

printf (“%7d\n”, 89);

printf (“%5d\n”, 89);

printf (“%d\n”, 123);

scanf(“%c”);

return 0;

}

COLUMNS

1 2 3 4 5 6 7 8 9 10

1 2 3

4 5 6 7

8 9

8 9

8 9

1 2 3

Setting Field Width and Justification

You can force the data to be left-justified by

placing a minus sign(-) directly after the %

sign

Format:

%<- fieldwidth specifier> <format specifier>;

For example, % -10s will left-justify a string in

a ten-character field

Setting Field Width and Justification

For example,

#include <stdio.h>

int main()

{ printf (“%10d\n”, 123);

printf (“% -10d\n”, 123);

printf (“% -10d\n”, 4567);

printf (“% -5d%-3s%-2c”, “LEFT”, ‘X’, ‘Y’);

printf (“%-5d %-3s”, “LEFT”, ‘X’, );

scanf(“%c”);

return 0;

}

COLUMNS

1 2 3 4 5 6 7 8 9 10

1 2 3

1 2 3

4 5 6 7

L E F T X Y

L E F T X

Setting Decimal Places

The number of decimal places of floating point

numbers (numbers with decimal parts) can be

specified by placing a decimal point after the field-

width specifier, followed by the decimal places you

want to display

Format:%<fieldwidth specifier>.<no. of decimal places><format specifier>;

For example, %10.3f displays a number at least ten

characters wide with three decimal places

Setting Decimal Places

For example,

#include <stdio.h>

int main()

{ printf (“%f\n”, 123.4567);

printf (“%10.3f\n”, 123.4567);

printf (“%10.0f\n”, 123.4567);

printf (“%10f\n”, 123.4567);

printf (“%5.2f\n”, 123.4567);

printf (“%5i\n”, 123.4567);

return 0;

}

COLUMNS

1 2 3 4 5 6 7 8 9 10

1 2 3 . 4 5 6 7 0 0

1 2 3 . 4 5 7

1 2 3

1 2 3 . 4 5 6 7 0 0

1 2 3 . 4 6

1 2 3 . 4 5 6 7 0 0

Formatting String Constants

Decimal point format can also be used on string

constants

Format:

%<fieldwidth specifier>.<maximum field length>s;

For example, %6.9s displays a string of at least six

characters and not more than nine

If the string is longer than the maximum field width,

the characters are truncated off the right end.

Formatting String Constants

For example,

#include <stdio.h>

#include <conio.h>

int main()

{ clrscr();

printf (“%s\n”, “Special!”);

printf (“%4.7s\n”, “Special!”);

printf (“%7.0s\n”, “Special!”);

printf (“%7.4\n”, “Special!”);

printf (“%10s\n”, “Special!”);

return 0;

}

COLUMNS

1 2 3 4 5 6 7 8 9 10

S p e c i a l !

S p e c i a l

S p e c

S p e c i a l !

The Basic Data Types and their Sizes

Example, the code:

double a=3.14;

printf(“%le \n”,a);

printf(“% .2le \n”,a);

printf(“% .4le \n”,a);

a=500.777777777;

printf(“%lf \n”,a);

printf(“% .2le \n”,a);

printf(“% .4le \n”,a);

Will produce

3.140000e+00

3.14e+00

3.1400e+00

5.007778e+02

5.01e+02

5.0078e+02

The INPUT Function: scanf()

Same syntax as the printf(), the only

difference is that the series of characters only

contains the format specifier that will be used

to interpret the value passed by the user

Format

scanf(<format string>, <address1>, <addr>, …)

– Where: format string is preceded by a %sign and

tells scanf() what type of data is to be read next

Scanf() Format Specifiers

%d, %i Read a decimal number

%o Read an octal number

%x Read an hexadecimal number

%c Read a single character

%s Read a string or sequence of characters

%f Read a float value

%e Read a double value

%h Read a short integer

%u Read an unsigned decimal

The INPUT Function: scanf()

All the variables used to receive values through scanf()must be passed by their addresses

All arguments must be pointers to the variables used asarguments

Examples:– scanf(“%d”, &salary); /*waits for you to type an integer which

can be stored at the address associated with the variable salary*/

– scanf(“%d%d”, &a,&b); /*waits you to type in two integer valuesseparated by a space(or a tab or a Enter keypress) */

– scanf(“%d”); /* a technique in still holding onto theUser screen for you to view your output, because if you don’t, theprogram will immediately go back to the IDE screen when it isdone with program execution */

The INPUT Function: scanf()

The presence of other characters in thecontrol string or <format string> aresignificant– A white-space character causes scanf() to skip

over one or more white-space characters in theinput stream (i.e. “%s %s”)

– A non-white-space character causes scanf() toread and discard a matching character

(i.e. “%s, %s”, &x, &y)

top related