Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Sunday, November 8, 2020

Printf() Function in C

welcome2protec: Printf() function is a library function used to display formatted output as it is on the monitor screen written inside the quotations.

Printf() function in C | welcome2protec

In C programming, there are two most essential predefined functions scanf() and printf() which are used for taking input from the user and displaying output on the monitor screen.

Both functions are declared in <stdio.h> header file in C library. That is why, to use these functions we need to include appropriate header file at the beginning of every program for example: #include <stdio.h>

In this tutorial, we will learn to use printf() function to display output on the monitor screen. See the next tutorial to learn the use of scanf() function.


Output in C:

Printf() function is a library function used to display formatted output as it is on the monitor screen written inside the double quotes to the user. It can be displayed two type of messages.

1) Printing text as it is on the screen (OR can say displaying text)

Syntax

/* text given inside the parenthesis will print as it is on the screen  */
printf("welcome2protec.com");

Let's take a simple example to understand it better.


Example 1: Use of printf() function

#include <stdio.h>  
#include <conio.h> 
void main ()  
{  
  clrscr();  //used to clear the screen
    
  /* printf() function is used to display the formatted string as it is 
   on the screen written inside the double quotes */
  printf("Welcome to Protec\n");
  printf("IT Training Center\n");
  printf("At Tara Market, beside Vishal MegaMart-Siwan\n");
  printf("Let's Join Protec!\n");    
  getch();  //used to take a character form keyboard
 }  

Output:

Welcome to  protec
IT Training Center
At Tara Market, beside Vishal MegaMart-Siwan
Let's Join Protec

Explanation: In the above example, you can see the text written inside the parenthesis with double quotes like printf ("Welcome to Protec") are printed as it is on the screen.

Note: All the messages will print in the same order in which they have been written. For Example first line "welcome to Protec" 2nd line "IT Training Center" similarly all the messages will print in the given order.


2) Printing value of expression or value of variable:

If you want to print the the value of either variable or expression so, you need to use the appropriate format specifier (Format specifier define the type of date to be printed output on the screen).

For Example: If the value of variable a = 10 and you want to print the value of  'a' on the screen so you need to write, printf ("%d", a); output will show 10, and If value of variable b = 20 so, printf("sum is %d", a + b); output of expression a + b is,.... Sum is 30.

Let's see at the below example 2 to understand it better.

Syntax:

/* Format specifier defines the type of data to be displayed on the screen */
printf("Format Specifier", VariableName or Expression);

Example 2: Integer Output

#include <stdio.h> 
#include <conio.h> 
void main ()  
{  
  int a = 10, b= 20;
  clrscr();  //used to clear the screen
    
  /* Format specifier %d, will be replace with a value of variable a i.e. 10 */
  printf("Value of a = %d\n", a);

  /* Format specifier %d, will be replace with a value of b i.e. 20 */  
  printf("Value of b = %d\n", b);  
 
  /* Same way, %d and %d will be replace with values of 
  variables a and b i.e.  10 and 20 */
  printf("Value of a = %d and b = %d\n", a, b);  

  /* Remember string written inside the double quotes like "whatever string is written"
  will print as it is . And In place of  format specifier like %d and %d = %d will be
  replace with value of variables a, b and expression a+b in the same order 
  in which they have written. */
  printf("Sum of %d and %d = %d\n", a, b, a + b);
    
  getch();  //used to take a character form keyboard
 } 

Output:

10
20
10 and 20
Sum of 10 and 20 = 30

Format specifier: define the type of date to be printed output on the screen for Example: To print float value, we use %f format specifier. Similarly we use %lf to print double values and use %c to print character.


Example 3: float and double Output

#include <stdio.h> 
#include <conio.h> 
void main ()  
{  
    float num1 = 12.5;
    double num2 = 10.4;
    clrscr();  //used to clear screen

    /* Format specifier %f, is used for float type value. */
    printf("num1 = %f\n ", num1);   
   
     /* Format specifier %lf, is used for double type value. */
    printf("num2 = %lf\n ", num2);   
    getch();  //used to take a character form keyboard
 }  

Note: To print float, we use %f format specifier, Similarly we use %lf to print double values.

Output

num1 = 12.500000
num2 = 10.400000

Example 4: Print Characters Output

#include <stdio.h>  
#include <conio.h> 
void main ()  
{  
    char chr = 'p';  
    clrscr();  //used to clear screen

    /* Format specifier %c, is used for character. */
    printf("Character = %c ",chr);   
  
    getch();  //used to take a character form keyboard
 }  

Output

Character  = p



Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us