Detailed Explanation of Variables and Input/Output in C Language

🧩 1. Basic Structure of a C Program

In the previous article, we have installed the Dev-C++ compilation environment. Today, we will write our first truly “interactive” C program—communicating with the user through input (scanf) and output (printf).

A simple C program looks like this👇

#include <stdio.h>   // Include standard input-output library
int main() {
    printf("Hello, World!\n");
    return 0;
}

After running, it will output in the console:

Hello, World!

📘 2. Variables and Data Types

In C language, variables act like a “container” for storing data.

✅ Common Data Types:

Data Type Description Example
<span>int</span> Integer type 10, -5
<span>float</span> Single precision floating point (decimal) 3.14
<span>double</span> Double precision floating point 3.1415926
<span>char</span> Character type ‘A’, ‘b’

🧠 Naming Rules:

  1. Can only consist of letters, numbers, and underscores.
  2. Cannot start with a number.
  3. Case-sensitive, for example, <span>age</span> and <span>Age</span> are different variables.
  4. Cannot duplicate keywords (such as <span>int</span>, <span>return</span>, <span>if</span>, etc.)

✅ Example:

int age = 20;
float score = 95.5;
char grade = 'A';

🖥️ 3. Output Function printf()

<span>printf()</span> is used to output information to the screen, and is the most commonly used function in C language.

✅ Basic Syntax:

printf("format control string", output variable);

🎯 Common Format Specifiers:

Format Specifier Meaning Example Output
<span>%d</span> Output integer 10
<span>%f</span> Output floating point 3.140000
<span>%.2f</span> Keep two decimal places 3.14
<span>%c</span> Output character A
<span>%s</span> Output string hello

Example:

int age = 20;
float score = 89.75;
printf("I am %d years old, and my score is %.1f points.\n", age, score);

Running result:

I am 20 years old, and my score is 89.8 points.

⌨️ 4. Input Function scanf()

<span>scanf()</span> is used to input data from the keyboard, allowing the program to interact with the user.

✅ Basic Syntax:

scanf("format control string", &variable name);

⚠️ Note: When inputting, **the variable name must be prefixed with &**, indicating that the data should be stored at the “address” of that variable.

Example:

int a, b;
printf("Please enter two integers:");
scanf("%d %d", &a, &b);
printf("Their sum is: %d\n", a + b);

Running effect:

Please enter two integers: 5 7
Their sum is: 12

🚫 5. Common Errors and Solutions

Error Cause Solution
Forgot to add <span>&</span> scanf cannot write to the variable Add <span>&</span> before the variable
Format specifier mismatch Type does not match the format specifier Ensure <span>%d</span> corresponds to int, <span>%f</span> corresponds to float
Forgot <span>#include <stdio.h></span> Function undefined Add this header file at the beginning of the file

🧠 6. Comprehensive Exercise

Exercise 1: Write a program to input the radius of a circle and output its circumference and area. (Hint: Circumference = 2πr, Area = πr², take π=3.14)

✅ Example Code:

#include <stdio.h>
int main() {
    float r, c, s;
    printf("Please enter the radius of the circle:");
    scanf("%f", &r);
    c = 2 * 3.14 * r;
    s = 3.14 * r * r;
    printf("The circumference of the circle is: %.2f\n", c);
    printf("The area of the circle is: %.2f\n", s);
    return 0;
}

🏁 7. Class Summary

Through this section, you have mastered:

  • ✅ Definition and naming rules of C language variables
  • ✅ printf() output format control
  • ✅ Usage of scanf() input
  • ✅ Solutions to common input/output errors

Leave a Comment