Formatted output Instructions
printf() in C
To display the output in C language the
printf() functions are used. These functions is predefine
library functions, defined in stdio.h (header file).
The printf()
function is used to print the value on screen contains by variable
printf()
The syntax of printf() function:
printf("Control string",arguments);
Control string –
it may be format specifier (%d, %f, %c) , characters or both in double
quotation mark (i.e. “ ”).
The format string can be
%d for integer
%f for float
%c for character
%s for string
(Note
- % is symbol which to indicting format
specifier)
Arguments – it is list
of variables separated by comma ( , )
Note- Every statement in c should end with semicolon ( ; ) mark.
For example,
1)
Int v=10;
printf(“value of V is %d”,v);
output – value of V is 10
2)
Printf(“ Vivekanand College Computer
Department ”)
Output - Vivekanand College Computer Department
Here – in
this second example we have not use any variables (i.e. arguments)
in printf() , which means that use of variable in printf() is optional.
Simple Programs:
1)
Write a program to print string i.e.
Vivekanand College Computer Department.
#include <stdio.h>
main()
{
clrscr();
// Displays the string inside
quotations
printf("Vivekanand College Computer Department");
getch();
}
Output
Vivekanand College Computer
Department
----------------------------------------------
2)
Write a program to print Characters.
#include <stdio.h>
main()
{
char chr = 'a';
clrscr();
printf("character = %c", chr);
getch();
}
Output
character = a
------------------------------------------------
3)
Write a program to print float
number
#include <stdio.h>
main()
{
float num = 27.5;
clrscr();
printf("num = %f",
num);
getch();
}
Output
number1 = 27.500000
--------------------------------------------
4) Write a program for Sum of two
integer.
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,sum;
a=10;
b=5;
clrscr();
printf("enter two numbers =
",a,b);
sum = a + b;
printf("Sum =%d",sum);
getch();
}
Output
Sum = 15
0 Comments