Easy Tutorial
❮ C Function Ldiv C Functions ❯

Input & Output

When we mention input, it means feeding some data into a program. Input can be in the form of a file or from the command line. C provides a set of built-in functions to read given input and fill it into the program as needed.

When we mention output, it means displaying some data on the screen, printer, or any file. C provides a set of built-in functions to output data to the computer screen and save data to text files or binary files.

Standard Files

C treats all devices as files. So devices (like the monitor) are handled in the same way as files. The following three files are automatically opened when a program executes to access the keyboard and screen.

Standard File File Pointer Device
Standard Input stdin Keyboard
Standard Output stdout Screen
Standard Error stderr Your screen

The file pointer is the way to access the file. This section will explain how to read values from the keyboard and how to output results to the screen.

I/O (Input/Output) in C is typically done using the printf() and scanf() functions.

The scanf() function is used to read and format from the standard input (keyboard), and the printf() function sends formatted output to the standard output (screen).

Example

#include <stdio.h>      // Required for the printf() function
int main()
{
    printf("tutorialpro.org");  // Displays the content within the quotes
    return 0;
}

Compiling the above program, the output is:

tutorialpro.org

Example Analysis:

%d Formatted Output for Integer

#include <stdio.h>
int main()
{
    int testInteger = 5;
    printf("Number = %d", testInteger);
    return 0;
}

Compiling the above program, the output is:

Number = 5

Using "%d" (integer) within the quotes of the printf() function matches the integer variable testInteger and outputs it to the screen.

%f Formatted Output for Floating-Point Data

#include <stdio.h>
int main()
{
    float f;
    printf("Enter a number: ");
    // %f matches floating-point data
    scanf("%f", &f);
    printf("Value = %f", f);
    return 0;
}

getchar() & putchar() Functions

The int getchar(void) function reads the next available character from the screen and returns it as an integer. This function reads only a single character at a time. You can use this method in a loop to read multiple characters from the screen.

The int putchar(int c) function outputs the character to the screen and returns the same character. This function outputs only a single character at a time. You can use this method in a loop to output multiple characters to the screen.

See the following example:

Example

#include <stdio.h>
int main()
{
   int c;
   printf("Enter a value :");
   c = getchar();
   printf("\nYou entered: ");
   putchar(c);
   printf("\n");
   return 0;
}

When the above code is compiled and executed, it waits for you to input some text. When you enter a text and press the Enter key, the program continues and reads only a single character, displaying as follows:

$./a.out
Enter a value :tutorialpro

You entered: r

gets() & puts() Functions

The char *gets(char *s) function reads a line from stdin into the buffer pointed to by s until a terminating newline or EOF.

The int puts(const char *s) function writes the string s and a trailing newline to stdout.

Example

#include <stdio.h>
int main()
{
   char str[100];
   printf("Enter a value :");
   gets(str);
   printf("\nYou entered: ");
   puts(str);
   return 0;
}

When the above code is compiled and executed, it waits for you to input some text. When you enter a text and press the Enter key, the program continues and reads the entire line until the end, displaying as follows:

$./a.out
Enter a value :tutorialpro

You entered: tutorialpro

scanf() and printf() Functions

The int scanf(const char *format, ...) function reads input from the standard input stream stdin and scans that input according to the provided format.

The int printf(const char *format, ...) function writes output to the standard output stream stdout and produces output according to the format provided.

format can be a simple constant string, but you can specify %s, %d, %c, %f, etc., to output or read strings, integers, characters, or floating-point numbers. There are many other available format options that can be used as needed. For full details, refer to the reference manual for these functions. Now, let's deepen our understanding with the following simple example:

Example

#include <stdio.h>
int main()
{
   char str[100];
   int i;
   printf("Enter a value :");
   scanf("%s %d", str, &i);
   printf("\nYou entered: %s %d ", str, i);
   printf("\n");
   return 0;
}

When the above code is compiled and executed, it waits for you to input some text. When you enter a text and press the Enter key, the program continues and reads the input, displaying as follows:

$./a.out
Enter a value :tutorialpro 123

You entered: tutorialpro 123

Here, it should be noted that scanf() expects input in the same format as provided by %s and %d, meaning you must provide valid input like "string integer". If you provide "string string" or "integer integer", it will be considered incorrect input. Also, when reading strings, scanf() stops reading at the first space, so "this is test" is considered three strings by scanf().

❮ C Function Ldiv C Functions ❯