编程笔记
-
基于tm结构输出日期和月份
#include <stdio.h> #include <time.h> int main(void) { const char *day[7] = { "Sunday" , "Monday", "Tuesday", "Wednesday", "Thursday", &qu…
-
获取日期
可以使用mktime()函数来确定给定日期的星期几。 该函数有原型: time_t mktime(struct tm *ptime); 参考以下代码: #include <stdio.h> #include <time.h> int main(void){ const char *day[7] = { &qu…
-
时间函数示例
#include <stdio.h> #include <time.h> int main( void ) { time_t start, finish, now; struct tm *ptr; char *c, buf1[80]; double duration; /* Record the time the program starts execution. */ …
-
使用typedef创建新的类型名称
可以使用typedef关键字后跟类型和别名来为类型创建别名。 typedef unsigned char BYTE; 别名可以用作其指定类型的同义词。 BYTE b; /* unsigned char */ 以下代码在结构上使用typedef。 #include <stdio.h> t…
-
联合体不同时在同一存储空间中存储不同的数据类型
以下是带有标记的联合体模板的示例: union myU { int digit; double bigfl; char letter; }; 以下是定义myU类型的三个union变量的示例: union myU fit; // union variable of myU type union myU save[10]; // arra…
-
一次使用多个联合体成员
#include <stdio.h> int main( void ){ union flag { char c; int i; long l; float f; double d; } shared; shared.c = '$'; printf("/nchar c = %c", shared.c); printf("/nint i =…
-
联合体的典型用法
#include <stdio.h> #define CHARACTER 'C' #define INTEGER 'I' #define FLOAT 'F' struct currency{ char type; union flag { char c; int i; float f; } shared; }; void print_f…
-
联合体是什么?
union类型与struct类型类似,只是所有字段共享相同的内存位置。union的大小是它包含的最大字段的大小。下面的代码中创建一个联合体类型,大小为4个字节。 union mix { char c; /* 1 byte */ short s; /* 2 bytes */…
-
存储类
存储类包括以下内容: auto register extern static 它们决定了变量范围和寿命。 1. auto 局部变量的默认存储类是auto。可以使用auto关键字明确指定。输入代码块时会分配自动变量的内存,退出时会释放。auto变量的范…
-
使用union在成员之间共享内存
结构体为每个成员保留单独的内存段,union为其最大成员保留单个内存空间。使用关键字union创建联合体。以下代码为电话簿创建联合体定义。 union phoneBook { char *name; char *number; char *address; }; 通过点(.…