Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

C 变量 - 实例


现实示例

在我们的示例中,我们通常会简化变量名以匹配其数据类型(对于 int 类型使用 myInt 或 myNum,对于 char 类型使用 myChar,等等)。这样做是为了避免混淆。

但是,为了提供一个使用变量的实际示例,我们创建了一个程序来存储有关大学生的不同数据。

示例

// 学生数据
int studentID = 15;
int studentAge = 23;
float studentFee = 75.25;
char studentGrade = 'B';

// 打印变量
printf("学生 ID: %d\n", studentID);
printf("学生年龄: %d\n", studentAge);
printf("学生费用: %f\n", studentFee);
printf("学生成绩: %c", studentGrade);
自己试试 »

计算矩形的面积

在这个现实示例中,我们创建了一个程序来计算矩形的面积(通过将长度和宽度相乘)。

示例

// 创建整数变量
int length = 4;
int width = 6;
int area;

// 计算矩形的面积
area = length * width;

// 打印变量
printf("长度是: %d\n", length);
printf("宽度是: %d\n", width);
printf("矩形的面积是: %d", area);
自己试试 »


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.