Introduction to C Language Basics: From the First Program to Variable Operations

Introduction to C Language Basics: From the First Program to Variable Operations

This article records some of the most fundamental knowledge points and practical examples I encountered while learning C language, suitable for beginners who are just getting started with C as a reference.

(1) The First C Program

#include <stdio.h>

int main()
{
    printf("Hello micu\n");
    //printf("Hello may\n");
    //printf("Hello micu \n  Hello may");  // Displays "Hello micu \n Hello may"
    return 0;
}

Program Analysis

  • <span>#include <stdio.h></span>: Includes the standard input-output library
  • <span>int main()</span>: Main function, the entry point of the program
  • <span>printf()</span>: Formatted output function
  • <span>\n</span>: Newline escape character
  • <span>//</span>: Single-line comment

(2) Variables and Assignment

① Basic Data Types

  • Integer (int)
int a = 1;  // Declare and initialize an integer variable
  • Floating Point (float)
float b = 3.14f;  // f indicates float type

② Variable Naming Rules

  1. 1. Can only contain letters, numbers, and underscores <span>_</span>
  2. 2. Must start with a letter or underscore, cannot start with a number
  3. 3. Cannot use C language keywords (such as <span>int</span>, <span>float</span>, <span>if</span>, etc.)
  4. 4. Case-sensitive (<span>age</span> and <span>Age</span> are different variables)
  5. 5. Can declare multiple variables at once, separated by commas

③ Example

#include <stdio.h>

int a = 1;      // Global variable
float b = 3.14f;

int main()
{
    a = 2;       // Modify variable value
    b = 5.6f;    // Modify variable value
    return 0;
}

(3) Operators

① Basic Arithmetic Operations

  • <span>+</span>: Addition
  • <span>-</span>: Subtraction
  • <span>*</span>: Multiplication
  • <span>/</span>: Division (integer division results in an integer)
  • <span>%</span>: Modulus (only for integers)

② Example

#include <stdio.h>

int main()
{
    int a = 7, b = 3;
    int c1, c2, c3, c4, c5;

    c1 = a + b;  // 10
    c2 = a - b;  // 4
    c3 = a * b;  // 21
    c4 = a / b;  // 2
    c5 = a % b;  // 1

    printf("a + b = %d\n", c1);
    printf("a - b = %d\n", c2);
    printf("a * b = %d\n", c3);
    printf("a / b = %d\n", c4);
    printf("a %% b = %d\n", c5);  // %% outputs %

    return 0;
}

(4) Input and Output

① printf — Formatted Output

Syntax:

printf("format string", argument1, argument2, ...);

② Common Format Specifiers:

  • <span>%d</span>: Integer
  • <span>%f</span>: Floating point
  • <span>%c</span>: Character
  • <span>%s</span>: String

③ Example:

#include <stdio.h>

int main()
{
    int a = 3;
    float b = 3.14f;

    printf("a=%d\n", a);           // Output: a=3
    printf("b=%f\n", b);           // Output: b=3.140000

    // Control width and precision
    printf("a=%5d\n", a);          // Width of 5
    printf("b=%.2f\n", b);         // Keep 2 decimal places

    return 0;
}

(5) scanf — Formatted Input

① Syntax:

scanf("format string", &variable1, &variable2, ...);

**⚠ Note:** The address-of operator <span>&</span> must be added before the variable.

② Example:

#include <stdio.h>

int main()
{
    int a;
    float b;

    printf("Please enter an integer and a floating point number (separated by space):");
    scanf("%d %f", &a, &b);

    printf("The integer you entered is: %d\n", a);
    printf("The floating point number you entered is: %.2f\n", b);

    return 0;
}

(6) Practical Project: Calculate the Volume of a Rectangular Prism

① Version One: Fixed Size

#include <stdio.h>

int main()
{
    int length = 3, width = 2, height = 1;
    int volume = length * width * height;
  
    printf("The volume of the rectangular prism is: %d\n", volume);
    return 0;
}

② Version Two: User Input

#include <stdio.h>

int main()
{
    int length, width, height, volume;

    printf("Length:");
    scanf("%d", &length);

    printf("Width:");
    scanf("%d", &width);

    printf("Height:");
    scanf("%d", &height);

    volume = length * width * height;
    printf("The volume of the rectangular prism is: %d\n", volume);

    return 0;
}

(7) Comments

① Purpose

  • • Explain the functionality of the code
  • • Facilitate understanding for others
  • • Ease future maintenance

② Two Ways

// Single-line comment

/*
 * Multi-line comment
 * Can span multiple lines
 */

(8) Preprocessor Directive: #define Macro Definition

① Purpose

  • • Define constants or macros, performing text replacement before compilation

② Syntax

#define identifier replacement text

③ Example

#define PI 3.14159f     // Pi
#define MAX_SIZE 100    // Maximum length of array
#define MSG "Hello"     // String constant

It is customary to use uppercase for macro names to enhance readability.

(9) Comprehensive Exercise: Calculate the Volumes of Two Spheres and Compare Sizes

#include <stdio.h>

// Define the constant for Pi
#define PI 3.14159f

int main()
{
    int radius1, radius2;
    float volume1, volume2;

    // Input radius
    printf("Please enter the radius of sphere 1:");
    scanf("%d", &radius1);

    printf("Please enter the radius of sphere 2:");
    scanf("%d", &radius2);

    // Volume formula V = (4/3) * π * r³
    volume1 = (4.0f / 3.0f) * PI * radius1 * radius1 * radius1;
    volume2 = (4.0f / 3.0f) * PI * radius2 * radius2 * radius2;

    printf("Volume of sphere 1: %.2f\n", volume1);
    printf("Volume of sphere 2: %.2f\n", volume2);

    if (volume1 > volume2)
        printf("Sphere 1 is larger than sphere 2\n");
    else if (volume1 < volume2)
        printf("Sphere 2 is larger than sphere 1\n");
    else
        printf("Both spheres have equal volume\n");

    return 0;
}

(10) Summary

  • C Program Structure: Header files, main function, statements
  • Variables: Declaration, initialization, naming rules
  • Operators: Addition, subtraction, multiplication, division, modulus
  • Input and Output: Usage of <span>printf</span> and <span>scanf</span>
  • Code Standards: Comments, indentation, naming
  • Macro Definitions: Use <span>#define</span> to define constants, improving code readability

Leave a Comment