0% found this document useful (0 votes)
47 views12 pages

PST Class-8

The document discusses input/output (I/O) statements in C programming. It describes: 1) Printf statements are used to display or write data to the screen. They have a format string and optional variables to print. 2) Scanf statements are used to read data from keyboard input. They have a format string specifying data types and address variables to store input. 3) Guidelines for printf and scanf include matching the number of format specifiers and variables, and using the correct data type conversions.

Uploaded by

Saish Mello
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)
47 views12 pages

PST Class-8

The document discusses input/output (I/O) statements in C programming. It describes: 1) Printf statements are used to display or write data to the screen. They have a format string and optional variables to print. 2) Scanf statements are used to read data from keyboard input. They have a format string specifying data types and address variables to store input. 3) Guidelines for printf and scanf include matching the number of format specifiers and variables, and using the correct data type conversions.

Uploaded by

Saish Mello
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/ 12

10/19/2020 1

I/O statements
KLE’S BCA HUBLI

Output: Print statement


✓ Print statements are used to display or write data on to screen
printf(): print format
✓ There are two simple forms of printf:
❖ 1. One that has just a literal string (a sequence of characters within
quotation marks)
❖ General form:
printf (“literal string”);
Example : printf (“KLE BCA”);
❖ Literal string is placed within a parenthesis. The symbols within that
string are printed.
10/19/2020 2
KLE’S BCA HUBLI

❖ 2. One that has values of one or more expressions to be printed.


❖ General form(Syntax) :
printf (“………” , ……);
(i) (ii)
printf(“format or Control string”, list of variables);
(i) Control String, including conversion characters along with
message
(ii) list of variables or expressions to be printed.

❖ Example: int x=20; float y=34.56; char ch=‘z’;


printf(“%d %f %c” , x, y, ch);
printf (“integer value= %d\n float value=%f \t character value=%c” , x,
y,ch);
10/19/2020 3

Another general syntax for printf() with return value


n= printf(“Format String”,list of variables);

Here the function returns integer value.


This returned value is number of characters that have been
successfully printed on the screen.
Ex: int x=10,y=20,n;
n = printf(“%d %d”,x,y); //output : 10 20
printf(“\nValue of n= %d”,n); //output: 5
10/19/2020 4
KLE’S BCA HUBLI

Guidelines/Rules for printf()


✓ A printf() always contains a literal string or format string in quotation
marks.
✓ The control string may or may not be followed by some variables or
expressions whose value we wants to print.
✓ Each value to be printed needs a ‘conversion specification’ like %d to
hold its place in the control string. This conversion specification
describes the exact way the value is to be printed..
✓ Number of format specifiers should be equal to number of varibles .
And need to maintain the order also.
✓ When printf() is executed each conversion specification is replaced by
the value of the corresponding expression, then print according to the
rules in specification.
✓ The symbols \n or \t in control string tell the machine to skip to new
line or tab. It affects the appearance of the output but not displayed as
part of it.
10/19/2020 5
KLE’S BCA HUBLI
Format specifiers(Control String) used in
printf()
10/19/2020 6

#include <stdio.h>
int main()
{
int a = 67;
float b = 12.67;

printf("%d\n", a); //output: 67


printf("%i\n", a); //output: 67
printf("%o\n", a); //output: 103 ...%o octal integer without leading zero.
printf("%x\n", a); //output: 43%x hexadecimal integer without 0x before the
number.

printf("%f\n", b); //output: 12.670000


printf("%e\n", b); //output: 1.267000e+01
return 0;
• }
10/19/2020 7

//Integer may be octal or in hexadecimal : %i


#include <stdio.h>
int main()
{
int a;
scanf("%i", &a); // input is 017 (octal of 15 )
printf("%d\n", a);
scanf("%i", &a); // input is 0xf (hexadecimal of 15 )
printf("%d\n", a);
return 0;
}
10/19/2020 8
KLE’S BCA HUBLI

Scan statement
▪ Scan statements are used to read data from keyboard
▪ Syntax:  
scanf (“format string”, address list);
▪ Example :
scanf (“%d%f%c”,&x,&y,&z);

format specifiers address list


 
✓  A format specifiers starts with % sign and is followed by conversion code (i.
e., d, f, c, s)
✓ Address list is a list of variables, each variable name must be preceded by
an (&) ampersand symbol.
✓ The numbers of variables must be equal to the number of format specifiers.
✓ ‘&’ is an ‘Address of’ operator. It gives the location number used by the
variable in memory.
10/19/2020 9

▪ General syntax of scanf() with return value


▪ n=scanf (“format string”, address list);

▪ Here the function scanf() returns an integer value. This


returned value is the number of items that have been
read successfully from the keyborad.

▪ For ex: n=scanf(“%c%d%f”, &x,&y,&z); //input: A 10 2.5


printf(“Value of n= %d”,n); // output: 3
Guidelines/Rules for scanf()
10/19/2020 10
✓ Always a format string should be enclosed withinKLE’S BCAdouble
HUBLI quotes.
✓ No words, punctuation symbols, escape sequence characters (such
as \t, \n etc) are allowed, only format specifiers are used in format
string.
✓ A format specifiers starts with % sign and is followed by conversion
code (i.e., d, f, c, s)
✓ Format string and address list separated by comma. Each variable
name should be preceded by & symbol. And within addresslist
variable names must be separated by comma.
✓ There must be format specifier for each variable. number of format
specifiers must be equal to the number of variables. And need to
maintain the order.
10/19/2020 11
KLE’S BCA HUBLI

The variables present in scanf must represent the address of memory


locations
Example:
int a;
scanf(“%d”,&a); //valid
The type conversion specified in each format specifier must match
with the corresponding type of variable present.
Sample program- for I/O statements
10/19/2020 12

▪ Expected output:
Enter integer number
---------
Entered number is = ------

#include<stdio.h>
void main()
{
int num;
printf(“Enter integer number\n”);
scanf(“%d” , &num);
printf(“Entered number is= %d”, num);
}

You might also like