'C' uses printf() and scanf() functions to write and read from I/O devices, respectively. These functions have been declared in the header file called 'stdio.h'
Let us use printf() function to rewrite our first program. The modified program is given below :
#include<stdio.h>
main()
{
printf("This is my first program in C language.");
}
This program displays the following message on the screen:
This is my first program in C language.
In fact, any text written within the pair of quotes ( " " ) is displayed as such by printf() function on the screen.
How to Display Data Using printf() Function
An integer, stored in a variable =, can be displayed on the screen by including a format specifier ( %d ) within a pair of quotes as shown below :
#include<stdio.h>
main()
{
int age=20;
printf("%d",age);
}
The above program does the following:
- Declares a variable 'age' of data type 'int'
- Initializes 'age to '20', an integer value.
- Specifies '%d', a format specifier indicating that an integer is to be displaying by printf() and the data is stored in the variable called 'age'.
It may noted that printf() has two arguments separated by a comma, i.e., format specifier written within quotes and the variable 'age'. It may be further noted that both messages and format specifiers can be included within tha pair of quotes as shown below :
#include<stdio.h>
main()
{
int age=20;
printf("The age of student = %d",age);
}
The above statement would display the following data on the screen :
The age of student = 20
Thus, the text between the quotes has been displayed as such but for ' %d ' , the data stored in the variable 'age' has been displayed.
A float can be displayed on the screen by including a format specifier ' %f ' , within the pair of quotes as shown below:
#include<stdio.h>
main()
{
float rate = 1.5;
printf("The rate of water = %f",rate);
}
Obviously, the above program would produce the following output on display screen :
The rate of water = 1.5
A char be displayed by including a format specifier ' %c ' within the pair of quotes as shown below :
#include<stdio.h>
main()
{
char ch = 'Abhi'
printf("The name of author is = %c",ch);
}
Obviously, the above program would produce the following output on display screen :
The name of author is Abhi
The other specifier which can be used in printf() functions are given on the next session.
0 comments:
Post a Comment