共用体:
# include <stdio.h>
union data
{
int i;
char ch;
float f;
};
void main()
{
union data data1={65};
printf("%d\n",data1.i);
printf("%c\n",data1.ch);
printf("%f\n",data1.f);
}
练习:
#include <stdio.h>
struct person
{
int id;
char name[10];
char gender;
char job;
union
{
int sclass;
char position[10];
} category;
} person[2];
void main()
{
int i;
for(i=0; i<2; i++)
{
scanf("%d %s %c %c", &person[i].id, &person[i].name,&person[i].gender, &person[i].job);
if(person[i].job == 'S')
scanf("%d", &person[i].category.sclass);
else if(person[i].job == 'T')
scanf("%s", person[i].category.position);
else
printf("Input error!");
}
printf("\n");
printf("No. name sex job class/position\n");
for(i=0; i<2; i++)
{
if (person[i].job == 'S')
printf("%-6d%-10s%-3c%-3c%-6d\n",person[i].id, person[i].name, person[i].gender, person[i].job,person[i].category.sclass);
else
printf("%-6d%-10s%-3c%-3c%-6s\n",person[i].id,
person[i].name,person[i].gender, person[i].job,
person[i].category.position);
}
}
关于补码
void main()
{
char i=127;
i++;
printf("%d\n",i);
i++;
printf("%d\n",i);
}
//01111111
//10000000
//10000000
//10000001
//11111111
位运算:
void main()
{
printf("%d",1|2);
}
void main()
{
char ch=1;
printf("%d",ch^-1);
}
//00000001
//11111111
//11111110
//10000001
//10000010
void main()
{
char ch=1;
printf("%d",ch<<2);
}
枚举
方便程序员写代码是以一种更为直观的写法表达概念值,本质上是整数
#include "math.h"
int main()
{
enum Gender{male,female};
struct Date
{
int year;
int month;
int day;
};
struct Student
{
int id;
char name[100];
enum Gender gender;
int age;
struct Date birthday;
char addr[100];
};
struct Student s1= {1,"张三",male,18,{1997,8,27},"南京仙林大学城"};
struct Student s2= {2,"李四",female,19,{1996,1,10},"南京仙林大学城"};
printf("第一个学生ID是%d,姓名是%s,性别是%d,年龄是%d,出生日期是%d年%d月%d日,住址是%s",
s1.id,s1.name,s1.gender,s1.age,s1.birthday.year,s1.birthday.month,s1.birthday.day,s1.addr);
return 0;
}
#include <stdio.h>
void main( )
{
enum color {red,yellow,blue,white,black};
enum color i,j,k,pri;
int n,loop;
n = 0;
for(i=red; i<=black; i++)
for(j=red; j<=black; j++)
if(i != j)
{
for(k=red; k<=black; k++)
if((i != j) && (k != j))
{
n=n+1;
printf("%-4d",n);
for(loop=1; loop<=3; loop++)
{
switch(loop)
{
case 1:
pri=i;
break;
case 2:
pri=j;
break;
case 3:
pri=k;
break;
default:
break;
}
switch(pri)
{
case red:
printf("%-10s","red");
break;
case yellow:
printf("%-10s","yellow");
break;
case blue:
printf("%-10s","blue");
break;
case white:
printf("%-10s","white");
break;
case black:
printf("%-10s","black");
break;
default:
break;
}
}
printf("\n");
}
}
printf("\ntotal:%5d\n",n);
}
typedef定义类型步骤
① 按定义变量方法写出定义体 如int i;
② 将变量名换成新类型名 如int INTEGER;
③ 最前面加typedef 如typedef int INTEGER;
④ 用新类型名定义变量 如INTEGER i,j;
typedef struct club
{
char name[20];
int size;
int year;
} GROUP;
typedef GROUP * PG;
PG pclub;
[此贴子已经被作者于2015-12-15 12:45:27编辑过]