C Programming Language is the most popular computer language and most used programming language till now. It is very simple and elegant language. This lecture series will give you basic concepts of structured programming language with C.
3. Input/Output Functions
There are several library functions which includes a
number of input/output functions.
Six common functions are: getchar, putchar, scanf,
printf, get and puts.
These six functions permit the transfer of
information between the computer and the standard
input/output devices.
4. Single Character Input- The getchar Functions
Single character can be entered into the computer
using getchar.
It returns a single character from standard input
device(typically keyboard).
Can be represented as:
character variable = getchar()
Where, character variable refers to some previously
declared character variable.
5. Single Character Input- The getchar Functions
(cont..)
Example:
char c;
………
c = getchar();
The first statement declares that c is a character-type
variable. The second statement causes a single character to
be entered from the standard input device and then assigned
to c.
6. Single Character Output- The putchar Functions
Single character can be displayed using putchar.
This function is complementary to the character input
function getchar.
It transmits a single character to a standard output device.
Can be represented as
putchar(character variable)
7. Single Character Output- The putchar Functions
(cont..)
Example:
char c;
………
putchar();
The first statement declares that c is a character-type variable. The
second statement causes the current value of c to be transmitted to
the standard output device where it will be displayed.
8. Example 1: getchar and putchar
#include<stdio.h>
#include<ctype.h>
main()
{
char ch;
printf(“Enter any character value: ”);
ch = getchar();
printf(“The equivalent upper case: ”);
putchar(toupper(ch));
}
9. Example 1: getchar and putchar (cont..)
This program converts lowercase to uppercase.
Sample input: a
Sample output: A
Sample input: d
Sample output: D
Sample input: l
Sample output: L
10. Entering Input Data- The scanf Function
The scanf() function reads data from the standard
input stream stdin into the locations given by each
entry in argument-list.
A non-whitespace character causes the scanf()
function to read, but not to store, a matching non-
whitespace character.
11. The scanf Function
The skeletal structure is:
#include<stdio.h>
main()
{
int a,b,c;
…………..
scanf(“%d %d %d”, &a, &b, &c);
………………..
}
12. The scanf Function (cont..)
Consider the last skeleton-
When the program is executed, three integer quantities will be entered
1 2 3
Then the following assignments will result:
a = 1, b = 2, c = 3
If the data had been entered as
123 456 789
Than the assignment would be
a = 123, b = 456, c = 789
13. The scanf Function (cont..)
Example:
#include<stdio.h>
main()
{
int i;
float x;
char c;
…………….
scanf(“%3d %5f %c”, &l, &x, &c);
………………………
}
14. The scanf Function (cont..)
If the data items are entered as
10 256.875 T
When the program is executed, then 10 will be assigned to I,
256.8 will be assigned to x and the character 7 will be assigned to
c. The remaining two input characters (5 and T) will be ignored.
15. The printf Function
Output data can be written from the computer onto a
standard output device using the library function printf.
printf(control string, arg1, arg2, ….. , argn)
Where control string refers to a string that controls
formatting information, and arg1, arg2…… argn are
arguments that represent the individual output data
items.
16. The printf Function (cont..)
Commonly used conversion characters for data i/o
17. The printf Function (cont..)
The following C program illustrates the use of the minimum field
width feature,
#include<stdio.h>
main()
{
int i = 12345;
float x = 345.678;
printf(“%3d %5d %8dnn”,i,i,i);
printf(“%3f %10f %13fnn”,x,x,x);
}
18. The printf Function (cont..)
When the program is executed, the following output is
generated
12345 12345 12345
345.678000 345.678000 345.678000
The first and second line of output displays a decimal
integer using three different minimum field widths.