C与C++中的常用符号与标点符号有:“+”、“-”、“*”、“/”、“%”、“&”、“\”、“|”、“~”、“^”、“&”、“|”、“!”、“>”、“<”、"="、“#”、“?”、“,”、“.”、“:”、单引号、双引号、大括号、中括号、小括号。这些符号与标点经过组合还可形成代表特定功能与操作的符号。
一 赋值运算符
赋值运算符为等号,示例:
int a;
a = 10;//将值10赋予变量a
二 四则运算与取模
四则运算符包含加、减、乘、除。分别用到“+”、“-”、“*”、“/”符号。示例:
int t1, t2, t3,t4;t1 = t1 + t2; t1 = t3 - t2;t1 = t2 * t3;t1 = t2 / t4;
取模即求两个整数向除的余数,使用的符号为“%”,注意:浮点数不可取模。示例:
#include using namespace std;int t1;int t2;int main(){t1 = 10;t2 = 3;cout << t1 % t2<< endl;cout << endl;}
运行结果如下:
修改程序,添加一个浮点变量,如下:
#include using namespace std;int t1;int t2;float t3 = 3.0;int main(){t1 = 10;t2 = 3;cout << t1 % t2<< endl;cout << endl;cout << t1 % t3 << endl;cout << endl;}
点击生成,结果如下:
说明浮点数不能取模。
四则运算符及取模符与等号组合: +=, -=,*=, /=, %=。其所表达的意义是:将右边表达式的值与左边变量的值进行运算,然后再将结果赋值给左边变量。 示例程序:
#include using namespace std;int t1 = 10;int t2 = 3;float t3 = 3.0;int main(){t1 *= t3;cout <<"t1 = " << t1 << endl;t1 -= 5;cout << "t1 = " << t1 << endl;t1 += 7;cout << "t1 = " << t1 << endl;t1 %= t2;cout << "t1 = " << t1 << endl;t1 += 11;t1 /= t2;cout << "t1 = " << t1 << endl;cout << endl;t3 *= t1 * t2 % 7;cout << "t3 = " << t3 << endl;return 0;}
运行结果如下:
连续两个除号,在C及C++中被用作单行行注释符,即如果程序的某行中如果含有//,该行中//后面的内容在程序编译时被忽略,不被编译。除号与乘号组合构成多行块注释符注释符:/* */。程序中包含在/* */中的内容在程序编译时被忽略,不被编译。注释示例:
typedef struct{STC32G_IOPORT port; //I/O portui8 pins;//I/O pinsSTC32G_IOMODE mode; // I/O modeBOOL pullUpEnable;//pull up enable/disableBOOL pullDownEnable; //Pull down enable/disableBOOL drvEn;//driving ability enhance enable/disableBOOL speedHi; // I/O speed high/normalBOOL dIEnable;// digtal signal input enable/disableBOOL sTEnable;//schmitt trigger enable/disable}STC32G_IOTypeDef;/****************************************Function: STC32G_IOInitDef(STC32G_IOTypeDef* pstruct);Return value: voidmstruct: address of struct to init to defaultdescription: init STC32G_IOTypeDefstruct to default exclude port and pinsExample:STC32G_IOTypeDef* pstruct;STC32G_IOInitDef(pstruct);****************************************/void STC32G_IOInitDef(STC32G_IOTypeDef* pstruct);
在字符串中除号(/)为转义符,在字符串格式化函数中取模运算符的意义为替代指示符。示例:
#include using namespace std;int t1 = 10;int t2 = 3;double t3 = 3.0;char temp[20] = "";int main(){t1 *= t3;sprintf_s(temp, "t1 = %d\n", t1);cout << temp;t1 -= 5;sprintf_s(temp, "t1 = %d\n", t1);cout << temp;t1 += 7;sprintf_s(temp, "t1 = %d\n", t1);cout << temp;t1 %= t2;sprintf_s(temp, "t1 = %d\n", t1);cout << temp;t1 += 11;t1 /= t2;sprintf_s(temp, "t1 = %d\n", t1);cout << temp;t3 *= t1 * t2 % 7;sprintf_s(temp, "t1 = %.2f\n", t3);cout << temp;sprintf_s(temp, "%s\n", "Hello world!");cout << temp;return 0;}
运行结果如下:
乘号(*)的另以重要作用为指针定义及访问指针指向地址中的数据。示例:
int a = 10;
int *pInt;
pInt = &a;
*pInt = 20;
加号(+)连用,组成++运算符。减号(-)连用,组成–运算符;++运算符为自加运算,即变量值加1后再赋值给原变量。–运算符为自减运算,即变量值减1后再赋值给原变量。++运算符与–运算符既可放在变量的左边,有可放在变量的右边,但二者表达的意义完全不一样。如果放在变量的左边,是在变量完成自加或自减运算后再参与表达式的运算。如果放在变量的右边,则是在变量参与表达式的运算完成后再进行自加或自减运算。实例:
#include using namespace std;int t1 = 10;int t2 = 3;double t3 = 3.0;char temp[20] = "";int main(){t1 = t2++ * 10;sprintf_s(temp, "t1 = %d\n", t1);cout << temp;sprintf_s(temp, "t2 = %d\n", t2);cout << temp;t1 = ++t2 * 10;sprintf_s(temp, "t1 = %d\n", t1);cout << temp;sprintf_s(temp, "t2 = %d\n", t2);cout << temp;t1 = t2-- * 10;sprintf_s(temp, "t1 = %d\n", t1);cout << temp;sprintf_s(temp, "t2 = %d\n", t2);cout << temp;t1 = --t2 * 10;sprintf_s(temp, "t1 = %d\n", t1);cout << temp;sprintf_s(temp, "t2 = %d\n", t2);cout << temp;t1 = t3++ * 10;sprintf_s(temp, "t1 = %d\n", t1);cout << temp;sprintf_s(temp, "t3 = %f\n", t3);cout << temp;t1 = ++t3 * 10;sprintf_s(temp, "t1 = %d\n", t1);cout << temp;sprintf_s(temp, "t3 = %f\n", t3);cout << temp;t1 = t3-- * 10;sprintf_s(temp, "t1 = %d\n", t1);cout << temp;sprintf_s(temp, "t3 = %f\n", t3);cout << temp;t1 = --t3 * 10;sprintf_s(temp, "t1 = %d\n", t1);cout << temp;sprintf_s(temp, "t3 = %f\n", t3);cout << temp;return 0;}
运行结果如下:
减号与大于符号组合用于结构体指针及类指针访问成员变量(或函数)。示例:
//*****************************************************************************************void STC32G_IOInitDef(STC32G_IOTypeDef* pstruct){pstruct -> mode = BI_IO;//bidirectional I/Opstruct -> pullUpEnable = 0; //pull up disablepstruct -> pullDownEnable = 0;//pull down disablepstruct -> drvEn = 0;//drive ability enhance disablepstruct -> speedHi = 0; //level cnvert speed lowpstruct -> dIEnable = 1;//digital signal input enablepstruct -> sTEnable = 1;//schmitt trigger enable}//End of STC32G_IOInitDef(STC32G_IOTypeDef* pstruct)
三 比较运算符
比较运算符包含等于:==、大于:>、大于等于:>=、小于:<、小于等于:<=。比较运算常用用于条件执行语句中。示例:
#include using namespace std;int t1 = 10;int t2 = 4;double t3 = 7;char temp[20] = "";int main(){if(t1 > t2 && t1 > t2)cout <<"t1 is maxiuam"<<endl;elsecout << "t1 isnot maxiuam" < t3 || t1 < t3)cout << "t1 is not equal t3" << endl;elsecout << "t1 equal t3" < t3 || t1 < t3)cout << "t1 is not equal t3" << endl;elsecout << "t1 equal t3" << endl;if (t1 <= t2 )cout << "t1 is not more than t2" << endl;elsecout << "t1 ismore than t2" <= t2)cout << "t1 is not less than t2" << endl;elsecout << "t1 isless than t2" << endl;return 0;}
运行结果:
四 逻辑运算符
逻辑运算包含:与(&&)、或(||)、非(!)。 逻辑运算多用于条件语句中,比较运算符示例中已用到过逻辑与与逻辑或,这里不再演示。逻辑示例:
//***********************************************************************void Uart1_SendByte(BYTE mData){TI = 0;SBUF = mData;while(!TI);//TI = 0;}//End of Uart1_SendByte(BYTE mData)
五 位运算符
位运算符包含移位运算符与位逻辑运算符。
位运算符
包含按位与(&)、按位或(|)、按位取反(~)、按位异或(^)。
移位运符
算包含左移与右移运算符。这两个运算符与C++ iostrem库中的cin、cout后面的输入、输出符一致,两个小于(大于)符号连在一起。
左移运算符:<< 。左移一位,被移位数除以2
右移运算符:>>。右移一位,被移位数乘以2
位操作在嵌入式编程,如单片机编程中应用较多,用于对寄存器的位操作,如置位与复位。示例:
//***********************************************************************/BYTE SetBit(BYTE* pBYTE, BITPOS bPos){ *pBYTE |= 1 << bPos; return*pBYTE;}//End of SetBit(BYTE* pBYTE, BITPOS bPos)//***********************************************************************/BYTE ResetBit(BYTE* pBYTE, BITPOS bPos){*pBYTE &= ~(1 << bPos); return*pBYTE;}//End of ResetBit(BYTE* pBYTE, BITPOS bPos)//***********************************************************************/BYTE ReplaceBits(BYTE* pBYTE,BITPOS hBit,BITPOS lBit,BYTE mData){ui8 tem = hBit - lBit + 1; tem = (1 << tem) - 1; mData &= tem;//discard bits may be redundant *pBYTE &= ~(tem << lBit);//clear specified bits*pBYTE |= (mData << lBit);//set specified bitsreturn*pBYTE;}//End of ReplaceBits(BYTE* pBYTE,BITPOS hBit,BITPOS lBit,BYTE mData)
按位异或可实现bit位的1位或多位取反。示例:
#include #include #include using namespace std;char t1 = 0xFF;char t2 = 0xF0;unsigned char temp[20] = "";typedefunsigned char ui8;ui8* UcharBToString(ui8 src, ui8* desString){ui8 i;memset(desString, 0, 20);for (i = 0; i < 8; i++){if ((src << i) & 0x80)desString[i] = '1';elsedesString[i] = '0';}return desString;}int main(){t1 ^= 0xFF;UcharBToString(t1, temp);cout << temp << endl;t1 = 0xFF;t1 ^= 0x00;UcharBToString(t1, temp);cout << temp << endl;t1 = 0xFF;t1 ^= t2;UcharBToString(t1, temp);cout << temp << endl;t1 = 0xFF;t2 = 0x0F;t1 ^= t2;UcharBToString(t1, temp);cout << temp << endl;return 0;}
运行结果如下:
按位与运算符(&)又是取值运算符。示例 int *pInt = &a;
按位与运算符(&)还用于引用类型声明。示例 int &b = a;
六 括号
括号包括大括号、中括号、小括号。
大括号{}
大括号(花括号)中一般包含语句块,其中定义的变量有自己的作用域。大括号常用于条件语句块(如if语句,while语句,switch 条件转移、for 循环等)、函数体、公用体体、结构体体及宏函数定义。示例:
//***********************************************************************ui8 BStringToUchar(ui8 src[],ui8* destData){ui8 len = strlen(src);ui8 i;*destData = 0;for(i = 0; i < len ; i++){*destData <<= 1;if(src[i]- '0')*destData += 1;}return *destData;}//End of BStringToUchar(ui8 src[],ui8* destData)//***************************************typedef enum{BI_IO = 0, //bidirectional I/OPP_OUT,//push_pull outHI_IN, //high impendence inODR_OUT //open drain out}STC32G_IOMODE;//***************************************typedef struct{STC32G_IOPORT port; //I/O portui8 pins;//I/O pinsSTC32G_IOMODE mode; // I/O modeBOOL pullUpEnable;//pull up enable/disableBOOL pullDownEnable; //Pull down enable/disableBOOL drvEn;//drive ability enhance enable/disableBOOL speedHi; // I/O speed high/normalBOOL dIEnable;// digtal signal input enable/disableBOOL sTEnable;//schmitt trigger enable/disable}STC32G_IOTypeDef;
我们知道在C语言中,变量必须先定义后使用,否则就会编译出错,特别是在Keil C51中更是如此。示例:下面程序编译不会有问题
/*main.cDesigned by Bill LiuVersion 0.0 Modified last by Bill Liu on 11/26/2022*/#include "main.h"//#include "config.h"//#include "STC32G_Timer.h"#include "STC32G_Delay.h"//#include "STC32G_comparator.h"ui8 t1 = 250;ui16 t2 = 65533;i32 t3 = -165533;f32 t4 = 51.25;f64 t5 = -3.14;ui8 t6 = 0;ui16 t7 = 0;i32 t8 = 0;f32 t9 = 0;f64 t10 = 0;ui8 str[20] = {0};ui8 str1[20] = "Hello World";void main(){SysInit();Uart1_Init(VBAUD_8BITS,G1, 0, 9600);EEPROM_SectorErase(0x00); EEPROM_Write_nChar(0x00,&t1,1);EEPROM_Write_Integer(0x01,t2);EEPROM_Write_Long(0x03,t3);EEPROM_Write_Float(0x07,t4);EEPROM_Write_Double(0x0B,t5);EEPROM_Write_String(0x13,str1);while(1){EEPROM_Read_nChar(0x00,&t6,1);LongToString(t6,str);Uart1_SendString(str);Uart1_SendString("\r\n");EEPROM_Read_Integer(0x01,&t7);LongToString(t7,str);Uart1_SendString(str);Uart1_SendString("\r\n");EEPROM_Read_Long(0x03,&t8);LongToString(t8,str);Uart1_SendString(str);Uart1_SendString("\r\n");EEPROM_Read_Float(0x07,&t9);FloatString(t9,str,2);Uart1_SendString(str);Uart1_SendString("\r\n");EEPROM_Read_Double(0x0B,&t10);FloatString(t10,str,2);Uart1_SendString(str);Uart1_SendString("\r\n");EEPROM_Read_String(0x13,str);Uart1_SendString(str);Uart1_SendString("\r\n");Uart1_SendString("\r\n");Delayxms(1000);}}//End of main()
编译结果如下:
下面在程序中临时定义一个变量(ui16 t11 = t1 * t2;),修改后的程序如下:
/*main.cDesigned by Bill LiuVersion 0.0 Modified last by Bill Liu on 11/26/2022*/#include "main.h"//#include "config.h"//#include "STC32G_Timer.h"#include "STC32G_Delay.h"//#include "STC32G_comparator.h"ui8 t1 = 250;ui16 t2 = 65533;i32 t3 = -165533;f32 t4 = 51.25;f64 t5 = -3.14;ui8 t6 = 0;ui16 t7 = 0;i32 t8 = 0;f32 t9 = 0;f64 t10 = 0;ui8 str[20] = {0};ui8 str1[20] = "Hello World";void main(){SysInit();Uart1_Init(VBAUD_8BITS,G1, 0, 9600);EEPROM_SectorErase(0x00); EEPROM_Write_nChar(0x00,&t1,1);EEPROM_Write_Integer(0x01,t2);EEPROM_Write_Long(0x03,t3);EEPROM_Write_Float(0x07,t4);EEPROM_Write_Double(0x0B,t5);EEPROM_Write_String(0x13,str1);while(1){EEPROM_Read_nChar(0x00,&t6,1);LongToString(t6,str);Uart1_SendString(str);Uart1_SendString("\r\n");EEPROM_Read_Integer(0x01,&t7);LongToString(t7,str);Uart1_SendString(str);Uart1_SendString("\r\n");EEPROM_Read_Long(0x03,&t8);LongToString(t8,str);Uart1_SendString(str);Uart1_SendString("\r\n");EEPROM_Read_Float(0x07,&t9);FloatString(t9,str,2);Uart1_SendString(str);Uart1_SendString("\r\n");EEPROM_Read_Double(0x0B,&t10);FloatString(t10,str,2);Uart1_SendString(str);Uart1_SendString("\r\n");EEPROM_Read_String(0x13,str);Uart1_SendString(str);Uart1_SendString("\r\n");Uart1_SendString("\r\n");ui16 t11 = t1 * t2;Delayxms(1000);}}//End of main()
编译结果如下:
编译出错。
下面将新加代码加上一个大括号,代码如下:
/*main.cDesigned by Bill LiuVersion 0.0 Modified last by Bill Liu on 11/26/2022*/#include "main.h"//#include "config.h"//#include "STC32G_Timer.h"#include "STC32G_Delay.h"//#include "STC32G_comparator.h"ui8 t1 = 250;ui16 t2 = 65533;i32 t3 = -165533;f32 t4 = 51.25;f64 t5 = -3.14;ui8 t6 = 0;ui16 t7 = 0;i32 t8 = 0;f32 t9 = 0;f64 t10 = 0;ui8 str[20] = {0};ui8 str1[20] = "Hello World";void main(){SysInit();Uart1_Init(VBAUD_8BITS,G1, 0, 9600);EEPROM_SectorErase(0x00); EEPROM_Write_nChar(0x00,&t1,1);EEPROM_Write_Integer(0x01,t2);EEPROM_Write_Long(0x03,t3);EEPROM_Write_Float(0x07,t4);EEPROM_Write_Double(0x0B,t5);EEPROM_Write_String(0x13,str1);while(1){EEPROM_Read_nChar(0x00,&t6,1);LongToString(t6,str);Uart1_SendString(str);Uart1_SendString("\r\n");EEPROM_Read_Integer(0x01,&t7);LongToString(t7,str);Uart1_SendString(str);Uart1_SendString("\r\n");EEPROM_Read_Long(0x03,&t8);LongToString(t8,str);Uart1_SendString(str);Uart1_SendString("\r\n");EEPROM_Read_Float(0x07,&t9);FloatString(t9,str,2);Uart1_SendString(str);Uart1_SendString("\r\n");EEPROM_Read_Double(0x0B,&t10);FloatString(t10,str,2);Uart1_SendString(str);Uart1_SendString("\r\n");EEPROM_Read_String(0x13,str);Uart1_SendString(str);Uart1_SendString("\r\n");Uart1_SendString("\r\n");{ui16 t11 = t1 * t2;}Delayxms(1000);}}//End of main()
编译结果如下:
编译通过。
中括号[]
主要用于数组声明及数组元素的访问。 示例:
ui8 str[] = “”; str[0] = ‘a’;
小括号()
小括号的用途有几下方面,函数及函数声明,宏函数及宏函数定义,表达式中用于改变运算的优先级,for循环,while及do while循环,switch 条件转移语句等。示例:
函数及函数声明:
//********************************************************
void SysInit(); //init System speed fastest
函数源文件:
//********************************************************
void SysInit() //init System speed fastest
{
EAXFR = 1;//visit XFR enable
CKCON = 0x00; //set outer data bus speed fastest
WTST = 0x00; //CPU wait 0 to run program
}
//End of SysInit()
函数调用:上面示例已有调用函数实例,不再举例。
改变表达式中的优先级: t1 = (t2 +10)*t3;
#号的用法
#用于包含头文件及宏定义,放在行的开头,以#打头的行末尾不可有分号。示例:
#include
#include
#include
# define PI 3.14f
标点符号
C与C++语言中常用的标点符号包含,逗号(,)、分号(;)、冒号(:)、问号(?)、感叹号(!)、单引号(‘)、双引号(“)、小数点(.)。
逗号(,)
逗号在C与C++语言中被用作分隔符,起分隔表达式、参数、元素等作用。
用在变量声明中示例: int a,b,c; float: x = 0.10, y = 0.3;
用在函数列表中做参数分隔示例:voidswap(int x, int y);
用在数组声明中做元素分隔示例:int a[10] ={0,1,2,3,4,5,6,7,8,9};
数值梯次传递 示例:
#include #include #include using namespace std;int t1 = 0xFF;int t2 = 0xF0;int t3 = 0;int t4 =20;unsigned char temp[20] = "";char a1[10] = {0,1,2,3,4,5,6,7,8,9};int main(){t1 = (t2,t3,t4);cout << t1 << endl;return 0;}
运行结果如下:
从上面的结果可以看出赋予t1的是t4的值。
分号(;)
分号(;)在C与C++语言中被用作结束符。如果一行代码只有分号,我们称之为空语句。我们需将它与空操作区分开。空语句不会被编译,或者说编译后不会增加大编译后的可执行文件的大小。空操作_nop_()则会让CPU在一个时钟周期内什么都不做,常被用来延时。
枚举、结构体、类的花括号外结尾处必须有分号。do while循环的while后面的小括号后需有分号。
for循环的for后面小括号里的前两个操作结束必须是分号,最后一个操作不能有分号。
函数体的右花括号后不需要分号。
冒号(:)
在C与C++语言中的程序中冒号被放在编号的后面,组成编号。常见于go to语句与switch 语句。
另外在C++中被用于继承类声明及子类访问父类对象及函数等。还与问号一起用于三目运算中,示例: t1 = a > b? a: b;
感叹号(!)
在C与C++语言中的程序中感叹号为非运算符。
单引号(‘)
在C与C++语言中的程序中,单引号须成对使用,用以表示字符,单引号对内有且只能有一个字符。
双引号(“)
在C与C++语言中的程序中,双引号须成对使用,用以表示字符串。双引号对中可以有多个字符,也可为空。
小数点(.)
在C与C++语言中的程序中, 小数点(.)除用于浮点数外,还用作运算符,用于结构体或类对象访问成员变量(或函数)。示例:
//***************************************************************************************void STC32G_IOInit(STC32G_IOTypeDef mstruct){switch(mstruct.port){case SCT_P0:STC32G_P0PinsInit(mstruct.pins, mstruct.mode, mstruct.pullUpEnable, mstruct.pullDownEnable,mstruct.drvEn,mstruct.speedHi,mstruct.dIEnable, mstruct.sTEnable);break;case SCT_P1:STC32G_P1PinsInit(mstruct.pins, mstruct.mode, mstruct.pullUpEnable, mstruct.pullDownEnable,mstruct.drvEn,mstruct.speedHi,mstruct.dIEnable, mstruct.sTEnable);break;case SCT_P2:STC32G_P2PinsInit(mstruct.pins, mstruct.mode, mstruct.pullUpEnable, mstruct.pullDownEnable,mstruct.drvEn,mstruct.speedHi,mstruct.dIEnable, mstruct.sTEnable);break;case SCT_P3:STC32G_P3PinsInit(mstruct.pins, mstruct.mode, mstruct.pullUpEnable, mstruct.pullDownEnable,mstruct.drvEn,mstruct.speedHi,mstruct.dIEnable, mstruct.sTEnable);break;case SCT_P4:STC32G_P4PinsInit(mstruct.pins, mstruct.mode, mstruct.pullUpEnable, mstruct.pullDownEnable,mstruct.drvEn,mstruct.speedHi,mstruct.dIEnable, mstruct.sTEnable);break;case SCT_P5:STC32G_P5PinsInit(mstruct.pins, mstruct.mode, mstruct.pullUpEnable, mstruct.pullDownEnable,mstruct.drvEn,mstruct.speedHi,mstruct.dIEnable, mstruct.sTEnable);break;case SCT_P6:STC32G_P6PinsInit(mstruct.pins, mstruct.mode, mstruct.pullUpEnable, mstruct.pullDownEnable,mstruct.drvEn,mstruct.speedHi,mstruct.dIEnable, mstruct.sTEnable);break;case SCT_P7:STC32G_P7PinsInit(mstruct.pins, mstruct.mode, mstruct.pullUpEnable, mstruct.pullDownEnable,mstruct.drvEn,mstruct.speedHi,mstruct.dIEnable, mstruct.sTEnable);break;}}//End of STC32G_IOInit(STC32G_IOTypeDef mstruct)