C Input and Output

C programming provides the suppport of Input and Output through some built-in functions present in the standard input-output header file stdio.h.

Input means to provide some input to the program either through input devices like keyboard or through files. Output is the text spitted out by the program onto the screen.

Following are some built-in functions in the C language to provide input and output support.

scanf() and printf() Functions

scanf(): It reads inputs for data types from standard input like command line interface or text and stores them according to the parameter format. Here f stands for formatted.

Syntax

int scanf (const char * format, ...);

Scanf returns the number of input items successfully matched and assigned. The variables in scanf must be preceeded with an ampersand & symbol; it is used to access the address of the memory location to store the data. There can be multiple variables present in the scanf function separated by commas.


printf(): It's purpose is to print formatted text to the standard output stream. Here f stands for formatted.

Syntax

int printf (const char * format, ...);

The printf function returns an integer value when executed. The value returned is equal to the number of characters printed. Printf can use strings along with format specifiers to display a hybrid output. There can be multiple variables present in the printf function separated by commas.


The following program demonstrates the usage of printf() and scanf() functions.

Code

#include<stdio.h>
int main(void)
{
    char name[100];
    int age;
    float salary;
    
    printf("Enter your name: ");
    scanf("%s",&name); //Don't use space
    printf("Enter your age: ");
    scanf("%d",&age);
    printf("Enter your salary: ");
    scanf("%f",&salary);
    printf("Hello %s!\nYour age: %d\nYour salary: %.2f\n",name,age,salary);
    return 0;
}

Output

Enter your name: John
Enter your age: 32
Enter your salary: 80000.00
Hello John! 
Your age: 32
Your salary: 80000.00

getc() and putc() Function

getc() is used to accept a character from any stream including the standard input (stdin). Returns EOF on failure.

Syntax

int getc (FILE *stream);

putc() is used to write a character (an unsigned char) to the specified stream and advances the position indicator for the stream.

Syntax

int putc (int char, FILE *stream);

Example

#include<stdio.h>
int main (void) 
{
   char var;
   printf("Enter a character: ");
   var = getc(stdin);
   printf("Character entered: ");
   putc(var, stdout);
   return 0;
}

Output

Enter a character: c
Character entered: c

getchar() and putchar() Function

getchar() is used to accept a character from the standard input stream (stdin).

Syntax

int getchar();

putchar() is used to write a character to the display or return the same character.

Syntax

int putchar (int character);

Example

#include<stdio.h>
int main(void)
{
    char var;
    
    printf("Enter a character: ");
    var = getchar();
    printf("Character entered: ");
    putchar(var);
    
    return 0;
}

Output

Enter a character: j
Character entered: j

Another way to input and output a character is using getch() and putch() respectively. They are non-standard functions defined in the header file conio.h and are supported by MS-DOS compilers.

gets() and puts() Function

gets() is used to accept a string from the standard input stream (stdin) into the buffer pointed by the str pointer. It will take input until a newline or End of File (EOF) is occured.

Syntax

char *gets (char *str);

puts() is used to write a string to the display or return the same string.

Syntax

char *puts (char *str);

Example

#include<stdio.h>
int main(void)
{
    char var[100];
    
    printf("Enter a string: ");
    gets(var);
    printf("String entered: ");
    puts(var);
    
    return 0;
}

Output

Enter a string: Good Morning!
String entered: Good Morning!

Difference between scanf() and gets()

While both of them can be used to read strings but the scanf() function cannot take whitespaces. Whenever a whitespace is encountered, the text before the white space is stored and the remaining text is left at the mercy of later code. The gets() function reads whitespace as a character.

Trending

C Programming

Remember to follow community guidelines and don't go off topic.