Sunday, 21 January 2018

c-printf-and-scanf


printf() and scanf() functions are inbuilt library functions in C programming language which are available in C library by default.

1. printf() and scanf() in C language:
  • These functions are inbuilt library functions in C programming language. 
  • They are available in C library by default.
  • As a child is known by parents, so here scanf() and printf() are children and their parents are stdio.h.
  • We have to include “stdio.h” file to make use of these printf() and scanf() library functions in C language.

Some important points for Printf():



  • In C programming language, printf() function is used to print the “character, string, float, integer, octal and hexadecimal values” onto the output screen.
  • We use printf() function with %d format specifier to display the value of an integer variable.
  • Similarly %c is used to display character, %f for float variable, %s for string variable, %lf for double and %x for hexadecimal variable.
  • To generate a newline,we use “\n” in C printf() statement.
  • %d got replaced by value of an integer variable  (no),
  • %c got replaced by value of a character variable  (ch),
  • %f got replaced by value of a float variable  (flt),
  • %lf got replaced by value of a double variable  (dbl),
  • %s got replaced by value of a string variable  (str),
  • %o got replaced by a octal value corresponding to integer variable  (no),
  • %x got replaced by a hexadecimal value corresponding to integer variable
  • \n got replaced by a newline.

Some Important points for Scanf()


In C programming language, scanf() function is used to read character, string, numeric data from keyboard.


  • The format specifier %d is used in scanf() statement. So that, the value entered is received as an integer and %s for string.
  • Ampersand is used before variable name “ch” in scanf() statement as &ch.
  • It is just like in a pointer which is used to point to the variable. For more information about how pointer works, please click here.



Note:
C language is case sensitive. For example, printf() and scanf() are different from Printf() and Scanf(). All characters in printf() and scanf() functions must be in lower case.


Basic Program to understant the concept of printf() and Scanf():



#include <stdio.h>
int main()
{
char ch;
/*user enters a character. This value is assigned to the variable “ch” */

char str[100];/
*User enters a string and this value is assigned to the variable “str” */

printf("Enter any character \n");
/*/n is used for next line*/
scanf("%c", &ch);
printf("Entered character is %c \n", ch);
printf("Enter any string ( upto 100 character ) \n");
scanf("%s", &str);
printf("Entered string is %s \n", str);
}


Output


Enter any character
a
Entered character is a
Enter any string ( upto 100 character )
Suraj
Entered string is Suraj



KEY POINTS TO REMEMBER IN C PRINTF() AND SCANF():

  • printf() is used to display the output and scanf() is used to read the inputs.
  • printf() and scanf() functions are declared in “stdio.h” header file in C library.
  • All syntax in C language including printf() and scanf() functions are case sensitive.


Return values of printf() and scanf() in C/C++:


printf() : It returns total number of Characters Printed, Or negative value if an output error or an encoding error



Example 1: The printf() function in the code written below returns 6. As ‘CODING’ contains 6 characters.



// C/C++ program to demonstrate return value 
// of printf() 
#include <stdio.h> 
  
int main() 
    char st[] = "CODING"; 
  
    printf("While printing "); 
    printf(", the value returned by printf() is : %d", 
                                    printf("%s", st)); 
  
    return 0; 


scanf() : It returns total number of Inputs Scanned successfully, or EOF if input failure occurs before the first receiving argument was assigned.


Example : The first scanf() function in the code written below returns 1, as it is scanning 1 item. Similarly second scanf() returns 2 as it is scanning 2 inputs and third scanf() returns 3 as it is scanning 3 inputs.

#include <stdio.h> 
int main() 
    char a[100], b[100], c[100]; 
  
    // scanf() with one input 
    printf("\n First scanf() returns : %d", 
                            scanf("%s", a)); 
  
    // scanf() with two inputs 
    printf("\n Second scanf() returns : %d", 
                       scanf("%s%s", a, b)); 
  
    // scanf() with three inputs 
    printf("\n Third scanf() returns : %d",  
                  scanf("%s%s%s", a, b, c)); 
  
    return 0; 


No comments:

Post a Comment