Input and output functions in C


This program demonstrates how to use the scanf, getchar, putchar, gets and puts functions in C programming language.

  • In the main function, the program declares three variables: an integer "a", a character "c", and an array of characters "name".
  • The program uses the printf function to display a message asking the user to enter a value for the integer "a" and then uses the scanf function to read the value entered by the user and store it in the variable "a".
  • Then, the program uses the printf function to display the value of the integer "a".
  • Next, the program uses the printf function to display a message asking the user to enter a character, then uses the fflush(stdin) function to clear the input buffer and the getchar function to read the character entered by the user and store it in the variable "c".
  • Then, the program uses the putchar function to display the character stored in the variable "c".
  • After that, the program uses the printf function to display a message asking the user to enter a name, then uses the fflush(stdin) function to clear the input buffer and the gets function to read the name entered by the user and store it in the array of characters "name".
  • Finally, the program uses the puts function to display the name stored in the array "name".
  • It's important to note that the use of the gets function is not recommended due to security reasons, it's safer to use the fgets function instead.
  • Additionally, the fflush(stdin) function is not a standard C function, it's specific to some compilers and its behavior may vary, it's better to use the fgets function instead of the gets function and fflush(stdin) function.

In conclusion, this program shows how to use input/output functions in C such as scanf, getchar, putchar, gets and puts to read and display data from and to the user.

Source Code

//Input and output functions in C Programming
 
#include<stdio.h>
int main()
{
    int a;
    char c;
    char name[50];
    printf("\nEnter The Value of A : ");
    scanf("%d",&a);
    printf("\n A : %d",a);
    printf("\nEnter The Character : ");
    fflush(stdin);
    c=getchar();
    putchar(c);
    printf("\nEnter Name: ");
    fflush(stdin);
    gets(name);
    puts(name);
    return 0;
}
 
To download raw file Click Here

Output

Enter The Value of A : 23

 A : 23
Enter The Character : s
s
Enter Name: sam
sam

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C