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

Module2 Part1

The document discusses input and output operations in C programming. It covers functions like getchar(), putchar(), scanf() for reading input from the keyboard or command line. It also discusses reading different data types like integers, characters, strings and real numbers from the user.

Uploaded by

kumarravi955r
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Module2 Part1

The document discusses input and output operations in C programming. It covers functions like getchar(), putchar(), scanf() for reading input from the keyboard or command line. It also discusses reading different data types like integers, characters, strings and real numbers from the user.

Uploaded by

kumarravi955r
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

MODULE- 2

Managing input and output operations


Reading a character
The simplest of all input/output operations reading a character from the „standard input‟(usually keyboard) and
writing it to the „standard output‟(usually the screen).Reading a single character can be done by using the
function getchar.
getchar()
• getchar() is used to
→ read a character from the keyboard one at a time and
→ store this character into a memory-location
• You have to press ENTER key after typing a character.
• The syntax is shown below:
char variable_name = getchar( );
• For ex:
char z;
z= getchar( );
• Example, program that displays a question of yes/no to user and reads the user input in single
character(Y or N). if the response is Y or y, it outputs the message “my name is busy bee”
otherwise it outputs “you are good for nothing”.
#include<stdio.h>
main()
{
char ans;
printf(“would you like to know my name?\n”);
printf(“type y for yes and n for no\n”);
ans=getchar();
if(ans==‟y‟||ans==‟y‟)
printf(“my name is busy bee\n”);
else
printf(“you are good for nothing\n”);
}
Output:
would you like to know my ame?
Type y for yes and n for no
Y
my name is busy bee
would you like to know my name?
type y for yes and n for no
N
you are good for nothing

Writing a character
putchar( )
• putchar( ) is a function used for writing characters one at a time to the terminal.
• It takes the form putchar(variable_name); where, variable_name is a type char variable containing a
character.
answer=’Y’;
putchar(answer); will display the character „Y‟ on the screen.
• The statement displays the character contained in the variable_name at the terminal.
• Example, program to read a character from the keyboard and then print it in reverse case .

Page 1
#include<stdio.h>
#include<ctype.h>
main()
{
char a;
printf(“enter an alphabet\n”);
putchar(„\n‟);
alphabet=getchar();
if(isslower(alphabet))
putchar(toupper(alphabet));
else
putchar(tolower(alphabet));
}
OUTPUT:
enter an alphabet
a
A
enter an alphabet
Q
q
FORMATTED INPUT
Formatted input refers to an input data that has been arranged in a particular format.
scanf()
• The scanf function does following tasks:
→ Scan a series of input fields one character at a time
→ Format each field according to a corresponding format-spesifiers passed in control string or format-string
(format means convert).
→ Store the formatted input at an address passed as an argument i.e. address-list
• Syntax is shown below:
scanf("controlstring", arg1,ag2…argn);
where control or format-string contains one or more format-specifier.
arg1 to argn are the addresses of memory where data are to be stored.
• For ex:
scanf("%d %f %c", &x, &y, &z);
• The format string consists of a conversion character % and a data type character(also known as type
specifier) and an optional number specifying the width of the field.
• The blanks, tabs and newlines in the control strings are ignored. Some of the format specifiers are
listed below.
Format specifiers Meaning
%d an int argument in decimal
%ld a long int argument in decimal
%c a character
%s a string
%f a float or double argument
%e same as %f, but use exponential notation
%o an int argument in octal (base 8)
%x an int argument in hexadecimal (base 16)
%u read a unsigned decimal integer
%[…] read a string of words
Inputting integer numbers
• The field specification of reading the integer number is %wsd, where % is a conversion character, w is
a integer number that specifies the field width of the number to be read and d is the data type character
for integer.

Page 2
• Example, scanf(“%2d %d”, &num1, &num2); will read 32 in to „num1‟ and 555 to „num2‟ for the inputs
32 and 555.
• When the scanf reads a particular value, reading the value will be terminated as soon as the number
of characters specified by the field width is reached.
• Reading input data may be skipped by specifying * in the place of field width. Example,
Scanf(“%d %*d %d”, &a,&b);
Will assign the data 123 456 789, „a‟ will be assigned with 123, 456 is skipped and 789 will be
assigned to „b‟.
• Example program to illustrate the reading of integer
values. #include<stdio.h>
void main( ) OUTPUT:
{ enter three integer numbers
int a,b,c x,y,z,p,q,r; 123
printf(“enter three integer numbers\n”); 1 3 -3577
scanf(“%d %*d %d”, &a, &b, &c); enter two 4-digit numbers
printf(“%d %d %d \n\n”,a,b,c); 6677 4433
printf(“enter two 4-digit numbers\n”); 66 77
scanf(“%2d %4d”,&x,&y); enter two integers
printf(“%d%d\n\n”,x,y); 44 66
printf(“enter two integers\n”); 4433 44
scanf(“%d%d\n\n”,&a,&x); enter nine digit number
printf(“%d%d\n\n”,a,x); 123456789
printf(“enter nine digit number\n”); 66 1234 567
scanf(“%3d %4d %3d”, &p,&q,&r); enter two three digit numbers
printf(“%d %d %d\n\n”,p,q,r); 123 456
printf(“enter two three digit numbers\n”); 89 123
scanf(“%d%d”,&x,&y);
printf(“%d %d”,x,y);
}

Inputting real numbers


• While inputting the real numbers, the field width is not to be specified in the scanf.
• Scanf reads the real numbers using simple specification %f for both notations namely decimal point
notation and exponential notation.
• Example, scanf(“%f %f %f”, &x, &y, &z);
• If the number to be assigned is double type, then the specification should be %lf instead of simple %f.
• Example, Reading a real number
#include<stdio.h> OUTPUT:
main()
{ values of x and y
float x,y;
12.3456 17.5e-2
double p,q;
X=12.3456
printf(“values of x and y\n”);
scanf(%f %e”,&x, &y);
Y=0.175000
printf(“\n”); values of p and q
printf(“x=%f\ny=%f\n\n”,x,y); p=4.142857142857
printf(“values of p and q\n”); q=1.856789012346e+001
scanf(“%lf %lf”,&p,&q);
printf(“\n\np=%.12lf\np=%.12e”,p,q);
}

Page 3
Inputting Character string
• A scanf function can input strings containing more than one character. Following are the specificationfor
reading the character strings.
%wc or %wc
• The corresponding argument should be a pointer of character array.
• %c may be used to read a single character when the argument is a pointer to a char variable.
• Example, reading a string using %ws and %wc is as follows
st
• Note that the %s terminates reading at the encounter of a blank space. Hence, name2 reads only 1 part
of “new York” and second part is automatically assigned to name3
• During second run, the string “new-york” is correctly assigned to name2.

#include<stdio.h> OUTPUT:
main()
{ Enter serial number and name1
Int no; 1 123456789012345
Char name1[15],name2[15],name3[15]; 1 123456789012345
Printf(“enter serial number and name1\n”); Enter serial number and name2
Scanf(“%d %15c”,&no,&name1); 2 new York
Printf(“%d %15s\n\n”,no,name1); 2 new
Enter serial number and name3
Printf(“enter serial number and name2\n”); 2 London
Scanf(“%d %s”,&no,&name2); 2 York
Printf(“%d %15s\n\n”,no,name2); Enter serial number and name1
Printf(“enter serial number and name3\n”); 1 123456789012
Scanf(“%d %15s”,&no,&name3); 1 123456789012
Printf(“%d %15s\n\n”,no,name3); Enter serial number and name2
} 2 new-York
2 new-York
Enter serial number and name3
3 London
3 London

• Scanf supports the following conversion specifications for strings:


%[characters]
%[^characters]
• The specification %[characters] allows only the characters specified within the brackets in to the
input string.
• The string get terminated when the first character that is not specified in [ ] is encountered.
• %[^characters] does not allow the charactes that are specified in the [^ ] reading of characters of
the string will be terminated at the encounter of one of these characters.
• Blank spaces can be read using %[].
Points to remember while using scanf()
1. All function arguments, except control string must be pointers to variables.
2. Format specifications in the control string should match the variables receiving the inputs in the same
order.
3. Input data must be separated by spaces and must match the variables reciving the input in same order.
4. The reading will be terminated when mismatch occurs between the read data and format specifier.
5. Scanf ignores blank spaces in the input and reads the next value.
6. Any unread data items will be considered as data input for next scanf.

Page 4
Rules for scanf()
1. Each variable read must have a field specification.
2. For each field specification, there must be a variable address of proper type.
3. Any non white space character in the format string must have a matching character in the user input.
4. Never end the format string with the white space. It is a fatal error.
5. The scanf() reads until:

A whitespace character is found in a numberic specification, or

The maximum number of characters have been read or

An error is detected or

The end of file is reached.
FORMATTED OUTPUT
• The printf function does following tasks:
→ Accept a series of arguments
→ Apply to each argument a format-specifier contained in the format-string
→ Output the formatted data to the screen
• The syntax is shown below:
printf(_”control string”,arg1,arg2,….argn);
• Control string consists of following three types of items:
1. Characters that will be printed on the screen as they appear.
2. Format specifiers that define the output format for display of each item.
3. Escape sequence characters such as \n,\t,\b.
• The arguments arg1, arg2… argn are the variables whose values are formatted and printed according to
the format specifiers.
• The syntax of format specifier is as follows.
%w p type-specifier
Where, w specifies the total number of columns for the output.
P is a integer that specifies the number of digits to the right of the decimal point(of a real number).
Both p and w are optional.
Output of integer numbers
• The format specification of printing the integer number is
%wd
• Where w specifies the minimum column/field width of the output. If number to be printed is greater than
the w, it will be printed in full overriding the minimum specification. And d specifies that the value to be
printed is an integer.
• The number will be printed to right justified for the width.
th
• It is possible to force the left justification by placing – sign after % character as shoen in 4
example below.
th
• It is possible to place zeros in the blank spaces by placing 0 after the % as shown in 5 example.
• Example,
1. Printf(“%d”,9876); 9 8 7 6

2. Printf(“%6d”9876);
9 8 7 6
3. Printf(“%2d”9876);
9 8 7 6

4. Printf(“%-6d”9876);
9 8 7 6

5. Printf(“%06d”9876);
0 0 9 8 7 6

Page 5
• Long integers can be printed by specifying ld in the place of d in the format specification.
• Similarly hd for printing the short integers.
• Example, illustrates the output of integer numbers under various formats.
#include<stdio.h> OUTPUT:
main()
{ 12345
int m=12345; 12345
long n=987654; 0000012345
printf(“%d\n”,m); 12345
printf(“%10d\n”,m); 98765
printf(“%010d\n”,m);
printf(“%-10d\n”,m);
printf(“%10ld\n”,n);
}

Output of real numbers


• The format specification of printing the integer number is
%w.p f (or) %w.p e
• Where w indicates the minimum number of positions that are to be used for the display of the value, p
indicates the number of digits to be displayed after the decimal point the default precision is 6
decimal places.
• printf() prints the values right justified to the width w.
• examples,

1. Printf(“%7.4f”,y);
9 8 . 7 6 5 4
2. Printf(“%7.2f”,y);

9 8 . 7 7
3. Printf(“%-7.2f”,y);

9 8 . 7 7
4. Printf(“%f”,y);

9 8 . 7 6 5 4
5. Printf(“%10.2e”,-y);

9 . 8 8 e + 0 1
6. Printf(“%-11.4e”,y);

- 9 . 8 7 6 5 e + 0 1
7. Printf(“%-10.2e”,y);

9 . 8 8 e + 0 1
8. Printf(“%e”,y);

9 . 8 7 6 5 4 0 e + 0 1

Page 6
• We can also use the following form to take w and p in printf()
printf(“%*.*f”,width,precision,numbr);
• Example, printf(“%*.*f”,7,2,number); is equal to printf(“57.2f”,number);

• Example, to illustrate the output of integer number under various formats.


#include<stdio.h> OUTPUT:
void main() 12345
{ 12345
int m=12345; 0000012345
long n=987654; 12345
printf(“%d\n”,m); 987654
printf(“%10d\n”,m); -987654
printf(“%010d\n”,m);
printf(“%-10d\n”,m);
printf(“%10ld\n”,n);
printf(“%10ld\n”,-n);
}

Printing of a single character

• A single character can be printed in the desired position using the format
%wc
• Character will be displayed right justified in the field of w columns. We can make the display left justified using
– sign immediately after %sign and before the integer w.

Printing of string

• The format of printing a string is as follows,


%w.ps
Where w specifies the field width, P is used to specify the number of characters to be printed.
• The output is right justified.
• Example, to print the string “new delhi 110001” contains 16 characters including blanks.

%s

n e w d e l h i 1 1 0 0 0 1
%20s

n e w d e l h i 1 1 0 0 0 1

%20.10s

n e w d e l h i
%.5s

n e w d
%-20.9s

n e w d e l h i
%5s

n e w D e l h i 1 1 0 0 0 1

Page 7
#include<stdio.h> OUTPUT:
void main() a
{ a
char x=‟a‟; a
char name[20]=”anil kumar gupta”; a
printf(“output of characters\n”); a
printf(“%c\n%3c\n%5c\n”,x,x,x); output of strings
printf(“%3c\n%c\n”,x,x); anil kumar gupta
printf(“\n”); anil kumar gupta
printf(“output of strings\n”); anil kumar
printf(“%s\n”,name); anil
printf(“%20s\n”,name); anil kumar
printf(“20.10s\n”,name);
printf(“%.5s\n”,name);
printf(“%-20.10s\n”,name);
}

Mixed Data Output


• It Is permitted to mix the data types in one printf( ) statement.
• Example,
Printf(“%a%f%s%c”,a,b,c,d);

Enhancing the Readability of Output


Following are the steps to be followed to improve the clarity and hence the readability and understandability of
outputs.
1. Provide enough blank space between two numbers.
2. Introduce appropriate headings and variable names in the output.
3. print special messages whenever a peculiar condition occurs in the output.
4. Introduce blank lines between the important sections of the output.

Page 8

You might also like