What is scanf in c programming?

Scanf is a function to read formatted data from stdin. That is the data is read from the keyboard.

The scanf function is used to enter data in C language True/False? Answer for this question is true.

scanf is found in stdio header file.

Look at the prototype below:

If you dont know about input output in c then read this article.

#include<stdio.h>
int scanf( char *formatted _string, params... );

Parameters

Look at the function parameters the formatted_string is to specify the input string from keyboard. It can contain various address of variables which have to be specified in params list.

Variables you have to specify the format specifiers that is preceeded with % symbol. Look at the list for various format specifiers for basic datatypes.

The character % has to be followed by one of the characters in below:

  • Conversion specifier.
  • Precision.
  • Length modifier.
  • Flag character.
  • Field width.

Basic conversion specifiers are as below:

int%d
float%f
double%lf
char%c
string (char array)%s

Return type

Scanf returns number of characters read from stdin.

If you are eager to learn more about different conversion specifiers then read the data-types in c post there I have listed every conversion specifiers for different datatypes.

Example program for scanf in c

//Example program for scanf in c
#include<stdio.h>
int main()
{
 int a;
 scanf("%d",&a);
 printf("Value of a is : %d",a);
 return 0;
}
Example program for scanf in c

If you are thinking that why printf is returning an int what is it?

Program for return value of scanf ?

We have already know the prototype of scanf. It returns an int value.

Look at the below program. You can clearly see that it prints number of character read from keyboard.

//What is the return value of scanf in c?
#include<stdio.h>
int main()
{
 char arr[10];
 printf("\nEnter a name");
 int rv=scanf("%s",arr);
 printf("\nReturn value of scanf is: %d",rv);
 return 0;
}
What is the return value of scanf in c

scanf vs std::cin, Which one is faster?

scanf is faster if you compare to the std::cin object of c++ programming language which is being used for Input in c++.

If you are a competetive programmer or practicing competetive coding then always use scanf instead of std::cin even if you are using c++ instead of c.

It is my own experience that scanf in c of stdio.h is much more faster than std::cin of iostream even though programming language is C++.

Now you may have a doubt that how can I use scanf in c in c++ , follow up the simple method below.

Look at the below example, while including a header file you have to mention preceeded by c with its header file name without .h extension.

You may also include #include<bits/stdc++.h> then you need not have to include it as #include<cstdio>.

//printf in cpp
#include<cstdio>
int main()
{
 int a;
scanf("%d",&a);
}

How to read sentence in scanf in c?

In many scenarios you may have to read a sentence. Those sentences may contain white spaces hence you may wonder how can we read it?

It is simple. You have to specify [^\n] as shown in below program example.

In the below example if you replace [^\n] with [^\r] it will read infinite number of lines if you didnt interrupt using ctrl+c in case of unix based systems. Try this.

#include<stdio.h>
int main(int argc, char const *argv[])
{
 char a[100];
 scanf("%[^\n]s",a);
 printf("\n%s",a);
 return 0;
}
How to read sentence in scanf in c

People also ask

What is the return value of scanf?

Scanf returns number of characters read from stdin.

scanf vs std::cin, Which one is faster?

scanf is faster if you compare to the std::cin object of c++ programming language which is being used for Input in c++.


People also read