C tutorial- formatted i/o (input/output) function in C Programming language

Programming languages or concepts

Formatted Input/Output Functions in C Programming: Learn C Programming Basics


Introduction To C

The C Programming Language, known as the "Mother of languages," was developed by Dennis Ritchie between 1969 and 1973. It belongs to the second and third generation of languages, providing both low-level and high-level features. C combines the power of low-level languages with the flexibility and simplicity of high-level languages.

Formatted Input/Output Functions in C:

C provides standard functions, scanf() and printf(), for performing formatted input and output. These functions accept a format specification string and a list of variables as parameters.

There are two kinds of console input/output functions -

  • Formatted input/output functions.
  • Unformatted input/output functions.

>
Some of the most important formatted console input/output functions are -



Formatted Input Function (scanf()):

The scanf() function is used for formatted input from standard input and shares many conversion facilities with the printf() function.
Syntax:
   scanf("Format String", expression...);
Example:
   scanf("%c %d", &subject, &RollNo)

 Formatted Output Function (printf()):

The printf() function is used for formatted output to the standard output based on a format specification. It is the common method for printing data in C programs.

Syntax:

   printf("format string"[, expression]...);

Example:

   #include<stdio.h>

   int main() {

      printf("Formatted I/O function in C programming language");

      return 0;

   }

   Output: Formatted I/O function in C programming language 

 

Understanding the Example Program:

- All valid C programs must contain the main() function, which serves as the entry point. Code execution begins from the start of the main() function.
- The printf() function is a library function used to send formatted output to the screen. It prints the string inside the quotation marks.
- To use printf() in our program, we need to include the "stdio.h" header file using the #include<stdio.h> statement.
- The return 0; statement inside the main() function is the exit status of the program, which is optional.

Example: Formatted Output with Variables
#include<stdio.h>
int main() {
   int tInteger = 5;
   printf("Num = %d", tInteger);
   return 0;
}
Output: Num = 5

C Input:
In C programming, scanf() is commonly used to take input from the user. The scanf() function reads formatted input from the standard input, such as keyboards.

Example: Integer Input/Output
#include<stdio.h>
int main() {
   int Integer;
   printf("Enter an Integer: ");
   scanf("%d", &Integer);
   printf("num = %d", Integer);
   return 0;
}
Output:
   Enter an Integer: 10
   num = 10

- We use %d format specifier inside the scanf() function to take an integer input from the user.
- When the user enters an integer, it is stored in the Integer variable.
- Note that we used &Integer inside scanf(). The ampersand (&) retrieves the address of Integer, allowing the entered value to be stored at that address.

List of commonly used C data types and their format specifiers.


 Data types.                              Format specifier

  • int.                                                  %d
  • char.                                               %c
  • float.                                               %f
  • double.                                           %lf
  • short int.                                       %hd
  • unsigned int.                                %u
  • long int.                                          %li
  • long long int.                                 %lli
  • unsigned long int.                        %lu
  • Signed char.                                  %c
  • unsigned char.                             %c
  • long double.                                  %lf

Unformatted input/output functions:-


Why the functions are called unformatted console I/O functions?

While calling any of the unformatted console input/output functions, we do not have to use any format specifiers in them, to read or display a value. Hence, these functions are named unformatted console I/O functions.

Unformatted input/output functions:-
    Unformatted console input/output functions are used to read a single input from the user at console and it also allows us to display the value in the output to the user at the console.

    Some of the most important formatted console input/output functions are -





close