/********************************
** wzsts《C语言深度剖析》2016 **
** **
** fun1~fun6代表6章节内容 **
**fun10~fun19代表fun1所调用函数 **
** **
** 世界因规则而美好 **
** #if(1)可运行,#if(0)不运行。 **
** Data Author PC **
**16.1.1 wz VM.CentOS6.5 **
*********************************/
//第一章 以前打过,然后换系统丢失文件。想想,有些东西,失去了,可以换更好的!*/
#include
#include
#define N 10 /* 定义分配空间,而extern和define声明,宏替换没有分配空间 */
#define EPSINON 0.0001
fun0() /* 函数没有类型,而返回值有类型 */
{
int fx=10;
return fx;
}
void static fun()/* 即便类型不同,也不能重名 */
{
/* int fun =10 函数名,变量名不要重复 */
}
void fun10()
{
int i=4;
int *p=NULL;
int a[10]={1,2,3,4,5};
char b[ ]="abcde";
char *c="abcde";
/* char *c[10]="abcde"; error*/
char d[ ]={"abcde"};
char e[]={'w','z','z','x'};
if(b!=NULL)
if(NULL==c) ;
else printf(" c isn't NULL\n");
else
{
NULL; /* 细节风格,防止误认 */
}
printf("*********************\n");
printf("%d\n",sizeof( p) ) ; /* 4 */
printf("%d\n",sizeof( *p) ) ; /* 4 */
printf("%d\n",sizeof( int)*i ) ;/* 4 i=0,is 0 i=4,is 16*/
printf("*********************\n");
printf("%d\n",sizeof( a) ) ; /* 40 */
printf("%d\n",sizeof( a[10] ) ) ; /* 4 */
printf("%d\n",sizeof( &a) ) ; /* 4 */
printf("%d\n",sizeof( &a[0]) ) ; /* 4 */
printf("%d,%d\n",sizeof( b),strlen(b) ) ; /* 6,5*/
printf("%d,%d\n",sizeof( c),strlen(c) ) ; /* 4,5*/
printf("%d,%d\n",sizeof( &c),strlen(e) ) ; /* 4,9*/
printf("%d,%d\n",sizeof( d),strlen(d) ) ; /* 6,5*/
printf("*********************\n");
}
void fun11()
{
int i=-20;
unsigned j=10;
float ftestval=0.0,ff=0;
if(ftestval==0.0) printf("test 1 \n");
if((ftestval>=-EPSINON)&&(ftestval<=EPSINON)) printf("test 2 \n");
ff=10000000.00+0.00000001;
printf("test 2 is better \n f=%f d=%d\n",ff,ff); /* 不能如此转化 */
i=(int)ff; printf( "f=%f d=%d\n",ff,i); /* 强制转化 */
//bool b; C++
//if(b==FALSE) ; VC++ 中TRUE是1VB中TRUE是-1
//printf("d=%d,u=%u\n",i+j,i+j); /*d=-10,u=4294967286*/
//for(j=9;j>=0;j--) {printf("%u\n",j);} /*死循环 一直打印 */
//for(j=9;j>=1;j--) {printf("%u\n",j);} /* 修改为打印9到1 */
scanf("%d\n",&i);
switch(i)
{/****** right ********/
case 1:printf("case 1\n");
case 2:printf("case 2\n");
/******* error *******/
// case A:printf("case A\n"); /* case语句 后应少于20行 */
// scanf("%d\n",&i);
switch(i)
{
case 1:printf("case 2.1\n");break;
case 2:printf("case 2.2\n");break;
}
default:printf(" error\n"); /* 可无,有则放默认错误程序 break有妙用 case 后是整型和字符型表达式 */
/********* error end **********/
}
}
void fun12()
{
/*
for(初值循环条件;循环语句)
for(i=0,j=1;i<10,j<3;i++,j++)
while(循环条件){}
do{} 程序至少执行一次
while(循环条件)
*/
static int i=0;
//for(;i<10;i++)
for(i=0;i<=10;i++) /* 半开半闭写法 */
{
;
}
printf("***************\n");
while(i)
{
i--;printf("%d",i);
}
printf("%c\n",13); /* 需要记忆一些特别的ASCII码值13 CR回车 */
printf("***************\n");
do
{printf("%d\n",i);} /* 括号外不可放其他代码 */
while(i);
printf("%d\n",i);
printf("***************\n");
/*
**长循环在内效率高,程序不要修改循环变量的值
**循环要短,代码清晰,3层以内
**死循环不一定死,也不一定没有用;(测试机子可以开多少空间,跑认为终止的程序)
*/
printf("c\n");
do
{
for(i=0;i<5;i++)
{
printf("*");
// if(i==1) goto wz;
if(i==2)
continue; /*continue终止 本次(本轮)循环*/
if(i==4) /*if(i=4)结果不一样*/
break; /*break 终止 本层循环*/
}
printf("%2d",i);
wz: /*goto 建议少用跳出本层循环,也跳不出函数*/
printf("%d",i);
printf("\n");
i-=4; /*轻易改的动数字则死循环打印*/
printf("%2d",i);
}
while(i>0);
printf("\n");
printf("%d\n",i);
}
void fun13()
{
// extern int i;
// static int i;
// unsigned int i;
// auto int i;
// const int i;
// long int i;
// short int i;
//volatile int i;
struct{}su;
printf("%d",sizeof(su));/* VC IS 1 ,VM IS 0;*/
typedef struct{int l;int a[0];}type;
//typedef struct{int l;int a[ ];}type;/*可变长结构体 可变长数组,柔性数组,不同编译器不同写法*/
//typedef struct{int l;int a[10];}type; /* 不同的意思*/
enum EN{A=0,B,C=100,d,e}EN;
union check{int i;char c;}un;
un.i=1;
printf("%d\n",un.c); /*01 0001->1000 小端存储 */
printf("%d\n", C);
printf("%d\n", sizeof(EN)); /* 是一个指针,32位机上为4 */
/*
区别 枚举 宏
——————————————————————————————————————
|编译时 |确定值 |替换,未确定值|
|定义常量 |一次多个 | 一次一个 |
|编辑器调试 | 可以 | 不可以 |
|定义函数 | 不可以 | 可以 |
*/
}
void fun14()
{
auto int a=0;
const int b=1;
register c=2;
int DataGotFromfun0=fun0();
printf("%d\n",a);
// printf("%d\n",&a); //测试正确则如此注释
printf("%d\n",printf("%d ",a));
printf("%d\n",printf("%d \n",a));
/* 0
** 0 2
** 0
** 3
*/
printf("%d\n",c);
/* printf("%d\n",&c); 测试错误则如此注释*/
/*
** static 修饰变量,函数,C++中
*/
printf("DataGotFromfun0 is %d\n",DataGotFromfun0);
/*
** 变量,函数 命名所涉及英语语法
*/
printf("%d\n",b);
/* b++; const==readonly */
printf("%d\n",sizeof(int) ) ;/* 4 */
printf("%d\n",sizeof( a ) ) ;/* 4 */
printf("%d\n",sizeof a ) ;/* 4 */
/* printf("%d\n",sizeof int ) ; */
/* 同return(a);括号可去 */
}
void main()
{
//fun10();
//fun11();
//fun12();
//fun13();
//fun14();
}