3. Standard Input
• Standard input refers to the input using keyboard
• A program may need certain input from the user
• C language provides many functions to get intput from the user:
• scanf()
• gets()
• getch()
• getche()
• Include the header file <stdio.h> in program to call scanf() and gets()
and include the header file <conio.h> in program to call getch() and
getche() function.
4. Standard Output
• The term standard output refers to the output displayed on the monitor
• The result of a program is called the out of the program
• C language provides many functions to display program output to the
user
• Printf()
• Puts()
• Include the header file <stdio.h> in program to call printf() and puts()
5. printf ( ) function
• 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
• Give in double quotes
• May consist of the following:
• Text
• Format specifiers
• Escape sequence
• Consists of constants, variable
or expression are to be printed
on the screen
• Separated by comma
• Use of argument list is
optional
6. • Using Format String without Argument List
printf(“Hello World”);
Control String
• Using Format String with Single Argument
int m =80;
printf(“ Your marks are %d”, m);
Control String
• Using Format String with Multiple Arguments
int m=80;
char c=‘A’;
• Printf(“your marks are %d and grade %c”, m, c);
• One printf can print several lines by using newline escape sequence – ‘n’
printf( “ Welcome n to n C “);
Format strings can have
multiple format specifier, if
you are printing multiple
values
7.
8. Program 10.1
Write a program that displays a message and values of integer and character
variables.
#include<stdio.h>
#include<conio.h>
void main()
{
int n=10;
char ch='*';
clrscr();
printf("Testing Output...");
printf("%d",n);
printf("%c",ch);
getch();
}
9. Program 10.2
Write a program that adds two floating point numbers and shows the
sum on the screen
#include<stdio.h>
#include<conio.h>
void main()
{
float a=2.5,b=3.0,sum;
sum = a+ b;
clrscr();
printf(“%f + %f = %f ”,a,b,sum);
getch();
}
10. Program 10.3
Write a program that calculate and print area of square with given height and width
#include<stdio.h>
#include<conio.h>
void main()
{
int area,h,w;
h=2;
w=4;
area= h *w;
printf(“Area of square is %d”,area);
// printf(“ Area of square is %d when height is %d and width is %d”,area,h,w);
getch();
}
11. Format Specifier
• Format specifier is used to specify the format according to which values
will be read and display
• It determines the following things:
• Data type of Variable
• Field width
• Format of the value
• Format specifier is started with symbol %
• Different format specifiers are used with different types of variable
• Integer format specifier
• Character format specifier
• Floating Point format specifier
3 4 5 1
3 4 5 1
12. Format Specifier
• Integer Format Specifier
Format Specifier Type
%d Used for signed decimal integer value
%i Used for signed integer value
%o Used for unsigned octal value
%u Used for unsigned integer value
%x Used for unsigned hexadecimal value with lower case like a,b,c
etc.
%X Used for unsigned hexadecimal value with Upper case like A,B,C
etc.
13. Format Specifier (Cont..)
• Floating-Point Format Specifier
• Character Format Specifier
Format Specifier Type
%f Used for signed floating point value
%e Used for exponent value
%g Used for short notation value
Format Specifier Type
%c Used for character value
%s Used for strings
14. How integer values are printed in C language?
• The format specifier %d is used to display integer values in printf
functions.
• If an integer variable m contains 100 value then it can print by
following statement:
printf(“Your marks are %d”, m);
output:
Your marks are 100
15. How floating point values are printed in C
language?
• The format specifier %f is used to display floating point value in printf
function.
• If an value of float variable a is 90.55 . The following statement:
printf(“Average marks are %f”, a);
output:
Average marks are 90.550000
17. Field Width specifier for integer
• %d format specifier is used to display integer value in printf function
• A value can be written between % and d in “%d” format specified
Example
int area =25;
printf(“Area = %4d”,area);
• The value will appear right-justified and two spaces will appear before the actual
value
21. Escape Sequence
• Special characters used in format string to modify the format
of output
• Character are not displayed in the output
• Always begin with backslash “”.
• Backslash is called an escape character
22. Escape sequence Character represented
b Backspace
f Form Feed
n New Line
r Carriage return
t Tab
’ Single quote
” Double quote
xdd
ASCII code in hexadecimal notation. Each d represent
digit
ddd ASCII code in octal notation. Each d represents a digit
23. • b use to insert backspace in the output
• f (Form Feed page break)
• n (New line) – We use it to shift the cursor control to the new line
printf(
• r (Carriage Return) – We use it to position the cursor to the
beginning of the current line.
• t (Horizontal tab) – We use it to shift the cursor to a couple of spaces
to the right in the same line.
• ’ (Apostrophe or single quotation mark) – We use it to display the
single-quotation mark.
• ” (Double quotation mark) – We use it to display the double-
quotation mark.
• (Backslash) – We use it to display the backslash character.
• a (Audible bell) – A beep is generated indicating the execution of the
program to alert the user.
25. Scanf Function
• 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
• Syntax
• scanf(“format string”, &variable);
28. Character Input
• Scanf function can be used for character input
• Scanf function is not suitable for all situations, specially when enter
key is not require on any input ( e.g. arrow keys in games)
• Specialized function for character input are available
• getch()
• getche()
• These functions are part of conio.h library file
29. getch()
• 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
• Character entered by the user is not displayed on the screen
• The function is frequently used to pause program execution
Syntax: [var =] getch();
Variable It indicates the variable in which the character is stored. The
use of variable is optional
31. getche()
• The getche() 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
• Character entered by the user displayed on the screen
Syntax: [var =] getche();
Variable It indicates the variable in which the character is stored. The
use of variable is optional
33. gets() Function
• Used to input string value from the user.
• User press Enter key and string is stored variable
• The null character is automatically entered at the end of string
• Requires stdio.h header file to use this function
Syntax: gets( String_Variable );
Example printf(“Enter a string”);
gets(str);
Suppose if user input “Pakistan” then the string will stores in str as follows:
P a k i s t a n 0
34. puts() Function
• Used to display string on the screen.
• It can display a string constant or string variable
• Requires stdio.h header file to use this function
Syntax: puts( parameter );
Parameter It indicates the string variable in which the string is stored.
In case of string constant, it is written in double quotes
Example puts(str);
35. • Program 10.25
Write a program that inputs a string and displays it on a screen
#include<stdio.h>
#include<conio.h>
void main()
{
char book[ 50];
clrscr();
printf(“Enter name of your favorit book”);
gets(book);
printf(“Your favorite book is”);
puts(book);
getch();
}
Enter name of your favourite book : Holy Quran
Your favourite book is : Holy Quran
36. clrscr() Function
• Used to clear screen
• after clear the screen cursor blinks on the top-left corner
• Requires conio.h header file to use this function
Syntax: clrscr();
37. sizeof Operator
• Used to find the size of any data value
• Its give the number of bytes occupied by that value
Syntax: sizeof(operand);
Examples : sizeof(10);
sizeof(4.5);
sizeof(“Pakistan”);
sizeof(‘A’);