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

Chapter 4 C Programming

Chapter 4 C Programming

Uploaded by

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

Chapter 4 C Programming

Chapter 4 C Programming

Uploaded by

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

Chapter 4

Managing I/O operations


Definition
◼ All input/output operations are carried out
through functions called as printf and scanf.
There exist several functions that have become
standard for input and output operations in C.
These functions are collectively known as
standard i/o library.
◼ Each program that uses standard I/O function
must contain the statement
▪ #include<stdio.h>
◼ The file name stdio.h is an abbreviation of
standard input-output header file.
READING A CHARACTER
◼ Reading a single character can be done by
using the function getchar.
◼ The getchar takes the following form:
–variable_name = getchar();
◼ Eg: char name; name=getchar();
Sample Program
#include<stdio.h>
main()
{
char ans;
printf(“Would you like to know my name? \n”);
printf(“Type Y for yes and N for no”);
ans=getchar();
if(ans ==’Y’ || ans = =’y’)
printf(“\n\n My name is India \n”);
else
printf(“\n\n You are good for nothing \n”);
}
WRITING A CHARACTER
◼ Like getchar, there is an analogous
function putchar for writing characters one
at a time to the terminal. It takes the form
as shown below:
▪ Syntax : putchar (variable_name);
– Eg: 1) answer=’y’;
putchar(answer);
will display the character y on the screen.
– 2) The statement putchar(‘\n’);
would cause the cursor on the screen to move to the
beginning of the next line.
Sample Program
#include<stdio.h>
#include<ctype.h>
main()
{
char alphabet;
printf(“Enter an alphabet”);
putchar(‘\n’);
alphabet = ghetchar();
if(islower(alphabet))
putchar(toupper(alphabet));
else
putchar(tolower(alphabet));
}

OUTPUT
Enter An alphabet
a
A
FORMATTED INPUT
◼ Formatted input refers to an input data that has
been arranged in a particular format.
◼ The formatted data can be read with the help of
scanf(). The general form of scanf() is
▪ scanf(“control string”, &arg1, &arg2…. &argn);
– The control string specifies the field format in which the
data is to be entered and
– the arguments arg1,arg2...argn specifies the address of
locations where the data are stored. Arguments are
separated by commas.
– Control string contains field specification which direct the
interpretation of input data.
◼ Control string contains field specification
which direct the interpretation of input
data.
◼ It may include Field (or format)
specifications, consisting of conversion
character %, a data type character, and
an optional number, specifying the field
width.
Inputting integer numbers
◼ The field specification for reading an
integer number is
%wd
◼ Eg: scanf(“%2d %5d”,&num1, &num2);
◼ An input field may be skipped by
specifying * in the place of field width.
◼ For eg ,
– scanf(“%d %*d %d”,&a, &b);
Sample Program
◼ /*Reading integer numbers*/ OUTPUT :
main()
{ Enter three integer numbers
123
int a, ,b, c, x, y, z; 1 3 –3577
int p, q, r; Enter two 4-digit numbers
printf(“Enter three integer numbers \n”); 6789 4321
scanf(“%d %*d %d”,&a, &b, &c); 67 89
printf(“%d %d %d \n \n”,a, b, c); Enter two integer numbers
44 66
printf(“Enter two 4-digit numbers \n”); 4321 44
scanf(“%2d %4d ”,&x, &y); Enter a nine digit numbers
printf(“%d %d \n \n”,x, y); 123456789
printf(“Enter two integer numbers \n”); 66 1234 567
scanf(“%d %d”,&a, &x); Enter two three digit numbers
123 456
printf(“%d %d \n \n”,a, x); 89 123
printf(“Enter a nine digit numbers \n”);
scanf(“%3d %4d %3d”,&p, &q, &r);
printf(“%d %d %d \n \n”,p, q, r);
printf(“Enter two three digit numbers \n”);
scanf(“%d %d”,&x, &y);
printf(“%d %d \n \n”,x, y);
}
Inputting Real Numbers

◼ Unlike integer numbers, the field width of real


numbers is not to be specified and therefore
scanf reads real numbers using simple
specification %f for both the notations, namely,
decimal point notation and exponential notation.
▪ Eg: scanf(“%f %f %f”, &x, &y, &z);
◼ If the number to be read is of double type, then
the specification should be %lf instead of simple
%f.
Inputting Character Strings
◼ Following are the specifications for reading
character strings:
%ws or %wc
◼ Some versions of scanf support the
following conversion specifications for
strings:
◼ %[characters] and %[^characters]
Sample Program
main()
{
int no;
char name1[15], name2[15];
printf(“Enter a number and name1\”);
scanf(“%d %15c”, &no,name1);
printf(“%d %15s\n\n”, no, name1);
printf(“Enter a number and name2\”);
scanf(“%d %s”, &no,name2);
printf(“%d %15s\n\n”, no, name1);
}
Contd…
◼ The specification %[characters] means that only
the characters specified within the brackets are
permissible in the input string. If the input string
contains any other character, the string will be
terminated at the first encounter of such a
character.
◼ The specification %[^characters] does exactly
the reverse. That is, the characters specified
after the circumflex (^) are not permitted in the
input string.
Sample Program

main()
main()
{ char address[80]; { char address[80];
printf(“Enter the address\n”);
scanf(“%[a-z]”, address); printf(“Enter the address\n”);
printf(“%-80s\n\n”, address); scanf(“%[^\n]”, address);
} printf(“%-80s\n\n”, address);
Output: }
Enter address Output:
New delhi 111102 Enter address
New delhi New delhi 111102
New delhi 111102
Reading Mixed Data Types
◼ It is possible to use one scanf statement
to input a data line containing mixed
mode data. In such cases, it should be
ensured that the input data items match
the control specifications in order and
type.

Eg: scanf(“%d %c %f %s”,&count, &code,


&ratio, &name);
◼ Refer commonly used scanf format codes
in the book Pg. No. 93
◼ Description Code Result

At least five wide printf("'%5d'", 10); ' 10’


At least five-wide, left-justified printf("'%-5d'", 10); '10 ’
At least five-wide, zero-filled printf("'%05d'", 10); '00010‘
At least five-wide, with a plus sign printf("'%+5d'", 10); ' +10‘
Five-wide, plus sign, left-justified printf("'%-+5d'", 10); '+10 '
Enhancing the readability of output
◼ Provide enough blank space between two
numbers
◼ Introduce appropriate headings and variable
names in the output
◼ Print the special messages whenever a peculiar
condition occurs in the output
◼ Blank lines between the important sections of
the output
◼ Eg. printf(“a =%d\n b=%d”,a,b);
printf(“\n Output Results\n”);

You might also like