0% found this document useful (0 votes)
11 views4 pages

OUTPUT Statements

Uploaded by

kirti Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

OUTPUT Statements

Uploaded by

kirti Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Output Statements in C

Formatted Output Statement


printf function (Formatted Output statement)

This is the most commonly used function for outputting a data of any type.
It is also known as formatted output statement.
It is used to display result of variables on the screen.
It is also used to display any message on the screen.
Syntax :

printf (“format string”, list of arguments);


OR
printf(“format string”,variable1,variable2,………);
Here format string consists of group of characters, each group having % symbol and
conversion characters like c, d, o, f, x etc.
List of arguments specify the variables whose values you want to print.

Format specifiers in printf statement


There are a number of format specifiers in printf statement.

%d print an int argument(variable) in decimal


% ld print a long int argument in decimal
%c print a Character
%s print a String
%f print a float or double argument
%e same as %f, but use exponential notation
%g use %e or %f, whichever is better
%o print an int argument in octal (base 8)
%x print an int argument in hexadecimal (base 16)

Examples:

(1)printf("%c %d %f %e %s \n",'x', 2,3.14, 56000000.,"eight");


Output :
x 2 3.140000 5.600000e+07 eight

(2)printf("%d %o %x\n", 100, 100, 100);


Output:
100 144 64

(3)printf("Hello, ");
printf("world!\n");
Output :
Hello, world!

CBPCC Page 1
Example:

#include <stdio.h>
#include <conio.h>
void main()
{
char ch = 'A';
char str[30] = "c.b.patel computer college";
float flt = 10.234;
int no = 150;
double dbl = 20.123456;

printf("Character is %c \n", ch);


printf("String is %s \n" , str);
printf("Float value is %f \n", flt);
printf("Float value is %5.2f \n", flt);
printf("Integer value is %d\n" , no);
printf("Double value is %lf \n", dbl);
printf("Octal value is %o \n", no);
printf("Hexadecimal value is %x \n", no);
getch();
}

Output:

Character is A
String is c.b.patel computer college
Float value is 10.234000
Float value is 10.23
Integer value is 150
Double value is 20.123456
Octal value is 226
Hexadecimal value is 96

While inputting or outputting data field, width can also be specified. For assigning
field width, width is placed before the conversion character like “%10f”, ”%8d”,
”%12e” and so on… Also we can display data to a fixed no of decimal places.

For example if we want to display x=30.2356 as 30.24 specification may be “%5.2f”


or simply “%.2f”.

Unformatted Output statements


CBPCC Page 2
1. putchar() function
putchar() displays any alphanumeric character to the standard output device(monitor).

It displays only one character at a time on the screen.

Syntax:

putchar(char c);

Example:
char c;
c = ‘a’;
putchar(c);

2. putch() function
putch displays any alphanumeric character to the standard output device.
It also displays only one character at a time on the screen.
Syntax:
putch(char c);

Example:
char c;
c = ‘a’;
putch(c);

3. puts() function

It is used to display a single or multiple characters of string including spaces to the


standard Output device.
It displays string to the screen.
Syntax:
puts(variablename);

Example:

#include<stdio.h>
#include<conio.h>
void main()
{
char a[20];
gets(a);
puts(a);
getch();
}

CBPCC Page 3
Note:

putch comes from standard library conio.h and it is not supported on Linux OS.

putchar comes from stdio.h and you can use it with no problems on Linux OS.

Programs

CBPCC Page 4

You might also like