TRENDING TOPICS

×

printf() Function in C Programming

printf() function in C programming and how to use printf() function in C language

printf() is a standard library function that is used to display the output on the screen.

  • printf() function is inbuilt library function.
  • printf() function is define within C library’s “stdio.h” header file.
  • To use printf() function, must include stdio.h header file in C program.
  • Data Types defines the type of value that can be stored in reserve block/location.
  • printf() function is case-sensitive i.e. printf() is different than Printf(), infact C is a case-sensitive programming language.

printf() Function Looks Like

For better understanding, we can use printf() function in two ways

  1. printf() function without variables/values.
  2. printf() function with variables/values.

printf() function without variables/values

This scenario of printf() function is used to print simple/general message as output in C programming, see below syntax and examples.

Syntax

printf(“Type your message here”);

Examples

Output

This message from printf function
---------------------------------
This is message 1
This is message 2
Both above two line are from same printf fuction
This is last message

Newline Character (\n)

\n is used for new link in output. All text/content after \n will appear in next line.

Placeholder

Placeholder is used to insert a value/variable value in a message in printf() function, the below table contains the type of value and placeholder representation.

Data Types Placeholder Representation
int %d
char %c
float %f
double %lf
string %s
octal %o
hexadecimal %x

printf() function with variables/values

This scenario of printf() function is used to print a message along with some value stored in variables. We use placeholder representation to insert the value of the variable in the message. Let's understand with an example.

Syntax

printf(“Message with list of placeholder symbol”, vaiable1, variable2, vaiable3,....);

Example 1

Output

Sum of 10 and 20 is: 30

Rules/Explanation:

  • In the above example a, b and sum are variables.
  • All three %d is called as a placeholder.
  • %d symbols are replaced by int type values.
  • variable order matters for example [ a,b,sum ---> 10,20,30 ] and [ a,sum,b ---> 10, 30, 20 ]
  • Placeholder representation symbol and variable data type must match and order should be correct.

Example 2

Output

A 20 73.56
-------------------------
Name=A Age=20 Marks=73.56
-------------------------
Name=A
Age=20
Marks=73.56

Note: At the place of variables, you can directly pass values in printf() function. See the below example.

Output

Mr. A age is 20 years and his salary is 35800.75

Advertise With Us

We offer sponsored posts and guest posts with do-follow links, back-links, and other advertising services, To advertise email us at "advertise@gdatamart.com"

© 2021 GDATAMART.COM (All Rights Reserved)