Home Show/Hide Menu
C Tutorials
➜ C Language
  - Features of C
  - First C Program
  - Compilation in C
  - C Character Set
  - Tokens
  - Keywords
  - Identifiers
  - Constants
  - Operators
  - Data Types
  - Variables
  - Type Casting
  - Comments
  - Input/Output
  - Escape Sequence
  - Programming Errors
➜ Control Statements
  - if Statement
  - if-else Statement
  - Nested if-else
  - else-if
  - switch Statement
  - Loop
  - while loop
  - do while loop
  - for loop
  - break
  - continue & goto
  - Nested Loop
  - Infinite Loop
➜ Functions
  - What is function
  - Call by Value & Reference
  - Recursive function
  - Storage Classes
➜ Array
  - 1-D Array
  - 2-D Array
  - Return an Array
  - Array to Function
➜ C Pointers
  - Pointers
  - Pointer to Pointer
  - Pointer Arithmetic
  - Dangling Pointers
  - sizeof() operator
  - const Pointer
  - void pointer
  - Dereference Pointer
  - Null Pointer
  - Function Pointer
  - Function pointer as argument
➜ Memory Management
  - Memory Layout of C
  - Dynamic memory
  - calloc()
  - malloc()
  - realloc()
  - free()
➜ Strings
  - gets() & puts()
  - String Functions
  - strlen()
  - strcpy()
  - strcat()
  - strcmp()
  - strrev()
  - strlwr()
  - strupr()
  - strstr()
➜ C Math
  - Math Functions
➜ Enum, Structure & Union
  - Enum
  - Structure
  - typedef
  - Array of Structures
  - Nested Structure
  - Structure Padding
  - Union
➜ File Handling
  - fprintf() fscanf()
  - fputc() fgetc()
  - fputs() fgets()
  - fseek()
  - EOF and feof()
  - fsetpos()
  - tmpfile()
  - rewind()
  - ftell()
  - lseek()
➜ Preprocessor
  - Preprocessor
  - Macros
  - #include
  - #define
  - #undef
  - #ifdef
  - #ifndef
  - #if
  - #else
  - #error
  - #pragma
➜ Command Line
  - Arguments

Input/Output in C Language

Input means to provide some data or information from input device to be used in the program and Output means to display data or information on the output devices (e.g. screen, printer or to a file on harddisk).
The C programming language provides several standard library functions that allow input and output in a program. The stdio.h is a header file which has the necessary information to include the input/output related functions (e.g. printf(), scanf() etc.) in our program. If we want to use printf or scanf function in our program, we should include the stdio.h header file in our source code.


In C language I/O functions are useful for a program to interact with users. stdio.h is the standard C library for input-output functions. In C two important streams play very important roles while dealing with input-output functions. These are:
    • Standard Input (stdin)
    • Standard Output (stdout)

Standard input or stdin is used to take input from devices such as the keyboard as an input data stream. Standard output or stdout is used to give output to a device such as a monitor. Programmers must include stdio header-file within the program to use I/O functionality of C language.

There are classifications of I/O functions as given below.


1. Formatted Input/Output - C language perform formatted inputs and outputs using standard functions scanf() and printf(). These functions accept a format specifier string and a list of variables as the parameters. The format specifier is a character string that specifies the data type of each variable to be inputor output and the size or width of the input/output.

Formatted Output Function
The printf() function is used to print or display as formatted output based on a format specification. The format specifier string, along with the output variables, are the parameters to the printf() function.

The basic form of a printf function call is:
printf (format_specifier_string, list_of_expressions);

A format specifier is a symbol that is used as a placeholder in a formatting string. For integer output, %d is the specifier that holds the place for integers.
Here are commonly used list of some format specifiers:

  %d  -  int (signed decimal integer) 
  %i  -  a decimal integer (detects the base automatically)
  %u  -  unsigned decimal integer 
  %hi -  short (signed)
  %hu -  short (unsigned)
  %f  -  floating point values (fixed notation) - float, double 
  %Lf -  long double
  %e  -  floating point values (exponential notation) 
  %E  -  a floating point number in scientific notation
  %o  -  an octal (base 8) integer
  %x  -  a hexadecimal (base 16) integer
  %s  -  string 
  %c  -  character 
  %p  -  an address (or pointer)
  %n  -  prints nothing
  %%  -  the % symbol
   
#include<stdio.h>
void main()
{
  int day=25, month=10, year=1985;
  int var1;
  float var2;
  char var3;
  char str[] = "Hello";
  
  printf("Hello, world!\n");
  printf("your date of birth: %d-%d-%d", &day, &month, &year);
  printf("Enter an integer, a float and a character\n> ");
  printf("value of var1=%d value of var2=%f value of var3=%c", &i, &f, &c);
  printf("%s", str);     // print the word "Hello"
}

Formatted Input Function
The scanf() function is used to read and convert characters from the standards input depending on the format specifier string and stores the input in variables represented by theparameters (var1, var2,….).

The basic form of a scanf function call is:
scanf(format_specifier_string, list_of_variable_addresses);

#include<stdio.h>
void main()
{
  int month, day, year;
  int i;
  float f;
  char c;
  char name[20];
  
  printf("Please, enter your date of birth: ");
  scanf("%d %d %d", &day, &month, &year);
  printf("Enter an integer, a float and a character\n> ");
  scanf("%d %f %c", &i, &f, &c);
  printf("Please, enter your name: ");
  scanf("%s", name);
}

2. Unformatted Input/Output - Unformatted input/output functions are used to read a single input from the user or to display the value of variables as output to the user console. These are input/output library functions that deal with one character or an array of characters (string) at a time. Unformatted input/output functions work only with character datatype (char).

Some of the most important formatted input/output functions are -
getch() - Reads a single character from the user at the console, without echoing it.
getche() - Reads a single character from the user at the console, and echoing it.
getchar() - Reads a single character from the user at the console, and echoing it, but needs an Enter key to be pressed at the end.
gets() - Reads a single string entered by the user at the console.
puts() - Displays a single string's value at the console.
putch() - Displays a single character value at the console.
putchar() - Displays a single character value at the console.

Example 1: getchar() & getch()

#include<stdio.h>
#include<conio.h>
void main()
{
 	char c;
	printf("Enter a alphabet : ");
	c = getchar();
	printf("\nEntered alphabet is : %c ", c);
	printf("\nPress Y to exit");
	getch();
}

Output:

Enter a alphabet : y
Entered alphabet is : y
Press Y to exit

Example 2: getche()

#include<stdio.h>
#include<conio.h>
void main()
{
	printf("\nPress Y to exit");
	getche();
}

Output:

Press Y to exit
Y

Example 3: gets()

#include<stdio.h>
#include<conio.h>
void main()
{
	char str[25];
	printf("Enter a name : ");
	gets(str);
	printf("\n%s is awesome ",str);
}

Output:

Enter a string : Rakhi
Rakhi is awesome

Example 4: putchar() & putch()

#include<stdio.h>
#include<conio.h>
void main()
{
	char c = 'K';
	putchar(c);
	printf("Press Y to continue\n ");
	c = getch();
	printf("input : ");
	putch(c);
}

Output:
K
Press Y to continue
input : Y

Example 5: puts()

#include<stdio.h>
#include<conio.h>
void main()
{
	char name[25];
	printf("Enter your name : ");
	gets(name);
	puts(name);	
}

Output:

Enter your name : Rakhi Gupta
Rakhi Gupta




ADVERTISEMENT