Printf in C

About Printf

Printf is a kind of function which is used for giving the output. This will help to print the given statement to the console.

Printf in C

Working of Printf :

The main functionality of printf function is to print the output according to the given inputs .

Important Note :

  • It displays the total amount of printed characters.
  • Basically, printf function is used to return the output.

Declaration of Printf:

printf("format string",argument_list);

Example 1

Write a program to implement the printf function.

Code

Run
#include<stdio.h>
int main ()
{
  int a = 100;
  int b = 200;
  printf ("Before swap a = %d\n", a);
  printf ("Before swap b = %d\n", b);
  a = a + b;		   
  b = a - b;			    
  a = a - b;			   
  printf ("\nAfter swap a = %d", a);
  printf ("\nAfter swap b = %d\n", b);
  return 0;
}

Output

Before swap a = 100
Before swap b = 200

After swap a = 200
After swap b = 100

Example 2

Write a program by using printf function.

Code

Run
#include<stdio.h>
int main()
{
    float number1;
    double number2;
    printf("Enter first number = ");
    scanf("%f", &number1);
    printf("Enter second number = ");
    scanf("%lf", &number2);
    printf("number 1 = %f\n", number1);
    printf("number 2 = %lf", number2);
    return 0;
}

Output

Enter first number = 4
Enter second number = 5
number 1 = 4.000000
number 2 = 5.000000

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription