Tuesday, November 28, 2017

Input and Output Functions in C - scanf() / printf()

So far we can write simple C programs, know about variables,identifiers,keywords and data types and also know how to assign different types of data into variable. Now it is time to know how to take different types of data as an input from user/keyboard and how to print them.


What it means by taking input from user/keyboard? Well, I don’t want to assign any data in my variable directly in our program. We want to assign it manually when our program will run.

There are several built in function to take input from user and display it. Here we will learn about them.

Input

scanf() function

Syntax of scanf() function is given below

scanf(“format_specifier”, &variable);

Note
  • scanf() function performs to read input from user.
  • Inside the double quote “” different Format Specifiers are used to take different type of data as an input. We saw a list of format specifier in Data Types section.
  • To take different types of data as an input from user we need to use corresponding format specifier.
  • Ampersand sign & is used before variable name to point the address in memory location.

Look into the following program to understand how to use format specifier in scanf() function.


#include <stdio.h>

int main(void) 
{
     //integer type data
     int var1;                       // variable declaration.
     scanf("%d", &var1);     // taking input from user.
     
     //long long type data
     long long var2;
     scanf("%lld", &var2);
     
     //double type data
     double var3;
     scanf("%lf", &var3);
     
     //float type data
     float var4;
     scanf("%f", &var4);
     
     //character type data
     char var5;
     scanf("%c", &var5);
     
     return 0;  
}



Output Function

printf() function

Syntax of printf() function is given below

printf(“format_specifier”,variable);

Note
  • printf() function performs to display output result on the screen.
  • Format specifiers are used inside the double quote “” to display corresponding data. We saw a list of format specifier in Data Types section.
  • To display different types of data as an output we should use corresponding format specifier.
  • No need of any Ampersand sign in this case.

Look into the following program to understand how to use format specifier in both scanf() printf() function.


#include <stdio.h>

int main(void) 
{
      //character type data
     char var5;
     scanf("%c", &var5);
     printf("This is a character type data = %c \n", var5);
     
     //integer type data
     int var1;
     scanf("%d", &var1);
     printf("This is an integer type data = %d \n",var1);
     
     //long long type data
     long long var2;
     scanf("%lld", &var2);
     printf("This is a long long type data = %lld \n",var2);
     
     //double type data
     double var3;
     scanf("%lf", &var3);
     printf("This is a double type data = %lf \n",var3);
     
     //float type data
     float var4;
     scanf("%f", &var4);
     printf("This is a float type data = %f \n", var4);
     
     
     return 0;
     
}

output


Note

Did you notice that every printf() function starts printing from new line. Why? \n is used in C programming to create new line. After printing it goes to the next line. Remove all \n from printf() function and Run this program on your machine and see the differences.

getchar() and putchar() functions

Instead of using scanf() and printf() we can also use getchar and putchar() function for the same purpose.

Note:
  • We can also use getchar() function to take character type input.
  • putchar() function used to display character type data.


#include <stdio.h>

int main() 
{
     char ch;            // variable declaration
     ch = getchar();     // taking input.
     putchar(ch);        // printing character 
 
     return 0;
}



Previous Topic

Next Topic



No comments:

Post a Comment