C Language Input scanf() function Conversion Specification

C Programming Input

There are the three basic functions of any program or application: It First takes some data as an input, Second, it processes that data (input) based on some defined rule(s), and finally it gives the relevant output based on that processing of data.

In C Language Input/Output is performed through a set of library functions that are by default supplied with every C compiler. These functions are formally not a part of the C language but they are considered standard for all input/output operations in C. The set of library functions that are used to perform the input/output operations are called the standard C Language Input/Output library functions.

There are also several header files that provide necessary information that supports various library functions. These header files are entered in the C program at the beginning of the program using #include directives. For instance, if a program is needed any function from a C Language Input/Output library, then the C program must include the stdio.h file as – #include.

Similarly, there are other header files like math.h, string.h, alloc.h that must be included to execute the corresponding function(s). scanf() and getchar() are widely used input functions while printf() and putchar() are the function used for output.

Conversion Specifications

The commonly used functions like scanf( ) and printf( ) use the conversion specifications (part of the C Language Input/Output) to specify the type and size of the data. Every conversion specification starts with (%) sign. Below is the list of some commonly used conversion specifications.

  1. %c – A single character.
  2. %d – A decimal integer.
  3. %e – A floating-point number.
  4. %f – A floating-point number.
  5. %g – A floating-point number.
  6. %h – A short integer.
  7. %i – A decimal, octal, or hexadecimal integer.
  8. %lf – A long range of floating-point numbers (for the double data type).
  9. %o – An octal integer.
  10. %s – A string.
  11. %x – A hexadecimal integer.
  12. The modifier l can be used before conversion specifications e, f, g to specify the double data type, while the modifier L can be used before them to specify a long double data type. For example, %ld, %hd, %Lf, %hx are also some valid conversion specifications.

scanf(): Reading Input Data

Input data is used to input into memory from a commonly used input device (keyboard). C provides the library function scanf( ) in order to enter the input data. This function can take the values of types of character, numeric, and string as an input. scanf() library function must have at least two parameters. 

  • The first parameter is a control string, which contains conversion specification characters written within double-quotes. There may be one or more conversion specifications character.
  • The other parameter in scanf() library function is the addresses of variables. In the scanf( ) function at least one address must be present. The address of a variable is found by preceding the variable name by the ampersand (&) sign. the ampersand (&) sign is also called the address operator and it basically gives the starting address of the variable name in memory.
  • Note: A string variable is not preceded by an ampersand (&) sign to get the address of the string.

The syntax for the scanf( ) function is defined as: scanf(“control string” , address 1, address2, ….);

Examples of scanf() function

Example.1:
#include<stdio.h>
main()
{
int i;
-------;
scanf("%d", &i);
--------;
}

The above example specifies that the control string contains only one conversion specification character %d, which implies that one integer value is to be entered as an input. This entered integer value will be stored in the variable i.

Example.2:
#include<stdio.h>
main()
{
char ch;
------;
scanf("%c", &ch);
------;
}

In this example, the control string contains conversion specification Character %c, which specifies that a single character must be entered as input. This entered character will be stored in the variable ch.

Example.3:
#include<stdio.h>
main()
{
float distance;
------;
scanf("%f", &distance);
------;
}

In this example, the control string contains conversion specification Character %f, which specifies that a floating-point number must be entered as input. This entered decimal number will be stored in the variable distance.

Example.4:
#include<stdio.h>
main()
{
char str[20];
------;
scanf("%s", str);
------;
}

In this example, the control string contains conversion specification Character %s, which specifies that a string of maximum 20 characters must be entered as input. This entered string will be stored in the variable str. It must also be noted that the variable str is not preceded by the ampersand (&) sign.

Note: More than one value of different datatype can also be given as an input at the same time through scanf() library function. A few example are listed below which are related to input more than one value using scanf().

Example.5:
#include<stdio.h>
main()
{
int class;
char section;
float marks;
------;
scanf("%d %c %f", &class,&section,&marks);
------;
}

In this example, the control string contains conversion specification Characters %d, %c, and %f which specifies that an integer, a character, and a floating-point number are entered as input. These entered input values will be stored in variables class, section, and marks respectively. As per the given example, the input value can be entered as – 9 A 98.50 (means class is 9, the section is A, and 98.50 marks are obtained).

Input with delimiters

In the above example number 5, three input values are entered with space since there is space between the conversion specification Characters %d, %c, and %f. Input can also be given using delimiters some examples are listed below for more understanding.

Example.6:
#include<stdio.h>
main()
{
int class;
char section;
float marks;
------;
scanf("%d,%c,%f", &class,&section,&marks);
------;
}

As per the given example, the input value can be entered as – 9, A,98.50 and These entered input values will be stored in variables class, section, and marks respectively.

Leave a Comment

Your email address will not be published. Required fields are marked *