Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

Function printf in C programming

September 30, 2013 Posted by: GPA Views: 26614 0 comments

Problem

What does printf mean in C programming?  

Solution

Output and input is performed streams. Streams are sequences of bytes. In output operations, bytes is used from memory of computer and we can see them on the screen, printer and so on. In input operations bytes flow from keyboard, disk drive and other input device to main memory.

About input operations You can read here standard Library function scanf

You can write your first program using printf function: 

printf("Hello world"); 

Together with the percent sign (%) this function can perform many type of data such as integer double and so on. This table describes main conversion specifiers.

Specifier  Description

d

Print as a signed decimal integer.
i Print as a signed decimal integer.
o
Print as an unsigned octal integer.
x or X
Print as an unsigned hexadecimal integer. Digits 0-9 and the letters A-F.
h or l (letter l)
Letters h and l are called length modifiers.
e or E
Print a floating-point value in exponential notation.
f
Print floating-point values in fixed-point notation.
g or G
Print a floating-point value in either the floating-point form f or the exponential form e (or E), based on the magnitude of the value.
L
Print long double.
c
Print char.
s
Print string or pointer to char.
%
Print the percent character.
n Nothing is displayed.

Example:



#include <stdio.h>
#include <stdlib.h>
//main function
int main(int argc, char *argv[])
{
printf("globalproganswer.com\n");
//test integer conversion specifiers.
printf("\nTest integer conversion specifiers:\n");
printf("d = %d\n", 845 );
printf("i = %i\n", 865 );
printf("hd = %hd\n", 5200000 );
printf("ld = %ld\n", 45464654450L );
printf("o = %o\n", 688 );
printf("u = %u\n", 56565 );
printf("x = %x\n", 55656 );
printf("X %X\n", 788 );
//Floating-point conversion specifiers.
printf("\nTest floating-point conversion specifiers:\n");
printf( "%e\n", 5698425.555 );
printf( "%e\n", +6658945.85 );
printf( "%e\n", -6985742.52 );
printf( "%E\n", 9857459.85 );
printf( "%f\n", 6399555.59 );
printf( "%g\n", 1231232.419 );
printf( "%G\n", 45454132.56 );
//Floating-point conversion specifiers.
printf("\nTest floating-point conversion specifiers:\n");
printf( "%e\n", 5698425.555 );
printf( "%e\n", +6658945.85 );
printf( "%e\n", -6985742.52 );
printf( "%E\n", 9857459.85 );
printf( "%f\n", 6399555.59 );
printf( "%g\n", 1231232.419 );
printf( "%G\n", 45454132.56 );
//Printing Strings and Characters
printf("\nTest Strings and Characters conversion specifiers:\n");
char character = 'C';
char* exampleStr="Example input string";
printf("Example char is: %c\n", character);
printf("Example string is: %s\n", exampleStr);
//Test percent characte and n
printf("\nTest percent characte and n : \n");
printf("%d%% of total cost : \n\n",59);//here we use %% to display 59% and n for new line
printf("\n");//new line
system("PAUSE"); //delay
return 0;
}

Output

Function printf

Download this example

0 Comments...

Leave a Reply

Please, Sing In to leave a Reply for this material.



Online Chat

LiveZilla Live Help

Popular posts

Function strtok in C programming Check prime numbers in C Programming C Programming Union Linked Lists in C programming File Input/Output in C programming C Programming Enumeration

RSS-subscribe

New materials

Subscribe