printf and scanf in c

             


 In this Tutorial we will learn about the use of scanf() and printf() function in C Programming.

  • printf():This function is most popular inbuilt function in c programming,the purpose of this function is used to display the output on screen.We can use this function to display any types f data i.e integer,float,character  double etc.

         Syntax for printf

            printf(""format string",argument_list "); 

            The format string can be %d (integer), %c (character), %s (string), %f (float) etc.

        Example for printf

        1.int age=13;    

           printf("%d",age);

        2.char gender='M ';

            printf("%c",age); 

        Similarily,We can do this for other Data Types as well with specified specifier.

        Program to simply display output-statement

#include <stdio.h>

int main() {
printf("Hello Everyone");
return 0;
}

Output

Hello Everyone

       Program to demonstrate the use of printf  for displaying   integer  value  

 #include <stdio.h>
int main() {
int age=13;
printf("Age:%d",age);
return 0;
}

Output

Age:13

     Program to demonstrate the use of printf  for displaying float value

     #include <stdio.h>
int main() {
double Percent=80.60;
printf("Percentage:%d",Percent);
return 0;
}

Output

Percentage:80.60

   Program to demonstrate the use of printf  for displaying character value

     #include <stdio.h>
int main() {
char Gender='M';
printf("Gender:%c",
Gender);
return 0;
}
      

Output

Gender:M

 Program to demonstrate the use of printf  for displaying double value

     #include <stdio.h>
int main() {
double divison=58.383888188181;
printf("Divison:%lf",divison);
return 0;
}

Output

Divison:58.383888

 In above examples,we saw that we can use printf() function to print different datatypes values.In printf() function we can either print the statement as it is or the values of variable.The statement which we want to  printed as it is,then it is should be enclosed with ""(double  quotes) and when we want the  value of  variable is to be printed then we have to use format specifier of that variable data type and variable name  outside the ""(double quotes).Let's understand with an example.

printf("hello World")//printing the value as it is 

output:hello World

int age=12;

printf("%d",age); //printing the value of age variable which is of integer data type

output:12

I hope,Now your all doubts regarding printf() function is clear,still if you have any doubt then feel free to ask on comment.

Scanf():In C Programming Scanf() is commonly used function to take input from user.This function takes input from the standard input such as Keyboard.

#include <stdio.h>
int main()
{
    float percent;
    printf("Enter an float value: ");
    scanf("%f", &percent);  
    printf("Percentage = %f",percent);
    return 0;
}

Output

Enter an float value: 79.59
Percentage = 79.59

 Float and Double Input/Output

#include <stdio.h>
int main()
{
    float num1;
    double num2;

    printf("Enter a number: ");
    scanf("%f", &num1);
    printf("Enter another number: ");
    scanf("%lf", &num2);

    printf("num1 = %f\n", num1);
    printf("num2 = %lf", num2);

    return 0;
}       

Output

Enter a number: 12.523 

Enter another number: 10.2 

num1 = 12.523000 

num2 = 10.200000

 C Character I/O

#include <stdio.h>
int main()
{
    char chr;
    printf("Enter a character: ");
    scanf("%c",&chr);     
    printf("You entered %c.", chr);  
    return 0;
}    

Output

Enter a character: g 

You entered g

 ASCII Value

#include <stdio.h>
int main()
{
    char chr;
    printf("Enter a character: ");
    scanf("%c", &chr);     

      printf("You entered %c.\n",chr);  

    
    printf("ASCII value is %d.", chr);  
    return 0;
}

Output

Enter a character: g You entered g. 

ASCII value is 103.

In above Example, we are simply taking percent as input from user and printing it.Here importing to note is when we want to take input from user we simply use the format specifier of particular datatype with " "(double quotes) and &(ampersand) and variable outside the ""(double) &  tells compiler that we have to store the value into the variable. 

Like We have written  program for printf() function for different  data type values  we can also do it for scanf for taking input from user of different data type values using particular format specifier. 

Format Specifiers for I/O

As you can see from the above examples, we use

  • %d for int
  • %f for float
  • %lf for double
  • %c for char

Here's a list of commonly used C data types and their format specifiers.

Data Type Format Specifier
int %d
char %c
float %f
double %lf
short int %hd
unsigned int %u
long int %li
long long int %lli
unsigned long int %lu
unsigned long long int %llu
signed char %c
unsigned char %c
long double%Lf  
If you any have doubt,suggestion regarding this post or website then feel free to share with us.
 
If you have learned something new from this post then share this post with your family and
friends.
 
Happy Learning :)😎 

Previous Next

1 Comments

Previous Post Next Post