Detailed Explanation of Input and Output Functions in C: scanf and printf

In C language, input and output are important ways for programs to interact with users.<span>scanf</span> and <span>printf</span> are the two most commonly used functions in C for handling input and output. This article will provide a detailed introduction to the usage of these two functions, along with code examples to help everyone understand.

1. printf Function

1.1 Function Prototype

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

1.2 Functionality

<span>printf</span> is used to print formatted data to standard output (usually the screen).

1.3 Format String

The format string can contain text, format specifiers, and escape characters. Common format specifiers include:

  • <span>%d</span>: Print integer
  • <span>%f</span>: Print floating-point number
  • <span>%c</span>: Print single character
  • <span>%s</span>: Print string

1.4 Example Code

The following example demonstrates how to use <span>printf</span> to output different types of data:

#include <stdio.h>
int main() {    int age = 25;    float height = 5.9;    char initial = 'A';    char name[] = "Alice";
    printf("Name: %s\n", name);    printf("Initial: %c\n", initial);    printf("Age: %d years\n", age);    printf("Height: %.2f feet\n", height);
    return 0;}

Output:

Name: AliceInitial: AAge: 25 yearsHeight: 5.90 feet

2. scanf Function

2.1 Function Prototype

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

2.2 Functionality

<span>scanf</span> is used to read formatted data from standard input (usually the keyboard).

2.3 Format String

Similar to <span>printf</span>, the format string of <span>scanf</span> also contains format specifiers to specify the type of data to be read. For example:

  • <span>%d</span>: Input integer
  • <span>%f</span>: Input floating-point number
  • <span>%c</span>: Input single character
  • <span>%s</span>: Input string

Note: When using <span>scanf</span>, you must provide the variable address to store the read data, which can be done using the address-of operator <span>&</span>.

2.4 Example Code

The following example demonstrates how to use <span>scanf</span> to get data from the user:

#include <stdio.h>
int main() {    int age;    float height;
    printf("Enter your age: ");    scanf("%d", &age); // Note the address-of operator here
    printf("Enter your height in feet: ");    scanf("%f", &height); // Address is also needed here
    printf("You are %d years old and %.2f feet tall.\n", age, height);
   return 0;}

User Interaction Example:

Assuming the user inputs the following:

30  6.0  

The program output will be:

You are 30 years old and 6.00 feet tall.

Conclusion

In this article, we have provided a detailed introduction to the basic input and output functions in C— <span>printf()</span> and <span>scanf()</span>. We learned their basic syntax, functionality, and common usage, and demonstrated how to print and receive data through examples. This foundational knowledge is crucial for beginners and is an essential part of writing any C program.

We hope this article helps you better understand and apply these basic I/O operations!

Leave a Comment