分享

面试中你不可回避的C、C++的问题(二)

 Null872 2014-12-19

1.如何将a,b的值进行交换,并且不适用任何中间变量

  1. #include <stdio.h>  
  2.   
  3. void first(int a,int b)  
  4. {  
  5.     a=a+b;  
  6.     b=a-b;  
  7.     a=a-b;  
  8.     printf("%d %d\n",a,b);  
  9. }  
  10.   
  11. void second(int a,int b)  
  12. {  
  13.     a=a^b;  
  14.     b=a^b;  
  15.     a=a^b;  
  16.     printf("%d %d\n",a,b);  
  17. }  
  18.   
  19. int main()  
  20. {  
  21.     int a=10,b=1000;  
  22.     printf("%d %d\n",a,b);  
  23.     first(a,b);  
  24.     second(a,b);  
  25.     return 0;  
  26. }  

2.使用预处理指令#define声明一个常数,用来表示一年中大约有多少秒?

  1. #define SECOND_PER_YEAR (60*60*24*365)UL  
3.写一个标准的宏MIN,这个宏输入两个参数并返回较小的一个

  1. #define MIN(a,b) ((a)<=(b)?(a):(b))  

4.Const的要求

       a)定义常量

       b)修饰函数的参数和返回值,甚至定义函数体

       c)在C语言中它总是要占用内存的,而且它的名字是全局符。C编译器不能把const看成一个编译期间的常量

       d)C语言默认const修饰的变量时外部链接,但是C++默认的是内部链接

例子:

  1. /*使用的是main.cpp文件,并且是用mingw的编译器,编译没有问题*/  
  2. const int bufsize=100;  
  3. char buf[bufsize];  
  1. /*使用的是main.c文件,并且是用mingw的编译器,编译有问题*/  
  2. const int bufsize=100;  
  3. char buf[bufsize];  
错误信息

error: variably modified 'buf' at file scope   char buf[bufsize];

5.Const与define的不同

         a)Const常量有数据类型,而宏常量没有数据类型。编译器可以对前者进行安全检查,而对后者只是进行简单的字符替换,

             没有安全检查,并且在字符串替换中可能产生意料不到的错误(边际效应)

         b)有些编译器对const常量可以进行调试,但是不能对宏常量进行调试

6.sizeof的基本要求--计算数据空间的字节数

  1. #include <iostream>  
  2. #include <string>  
  3.   
  4. using namespace std;  
  5.   
  6. struct s1  
  7. {  
  8.     char a;  
  9.     double b;  
  10.     int c;  
  11.     char d;  
  12. };  
  13.   
  14. struct s2  
  15. {  
  16.     char a;  
  17.     char b;  
  18.     int c;  
  19.     double d;  
  20. };  
  21.   
  22. void basic()  
  23. {  
  24.     cout << sizeof(char) << endl;//1  
  25.     cout << sizeof(short) << endl;//2  
  26.     cout << sizeof(int) << endl;//4  
  27.     cout << sizeof(float) << endl;//4  
  28.     cout << sizeof(double) << endl;//8  
  29.     cout << sizeof(long) << endl;//4  
  30.     cout << sizeof(string) << endl;//4  
  31. }  
  32.   
  33. int main()  
  34. {  
  35.     basic();  
  36.     //编译器会按照结构体中最大的一个类型对数据进行对齐  
  37.     cout<<sizeof(s1)<<endl;//24  
  38.     cout<<sizeof(s2)<<endl;//16  
  39.     return 0;  
  40. }  
但是也有问题:

  1. #include <iostream>  
  2. #include <string>  
  3.   
  4. using namespace std;  
  5.   
  6. struct s1  
  7. {  
  8. char a[8];  
  9. };  
  10.   
  11. struct s2  
  12. {  
  13. double d;  
  14. };  
  15.   
  16. struct s3  
  17. {  
  18. s1 s;  
  19. char a;  
  20. };  
  21.   
  22. struct s4  
  23. {  
  24. s2 s;  
  25. char a;  
  26. };  
  27.   
  28. void basic()  
  29. {  
  30.     cout << sizeof(char) << endl;//1  
  31.     cout << sizeof(short) << endl;//2  
  32.     cout << sizeof(int) << endl;//4  
  33.     cout << sizeof(float) << endl;//4  
  34.     cout << sizeof(double) << endl;//8  
  35.     cout << sizeof(long) << endl;//4  
  36.     cout << sizeof(string) << endl;//4  
  37. }  
  38.   
  39. int main()  
  40. {  
  41.     basic();  
  42.     //在自己定义结构体的时候,如果空间紧张的话,最好考虑对齐因素来排列结构体里的元素  
  43.     cout<<sizeof(s1)<<endl; // 8  
  44.     cout<<sizeof(s2)<<endl; // 8  
  45.     cout<<sizeof(s3)<<endl; // 9  
  46.     cout<<sizeof(s4)<<endl; // 16;  
  47.     return 0;  
  48. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多