C_review_2

C语言回顾(二)

1.从一个简单程序初探C

#include <stdio.h> //头文件:包含诸多变量与函数的声明,这些是编译时需要的信息

int main() //main函数:程序的开端

{

int num=0; //变量定义及初始化

//变量即容器,存储信息的地方,其内容可以被改变,变量的名称需要有意义,这样容易令人理解

scanf("%d",&num); //向用户请求输入

printf(“num is %d\n”); //向终端输出

//scanf,printf均为C标准库中定义的函数,函数是一块功能单一的代码,main函数通过调用它们实现某些功能

return 0; //函数返回某个值,也有不返回值的函数

}

头文件,变量,函数…,其中函数是C代码的主要成员,模块化的设计从函数的使用中体现。

2.从变量到计算

  • 变量的定义及初始化:<类型名称> <变量名称> = <初始值>

当我们在代码声明了一个变量,计算机就在内存中选择一块空间,变量名称起到地址的作用(但不是地址),

通过变量名我们可以访问这个变量,获取其内容。当然,未初始化的变量,其内容是随机的,因此最好在变

量定义后便初始化为某个值。

(1)标识符

​ 变量名,函数名等等均属于此范畴,可由数字,字母,下划线组成,数字不能打头,区分大小写。

(2)数据类型(此处只讨论常用的数据类型)

​ 整数类型:int,输入输出格式:%d

​ 浮点数类型:float,double(浮点数运算并不精确)

​ 输入输出格式:%lf,%f

​ 字符类型:char,输入输出格式:%c (字符类型是特殊的整数类型)

​ 字符串类型:一般以字符数组或字符指针形式出现,输入输出格式:%s

​ 类型匹配问题:强制类型转换(但有越界风险)

(3)运算符

​ 单目运算符:±,++,–(++a和a++有什么异同?)

​ 双目运算符:+,-,*,/,%(取余)

​ 三目运算符:a?:b:c(相当于一个两分支条件语句)

​ 逻辑运算符:&&,||,!(注意短路问题)

​ 关系运算符:==,>=,<=,!=

​ 复合赋值:*=,/=,+=,-=(先运算再赋值)

(4)C中默认的运算规则

​ 整数与浮点数一起运算:先将整数转化为浮点数,再运算,最后结果是浮点数

​ 单精度浮点与双精度浮点一起运算:先转化为双精度浮点再运算

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This error message is typically encountered in programming languages like C, C++, or Java when there is a syntax error in the code, usually involving a missing or misplaced operator or keyword. Without seeing the specific line of code that triggered this error message, it is difficult to provide a more specific explanation. However, the error message suggests that there may be an issue with the syntax of the code related to the variable or identifier "plant_2". Here are a few examples of code snippets that could trigger this error message: Example 1: ``` if (plant_1 > 10) { // some code here } plant_2 = 5; // missing semicolon at end of previous line ``` In this example, the error message would be triggered because there is no semicolon at the end of the line that contains the if statement. The compiler thinks that the if statement is continuing onto the next line and sees "plant_2" as the start of a new statement, hence the "expected expression before 'plant_2'" error. Example 2: ``` int plant_1 = 5; int plant_2 = 10; if (plant_1 > 10) // missing parentheses { // some code here } else if plant_2 < 5 // missing parentheses and curly braces // some code here ``` In this example, there are a few syntax errors that could trigger the "expected expression before 'plant_2'" error. Firstly, the if statement is missing parentheses around the condition. Secondly, the else if statement is missing both parentheses around the condition and curly braces around the code block. These errors can cause the compiler to get confused and trigger the error message. To fix this error, you will need to carefully review your code and look for any syntax errors or missing pieces of code. Make sure that all statements are properly terminated with semicolons, all conditional statements have the correct syntax and formatting, and all variables and identifiers are properly declared and initialized.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值