Scanf In C

Scanf In C

In C programming, scanf is a function used to read data from the standard input (typically the keyboard) and store the result in specified variables. It can handle character, string, and numeric input and utilizes format specifiers similar to printf.

Scanf Syntax

The syntax of scanf() in C is similar to the syntax of printf()

int scanf( const char *format, ... );
  • Return Type: int is the return type, indicating that printf returns an integer value, typically denoting the number of characters printed.
  • Format String: “format” is a string that contains format specifiers, allowing you to control the formatting of the output.
  • “…” (ellipsis): The ellipsis (…) indicates that printf can accept a variable number of arguments, corresponding to the format specifiers in the format string. This flexibility allows you to print multiple values of different types in a single printf statement

Example format specifiers recognized by scanf:

%d to accept input of integers.

%ld to  accept input of long integers

%lld to accept input of long long integers

%f to accept input of real number.

%c to accept input of character types.

%s to accept input of a string.

Example

int var;
scanf(“%d”, &var);

Return Value of scanf

  • Greater than 0: This indicates that the number of values converted and assigned successfully. It means that scanf successfully read and assigned data according to the provided format specifiers.
  • Equal to 0: This means that no value was assigned. It typically occurs when scanf couldn’t match the input with the specified format, or when no input was provided.
  • Less than 0: This indicates a read error encountered or that the end-of-file (EOF) was reached before any assignment was made. It suggests a problem with reading input, such as reaching the end of a file or encountering an error while reading.

Why &?

  • The ampersand (&) operator is often referred to as the “address of” operator.
  • It is used to obtain the memory address of a variable.
  • For example, &var represents the address of the variable var.

Example Of Scanf

C Program to implement Scanf

// C program to implement
// scanf
#include <stdio.h>
 
// Driver code
int main()
{
    int a, b;
   
      printf("Enter first number: ");
      scanf("%d", &a);
   
      printf("Enter second number: ");
      scanf("%d", &b);
   
      printf("A : %d \t B : %d" ,
            a , b);
   
    return 0;
}

Output

Enter first number: 5
Enter second number: 6
A : 5      B : 6

FAQ- Scanf In C

Q1. What does scanf do in C?

Ans. scanf stands for “Scan Formatted String.”
It is used to read data from the standard input (usually the keyboard) and stores the result into specified variables.
scanf can accept character, string, and numeric data from the user via the standard input stream.

Q2. Why should we use scanf?

Ans. scanf is used to receive input from the standard input, often the keyboard. While it offers versatility, it can be unreliable when it comes to handling human input errors.
For straightforward programs, scanf is a convenient and straightforward method for receiving input, as demonstrated in the example scanf("%d", &b);, which reads an integer value from the standard input and stores it in the variable b.

Q3. What is the difference between scanf %d and scanf %i?

Ans. Both %d and %i are used to read signed integers.
However, %i has additional functionality. It interprets the input differently based on certain prefixes:If the input is preceded by “0x” (e.g., “0x1A”), %i interprets it as a hexadecimal number.
If the input is preceded by “0” (e.g., “012”), %i interprets it as an octal number.
Otherwise, %i interprets the input as a decimal number.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment