Module2 Part1
Module2 Part1
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);
}
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
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);
}
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);
• 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
%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);
}
Page 8