[C/cpp Series Knowledge] Type difference of character literals and bool in C and C + +

Source: Internet
Author: User

Each solid in the c/c+ (every literal) has a type, for example, 10 is an int, so SIZIOF (10) and sizeof (int) are the same, but the character-solid (' a ') has different variable types in C and C + +.

In C, ' a ' is considered int, and in C + +, ' a ' is considered to be a char type.

int Main () {   printf("sizeof (' V ') =%d sizeof (char) =%d"sizeof('  V'sizeof(char));    return 0 ;}

Results:

C result– sizeof (' V ') = 4 sizeof (char) = 1

C + + result– sizeof (' V ') = 1 sizeof (char) = 1

The above behavior can also reflect C + + function overloading

 void  foo (char   c) {printf (  from Foo:char   "  void  foo (int   i) {printf (  from foo:int   " );}  int   main () {foo (  '  

Then call void foo (char c)

3) Types of Boolean results is different in C and C + +.

// output = 4 in C (which is size of int)printf ("%d"sizeof(1= =1< /c8>//  output = 1 in C + + (which is the size of the Boolean datatype)sizeof(1==1

[C/cpp Series knowledge] Type difference of character literals and bool in C and C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.