且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C

更新时间:2022-09-13 17:21:34

precision精确 int width();int width(int w);返回当前宽度,设置宽度大小,宽度是指每一次输出中显示字符的最小数目;
向函数传递指针的缺陷在于函数现在可以对调用程序的结构变量进行修改,可以在函数中使用const关键字防止修改
void print_receipt(register Transaction const *trans);

Transaction compute_amount(Transaction trans) //返回值类型是结构体类型
{
trans.total_amount = trans.quantity * trans.unit_price;
return trans;
}
结构的一份拷贝作为参数传递给函数并被修改,然后一份修改后的结构拷贝从函数返回,所以这个结构被复制两次;
只修改返回值,而不是修改整个结构
float compute_total_amount(Transaction trans)
{
return trans.quantity * trans.unit_price;
}
current_trans.total_amount = compute_total_amount(current_trans);
void compute_total_amount(register Transaction *trans)
{
trans->total_amount = trans->quantity * trans->unit_price;
}
bit field;
位段的 declared defined和结构类似,成员是一个或多个位的字段,一个或多个字节字段
不同长度的字段实际存储在一个或多个整型变量中;
位段的声明和普通结构成员声明相同,位段成员必须声明为int,signed int OR unsigned int,成员名后面: 整数,整数指定该位段占用的位的数目;
int ruiy:5;
signed int ruiy:5;
位段中成员在内存中从左到右分配,还是? 未定!

bit filed
struct CHAR
{
unsigned ch    : 7;
unsigned font    : 6;
unsigned    : 19;
}; //声明位段数据类型
struct CHAR ch1;//使用上面定义的位段数据类型定义位段变量
访问整型值的部分内容
允许程序对寄存器不同位段进行访问
struct DISK_REGISTER_FORMAT {
unsigned    command    : 5;
unsigned    sector    : 5;
unsigned    track    : 9;
unsigned    error_code : 8;
unsigned    head_load : 1;
unsigned    write_protect : 1;
unsigned    disk_spinning : 1;
unsigned    error_occurred : 1;
unsigned    ready : 1;
};