Learn how to perform input/output using printf and scanf functions in C.

C is a small language, and the core of C does not include any built-in Input/Output (I/O) functionality. However, the C Standard Library provides I/O functions through the stdio.h header file.

To use these functions, include the stdio.h header file in your C file:

#include <stdio.h>

Using this library, you have access to functions such as printf(), scanf(), sscanf(), fgets(), and fprintf().

Before diving into the functions, let’s talk about I/O streams in C. In C, we have three types of I/O streams:

  1. stdin (standard input)
  2. stdout (standard output)
  3. stderr (standard error)

When working with I/O functions in C, we always work with streams. A stream is a high-level interface that represents a device or a file. From the C standpoint, there’s no difference between reading from a file or reading from the command line - it’s all considered an I/O stream.

Now let’s explore some of the commonly used I/O functions:

printf()

The printf() function is one of the first functions you’ll encounter when learning C programming. It is used to print characters or values to the screen.

Here’s a simple example of using printf() to print a string literal:

printf("hey!");

Output: “hey!”

You can also print the value of a variable by using placeholders. For example, using %d as a placeholder for a signed decimal integer:

int age = 37;
printf("My age is %d", age);

Output: “My age is 37”

Multiple variables can be printed by separating them with commas:

int age_yesterday = 36;
int age_today = 37;
printf("Yesterday my age was %d and today is %d", age_yesterday, age_today);

Output: “Yesterday my age was 36 and today is 37”

There are various format specifiers available for different data types, such as %c for a char, %s for a string, %f for floating-point numbers, and %p for pointers.

You can even use escape characters in printf(), like \n to create a new line in the output.

scanf()

While printf() is an output function, scanf() is an input function used to get values from the user via the command line.

To use scanf(), you need to define a variable to hold the input value:

int age;

Then, call scanf() with two arguments: the format specifier for the variable type, and the address of the variable:

scanf("%d", &age);

For strings, since a string name is a pointer to the first character, you don’t need to use the & character:

char name[20];
scanf("%s", name);

Here’s an example program that demonstrates the use of both printf() and scanf():

#include <stdio.h>

int main(void) {
    char name[20];
    printf("Enter your name: ");
    scanf("%s", name);
    printf("You entered: %s", name);
}

Output:

Enter your name: John
You entered: John

In conclusion, understanding the basic Input/Output (I/O) concepts in C is crucial for developing applications that interact with users. The printf() function enables you to output values to the screen, while scanf() allows you to receive input from the user. These functions, along with the various format specifiers available, provide powerful tools for handling I/O operations in C.