printf in C (original) (raw)

In C language, **printf() function is used to print formatted output to the standard output **stdout (which is generally the console screen).

C `

#include <stdio.h>

int main() {

// Using printf to print the text "Hi!"
printf("Hi!");

return 0;

}

`

**Explanation: In this program, the **printf function print the text "**Hi!" on the console screen.

Syntax of printf

The printf() function is defined inside ****<stdio.h>** header file.

C `

printf("format_string", args...);

`

**Parameter:

**Return Value:

Format Specifier in printf

In addition to working as placeholders, format specifiers can also contain a few more instructions to manipulate how the data is displayed in the output.

Examples of printf() in C

The below examples demonstrate the use of printf() in our C program for different purposes.

C `

#include <stdio.h>

int main() { int a = 99; int b = 1;

  // Printing Variables and 
  // value of expressions
printf("Sum of %d and %d is %d", a, b,
       a + b);
return 0;

}

`

Output

Sum of 99 and 1 is 100

**Explanation: In this program, the format specifier ****%d** is used to print integers variables using printf. Here, it prints the values of a, b, and the result of a + b.

C `

#include <stdio.h>

int main() {

  // Printing Variables 
  // and value of expressions
printf("Sum of %d and %d is %d", 99, 1,
       99 + 1);
return 0;

}

`

Output

Sum of 99 and 1 is 100

**Explanation: The format specifier %d is used to print integer literals. It substitutes 99, 1, and the result of 99 + 1 into the string.

Right Align the Output

We can right align the output using the width specifier with positive value.

C `

#include <stdio.h>

int main() { char s[] = "Welcome to GfG!";

  // Printing right aligned string of width 40
printf("%40s", s);
return 0;

}

`

**Explanation: The format specifier ****%40s** prints the string **s right-aligned with a minimum width of **40 characters. If the string is shorter than **40 characters, it is padded with spaces on the left.

Left Align the Output with Specified Width

If we pass negative width, the minimum width will be the absolute value of the width, but the text will be left aligned.

C `

#include <stdio.h>

int main() { char s[] = "Welcome to GfG!";

  // Printing left aligned string of width 50
printf("%-50s", s);
  printf("Geeks");
return 0;

}

`

Output

Welcome to GfG! Geeks

**Explanation: The format specifier %-50s prints the string **s left-aligned with a minimum width of **50 characters. The remaining spaces are padded with blanks, followed by printing **Geeks.

**Add Leading Zeroes to Integer

The precision sub-specifier adds leading zeroes to the integer.

C `

#include <stdio.h>

int main() { int n = 2451;

// Precision for integral data
printf("%.10d\n", n);
return 0;

}

`

**Explanation: The format specifier ****%.10d** ensures the integer n is printed with a precision of **10 digits. If n has fewer digits, it is left-padded with **zeros to meet the required precision.

**Limit Digits After Point in Float

For floating point values, precision limits the number of digits to be printed after decimal points.

C `

#include <stdio.h>

int main() { float f = 2.451;

// Precision for float data
printf("%.2f", f);
return 0;

}

`

**Explanation: The format specifier ****%.2f** ensures the floating-point number **f is printed with **2 digits after the decimal point. It rounds the value if necessary.

**Limit Number of Characters in a String

For strings, precision limits the number of characters to be printed.

C `

#include <stdio.h>

int main() { char s[] = "Welcome to GfG!";

// Print with 3 decimal places
printf("%.7s", s);  
return 0;

}

`

**Explanation: The format specifier ****%.7s** prints only the first **7 characters of the string **s, truncating the rest if it exceeds this length.