福州网站定制公司有免费注册网站吗
news/
2025/9/22 16:50:14/
文章来源:
福州网站定制公司,有免费注册网站吗,太原有网站工程公司吗,廊坊做网站优化的公司1.4 C对C语言数据类型的扩展1.4.1 结构体1.4.2 联合1.4.3 枚举1.4.4 布尔1.4.5 字符串 1.4 C对C语言数据类型的扩展
基本的数据类型 char、unsigned char、int、short、unsigned shor、long、unsigned long、float、double、long double与C语言相同。扩展了bool类型#xff0… 1.4 C对C语言数据类型的扩展1.4.1 结构体1.4.2 联合1.4.3 枚举1.4.4 布尔1.4.5 字符串 1.4 C对C语言数据类型的扩展
基本的数据类型 char、unsigned char、int、short、unsigned shor、long、unsigned long、float、double、long double与C语言相同。扩展了bool类型对结构体、联合、枚举做了改进。
1.4.1 结构体
C中定义结构型变量可以省略struct关键字C结构体中可以直接定义函数谓之成员函数方法
#include iostream
#include cstringusing namespace std;int main (void) {struct std {int age;char name[20];void who() {cout 我是 name 今年 age endl;}};std s1; //不需要 struct std s1; 这样写可以省略structs1.age 20;strcpy(s1.name, 王五);s1.who();return 0;
}1.4.2 联合 C中定义联合体变量可以省略union关键字 union XX{……};
XX x; //定义联合体变量直接省略union支持匿名联合 union {//没有名字……
};#include iostreamusing namespace std;int main (void) {union { //匿名联合int num;char c[4];};num 0x12345678;cout hex (int)c[0] (int)c[1] endl;return 0;
}1.4.3 枚举 C中定义枚举变量可以省略enum关键字 C中枚举是独立的数据类型不能当做整型数使用 #include iostream
using namespace std;int main (void) {enum COLOR{RED, GREEN, BLUE};COLOR c GREEN;
// c 2; //errorcout c endl;return 0;
}1.4.4 布尔
C中布尔bool是基本数据类型专门表示逻辑值
布尔类型的字面值常量
true 表示逻辑真
false 表示逻辑假
布尔类型的本质单字节的整数使用1表示真0表示假
任何基本类型都可以被隐式转换为布尔类型
#include iostream
using namespace std;int main (void) {bool a true;cout a endl; //输出1cout boolalpha a endl; //输出truea 5 3;cout boolalpha a endl; //隐式转换bool型 输出truereturn 0;
}1.4.5 字符串 C兼容C中的字符窜表示方法和操作函数 C专门设计了string类型表示字符串 string类型字符串定义 string s; //定义空字符串
string s(hello);
string s hello;
string s string(hello);字符串拷贝 string s1 hello;
string s2 s1;字符串连接 string s1 hello, s2 world;
string s3 s1 s2; // s3: hello world
s1 s2; //s1: hello world字符串比较 string s1 hello, s2 world;
if(s1 s2){ cout false endl; } //类似C语言中strcmp(……)
if(s1 ! s2){ cout true endl; }随机访问 string s hello;
s[0] H; //Hello获取字符串长度 size_t size();//都不统计\0
size_t length();//都不统计\0转换为C风格的字符串 const char* c_str();string s2(hello);const char *p s2.c_str();printf(%s\n, p);字符串交换 void swap(string s1, string s2)
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/909697.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!