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 myNum = 15;  // myNum 为 15
myNum = 10;  // 现在 myNum 为 10
自己试一试 »

您还可以将一个变量的值赋给另一个变量。

示例

int myNum = 15;

int myOtherNum = 23;

// 将 myOtherNum (23) 的值赋给 myNum
myNum = myOtherNum;

// myNum 现在为 23,而不是 15
printf("%d", myNum);
自己试一试 »

或者将值复制到空变量中

示例

// 创建一个变量并将其值赋为 15
int myNum = 15;

// 声明一个变量但不为其赋值
int myOtherNum;

// 将 myNum 的值赋给 myOtherNum
myOtherNum = myNum;

// myOtherNum 现在值为 15
printf("%d", myOtherNum);
自己试一试 »

将变量加在一起

要将一个变量加到另一个变量,可以使用+运算符。

示例

int x = 5;
int y = 6;
int sum = x + y;
printf("%d", sum);
自己试一试 »

C 练习

通过练习测试自己

练习

使用两个变量:xy,显示 5 + 10 的和。

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

开始练习



×

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.