answersLogoWhite

0


Best Answer
Copy

defines are handled by a preprocessor (a program run before the actual c compiler) which works like replace all in you editor. Typedef is handled by the c compiler itself, and is an actual definition of a new type. The distinction given between #define and typedef has one significant error: typedef does not in fact create a new type. According to Kernighan & Richie, the authors of the authoritative and universally acclaimed book, "The C Programming Language": It must be emphasized that a typedef declaration does not create a new type in any sense; it merely adds a new name for some existing type. Nor are there any new semantics: variables declared this way have exactly the same properties as variables whose declarations are spelled out explicitly. In effect, typedef is like #define, except that since it is interpreted by the compiler, it can cope with textual substitutions that are beyond the capabilities of the preprocessor. There are some more subtleties though. The type defined with a typedef is exactly like its counterpart as far as its type declaring power is concerned BUT it cannot be modified like its counterpart. For example, let's say you define a synonim for the int type with: typedef int MYINT Now you can declare an int variable either with int a; or MYINT a; But you cannot declare an unsigned int (using the unsigned modifier) with unsigned MYINT a; although unsigned int a; would be perfectly acceptable. typedefs can correctly encode pointer types.where as #DEFINES are just replacements done by the preprocessor. For example, # typedef char *String_t; # #define String_d char * # String_t s1, s2; String_d s3, s4; s1, s2, and s3 are all declared as char *, but s4 is declared as a char, which is probably not the intention. typedef also allows to delcare arrays, # typedef char char_arr[]; # char_arr my_arr = "Hello World!\n"; This is equal to

# char my_arr[] = "Hello World!\n"; This may lead to obfuscated code when used too much, but when used correctly it is extremely useful to make code more compat and easier to read.

User Avatar

Wiki User

โˆ™ 2013-03-15 05:59:40
This answer is:
๐Ÿ™
0
๐Ÿคจ
0
๐Ÿ˜ฎ
0
๐Ÿ˜‚
0
User Avatar

Add your answer:

Earn +5 pts
Q: What is difference between define and typedef in c?
Write your answer...
Submit

Related Questions

What is difference between define and typedef in c plus plus?

#define is a preprocessor directive used to declare macros. typedef is a C++ keyword to define a data type.


Difference between define and typedef?

typedef keep the property of attributes whereas #define won't for example #define char* m_type typedef char* p_type m_type a, b; p_type c,d In above case a, c and d are pointer types , where as b in char type only. Which is not our intention. In another example which shows #define keeps string attributes whereas typedef won't. #define int INT; typedef int MYINT unsigned MYINT a; unsigned INT a; The above example won't work as it won't change to unsigned int a. And in #define case it will simply works!!!.


What is the use of type def in c?

A typedef is used to define a type. The clue is in the name: typedef = type definition.


How is typedef different from int in c language?

They are entirely different things; int is a type, typedef is a way to define types.


What is the difference between Typedef and Reference in C plus plus?

A typedef is a compiler macro. A reference is a pointer, usually implemented with transparent syntax. They have no relationship between each other.


What is the difference between class and object in c plus plus?

There is no difference; to define an object in C++ you use the 'class' definition


What is typedef in c programming language?

A typedef is a type definition.


How do you store the data into byte in c?

There is not built-in 'byte' type in C, but you can define it: typedef unsigned char byte; byte bmin=0, bmax=255;


Typedef int a a a Is this possible in C?

No, but 'typedef int a;' is possible, it defines the type 'a'.


What is a typedef in C programming?

A typedef is a type definition. That is, we are defining a new type. In some cases, a typedef is merely an alias for an existing type. We typically use aliases as a shorthand for complex types: typedef unsigned long ULONG; Here, we can use the alias ULONG in place of unsigned long. We can also use typedef to define user-defined types, such as structures and unions: typedef struct person { char* first_name; char* last_name; int age; };


What is typedef in C?

AnswerA typedef declaration lets you define your own identifiers that can be used in place of type specifiers such as int, float, and double. A typedef declaration does not reserve storage. The names you define using typedef are not new data types, but synonyms for the data types or combinations of data types they represent. The name space for a typedef name is the same as other identifiers. The exception to this rule is if the typedef name specifies a variably modified type. In this case, it has block scope.When an object is defined using a typedef identifier, the properties of the defined object are exactly the same as if the object were defined by explicitly listing the data type associated with the identifier.Examples of typedef DeclarationsThe following statements declare LENGTH as a synonym for int and then use this typedef to declare length, width, and height as integer variables:typedef int LENGTH;LENGTH length, width, height;The following declarations are equivalent to the above declaration:int length, width, height;Similarly, typedef can be used to define a class type (structure, union, or C++ class). For example:typedef struct { int scruples;int drams;int grains;} WEIGHT;The structure WEIGHT can then be used in the following declarations:WEIGHT chicken, cow, horse, whale;In the following example, the type of yds is "pointer to function with no parameter specified, returning int".typedef int SCROLL();extern SCROLL *yds;In the following typedefs, the token struct is part of the type name: the type of ex1 is struct a; the type of ex2 is struct b.typedef struct a { char x; } ex1, *ptr1;typedef struct b { char x; } ex2, *ptr2;Type ex1 is compatible with the type struct a and the type of the object pointed to by ptr1. Type ex1 is not compatible with char, ex2, or struct b.C++ The remainder of this section pertains to C++ only.In C++, a typedef name must be different from any class type name declared within the same scope. If the typedef name is the same as a class type name, it can only be so if that typedef is a synonym of the class name. This condition is not the same as in C. The following can be found in standard C headers:typedef class C { /* data and behavior */ } C;A C++ class defined in a typedef without being named is given a dummy name and the typedef name for linkage. Such a class cannot have constructors or destructors. For example:typedef class { Trees(); } Trees;Here the function Trees() is an ordinary member function of a class whose type name is unspecified. In the above example, Trees is an alias for the unnamed class, not the class type name itself, so Trees() cannot be a constructor for that class.External Links:You can find more questions with answers for C typedef from http:/www.indiabix.com/c-programming/question-answer.php?topic=nqvoiusqthttp:/www.indiabix.com/c-programming/index.php


How do you write a c program that define a structure linesegment that will represent a line segment by the co-ordinate of its endpoint?

There will a part like this: typedef struct Point { double x, y; } Point; typedef struct LineSegment { Point from, to; } LineSegment;


What is difference between c plus plus struct and c struct?

The internal members of a struct are the same for both C and C++. However, a C struct has no member methods and is entirely public. A C++ struct, on the other hand, is public by default but otherwise works exactly the same as a class.Aside from object-oriented nature of the C++ struct, the major difference between the two is in the declaration.In C, you could declare a struct in a variety of ways:struct foo {...}; struct foo bar;struct foo {...} bar;typedef struct foo {...} bar;The above three lines are all functionally equivalent (although you can't use all three in the same namespace, of course). However, the explicit use of typedef is the preferred method in C, thus allowing new instances of the structure to be created without the need to continually use the struct keyword:typedef struct foo {...} bar;foo another_bar;foo yet_another_bar;In C++ the use of typedef is not necessary, so the declarations can use the more simplified form:struct foo {...} bar;foo another_bar;foo yet_another_bar;Note that typedef is not implied, it's simply not required. You can still use a typedef if you want, but it's existence is mostly for backward compatibility with C. In fact, the only time a typedef is ever required is when the tag name or instance name clash with a function name in the same namespace. Although its ultimately better to avoid name clashes altogether, a typedef will resolve the clash.


What is the main use for type def seems to be defining structures?

When you define a structure, C does not provide a type for that structure. In order to subsequently declare an instance of that structure, you need to use the word struct again. The typedef allows you to declare a type equivalent to the structure. For example... struct person { char* name, int phone}; struct person myperson; With typedef, you can simplify to... typedef struct person { char * name, int phone} person;person myperson; In C++, this is automatic, but not in C.


User defined data type in c plus plus?

Use "typedef" : both in C and C++.


What is the difference between C and advanced C?

The difference between C and the advanced C is that C is basic. On the other hand, the advanced C is thorough and to the detail.


What is storage claases in c language?

They is static, auto, extern and typedef. (Well, typedef aint't an actual storage class, it means type-definition.)


Difference between C and C programming language?

The C and C programming languages are one and the same. There is no difference between those languages.


What is the difference between structure and structure using typedef with example?

Consider the following structure:struct data_t { /* ... */ };The name of this type is struct data_t. This means that we must include the struct keyword whenever we declare any instances of the type or declare a pointer to the type:struct data_t object;struct data_t* ptr;To remove the verbosity of the otherwise redundant structkeyword, we can use an alias:typedef struct data_t data;data object;data* ptr;To simplify things further, we can combine the alias with the structure's definition:typedef struct data_t { /* ... */ } data;Note that the _t suffix is conventionally used to denote a user-defined type as opposed to an alias. However, its usage is not consistent. For instance, the wchar_t type is not a type, it is implemented as an alias in the C standard library header:typedef unsigned short wchar_t;In C++, the typedef keyword is not required; it is retained purely for backward compatibility with C. Aliases are introduced with the using keyword, never with typedef. We only use typedef when we are explicitly writing code intended for compilation under both C and C++.Note also that wchar_t (amongst others) is a built-in type in C++, so we don't need to include the C standard library to make use of it, unless we are writing code for C.


What level of data is type of school?

It would be a user-defined type (UDT). In C, you would define a struct for it: typedef struct { char *name; unsigned int students; /* More fields */ } school;


What is difference between c and c?

nothing


What is the difference between c c?

ketchup!


What is the difference between solipsism and solipcism?

What is the difference between a C; and an S?


What is the difference between enumeration and set of preprocessor define in c?

Enums are compile time substituted constants whereas #defines are preprocessing substituted time constants


What is the difference between the c and C programming?

The question is similar to : What is the Difference between "English" and "Talking in English" "C" is a Language. "C Programming" is a verbal usage