GET EDUCATE

Contents
Basics of C Programming

⯈Basic C Programs

scanf in C

  • This function is used to read the variables of primitive datatype and strings from the end user through the keyboard.
  • It is formatted input function, since we use the format specifiers.
  • Prototype:
int scanf("format-specifier", address_of_variable(s));
  • The scanf function needs the address of the variable. 
  • To get address of the variable we use ‘&’ operator.
  • It returns number of variables read successfully as the answer.
  • It also needs format-specifier to say what kind of data we want to read.

We will also discuss how to read multiple variables in a scanf, as well as intput buffer problem with scanf

We will also discuss what happens if we don’t put & in scanf and how it causes segmentation fault.

You may also watch the video posted at the end of this page.

& operator

  • It is a unary operator.
  • It gives address of a variable. 
implicit conversion
  • m gives 3.0 as answer.
  • &m gives 0x3000 as answer (address of m)

Format Specifier

  • It specifies what type of data we want to read or print.
  • This is used with formatted input-output statements.
Data Type Format Specifier

int

%d or %i

char

%c

double

%lf

float

%f

long long int

%lld

long double

%Lf

Examples of scanf

1. int a;

scanf
  • To give the value to this variable (any value that the end user wants) we use scanf.
  • We need to give the format specifier to say what kind of data we want to read.
  • Here it is an integer, therefore we use %d or %i

Hence, we write the scanf statement as,

scanf("%d", &a);
  • It says, read an integer value (Format specifier: %d) and store at the address of a (0x1000).
  • Suppose the user enters the integer as 10, GARBAGE at address 0x1000, is over-written with 10.
scanf

 

Ex 2.  double b;

scanf
  • Since we are reading a double number, we use %lf as the format specifier.

Hence, we write the scanf statement as,

scanf("%lf", &b);
  • It says, read a double number (Format specifier: %lf) and store at the address of b (0x2000).
  • Suppose the user enters the number as 1.1, GARBAGE at address 0x2000, is over-written with 1.1
scanf

 

Ex 3: char ch;

scanf
  • Since we are reading a char, we use %c as the format specifier.

Hence, we write the scanf statement as,

scanf("%c", &ch);
  • It says, read a character (Format specifier: %c) and store at the address of ch (0x3000).
  • Suppose the user enters the character as Z, GARBAGE at address 0x3000, is over-written with Z
scanf

 

Ex 4: float e;

scanf
  • Since we are reading a float number, we use %f as the format specifier.

Hence, we write the scanf statement as,

scanf("%f", &e);
  • It says, read a float number (Format specifier: %f) and store at the address of e (0x4000).
  • Suppose the user enters the float number as as 1.56, GARBAGE at address 0x4000, is over-written with 1.56
scanf

 

Reading multiple variables in a scanf

Example 1:

int a, b;

To read the values of both ‘a’ and ‘b’, we write

scanf("%d%d", &a,&b);
  • It says read two integers, as we have written two %d, the first integer read should be stored at address of a (&a) and the second integer read is stored at address of b (&b).
  • The format-specifiers and the address of variables should be in 1-1 correspondence.
  • Suppose the first entered integer is 10 and second integer is 20, 10 is stored in ‘a’ and 20 in ‘b’.
#include<stdio.h>
int main()
{
int a, b;
scanf("%d%d",&a,&b);
printf("a=%d\n",a);
printf("b=%d\n",b);
}
scanf
output window of scanf

We can clearly see that the first read value is stored in ‘a’ and second read value is stored in ‘b’

Example 2:

int a;

double b;

To read the values of both int and double in a single scanf statement we write,

scanf("%d%lf",&a,&b);
  • It says read one integer and one double number, as we have written %d%lf, the first integer read should be stored at address of a (&a) and the second double number read is stored at address of b (&b).
  • The format-specifiers and the address of variables should be in 1-1 correspondence.
  • Suppose the first entered integer is 10 and second double number is 5.99, 10 is stored in ‘a’ and 5.99 in ‘b’.
#include<stdio.h>
int main()
{
int a;
double b;
scanf("%d%lf",&a,&b);
printf("a=%d\n",a);
printf("b=%lf\n",b);
}

 

Input buffer problem in scanf

  • The scanf reads the data from the input buffer.
  • Whatever we type from the keyboard the data is stored in input buffer and from there the scanf reads.
input buffer
  • Suppose we are reading two characters using scanf for x and y, we will write scanf as,
  • char x, y;
scanf("%c%c",&x,&y);

We entered the input as,

A (A space B)

The input is stored in input buffer.

input buffer
  • Now the scanf has to read two characters, since we have written two %c.
  • The first character in input buffer is and is stored in variable ‘a’.
  • After is read the input buffer is left out with (space B).
  • Since space (‘ ‘) is also a character it is read and stored in variable ‘b’.
  • Therefore,
    • a = ‘A’ and b = ‘  ‘
input buffer
  • But we need to store in variable ‘b’.
  • It means we need to remove space from the input buffer, so that scanf can read B
  • We need to read space character, but don’t want to store in any variable, for that we write %*c

Note: %*c, %*d, etc,. means read a value but don’t store in any variable.

#include<stdio.h>
int main()
{
char a;
char b;
scanf("%c%*c%c",&a,&b);
printf("a=%c\n",a);
printf("b=%c\n",b);
}
input buffer

Note:

  • If character is read for first time, (nothing is read before character) no need of %*c as the input buffer is empty.
  • If we read the character not for the first time, we need %*c  to remove a space or any white-space character from input buffer.

What happens if you don't put & in scanf

  • If we don’t write & in scanf for reading variables, (primitive data types), it results in segmentation fault.

Let us see how it causes segmentation fault with the help of an example.

char ch;

scanf

Suppose if we write,

scanf("%c", ch);
  • It means read a character and store at ch. Do note that the contents of ch is GARBAGE.
  • That means, scanf statement says, read a character and store at GARBAGE location. The scanf looks as follows,
scanf("%c", GAR);
  • There is no location or memory address called as GARBAGE.
  • Since we are unable to find the memory address (memory is called as segment and it is not found that is fault), therefore it is called as “Segmentation Fault”

Note: If address of a variable is not given in scanf statement, then it causes segmentation fault.

Scroll to Top