Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Monday, 6 November 2017

Printf and Scanf Function in C Language

What is printf and scanf in C Language

printf and scanf in c language

After learning the concept of types of variables in c language, Here we are going to discuss printf and scanf function in c language with simple examples.

Let's understand what is use of printf and scanf in c language with simple and easy examples.


What is printf() in c ?

In c programming language, printf() is a function which is defined in "stdio.h" header file. 

C printf() function is used  to display output on the console. By using printf() function we can print any massage or data on the console. 

Syntax 1: For printing massage only on the console

printf("hello world");//It will print same massage whatever you write in double quotes(" ").

Syntax 2: For printing massage and data(value) on the console

printf("Format specifiers", Value1, Value2...);

Here format specifiers can be %d(integer), %c(character), %s(string), %f(float), %d(double) and printf() function can take any number of arguments i.e Value1, Value2, Value3, but separated with comma(,).

For example1:

#include<stdio.h>
#include<conio.h>
void main()
{
int a = 30;
clrscr();
printf("Hello World\n");
printf("Number is %d ", a);
getch();
}

Output: Hello World
             Number is 30

In the above example, We have printed simple "Hello world" massage and print the data of "a" by using format specifier %d in printf() function.

For example 2:

#include<stdio.h>
#include<conio.h>

void main()
{
int a = 30;
float f = 5.2;
clrscr();
printf(" C programming \n ");
printf(" Value of integer only : %d  \n" , a);
printf(" Value of float only : %f  \n" , f);
printf("Both Values : %d %f " , a, f);
getch();
}

Output: C programming
             Value of integer only : 30
             Value of float only : 5.200000
             Both Values : 30 5.200000


What is scanf() in c ?

In c programming language, scanf() is also a function which is defined in "stdio.h" header file.

C scanf() function is used to read the input data from console or keyboard

Syntax: 

scanf("Format specifiers", &Value1, &Value2...);

For example 1:

#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf("What is your age :");//print massage on the console
scanf(" %d ", &age);//taking number from console and store in age variable 
printf("My age is : %d ", age);//print the age's value on console
getch();
}

Output: What is your age : 50
              My age is : 50



For example 2:

#include<stdio.h>
#include<conio.h>

void main()
{
int a;
float f;
clrscr();
printf("Enter your numbers: ");
scanf("%d %f " , &a, &f);
printf("Your numbers : %d %f ", a, f);
getch();
}

Output: Enter your numbers: 4 5.2
             Your numbers : 4 5.200000


Printf and Scanf Example : Sum of 2 Numbers

This is the simple example of using printf() and scanf() function for addition of two number. We will take input or number from console and add them.

#include<stdio.h>
#include<conio.h>

void main()
{
int a;
int b;
int sum;
clrscr();
printf("Enter your first number : ");
scanf("%d", &a);
printf("Enter you second number : ");
scanf("%d", &b);

sum = a+b;
printf("addition of 2 number is : %d ", sum);
getch();
}

Output: Enter your first number : 6
             Enter your second number : 5
             addition of 2 number is : 11


Difference between printf and scanf in C language

The difference between printf and scanf in c language is printf() function is used to display the output on the console e.g massage or data values and scanf() function is used to read the input data from console or keyboard.

This your printf and scanf in c language with simple examples. I hope it will be useful to you.

Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate