What is the difference between typedef and define
A define is a preprocessor directive and it replaces the value before compiling the code. One of the major problems with the macro that there is no type checking. Generally, the macro is used to create the alias, in C language macro is also used as a file guard. Explanation of the code, In the above code, both created types have the same name and they declare in the same scope. So when we will compile the code, we will get the compiler error.
In another case when we used CharPtr to declare the variable then Data3 and Data4 both become character pointer. Typedef vs define in C by admin May 26, C 0 comments.
Typedef VS define in C Typedef In the C programming language , a keyword called typedef can be used to give a type a new name. The of three numbers n1 , n2 , and n3 is : BYTE b1, b2;. BYTE b1 , b2 ;. Book book ;. The title of the book is : C programming. The book of the book is : Dennis Ritchie. The subject of the study is : Tutorials and examples. Alex is 10 years old. Enter the radius of the circle: 3 The area of the circle is: It should be noted that semicolons cannot exist in define text replacement , while commas exist in typedef rename of type.
At the same time, define is processed by the preprocessor, which only does simple text replacement work without any checks correctness check, scope check, type check. Define as long as it is defined once, then there is no scope limitation, and it can be used anywhere:. The definition of data types is generally platform-related. For large code, if all the definitions are: long double a; But when the platform does not support the long long type, it is really terrible to enter the code body to search one by one:.
The degree of encapsulation between typedef and define is different. Typedef can be regarded as a kind of complete "encapsulation" type, that is to say, after the typedef is declared, no more things can be added to it.
0コメント