Search Result
Details here...

Basic Input/Output in C | printf and scanf

Previous

Escape Sequences in C

Next

C Operators | Basics


In many of the previous chapters, we have seen the inclusion of libraries (like; stdio.h) and use of printf function. printf is a basic I/O function in C. In this chapter, we will learn about the Formatted I/O functions in C.

Did you know?

A library is a collection of pre-written functions that you can use in your program, to add functionality to your program, or even for quicker development.

About stdio.h

The standard input output library (stdio.h) is a standard library of the C system that provides a set of functions for input and output. The functions in this library are:
  • scanf()
  • printf()
  • getc()
  • putc()
  • fopen()
  • fclose()

The stdio.h header file has the definitions of the functions like scanf(), printf(), etc.. To use this library, you need to include the header file stdio.h in your program.
#include <stdio.h>

About printf

The printf() function, in C, prints the value passed as the parameter to it, on the console screen.

The syntax of the printf() function is:
printf ("%X", variableOfTypeX);
// Here, %X is the format specifier which tells the compiler the type of the data to store in the variable.

Let's take an example

C

#include <stdio.h>
int main()
{
int age = 18;
printf("%d", age);
return 0;
}

Output

18

Explanation

In the above example, a int variable age is declared and initialized to 18. The printf() function is used to output the value of the variable age on the console screen.

Here, %d is the format specifier for the integer data type.

Info

We'll learn about the format specifiers in detail in this chapter, lastly.

About scanf

The scanf() function, in C, reads the value from the console as per the type specified in the format specifier.

The syntax of the scanf() function is:
scanf ("%X","&variableOfTypeX");
// Here, %X is the format specifier which tells the compiler the type of the data is in the variable.
// Also, & is the address operator which gives the address of the variable to the scanf function.

Let's take an example

C

#include <stdio.h>
int main()
{
int age;
printf("Enter your age : ");
scanf("%d", &age);
printf("%d", age);
return 0;
}

Output

Enter your age : 17
17

Explanation

In the above example, an int variable age is declared and using the scanf function, an integer input is expected. After the integer input is given, then it is stored in the address of the age variable (&age). Which means, when the variable age is printed, the input is then printed on the console screen.

Here, %d is the format specifier for the integer data type.

Info

Here, & is necessary to pass the address of the variable to the scanf function. Omitting this, the compiler will not know where to store the input. As a result, an error will be generated.

Format Specifiers

The format specifier is used to tell the compiler the type of the data is in the variable, or the type of the data the user is expected to input. The format specifier is used in the scanf() and printf() functions. Some of the format specifiers are:

Format Specifier Description
%d Integer
%f Floating point
%c Character
%s String
%x Hexadecimal
%lf Double
%ld Long
%hd Short
%lld Long long
%llu Unsigned long long
%lu Unsigned long
%hu Unsigned short
%hhu Unsigned char
%e or %E Scientific notation
%% Used to print a single %

Examples using different format specifiers

C

#include <stdio.h>
int main()
{
int age;
char favouriteLetter;
float g;
double PI;
printf("Enter your age : ");
scanf("%d", &age);
printf("Enter your favourite letter : ");
scanf(" %c", &favouriteLetter);
printf("Enter the value of g : ");
scanf("%f", &g);
printf("Enter the value of PI : ");
scanf("%lf", &PI);
printf("Your age is %d, ", age);
printf("your favourite English Letter is %c, ", favouriteLetter);
printf("the value of g is %f and, ", g);
printf("the value of PI is %lf.", PI);
return 0;
}

Output

Enter your age : 18
Enter your favourite letter : D
Enter the value of g : 9.18
Enter the value of PI : 3.1415
Your age is 18, your favourite English Letter is D, the value of g is 9.180000 and, the value of PI is 3.141500.

Did you notice?

It is obvious that, %c is used for the input of the char(the favourite letter). But a whitespace is used before %c to ignore the first character that it reads. It is because, the first character that it reads is the new line character due to the return key pressed by the user after the input of the int(age). So, to store the expected input, the whitespace is used.

Note that, this can also be implemented using the fflush(stdin) function.

Additional Information

Between the % and the X in %X, there are options that can be used. They are called flags.

Flag Description
- The value is padded with spaces and is left justified in the field. Right justification is the default.
+ The plus sign forces to display the sign of the number, even if the number is positive. By default, only the minus sign is displayed if the number is negative.
0 The value is padded with zeros from the left.
Any number It specifies the minimum field width for the value. If the value is shorter than the field width, the field is padded with spaces from the left.
. A period is used to seperate the field width and the precision. It is used in the case of floating point numbers, to specify the number of digits after the decimal point.

Examples using different format specifiers

C

#include <stdio.h>
int main()
{
int age = 18;
float PI = 3.1415;
printf("%10d\n", age);
printf("%010d\n", age);
printf("%-d\n", age);
printf("%-10d\n", age);
printf("%+d\n", age);
printf("%.2f\n", PI);
return 0;
}

Output

        18
0000000018
18
18
+18
3.14
In this chapter, we have covered only the basic input/output functions. There are many more functions that can be used to input/output data, which we will be covering in the coming chapters.
By darpan.kattel
Previous

Escape Sequences in C

Next

C Operators | Basics