Programming and Problem Solving through C Language
O Level / A Level
Chapter -3 : Introduction to ‘C’ Language
1. Basic Input Output Statements
1. The basic input/output functions are
a. getchar,
b. putchar,
c. gets,
d. puts,
e. scanf and
f. printf.
2. The first two functions, getchar and putchar, are used to transfer single characters.
3. The next function gets and puts are used to input and output strings, and
4. the last two functions, scanf and printf, permit the transfer of single characters, numerical
values and strings.
2. getchar() Function
getchar( ) function is used to read one character at a time from the key board.
Syntax ch = getchar( ); where ch is a char Var.
Example
When this function is executed, the computer will wait for a key to be pressed and assigns the
value to the variable when the “enter” key pressed.
void main()
{
char ch;
printf("Enter a char");
ch=getchar( );
printf("ch=%c",ch);
}
Output
Enter a char M
M
3. putchar() Function
putchar( ) function is used to display one character at a time on the monitor.
Syntax: putchar (ch);
Example
The Computer display the value char of variable ‘ch’ i.e M on the Screen.
void main()
{
char ch="M";
putchar( ch);
}
Output
4. gets() Function
gets( ) function is used to read a string of characters including white spaces.
Note that white spaces in a string cannot be read using scanf( ) with %s format specifier.
Syntax: gets (S); where ‘S’ is a char string variable.
Example
When this function is executed the computer waits for the string to be entered.
void main()
{
char S[20] ;
gets( S);
}
5. puts() Function
puts() is a function used to display strings on screen.
Syntax: puts (S); where ‘S’ is a char string variable.
Example
When this function is executed the computer waits for the string to be entered and
then display the entered string on the screen.
void main()
{
char S[20] ;
gets( S);
puts(S)
}
Output
Hello Gorakhpur
Hello Gorakhpur
6. scanf() function
The most flexible way the program can read numeric data from the keyboard is by using the
scanf() library function.
The scanf() function reads data from the keyboard according to a specified format and
assigns the input data to one or more program variables.
For example:
o The statement reads a decimal integer from the keyboard and assigns it to the integer
variable x as shown below:
o scanf("%d", &x);
The ‘%’ indicates that the conversion specification.
The ‘d’ represents the data type and indicates that the number should be read as a integer.
The ‘&’ is ‘C’ Language unary operator that gets the memory address of the variable
following it.
Likewise, the following statement reads a floatingpoint value from the keyboard and
assigns it to the variable rate:
o scanf("%f", &rate);
The ‘f’ represents the data type and indicates that the number should be read as a float.
7. printf() function
The printf() function, part of the standard C library, is the most versatile way for a program
to display data onscreen.
Printing a text message onscreen is simple.
Call the printf() function, passing the desired message enclosed in double quotation marks.
For example, to display an error that has occurred! onscreen, the user write the following:
o printf("An error that has occurred!");
In addition to text messages, we frequently needs to display the value of program variables.
It accepts a string parameter (called the format string), which specifies a method for
rendering a number of other parameters into a string.
For example, suppose the user′s want to display the value of the numeric variable x
onscreen, along with some identifying text.
Furthermore, he wants the information to start at the beginning of a new line.
The printf() function as shown below:
o printf("\nThe value of x is %d", x);
o \n represents a new line character.
o The resulting screen display, assuming that the value of x is 12, would display the
following:
The value of x is 12.
In this example, two arguments are passed to printf().
o The first argument is enclosed in double quotation marks and is called the format
string.
o The second argument is the name of the variable (x) containing the value to be
printed.
Format character to be used with scanf or prinf function
int %d
long int %ld
float %f
double %lf
char %c
string %s
octal %o
hexadecimal %x or %X
Assignment
1. Write a program to read three integer number and print the sum of it.
2. Write a program to read three float number and print the sum of it.
3. Write a program to read the sides of rectangle and print the area and perimeter of rectangle.
4. Write a program to read the radius of circle and print the area and perimeter of circle.