Friday, March 29, 2024
HomeC Programmingprintf and scanf in c

printf and scanf in c


             

 On this Tutorial we are going to study using scanf() and printf() perform in C Programming.

  • printf():This perform is hottest inbuilt perform in c programming,the aim of this perform is used to show the output on display.We will use this perform to show any varieties f information i.e integer,float,character  double and so forth.

         Syntax for printf

            printf(““format string”,argument_list “); 

            The format string could be %d (integer), %c (character), %s (string), %f (float) and so forth.

        Instance for printf

        1.int age=13;    

           printf(“%d”,age);

        2.char gender=”M “;

            printf(“%c”,age); 

        Similarily,We will do that for different Knowledge Sorts as effectively with specified specifier.

        Program to easily show output-statement

#embrace <stdio.h>

int principal() {
printf("Hi there Everybody");
return 0;
}

Output

Hi there Everybody

       Program to reveal using printf  for displaying   integer  worth  

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

Output

Age:13

     Program to reveal using printf  for displaying float worth

     #embrace <stdio.h>
int principal() {
double %=80.60;
printf("Share:%d",%);
return 0;
}

Output

Share:80.60

   Program to reveal using printf  for displaying character worth

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

Output

Gender:M

 Program to reveal using printf  for displaying double worth

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

Output

Divison:58.383888

 In above examples,we noticed that we are able to use printf() perform to print totally different datatypes values.In printf() perform we are able to both print the assertion as it’s or the values of variable.The assertion which we wish to  printed as it’s,then it’s needs to be enclosed with “”(double  quotes) and once we need the  worth of  variable is to be printed then we’ve got to make use of format specifier of that variable information kind and variable identify  exterior the “”(double quotes).Let’s perceive with an instance.

printf(“hey World”)//printing the worth as it’s 

output:hey World

int age=12;

printf(“%d”,age); //printing the worth of age variable which is of integer information kind

output:12

I hope,Now your all doubts relating to printf() perform is obvious,nonetheless when you’ve got any doubt then be at liberty to ask on remark.

Scanf():In C Programming Scanf() is often used perform to take enter from consumer.This perform takes enter from the usual enter equivalent to Keyboard.

#embrace <stdio.h>
int principal()
{
    float p.c;
    printf("Enter an float worth: ");
    scanf("%f", &p.c);  
    printf("Share = %f",p.c);
    return 0;
}

Output

Enter an float worth: 79.59
Share = 79.59

 Float and Double Enter/Output

#embrace <stdio.h>
int principal()
{
    float num1;
    double num2;

    printf("Enter a quantity: ");
    scanf("%f", &num1);
    printf("Enter one other quantity: ");
    scanf("%lf", &num2);

    printf("num1 = %fn", num1);
    printf("num2 = %lf", num2);

    return 0;
}       

Output

Enter a quantity: 12.523 

Enter one other quantity: 10.2 

num1 = 12.523000 

num2 = 10.200000

 C Character I/O

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

Output

Enter a personality: g 

You entered g

 ASCII Worth

#embrace <stdio.h>
int principal()
{
    char chr;
    printf("Enter a personality: ");
    scanf("%c", &chr);     

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

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

Output

Enter a personality: g
You entered g. 

ASCII worth is 103.

In above Instance, we’re merely taking p.c as enter from consumer and printing it.Right here importing to notice is once we wish to take enter from consumer we merely use the format specifier of explicit datatype with ” “(double quotes) and &(ampersand) and variable exterior the “”(double) &  tells compiler that we’ve got to retailer the worth into the variable. 

Like We have now written  program for printf() perform for various  information kind values  we are able to additionally do it for scanf for taking enter from consumer of various information kind values utilizing explicit format specifier. 

Format Specifiers for I/O

As you may see from the above examples, we use

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

This is an inventory of generally used C information varieties and their format specifiers.

Knowledge Sort Format Specifier
int %d
char %c
float %f
double %lf
brief int %hd
unsigned int %u
lengthy int %li
lengthy lengthy int %lli
unsigned lengthy int %lu
unsigned lengthy lengthy int %llu
signed char %c
unsigned char %c
lengthy double %Lf    

If you happen to any have doubt,suggestion relating to this put up or web site then be at liberty to share with us.

 

If in case you have discovered one thing new from this put up then share this put up with your loved ones and
pals.

 

Blissful Studying :)😎 

Earlier

Subsequent

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments