ÉèΪÊ×Ò³ ¼ÓÈëÊÕ²Ø

TOP

C/C++»ù´¡ÖªÊ¶ÎÊÌâ(Èý)
2014-11-24 01:20:07 ¡¾´ó ÖРС¡¿ ä¯ÀÀ:425´Î
Tags£ºC/C »ù´¡ÖªÊ¶ ÎÊÌâ
ÖÐ×ö¸¡µãÔËËã¡£´ËÍ⣬ISRÓ¦¸ÃÊǶ̶øÓÐЧÂʵģ¬ÔÚISRÖÐ×ö¸¡µãÔËËãÊDz»Ã÷Öǵġ£
4). ÓëµÚÈýµãÒ»ÂöÏà³Ð£¬printf()¾­³£ÓÐÖØÈëºÍÐÔÄÜÉϵÄÎÊÌâ¡£Èç¹ûÄ㶪µôÁ˵ÚÈýºÍµÚËĵ㣬ÎÒ²»»á̫ΪÄÑÄãµÄ¡£²»ÓÃ˵£¬Èç¹ûÄãÄܵõ½ºóÁ½µã£¬ÄÇôÄãµÄ±»¹ÍÓÃÇ°¾°Ô½À´Ô½¹âÃ÷ÁË¡£


´úÂëÀý×Ó£¨Code examples£©
12 . ÏÂÃæµÄ´úÂëÊä³öÊÇʲô£¬ÎªÊ²Ã´£¿


void foo(void)
{ unsigned int a = 6;
int b = -20;
(a+b > 6) puts(¡°> 6¡å) : puts(¡°<= 6");
}


Õâ¸öÎÊÌâ²âÊÔÄãÊÇ·ñ¶®µÃCÓïÑÔÖеÄÕûÊý×Ô¶¯×ª»»Ô­Ôò£¬ÎÒ·¢ÏÖÓÐЩ¿ª·¢Õ߶®µÃ¼«ÉÙÕâЩ¶«Î÷¡£²»¹ÜÈçºÎ£¬ÕâÎÞ·ûºÅÕûÐÍÎÊÌâµÄ´ð°¸ÊÇÊä³öÊÇ¡°>6¡±¡£Ô­ÒòÊǵ±±í´ïʽÖдæÔÚÓзûºÅÀàÐͺÍÎÞ·ûºÅÀàÐÍʱËùÓеIJÙ×÷Êý¶¼×Ô¶¯×ª»»ÎªÎÞ·ûºÅÀàÐÍ¡£Òò´Ë-20±ä³ÉÁËÒ»¸ö·Ç³£´óµÄÕýÕûÊý£¬ËùÒԸñí´ïʽ¼ÆËã³öµÄ½á¹û´óÓÚ6¡£ÕâÒ»µã¶ÔÓÚÓ¦µ±Æµ·±Óõ½ÎÞ·ûºÅÊý¾ÝÀàÐ͵ÄǶÈëʽϵͳÀ´ËµÊÇ·á³£ÖØÒªµÄ¡£Èç¹ûÄã´ð´íÁËÕâ¸öÎÊÌ⣬ÄãÒ²¾Íµ½Á˵ò»µ½Õâ·Ý¹¤×÷µÄ±ßÔµ¡£


13. ÆÀ¼ÛÏÂÃæµÄ´úÂëƬ¶Ï£º


unsigned int zero = 0;
unsigned int compzero = 0xFFFF;
/*1¡äs complement of zero */


¶ÔÓÚÒ»¸öintÐͲ»ÊÇ16λµÄ´¦ÀíÆ÷Ϊ˵£¬ÉÏÃæµÄ´úÂëÊDz»ÕýÈ·µÄ¡£Ó¦±àдÈçÏ£º


unsigned int compzero = ~0;


ÕâÒ»ÎÊÌâÕæÕýÄܽÒ¶³öÓ¦ÊÔÕßÊÇ·ñ¶®µÃ´¦ÀíÆ÷×Ö³¤µÄÖØÒªÐÔ¡£ÔÚÎҵľ­ÑéÀºÃµÄǶÈëʽ³ÌÐòÔ±·Ç³£×¼È·µØÃ÷°×Ó²¼þµÄϸ½ÚºÍËüµÄ¾ÖÏÞ£¬È»¶øPC»ú³ÌÐòÍùÍù°ÑÓ²¼þ×÷Ϊһ¸öÎÞ·¨±ÜÃâµÄ·³ÄÕ¡£
µ½ÁËÕâ¸ö½×¶Î£¬Ó¦ÊÔÕß»òÕßÍêÈ«´¹Í·É¥ÆøÁË»òÕßÐÅÐÄÂúÂúÖ¾Ôڱصá£Èç¹ûÏÔȻӦÊÔÕß²»ÊǺܺã¬ÄÇôÕâ¸ö²âÊÔ¾ÍÔÚÕâÀï½áÊøÁË¡£µ«Èç¹ûÏÔȻӦÊÔÕß×öµÃ²»´í£¬ÄÇôÎÒ¾ÍÈÓ³öÏÂÃæµÄ×·¼ÓÎÊÌ⣬ÕâЩÎÊÌâÊDZȽÏÄѵģ¬ÎÒÏë½ö½ö·Ç³£ÓÅÐãµÄÓ¦ÊÔÕßÄÜ×öµÃ²»´í¡£Ìá³öÕâЩÎÊÌ⣬ÎÒÏ£Íû¸ü¶à¿´µ½Ó¦ÊÔÕßÓ¦¸¶ÎÊÌâµÄ·½·¨£¬¶ø²»ÊǴ𰸡£²»¹ÜÈçºÎ£¬Äã¾Íµ±ÊÇÕâ¸öÓéÀÖ°É¡­


¶¯Ì¬ÄÚ´æ·ÖÅ䣨Dynamic memory allocation£©


14. ¾¡¹Ü²»Ïñ·ÇǶÈëʽ¼ÆËã»úÄÇô³£¼û£¬Ç¶Èëʽϵͳ»¹ÊÇÓдӶѣ¨heap£©Öж¯Ì¬·ÖÅäÄÚ´æµÄ¹ý³ÌµÄ¡£ÄÇôǶÈëʽϵͳÖУ¬¶¯Ì¬·ÖÅäÄÚ´æ¿ÉÄÜ·¢ÉúµÄÎÊÌâÊÇʲô£¿


ÕâÀÎÒÆÚÍûÓ¦ÊÔÕßÄÜÌáµ½ÄÚ´æËéƬ£¬ËéƬÊÕ¼¯µÄÎÊÌ⣬±äÁ¿µÄ³ÖÐÐʱ¼äµÈµÈ¡£Õâ¸öÖ÷ÌâÒѾ­ÔÚESPÔÓÖ¾Öб»¹ã·ºµØÌÖÂÛ¹ýÁË£¨Ö÷ÒªÊÇ P.J. Plauger, ËûµÄ½âÊÍÔ¶Ô¶³¬¹ýÎÒÕâÀïÄÜÌáµ½µÄÈκνâÊÍ£©£¬ËùÓлعýÍ·¿´Ò»ÏÂÕâЩÔÓÖ¾°É£¡ÈÃÓ¦ÊÔÕß½øÈëÒ»ÖÖÐé¼ÙµÄ°²È«¸Ð¾õºó£¬ÎÒÄóöÕâôһ¸öС½ÚÄ¿£ºÏÂÃæµÄ´úÂëƬ¶ÎµÄÊä³öÊÇʲô£¬ÎªÊ²Ã´£¿


char *ptr;
if ((ptr = (char *)malloc(0)) == NULL)
puts(¡°Got a null pointer¡±);
else
puts(¡°Got a valid pointer¡±);


ÕâÊÇÒ»¸öÓÐȤµÄÎÊÌâ¡£×î½üÔÚÎÒµÄÒ»¸öͬʲ»¾­Òâ°Ñ0Öµ´«¸øÁ˺¯Êýmalloc£¬µÃµ½ÁËÒ»¸öºÏ·¨µÄÖ¸ÕëÖ®ºó£¬ÎÒ²ÅÏëµ½Õâ¸öÎÊÌâ¡£Õâ¾ÍÊÇÉÏÃæµÄ´úÂ룬¸Ã´úÂëµÄÊä³öÊÇ¡°Got a valid pointer¡±¡£ÎÒÓÃÕâ¸öÀ´¿ªÊ¼ÌÖÂÛÕâÑùµÄÒ»ÎÊÌ⣬¿´¿´±»ÃæÊÔÕßÊÇ·ñÏëµ½¿âÀý³ÌÕâÑù×öÊÇÕýÈ·¡£µÃµ½ÕýÈ·µÄ´ð°¸¹ÌÈ»ÖØÒª£¬µ«½â¾öÎÊÌâµÄ·½·¨ºÍÄã×ö¾ö¶¨µÄ»ù±¾Ô­Àí¸üÖØҪЩ¡£


Typedef


15. Typedef ÔÚCÓïÑÔÖÐƵ·±ÓÃÒÔÉùÃ÷Ò»¸öÒѾ­´æÔÚµÄÊý¾ÝÀàÐ͵ÄͬÒå×Ö¡£Ò²¿ÉÒÔÓÃÔ¤´¦ÀíÆ÷×öÀàËƵÄÊ¡£ÀýÈ磬˼¿¼Ò»ÏÂÏÂÃæµÄÀý×Ó£º
#define dPS struct s *
typedef struct s * tPS;


ÒÔÉÏÁ½ÖÖÇé¿öµÄÒâͼ¶¼ÊÇÒª¶¨ÒådPS ºÍ tPS ×÷Ϊһ¸öÖ¸Ïò½á¹¹sÖ¸Õë¡£ÄÄÖÖ·½·¨¸üºÃÄØ£¿£¨Èç¹ûÓеĻ°£©ÎªÊ²Ã´£¿
ÕâÊÇÒ»¸ö·Ç³£Î¢ÃîµÄÎÊÌ⣬ÈκÎÈË´ð¶ÔÕâ¸öÎÊÌ⣨Õýµ±µÄÔ­Òò£©ÊÇÓ¦µ±±»¹§Ï²µÄ¡£´ð°¸ÊÇ£ºtypedef¸üºÃ¡£Ë¼¿¼ÏÂÃæµÄÀý×Ó£º
dPS p1,p2;
tPS p3,p4;


µÚÒ»¸öÀ©Õ¹Îª
struct s * p1, p2;


ÉÏÃæµÄ´úÂ붨Òåp1Ϊһ¸öÖ¸Ïò½á¹¹µÄÖ¸£¬p2Ϊһ¸öʵ¼ÊµÄ½á¹¹£¬ÕâÒ²Ðí²»ÊÇÄãÏëÒªµÄ¡£µÚ¶þ¸öÀý×ÓÕýÈ·µØ¶¨ÒåÁËp3 ºÍp4 Á½¸öÖ¸Õë¡£


»ÞɬµÄÓï·¨


16. CÓïÑÔͬÒâһЩÁîÈËÕ𾪵Ľṹ,ÏÂÃæµÄ½á¹¹ÊǺϷ¨µÄÂð£¬Èç¹ûÊÇËü×öЩʲô£¿
int a = 5, b = 7, c;
c = a+++b;


Õâ¸öÎÊÌ⽫×öΪÕâ¸ö²âÑéµÄÒ»¸öÓä¿ìµÄ½áβ¡£²»¹ÜÄãÏ಻ÏàÐÅ£¬ÉÏÃæµÄÀý×ÓÊÇÍêÈ«ºÏºõÓï·¨µÄ¡£ÎÊÌâÊDZàÒëÆ÷ÈçºÎ´¦ÀíËü£¿Ë®Æ½²»¸ßµÄ±àÒë×÷Õßʵ¼ÊÉÏ»áÕùÂÛÕâ¸öÎÊÌ⣬¸ù¾Ý×î´¦ÀíÔ­Ôò£¬±àÒëÆ÷Ó¦µ±ÄÜ´¦Àí¾¡¿ÉÄÜËùÓкϷ¨µÄÓ÷¨¡£Òò´Ë£¬ÉÏÃæµÄ´úÂë±»´¦Àí³É£º
c = a++ + b;
Òò´Ë, Õâ¶Î´úÂë³ÖÐкóa = 6, b = 7, c = 12¡£
Èç¹ûÄãÖªµÀ´ð°¸£¬»ò²Â³öÕýÈ·´ð°¸£¬×öµÃºÃ¡£Èç¹ûÄã²»ÖªµÀ´ð°¸£¬ÎÒÒ²²»°ÑÕâ¸öµ±×÷ÎÊÌâ¡£ÎÒ·¢ÏÖÕâ¸öÎÊÌâµÄ×î´óºÃ´¦ÊÇ:ÕâÊÇÒ»¸ö¹ØÓÚ´úÂë±àд·ç¸ñ£¬´úÂëµÄ¿É¶ÁÐÔ£¬´úÂëµÄ¿ÉÐÞ¸ÄÐԵĺõĻ°Ìâ


What will print out


main()
{ char *p1=¡°name¡±;
char *p2;
p2=(char*)malloc(20);
memset (p2, 0, 20);
while(*p2++ = *p1++);
printf(¡°%sn¡±,p2);


}


Answer:empty string.


What will be printed as the result of the operation below:


main()
{ int x=20,y=35;
x=y++ + x++;
y= ++y + ++x;
printf(¡°%d%dn¡±,x,y);
}


Answer : 5794


What will be printed as the result of the operation below:


main()
{ int x=5;
printf(¡°%d,%d,%dn¡±,x,x<<2,x>>2);
}


Answer: 5,20,1


What will be printed as the result of the operation below:


#define swap(a,b) a=a+b;b=a-b;a=a-b;
void main()
{ int x=5, y=10;
swap (x,y);
printf(¡°%d %dn¡±,x,y);
swap2(x,y);
printf(¡°%d %dn¡±,x,y);
}


int swap2(int a, int b)
{ int temp;
temp=a;
b=a;
a=temp;
return 0;


}


Answer: 10, 5
10, 5


What will be printed as the result of the operation below:


main()
{ char *ptr = ¡± Cisco Systems¡±;
*ptr++; printf(¡°%sn¡±,ptr);
ptr++;
printf(¡°%sn¡±,ptr);
}


Answer:Cisco Systems
isco systems


What will be printed as the result of the operation below:


main()
{ char s1[]=¡°Cisco¡±;
char s2[]= ¡°systems¡±;
printf(¡°%s¡±,s1);
} Answer: Cisco


What will be printed as the result of the operation below:


main()
{ char *p1;
char *p2;
p1=(char *)malloc(25);
p2=(char *)malloc(25);


strcpy(p1,¡±Cisco¡±);
strcpy(p2,¡°systems¡±);
strcat(p1,p2);


printf(¡°%s¡±,p1);


}


Answer: Ciscosystems


The following variable is available in file1.c, who can access it :


static int average;


Answer: all the functions in the file1.c can access the variable.


WHat will be the result of the following code


#define TRUE 0 // some code
while(TRUE)
{


// some code


}


Answer: This will not go into the loop as TRUE is defined as 0.


What will be printed as the result of the operation below:


int x;
int modifyvalue()
{ return(x+=10);
} int changeva lue(int x)
{ return(x+=1);
}


void main()
{ int x=10;
x++;
changeva lue(x);
x++;
modifyvalue();
printf(¡°First output:%dn¡±,x);


x++;
changeva lue(x);
printf(¡°Second output:%dn¡±,x);
modifyvalue();
printf(¡°Third output:%dn¡±,x);


}


Answer: 12 , 13 , 13


What will be printed as the result of the operation below:


main()
{ int x=10, y=15;
x = x++;
y = ++y;
printf(¡°%d %dn¡±,x,y);
}


Answer: 11, 16


What will be printed as the result of the operation below:


main()
{ int a=0;
if(a==0)
printf(¡°Cisco Systemsn¡±);
printf(¡°Cisco Systemsn¡±);
}


Answer: Two lines with ¡°Cisco Systems¡± will be printed.


ÔٴθüÐÂC++Ïà¹ØÌ⼯


1. ÒÔÏÂÈýÌõÊä³öÓï¾ä·Ö±ðÊä³öʲô£¿[CÒ×]
char str1[] = ¡°abc¡±;
char str2[] = ¡°abc¡±;
const char str3[] = ¡°abc¡±;
const char str4[] = ¡°abc¡±;
const char* str5 = ¡°abc¡±;
const char* str6 = ¡°abc¡±;
cout << boolalpha << ( str1==str2 ) << endl; // Êä³öʲô£¿
cout << boolalpha << ( str3==str4 ) << endl; // Êä³öʲô£¿
cout << boolalpha << ( str5==str6 ) << endl; // Êä³öʲô£¿


13. ·ÇC++ÄÚ½¨Ðͱð A ºÍ B£¬ÔÚÄļ¸ÖÖÇé¿öÏÂBÄÜÒþʽת»¯ÎªA£¿[C++ÖеÈ]
´ð£º
a. class B : public A { ¡­¡­} // B¹«Óм̳Ð×ÔA£¬¿ÉÒÔÊǼä½Ó¼Ì³ÐµÄ
b. class B { operator A( ); } // BʵÏÖÁËÒþʽת»¯ÎªAµÄת»¯
c. class A { A( const B& ); } // AʵÏÖÁËnon-explicitµÄ²ÎÊýΪB£¨¿ÉÒÔÓÐÆäËû´øĬÈÏÖµµÄ²ÎÊý£©¹¹Ô캯Êý
d. A& operator= ( const A& ); // ¸³Öµ²Ù×÷£¬Ëä²»ÊÇÕý×ÚµÄÒþʽÀàÐÍת»»£¬µ«Ò²¿ÉÒÔÃãÇ¿ËãÒ»¸ö


12. ÒÔÏ´úÂëÖеÄÁ½¸ösizeofÓ÷¨ÓÐÎÊÌâÂð£¿[CÒ×]
void UpperCase( char str[] ) // ½« str ÖеÄСд×Öĸת»»³É´óд×Öĸ
{ for( size_t i=0; i if( 'a'<=str[i] && str[i]<='z' )
str[i] -= ('a'-'A' );
} char str[] = "aBcDe";
cout << "str×Ö·û³¤¶ÈΪ: " << sizeof(str)/sizeof(str[0]) << endl;
UpperCase( str );
cout << str << endl;


7. ÒÔÏ´úÂëÓÐʲôÎÊÌ⣿[CÄÑ]
void char2Hex( char c ) // ½«×Ö·ûÒÔ16½øÖƱíʾ
{ char ch = c/0x10 + '0'; if( ch > ¡¯9¡ä ) ch += (¡®A¡¯-¡¯9¡ä-1);
char cl = c%0¡Á10 + ¡¯0¡ä; if( cl > ¡¯9¡ä ) cl += (¡®A¡¯-¡¯9¡ä-1);
cout << ch << cl << ' ';
} char str[] = "I love Öйú";
for( size_t i=0; i char2Hex( str[i] );
cout << endl;


4. ÒÔÏ´úÂëÓÐʲôÎÊÌ⣿[C++Ò×]
struct Test
{ Test( int ) {}
Test() {}
void fun() {}
};
void main( void )
{ Test a(1);
a.fun();
Test b();
b.fun();
}


5. ÒÔÏ´úÂëÓÐʲôÎÊÌ⣿[C++Ò×]
cout << (true 1:"1") << endl;


8. ÒÔÏ´úÂëÄܹ»±àÒëͨ¹ýÂð£¬ÎªÊ²Ã´£¿[C++Ò×]
unsigned int const size1 = 2;
char str1[ size1 ];
unsigned int temp = 0;
cin >> temp;
unsigned int const size2 = temp;
char str2[ size2 ];


9. ÒÔÏ´úÂëÖеÄÊä³öÓï¾äÊä³ö0Âð£¬ÎªÊ²Ã´£¿[C++Ò×]
struct CLS
{ int m_i;
CLS( int i ) : m_i(i) {}
CLS()
{ CLS(0);
} };
CLS obj;
cout << obj.m_i << endl;


10. C++ÖеĿÕÀ࣬ĬÈϲúÉúÄÄЩÀà³ÉÔ±º¯Êý£¿[C++Ò×]
´ð£º
class Empty
{ public:
Empty(); // ȱʡ¹¹Ô캯Êý
Empty( const Empty& ); // ¿½±´¹¹Ô캯Êý
~Empty(); // Îö¹¹º¯Êý
Empty& operator=( const Empty& ); // ¸³ÖµÔËËã·û
Empty* operator&(); // È¡Ö·ÔËËã·û
const Empty* operator&() const; // È¡Ö·ÔËËã·û const
};


3. ÒÔÏÂÁ½ÌõÊä³öÓï¾ä·Ö±ðÊä³öʲô£¿[C++ÄÑ]
float a = 1.0f;
cout << (int)a << endl;
cout << (int&)a << endl;
cout << boolalpha << ( (int)a == (int&)a ) << endl; // Êä³öʲô£¿
float b = 0.0f;
cout << (int)b << endl;
cout << (int&)b << endl;
cout << boolalpha << ( (int)b == (int&)b ) << endl; // Êä³öʲô£¿


2. ÒÔÏ·´Ïò±éÀúarrayÊý×éµÄ·½·¨ÓÐʲô´íÎó£¿[STLÒ×]
vector array;
array.push_back( 1 );
array.push_back( 2 );
array.push_back( 3 );


for( vector::size_type i=array.size()-1; i>=0; ¨Ci ) // ·´Ïò±éÀúarrayÊý×é
{ cout << array[i] << endl;
}


6. ÒÔÏ´úÂëÓÐʲôÎÊÌ⣿[STLÒ×]
typedef vector IntArray;
IntArray array;
array.push_back( 1 );
array.push_back( 2 );
array.push_back( 2 );
array.push_back( 3 );
// ɾ³ýarrayÊý×éÖÐËùÓеÄ2
for( IntArray::iterator itor=array.begin(); itor!=array.end(); ++itor )
{ if( 2 == *itor ) array.erase( itor );
}


11. дһ¸öº¯Êý£¬Íê³ÉÄÚ´æÖ®¼äµÄ¿½±´¡£[¿¼ÂÇÎÊÌâÊÇ·ñÈ«Ãæ]
´ð£º


void* mymemcpy( void *dest, const void *src, size_t count )
{
char* pdest = static_cast ( dest );
const char* psrc = static_cast ( src );
if( pdest>psrc && pdest {
for( size_t i=count-1; i!=-1; ¨Ci )
pdest[i] = psrc[i];
}
else
{
for( size_t i=0; i pdest[i] = psrc[i];
}
return dest;
}
int main( void )
{
char str[] = ¡°0123456789¡å;
mymemcpy( str+1, str+0, 9 );
cout << str << endl;


system( ¡°Pause¡± );
return 0;
}


±¾ÎÄÀ´×ÔCSDN²©¿Í£¬×ªÔØÇë±êÃ÷³ö´¦£ºhttp://blog.csdn.net/lovecn/archive/2009/06/12/4264644.aspx


Ê×Ò³ ÉÏÒ»Ò³ 1 2 3 4 5 ÏÂÒ»Ò³ βҳ 3/5/5
¡¾´ó ÖРС¡¿¡¾´òÓ¡¡¿ ¡¾·±Ìå¡¿¡¾Í¶¸å¡¿¡¾Êղء¿ ¡¾ÍƼö¡¿¡¾¾Ù±¨¡¿¡¾ÆÀÂÛ¡¿ ¡¾¹Ø±Õ¡¿ ¡¾·µ»Ø¶¥²¿¡¿
ÉÏһƪ£ºÊ²Ã´ÊǹþÏ£±í£¿ ÏÂһƪ£º³ÌÐòԱӦƸ×î±ä̬µÄÃæÊÔ¾­Àú

×îÐÂÎÄÕÂ

ÈÈÃÅÎÄÕÂ

Hot ÎÄÕÂ

Python

C ÓïÑÔ

C++»ù´¡

´óÊý¾Ý»ù´¡

linux±à³Ì»ù´¡

C/C++ÃæÊÔÌâÄ¿