c语言基础知识应用

1. NULL、'\0'和0之间的区别

 '\0'的十进制值为:0
    所以 (char)0=='\0'

  
  
  
  
  1. printf("%d\n",'\0');  //0 

ASCII码表中第一个字符为:NULL,对应的十进制值为:0。
NULL和'\0'的十进制值都为0。只是根据规范,它们用在不同的场合。

  
  
  
  
  1. NULL is a macro defined in several standard headers, 0 is an integer constant, '\0' is a character constant, and nul is the name of the character constant. All of these are *not* interchangeable:  
  2.  
  3. NULL is to be used for pointers only since it may be defined as ((void *)0), this would cause problems with anything but pointers.  
  4.  
  5. 0 can be used anywhere, it is the generic symbol for each type's zero value and the compiler will sort things out.  
  6.  
  7. '\0' should be used only in a character context.  
  8.  
  9. nul is not defined in C or C++, it shouldn't be used unless you define it yourself in a suitable manner, like:  
  10.  
  11. #define nul '\0'  

NULL用于给一个指针赋值;0可以在任何地方使用;而'\0'是一个字符。

strlen()求字符串长度,不包括'\0'

  
  
  
  
  1. const char str[]="hui";  
  2. printf("%d\n",strlen(str));  //3 

2.  ## 连接符号功能
    它由两个井号组成,其功能是在带参数的宏定义中将两个子串(token)联接起来,从而形成一个新的子串。

  
  
  
  
  1. #include <stdio.h>  
  2. #define CALL(x,y) x##y  
  3.  
  4. int main()  
  5. {  
  6.     int xy = 20, x = 5, y = 10;  
  7.     printf("%d\n",xy+CALL(x,y));  
  8.     return 0;  

    x##y的功能是连接x与y,即结果为:xy。所求变为:xy+xy=20+20=40

3.  sizeof( )说明

  
  
  
  
  1. #include <stdio.h>  
  2. #define CALL(x,y) x##y  
  3.  
  4. int main()  
  5. {  
  6.     int p=0;  
  7.     int i=1;  
  8.     p=sizeof(++i+ ++i);  
  9.     printf("i=%d p=%d\n",i,p);//1 4  
  10.       
  11.     return 0;  

    i的值未变,仍为1。
    因为:根据C99规范,sizeof是一个编译时刻就起效果的运算符,在其内的任何运算都没有意义
    所以:p = sizeof(++i+ ++i); 在编译的时候被翻译成 p = sizeof((++i+ ++i)的数据类型) 即,p = sizeof(int);  p= 4; 
    (这里是32bit系统,如果是16位系统,则p=2),然后才会继续编译成最终的程序,当然在最终程序执行的时候,自然不会执行任何++i了。

4.  ++i
1)

  
  
  
  
  1. #include <stdio.h>  
  2.  
  3. int main()  
  4. {  
  5.     int i=0;  
  6.     printf("%d %d\n",++i,++i);  //2 1  
  7.  
  8.     return 0;  

     2) 

  
  
  
  
  1. #include <stdio.h>  
  2.  
  3. int main()  
  4. {  
  5.     int *p=(int *)0;  
  6.     printf("%d %d\n",++p,++p); //8 4  
  7.  
  8.     return 0;  

    p指向int型变量的地址,p=0x00。
    一个int型变量占4个字节(0x00,0x01,0x02,0x03),所以p++时,p则指向了下一个int型变量(0x04,0x05,0x06,0x07),即p=0x04

5.  初始化
    1)静态/全局变量定义时如果没有显式初始化,则会自动初始化为0,而局部变量如果没有,则不会。(包括字符型和整型)
    2)用字符串来声明一个字符指针或数组时,最后会以'\0'结尾,因为字符串字面量末尾隐含了'\0'。

  
  
  
  
  1. /*  
  2.     //全局变量默认初始化为0,局部变量默认没有初始化  
  3.     //字符串存储在文字常量区,其结尾默认有'\0',如:"helloworld",共有10+1个字符,包括了'\0'  
  4. */ 
  5.  
  6. #include <stdio.h>  
  7. #include <string.h>  
  8. char BUF[80];   //全局变量默认初始化为0  
  9.  
  10. int main(void)  
  11. {  
  12.     char buf1[80];                  //局部变量默认没有初始化  
  13.     char buf2[80];  
  14.     strcpy(buf1,"helloworld");  //拷贝了10+1=11个字符,包括了'\0'; 所以打印没有乱码。  
  15.     strncpy(buf2,"helloworld",strlen("helloworld")); //拷贝了10,所以打印出乱码  
  16.     strncpy(BUF,"helloworld",strlen("helloworld"));//虽然没有拷贝'\0',但已经初始化,所以没有乱码  
  17.     printf("%s\n",buf);   
  18.     printf("%s\n",BUF);       
  19.     return 0;  


    3)scanf、gets等以字符串方式读输入流的函数,都会在末尾自动添加'\0',以保证读入的字符串有效。

6. 指数函数
    库函数pow 用于实现指数函的功能,如求x的y次方。
    头文件: #include <math.h>
    函数名: pow
    功    能: 指数函数(x的y次方)
    用    法: double pow(double x, double y);
    函数名: pow10
    功    能: 指数函数(10的p次方)
    用    法: double pow10(int p);

7. 堆和栈的区别

8. 含参数的宏与函数的优缺点

9. vc中long long类型定义出错
 
   定义变量出现错误:long long int i;
error C2632: 'long' followed by 'long' is illegal
    解决:_int64

10. 光标定位函数

  
  
  
  
  1. /*  
  2.     vc6.0 光标定位函数  
  3. */ 
  4. #include <windows.h>  
  5. void gotoxy(int x,int y)  
  6. {  
  7.     COORD c;  
  8.     c.X=x-1;  
  9.     c.Y=y-1;  
  10.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);  

 

你可能感兴趣的:(c,职场,基础知识,休闲)