Systematic Learning of C Language Without Textbooks: Variables, Constants, and Data Types

<Variables, Constants, and Data Types>From novice to expert, from Hello World to ACMAll practical content, no textbooks required!

1. Definition and Use of Variables and Constants

What is a variable?

Imagine a storage box: a variable is a “storage box” in memory used to hold data.

Variable definition syntax:

data_type variable_name;

Example:

int age;        // Define an integer variable age
float score;    // Define a single-precision floating-point variable score

What is a constant?

A constant is a fixed value, such as the value of pi, π=3.14.

Constant definition methods:

#define PI 3.14159      // Macro definition of a constant
const int MAX = 100;    // Constant defined with const

Common Mistakes Reminder

Variables must be declared before use!

Incorrect example:

printf("%d", x); int x = 10;

Correct example:

int x = 10; printf("%d", x);

2. Detailed Explanation of Basic Data Types

Four Basic Data Types

Data Type

Meaning

Value Range

Example

<span>int</span>

Integer

-2,147,483,648 to 2,147,483,647

<span>int age = 18;</span>

<span>float</span>

Single-precision floating-point

Approximately 6-7 significant digits

<span>float price = 19.99;</span>

<span>double</span>

Double-precision floating-point

Approximately 15-16 significant digits

<span>double pi = 3.1415926;</span>

<span>char</span>

Character

-128 to 127

<span>char grade = 'A';</span>

Memory Size Verification

#include<stdio.h>
int main() {
    printf("int size: %d bytes\n", sizeof(int));
    printf("float size: %d bytes\n", sizeof(float));
    printf("double size: %d bytes\n", sizeof(double));
    printf("char size: %d bytes\n", sizeof(char));
    return 0;
}

3. Identifier Naming Rules and Standards

Naming Rules

  1. Can only contain: letters, numbers, and underscores

  2. Must start with: a letter or underscore

  3. Case-sensitive: <span><span>age</span></span> and <span><span>Age</span></span> are different variables

  4. Cannot be keywords: such as <span><span>int</span></span>, <span><span>if</span></span>, <span><span>for</span></span>, etc.

Naming Standards

// Good naming
int student_age;        // Underscore separated
float averageScore;    // Camel case
const int MAX_SIZE;    // Constant in all uppercase
// Bad naming
int a;                 // Meaningless
float b123;            // Unclear meaning

Common Questions and Answers

Q: Can variable names be in Chinese?

A: Technically yes, butstrongly discouraged! It can lead to compatibility issues.

Q: What is the maximum length of a variable name?

A: The C standard requires at least 31 characters to be valid, but it is recommended not to exceed 20 characters.

4. Data Type Conversion and Storage Class Basics

Automatic Type Conversion (Implicit Conversion)

int a = 10;
float b = 3.5;
float result = a + b;  // a is automatically converted to float type

Explicit Type Conversion (Casting)

int a = 10, b = 3;
float result1 = a / b;           // Result: 3.0 (integer division)
float result2 = (float)a / b;     // Result: 3.333... (explicit conversion)

Storage Class Basics

auto: automatic variable (default, can be omitted)

auto int x = 10;    // Equivalent to int x = 10;


In-Class Test

Task 1: Variable Declaration and Initialization

#include<stdio.h>
int main() {
    // Declare and initialize variables
    int student_id = 2023001;
    float math_score = 95.5;
    char grade = 'A';
    // Output variable values
    printf("Student ID: %d\n", student_id);
    printf("Math Score: %.1f\n", math_score);
    printf("Grade: %c\n", grade);
    return 0;
}

Task 2: Data Type Conversion Experiment

#include<stdio.h>
int main() {
    int a = 7, b = 2;
    printf("7/2 = %d\n", a/b);           // Integer division
    printf("7.0/2 = %.2f\n", 7.0/b);     // Floating-point division
    printf("Casting: %.2f\n", (float)a/b);
    return 0;
}

Common Mistakes Detection

What issues can you find in the following code?

#include<stdio.h>
int main() {
    int 123num = 10;      // Error: starts with a number
    float score = 98.5;     char name = "Tom";    // Error: character should use single quotes
    printf("%d", Score);  // Error: inconsistent case
    return 0;
}


Homework

Basic Questions (Required)

  1. Define a program that includes the following variables:

    Integer variable: student age (18)

    Float variable: height (1.75)

    Character variable: gender (‘M’ or ‘F’)

    And output the values of these variables.

  2. Calculate and output:<span>(5 + 3.0) * 2</span> and observe the data type conversion.

Advanced Questions (Optional)

  1. Write a program to convert Fahrenheit to Celsius:

    Celsius = (Fahrenheit - 32) × 5/9
    
    

    Input Fahrenheit temperature and output Celsius temperature (keeping 2 decimal places).

Thought Questions

  1. Why is the result of <span>5/2</span> 2 instead of 2.5? How can we get 2.5?

Homework Answers

Answer to Basic Question 1:

#include &lt;stdio.h&gt;
int main() {
    int age = 18;
    float height = 1.75;
    char gender = 'M';
    printf("Age: %d\n", age);
    printf("Height: %.2f meters\n", height);
    printf("Gender: %c\n", gender);
    return 0;
}

Answer to Basic Question 2:

#include&lt;stdio.h&gt;
int main() {
    float result = (5 + 3.0) * 2;
    printf("Result: %.1f\n", result);  // Output: 16.0
    return 0;
}

Answer to Advanced Question:

#include&lt;stdio.h&gt;
int main() {
    float fahrenheit, celsius;
    printf("Please enter Fahrenheit temperature: ");
    scanf("%f", &amp;fahrenheit);
    celsius = (fahrenheit - 32) * 5 / 9;
    printf("Celsius temperature: %.2f\n", celsius);
    return 0;
}

Answer to Thought Question:

<span>5/2</span> is integer division, and the result is truncated. To get 2.5, you need to change it to <span>5.0/2</span> or <span>(float)5/2</span>.

In the next class, we will learn about: Operators

If you have any questions, feel free to comment!

Follow YunJie Algorithm, no textbooks needed, just a finger tap to systematically learn programming!

Leave a Comment