C programming Output printf() Library function

C Programming Output

printf(): Writing Output Data

In C Language Output data can be written from computer memory to the standard output device commonly on monitor or screen of the output device using printf() library function. With printf() library function, all types of values (numeric, character, or string) can be written as output. In this library function, the control string contains conversion specification characters and/or text that must be enclosed within the double-quotes. Unlike scanf() the name of variables should not be preceded by an ampersand (&) sign in printf() library function.

Note: If the control string does not contain any conversion specification character, then the variable names are not specified.

Examples on printf() Library Function

Example.1:
#include<stdio.h>
main()
{
printf("Learn C Programming With Our Free Online Tutorial\n");
}
Output: Learn C Programming With Our Free Online Tutorial

In this C Program, the control string contains only text without any conversion specification character, hence the output of this code will be the text only.

Example.2:
#include<stdio.h>
main()
{
int class;
printf("Enter Your Class: ");
------------;
}

In this example, the printf() does not contain any conversion specification character. When this C program (code) will run a message will be displayed on the user’s screen which asks the programmer to enter his class.

Example.3:
#include<stdio.h>
main()
{
int class=9;
-----------;
printf("%d",class);
-----------;
}

In this example, the printf() library function contains a conversion specification character (%d). When this C program (code) will run an integer value will be displayed on users’ screen. The integer variable class has a value that will be displayed as an output.

Example.4:
#include<stdio.h>
main()
{
int i=200;
-----------;
printf("Highest ODI Score Of Sachin Is = %d",i);
-----------;
}
Output: Highest ODI Score Of Sachin Is = 200

In this example, the printf() library function contains a text message along with the conversion specification character (%d). When this C program (code) will run the text message will be displayed as it is and the value of the variable I will be displayed in place of (%d).

Note: More than one conversion specification characters can be passed as a parameter to the printf() library function.

Example.5:
#include<stdio.h>
main()
{
char ch='A';
int sal=40000;
float hr=1200.50;
printf("Employee Grade = %c , Monthly Salary = %d , and Monthly HRA = %f",ch,sal,hr);
-----------;
}
Output: Employee Grade = A , Monthly Salary = 40000 , and Monthly HRA = 1200.50

In this example, the control string control contains three conversion specification characters %c, %d, and %f. They are used to display the single character, integer, and floating-point number respectively.

Example.6:
#include<stdio.h>
main()
{
char ch='A';
int sal=40000;
float hr=1200.50;
printf("Employee Grade = %c\nMonthly Salary = %d\nMonthly HRA = %f\n",ch,sal,hr);
-----------;
}
Output:
Employee Grade = A
Monthly Salary = 40000
Monthly HRA = 1200.50

Since (\n) moves the cursor to the beginning of the next line. Here ‘\n’ is placed at the end of the control string, it ensures that the output of the next variable starts at a new line.

Example.7:
#include<stdio.h>
main()
{
char ch='A';
int sal=40000;
float hr=1200.50;
printf("Employee Grade = %c\tMonthly Salary = %d\tMonthly HRA = %f\t",ch,sal,hr);
-----------;
}
Output:
Employee Grade = A Monthly Salary = 40000 Monthly HRA = 1200.50

Since (\t) moves the cursor to the next tab stop. Here ‘\t’ is placed at the end of the control string, it ensures that the output of the next variable starts at the next tab stop.

Leave a Comment

Your email address will not be published. Required fields are marked *