0% found this document useful (0 votes)
12 views

Chap10 (ICS12)

ICS

Uploaded by

Faizan Mahmood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Chap10 (ICS12)

ICS

Uploaded by

Faizan Mahmood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

COMPUTER SCIENCE

12
(MS Access and C)

CHAPTER 10: Input and Output

Copyright @ IT Series www.itseries.com.pk


Topics
• Standard Input and Output
• printf Function
• Format Specifier
• Display Integer, Floating point, Character and String Values
• Use of Field Width Specifiers for Integers
• Use of Field Width Specifiers for Floating point Values
• Escape Sequences
• scanf Function
• Character Input
• getch Function
• getche Function
• gets Function
Copyright @ IT Series www.itseries.com.pk
Topics
• puts Function
• clrscr Function
• sizeof Operator

Copyright @ IT Series www.itseries.com.pk


The term standard input refers to the input using keyboard.
The process of
 A program may need certain inputs from the user giving something to
the computer is
known as input.
 C language provides many functions to get input from the user:
 scanf()
 gets() possible to perform mathematical operation on character values
 getch()
 getche()

Copyright @ IT Series www.itseries.com.pk


The term standard output refers to the output displayed on the monitor.
The process of
 The result of a program is the output of that program. getting something
from the computer
is known as output.
 C language provides many functions to display program output to the user:
 printf()
possible to perform mathematical operation on character values
 puts()

Copyright @ IT Series www.itseries.com.pk


 The printf function is used to display output on the monitor
 It can display text, constants, values of variables or expressions on monitor in specified format
Syntax printf(Format String, argument_list);

Format String Argument list


• Also called control string. • Consists of constants, variables or expressions whose
possible to perform mathematical
values operation
are to be on character
printed on values
the screen
• Given in double quotes.
• May consist of the following: • Separated by comma.
 Text • Use of argument list is optional.
 Format specifiers
 Escape sequences
Using Format String without Argument List

Copyright @ IT Series www.itseries.com.pk


Using Format String with Single Argument
int mark=80;

%d is a format specifier(conversion specifier)


marks the display position for a type integer
Output: Your marks are 75 variable

Using Format String with Multiple Arguments


int mark=80;possible to perform mathematical operation on character values
char c =‘A’
Format strings can
have multiple format
specifier, if you are
printing multiple values

Output : Your marks are 80 and grade is A.

 One printf can print several lines by using newline escape sequence – ‘\n’
 Each time the \n (newline) escape sequence is encountered, output continues at the beginning of the next line.
printf(“Welcome\nto\nC\n”);

Copyright @ IT Series www.itseries.com.pk


possible to perform mathematical operation on character values

Copyright @ IT Series www.itseries.com.pk


 Format specifier is used to specify the format according to which values will be read and displayed.
 It determines the following things:
 Data type of Variable
 Field Width
 Format of the value
 Format specifier is started with the symbol %.
possible to perform mathematical operation on character values
 Different format specifiers are used for different types of variables.
Integer format specifier

Copyright @ IT Series www.itseries.com.pk


Floating-Point Format Specifier

Character Format Specifier


possible to perform mathematical opera

tion on character values

Copyright @ IT Series www.itseries.com.pk


 Displaying Integer Values
 Format specifier “%d” is used to display integer values in printf function.
Example
int m = 100;
printf(“ Your marks are possible
%d”, m) to perform mathematical operation on character values
will display Your marks are 100
 Displaying Floating point Values
 Format specifier “%f” is used to display integer values in printf function.
float a = 70.55;
printf(“ Your average is %f”, a)
will display Your average is 70.550000

Copyright @ IT Series www.itseries.com.pk


 Displaying Character Values
 Format specifier “%c” is used to display character type values values in printf function.
Example
Char g = ‘A’;
printf(“ Your grade is %c”, g)
will display Your grade is A
 Displaying String possible
Values to perform mathematical operation on character values
 Format specifier “%s” is used to display string in printf function. \0 and is used to indicate
the end of string. It
consists of back slash
char name[ ] = “ICS”; and zero.

printf(“ Name is %s”, name)


Collection of characters written in
will display Name is ICS double quotations is called string or
string constant.

Copyright @ IT Series www.itseries.com.pk


The number of columns used to display a value on the screen is called field width.
 Format string may contain one or more field specifications.
Syntax
% flag width.precision
Flags
• Supplement formatting capabilities
possible
• Place flag immediately to perform
to the mathematical
right of percent operation on character values
sign (%)
• Several flags may be combined

Width It specifies total number of columns used to display value.


Precision It indicates number of columns used after decimal point.
 For each field specification, have one data value after format string, separated by commas.
• If width larger than data, default right justified
• If field width too small, increases to fit data
• Minus sign uses one character position in field

Copyright @ IT Series www.itseries.com.pk


The value specifies
 %d format specifier is used to display integer value in printf function the field width or
the number of
 A value can be written between % and d in "%d" format specified. columns will be
used for
displaying value.
Example
int area = 25;
printf(“Area = %4d", area);
Area = 25 possible to perform mathematical operation on character values
• Symbol  represents a blank space in the above line.
• The value will appear right-justified and two spaces will appear before the actual value.
Specifying Left Justified Output
 Symbol – is used with % sign to display the output left-justified.
Example
int n = 225;
printf(“%-10d”, n)

Copyright @ IT Series www.itseries.com.pk


Displaying Integer values (786 and -786 ) Using Different Format Specifiers

possible to perform mathematical operation on character values

 Symbol  represents a blank space in the output.

Copyright @ IT Series www.itseries.com.pk


 The format specifier “%f” is used to display floating point values in printf function.
 A value can be written between % and f in "%f" format specified.
 The total field width should be large enough to display all digits before and after the decimal point.
 Example -The total field width to display 15.245 should be six.
 A zero (0) is also printed beforetodecimal
possible performpoint for a number
mathematical that ison
operation smaller than values
character zero.
 Example - if the value is .5, it will appear as 0.5.
Syntax
The general format for the format specifier for floating point is as follows:
%m.nf
% It specifies the beginning of a format specifier.
m It represents the total field width including decimal point and fractional part.
n It represents the desired number of decimal places.

Copyright @ IT Series www.itseries.com.pk


Examples
printf("%8.2f", n);
 The above statement will display a float value using space of eight characters
 The space of two columns is used for the digits after decimal point
 The value of n will be rounded off to two decimal places
 The second digit of fractional part is increased by 1 if the third digit of fractional part is 5 or more. Otherwise,
the third is discarded
 float n= 20.346; the above statement will display 20.35
 float n = 20.3444, the above statement will display 20.34
possible to perform mathematical operation on character values
 If the value needs more space than the width specifies, the additional space is automatically used
Formatting Type double Values

Copyright @ IT Series www.itseries.com.pk


possible to perform mathematical operation on character values

Copyright @ IT Series www.itseries.com.pk


possible to perform mathematical operation on character values

Copyright @ IT Series www.itseries.com.pk


 Special characters used in format string to modify the format of output. e.g.
 Characters are not displayed in the output
 Always begin with backslash "\"
 backslash (\) is called an escape character
\n This escape sequence is used to insert new line in output.
Remember to put \n in
Example possible (“ perform
printf to Hello\nWorld ”); will operation
mathematical display on character values
double quotations marks
Hello
World
\t is used to insert a TAB in the output.
Example printf (“ Hello\tWorld ”); will display Hello World
\“ is used to display double quotes in the output.
Example printf( “ \"Hello World\" ”); will display “Hello World”
\‘ is used to display single quotes in the output.
Example printf( “ \'Hello World\ ' ”); will display ‘Hello World’

Copyright @ IT Series www.itseries.com.pk


\r is used to move the cursor at the beginning of current line
It is known as carriage return
Example printf ( “ Hello\rWorld ” );
• will display Hello.
• \r will move the cursor at the beginning of current line
• Then the word "World" will appear that overwrites "Hello"
• only World is displayed on screen
\b is used to insert backspace in the output
possible to perform mathematical operation on character values
Example printf (“ Hello\bWorld ” );
• will display Hello.
• The \b will delete the letter o
• Then the word World will appear
• The output will appear as HellWorld
\f Also called form feed is used to insert a blank paper in the printed output
Example printf( “ Hello\fWorld ” );
• will print Hello and the computer will include a blank paper. Then the word World will appear

Copyright @ IT Series www.itseries.com.pk


possible to perform mathematical operation on character values

Copyright @ IT Series www.itseries.com.pk


 Read data from the standard input device (usually keyboard) and store it in a variable
 Requires stdio.h header file used to read input from keyboard & (address of) operator
causes the address of
Syntax the variable to be passed
to scanf rather than the
scanf(“Format string”, &variable); value in the variable

Format String &Variable


possible to perform mathematical operation on character values
• Given in double quotes. • An ampersand (&) called address operator in C followed by
• It may consist of different format specifiers. the variable name.
• Format specifiers indicates the type of data • The ampersand (&), when combined with the variable name,
that should be input by the user. tells scanf the location (or address) in memory at which the
• Common format specifier used in scanf variable is stored.
functions. • The computer then stores the value for variable at that
location.

Copyright @ IT Series www.itseries.com.pk


Getting Single Input
 scanf function is normally used in combination with printf function
Prompt ‘waits’ on same
 When using scanf for input, it is best to first issue a prompt line for keyboard input.

 Including printf prompt before scanf maximizes user-friendly input/output


printf (“Enter a number:”);
This message is called a prompt because
it tells the user to take a specific action

possible to perform mathematical operation on character values

 Waits for user input, then stores the input value in the memory space that was assigned to n
Getting Multiple same Inputs
printf(“Please enter your height and weight:”);
scanf(“%f%f”, &height, &weight);
Getting Multiple different Inputs
scanf(“%d %d %f”, &n1, &n2, &avg); // will get different types of input

Copyright @ IT Series www.itseries.com.pk


possible to perform mathematical operation on character values

Copyright @ IT Series www.itseries.com.pk


possible to perform mathematical operation on character values

Copyright @ IT Series www.itseries.com.pk


The process to enter
characters into
 C provides different functions to input characters. computer is called
character input
 The scanf() function can be used for character input.
 C language also provides functions that are specially used for characters input
 getch()
 getche() possible to perform mathematical operation on character values

 Requires conio.h header file to use these functions

Copyright @ IT Series www.itseries.com.pk


The user does not
need to press Enter
 The getch function is used to input single character from the user. key to complete the
input.
 Requires conio.h header file to use this function.
 When this function is executed, it waits for any key to be pressed.
 Character entered by the user is not displayed on the screen.
 possible
The function is frequently usedtotoperform
pause mathematical operation on character values
program execution.
Syntax
getch();
Another way to use this function is as follows:
variable = getch();

variable It indicates the variable in which the character is stored. The use of variable is optional.

Copyright @ IT Series www.itseries.com.pk


 The getch function is used to input single character from the user.
 Requires conio.h header file to use this function.
 When this function is executed, it waits for any key to be pressed.
 The function echoes (displays) the character
on the screen enteredpossible
by the user.
to perform mathematical operation on character values
Syntax
getche();
Another way to use this function is as follows:
variable = getche();
variable

It indicates the variable in which the character is stored. The use of variable is optional.

Copyright @ IT Series www.itseries.com.pk


possible to perform mathematical operation on character values

The above program uses getche() function to input character. The function stores the entered character in
variable c without waiting to press Enter. The printf statement displays the character and its ASCII code on the
screen.

Copyright @ IT Series www.itseries.com.pk


Collection of
characters written in
 The gets function is used to input string value from the user. double quotations is
called string or
string constant.
 Input is stored in a string variable.
 The user can enter any type of letters including alphabets, digits, special symbols and spaces.
 The user also needs to press Enter key after typing the string to complete the input.
 The null character \0 possible
is automatically entered
to perform at the endoperation
mathematical of string. on character values
\0 and is used to
Syntax indicate the end of
string. It consists of
back slash and zero.
gets(); Requires stdio.h
header file to use
this function
Example
The following statement inputs a string from user and stores it in a string variable str:
printf(“Enter a string: ”);
gets(str);

Copyright @ IT Series www.itseries.com.pk


 The puts function is used to display string on the screen..
 It can display a string constant or string variable.
Syntax
puts(parameter);
parameter possible
Indicates to perform
the string variablemathematical operation
in which the string on character
is stored. In casevalues
of string constant, it is written
in double quotes.
Example
 Displays the contents of a string variable str on screen:
Requires stdio.h
puts(str); header file to use
this function
 Displays a string constant on the screen:
puts(“Programming makes life interesting.”);

Copyright @ IT Series www.itseries.com.pk


possible to perform mathematical operation on character values

Copyright @ IT Series www.itseries.com.pk


 The function is used to clear the contents of the screen.
 When this function is executed, the screen is cleared and the cursor moves to the start of first line on the
screen.
Syntax
clrscr(); Requires conio.h
possible to perform mathematical operation
header file to useon character values
this function

Copyright @ IT Series www.itseries.com.pk


 The sizeof operator gives the size in number of bytes of any data type or variable

Syntax possible to perform mathematical operation on character values

sizeof(operand); The operand can be a variable or a constant.

Examples
sizeof(n);
sizeof(10);
sizeof(4.5);
sizeof(“Pakistan”);

Copyright @ IT Series www.itseries.com.pk


possible to perform mathematical operation on character values

Copyright @ IT Series www.itseries.com.pk

You might also like