Tech Study

C Input/Output (I/O): printf() and scanf()

Introduction

In C language, to perform the tasks we need to process data given by the programmer/user and display the results on console. C language provides Input/Output functions to carry out the tasks.

The 3 basic steps happen in a C program are, 

  • Input the data
  • Process data & logic
  • Display the results

Why I/O functions?

To conclude that the program is properly developed, programmer should feed some data to the program up on which the logic built is tested, this is done using the input functions available in C programming. Similarly, the results of the execution are intended to be displayed on the console using the output functions.

C Standard files

C language treats all devices as files. Hence, the input and output devices are also treated as standard files.

Standard File (File Pointer) Device
Standard Input Keyboard
Standard Output Console (Screen)
Standard Error Your Screen

 

Types of Input/Output Functions

Based on the type of the data to be processed, C language supports two types of functions for I/O operations, they are:

  • Formatted functions
  • Unformatted functions

Formatted functions

Formatted functions accept the data in a specific format. The standard library in C provides I/O functions such as printf() and scanf(), for printing the data on console and accepting input from keyboard respectively.

Syntax for scanf():

Where, “format_specifier” changes based on the input to be read from the standardinput. We can also include any prompt message along with the format specifier. ’&’ indicates that the value to be stored in var_name.

Format specifier Data type
%d,%i int
%lf double
%f float
%c char
%hd short int
%li long int
%Lf long double
%u unsigned int
%lu Unsigned long int

 

Syntax for

printf():

Example:

#include <stdio.h>

 

int main()

{

    int a;

    char b;

    float c;

    

    scanf("%d %c %f",&a,&b,&c);

    printf("The data:%d %c %f",a,b,c);

 

    return 0;

}

Output:

5

c

7.5

The data:5 c 7.500000

 

Unformatted functions

Unformatted functions cannot control the format for the data. The input data and the display data will not be in user defined form. These unformatted functions are of two types:

  • Character I/O functions
  • String I/O functions

Character I/O functions are capable of reading and writing only a single character from the keyboard and to the console respectively.

String I/O functions can easily read and write string data. In programming strings and character arrays are referred as a collection of characters.

Character I/O functions String I/O functions
Input functions Output functions
getch()

getchar()

getche()

putch()

putchar()

Input functions Output functions
puts() gets()

 

Example(character functions):

#include <stdio.h>

 

int main()

{

    char b;

    char str[50];

    printf("enter character: ");

    b = getchar();

    // print character

    putchar(b);

    return 0;

}

Output:

enter character: Char  

C

 

Example(String functions):

#include <stdio.h>

 

int main()

 

{

    char str[50];

    printf("enter string:");

    gets(str);

    

    puts(str);

}

Output:

enter string:String functions

String functions

 

C String Functions

C String Functions perform certain operations, It provides many useful string functions which can come into action. The <string.h> header …

Read more

Function Pointer in C++

Introduction : Function Pointer in C++ Pointers: In function pointer in c++ are the variables that consist of addresses of …

Read more