[C + +] introduces the difference between # define and typedef through a simple program

Source: Internet
Author: User

Needless to say, please see the following procedure:

#include <iostream>using namespace std; #define SIGN1 char*  //typedef char * sign2;//typedef is a statement so end with a semicolon int Main () {SIGN1 ch1,ch2; SIGN2 ch3,ch4;cout<<sizeof (CH1) << "" <<sizeof (CH2) <<endl;cout<<sizeof (CH3) << " "<<sizeof (CH4) <<endl;return 0;}

think about what the answer is???




#define宏替换只是 Simple Replacement : (completed at preprocessing) (the preprocessing command begins with #, with no semicolon at the end, because they are not statements)

SIGN1 CH1,CH2; ----------->>>> char * CH1,CH2;

CH1 is a pointer (char *) four bytes CH2 type char one byte


typedef type Rename, named identifiers have the function of type definition description ( compile-time processing) (is a statement, so end with a semicolon)

SIGN2 CH3,CH4; ----------->>>> char * CH3,*CH4;

CH3 and CH4 are pointers.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[C + +] introduces the difference between # define and typedef through a simple program

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.