Home > Backend Development > C++ > body text

C Programming Unveiled: A Gentle Introduction to its Potential

WBOY
Release: 2024-10-10 15:12:31
Original
647 people have browsed it

The C programming language is general-purpose, structured and process-oriented, and is widely used in various fields. Its basic syntax includes functions, variables (such as int, float, char), conditional statements (if, else), and loops (for, while, do-while). A practical example shows how to write a C program to calculate the average score.

C Programming Unveiled: A Gentle Introduction to its Potential

C Programming Revealed: Tap into Its Unlimited Potential

Introduction

C Programming The language is a general-purpose, structured and procedure-oriented programming language known for its power and efficiency. It is widely used in various fields, from operating system development to embedded system programming. This article will provide a beginner's guide to C language and take you to the door of its potential.

Basic syntax

C programs are composed of functions and variables. Every program starts with a main() function and is executed there. Variables are used to store data, and their types include int (integer), float (floating point number), and char (character).

// 一个简单的C程序
#include <stdio.h>

int main() {
    int age = 25; // 整型变量,存储年龄
    float salary = 10000.50; // 浮点变量,存储薪水
    char name[] = "John"; // 字符数组,存储名字

    printf("年龄:%d\n", age); // 打印年龄
    printf("薪水:%.2f\n", salary); // 打印薪水,保留两位小数
    printf("名字:%s\n", name); // 打印名字

    return 0;
}
Copy after login

Conditional Statements

C language uses if, else and else if statements to control program flow. These statements check whether a condition is true and execute different blocks of code depending on the result.

// 检查年龄并打印响应消息
if (age >= 18) {
    printf("您已成年。\n");
} else {
    printf("您未成年。\n");
}
Copy after login

Loops

C language provides for, while and do-while loops to repeatedly execute blocks of code.

// 使用for循环打印1到10的数字
for (int i = 1; i <= 10; i++) {
    printf("%d ", i);
}
Copy after login

Practical case: Calculate the average score

Write a C program to get 5 scores from the user and calculate the average score.

#include <stdio.h>

int main() {
    int scores[5];
    float sum = 0;

    // 获取5个分数
    for (int i = 0; i < 5; i++) {
        printf("输入第%d个分数:", i+1);
        scanf("%d", &scores[i]);
        sum += scores[i];
    }

    // 计算平均分
    float average = sum / 5;

    // 打印平均分
    printf("平均分:%.2f\n", average);

    return 0;
}
Copy after login

Conclusion

This article provides a brief overview of the C language, covering basic syntax, conditional statements and loops. Through practical cases, you can experience the powerful functions of C language and inspire your programming journey.

The above is the detailed content of C Programming Unveiled: A Gentle Introduction to its Potential. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!