You are here:   HomeC Programming (Theory)Basic Of C Prog.Write a C program to prints 3 characters forward and backward to demonstrate character printing.
by Dinesh Thakur Category: Basic Of C Programming

The function printf () is used for displaying the output of a program on the monitor, while the function scanf () is used for providing input to the program from keyboard. We shall first discuss printf(). If only a text statement is to be displayed on the monitor as was done in Program, then the argument of the function printf() is the string enclosed in double quotes (" " ) as illustrated below.

printf ("Statement to be displayed on monitor.");

However, many times, numerical values of different data types or character values of variables along with text statements need to be displayed on the monitor. In such cases, the function printf() has the following general form:

printf ("format_control_string", argumentl, argument2,...);

The first argument is known as the formatting string, i.e., format_control_string, which is enclosed in double quotes. It contains the formatting instructions such as the type of data to be displayed, field width, etc., and may also contain text which is desired to be displayed on the monitor. For displaying the value of a variable, the format-control statement (enclosed in double quotes) contains the percentage symbol (%) followed by a conversion character which specifies the data type to be printed. This may be followed by escape sequence character if desired. For example, if you want to display several values in separate lines then the escape character "\ n" may be used. On the other hand, if you just want to separate the values by a few spaces in the same line then "\ t" may be used. The control statement after the closing double quotes is followed by comma which is followed by names of variables to be displayed. Example

int m = 10;

double F = 45.8;

char ch = 'A';

printf("m = %d\n F = %.llf \t ch = %c\n", m,F,ch);

Illustrates the application of printf () with different data types

#include<stdio.h>


#include<conio.h>

void main()

{

   char ch;

   float f;

   double d;

   ch = 'X';

   clrscr();

   f = 10.23;

   d = 13.09;

   printf("ch = %c\t",ch);

   printf("f = %f\t", f);

   printf("d = %f", d);

   getch();

}

Write a C program to printf: output char, float and double.





Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular





Popular Article