CodesandTutorials
Next generation website theme is here (black, neon and white color) - Reduces eye strain and headache problems.
You are here - Home > Programming > C > Basics

Printf


Description

Printf stands for "print formated" is an built-in function defined in stdio.h header file that prints the output on the screen supplied with proper formated string. Basically formated string consists of text, speical character codes for format specifiers and arguments in sequence.

Syntax

  • printf("format string", argument);

The first format string argument determines how many arguments are there and are of what data types. Second argument is the list of variable names seperated by commas in correct sequence, if wrong will result wrong output.

Eg: printf("Value of a and b = %d : %d", a,b);

Format Specifiers

%c char
%i or %d int
%e scientific notations
%f float
%o octal
%s string
%u unsigned int
%x hexadecimal
%p pointer
%% % sign
%lf double

Escape Sequences

\n new line
\t tab
\v vertical tab
\b backspace
\f new page
\r carrier return

Some printf statements

  • int a=23;
  • float b=2.5234;
  • printf("%d\n", a);
  • printf("%4d\n", a);
  • printf("%f\n", b);
  • printf("%.2f\n", b);
  • printf("%4.1f\n", b);
  • printf("%s\n", "My sixth statement");
  • printf("%30s\n", "My seventh statement");
  • printf("%.12s\n", My eighth statement");
  • printf("%-20s\n", My ninth statement");
  • prinft("%10.6s\n", My tenth statement");
  • printf("Octal of 31 : %o\n", 31);
  • printf("Hexamdecimal of 52 : %x\n, 52);

Output of statements

  • 23
    
  •   23
    
  • 2.523400
  • 2.52
  •  2.5
    
  • My sixth statement
    
  •           My seventh statement
    
  • My eighth st
  • My ninth statement
  •     My ten
    
  • Octal of 31 : 37
  • Hexamdecimal of 52 : 34

Explanation

  • Value of a & b are declared with their data types and \n is used to enter a new line in each statement.
  • In first printf statement %d prints the value of a in decimal integer.
  • Second statement prints the value of a in decimal integer with width of at least 4 wide that creates two blank spaces at the start.
  • %f prints floating point value of b.
  • %.2f prints floating point with a precision of 2 after decimal point.
  • %4.1f prints floating point with width of four and precision of 1, so there is one space at the start.
  • %s prints the contents within double quotes as it is.
  • %30s as string content is 20 characters, an output should be 30 characters wide it adds 10 blank spaces at the start.
  • %.12s prints the first twelve characters of the string.
  • %-20s does opposite of seventh statement, it adds two blank spaces at the end as string characters are eighteen.
  • %10.6s prints six characters from the string by adding four blank spaces at the start, as the output should be of ten characters wide. Similarly if it is %-10.6s, it will add blankspaces at the end.
  • Prints the octal value of 31 : 37
  • Prints the hexadecimal value of 52 : 34

Tip Box

printf, sprintf & fprintf

printf outputs formated data to standard output stream stdout.

sprintf writes formated data to a string array in a memory.

fprintf writes formated data to a file output stream.


Advertisement





Terms of Use | Privacy Policy | Contact Us | Advertise
CodesandTutorials © 2014 All Rights Reserved