answersLogoWhite

0


Best Answer

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

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

In The Defintion of a variable space is reserved for the and some initial value is given to it, whereas a declaration only identifies the type of the variable for the function . thus definition is the place where the variable is created or assigned storage whereas declaration refers to places where the nature of the variable is stated but no storage is allocated.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago
Answer

A 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 Declarations

The 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:


  1. You can find more questions with answers for C typedef from http:/www.indiabix.com/c-programming/question-answer.php?topic=nqvoiusqt
  2. http:/www.indiabix.com/c-programming/index.php
This answer is:
User Avatar

User Avatar

Wiki User

15y ago

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!!!.

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

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;

};

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

"typedef" is not a function, it is a keyword. It is primarily used to declare an alias, an alternate name for a type that already exists. This is typically done when the existing type is used extensively but is overly verbose. For example, an "unsigned int" is a fairly common type, but it's much quicker to type "uint" instead. To do so we need a type definition; a typedef:

typedef unsigned int uint;

From that point on we can use uint as a shorthand for unsigned int.

Type definitions are also used when declaring user-defined types. Consider the following:

typedef struct s {

// ...

};

int main() {

s x;

// use x...

return 0;

}

Note that if we had not included the typedef keyword when we declared the struct s, our main function would look like this:

int main() {

struct s x;

// use x...

return 0;

}

This is actually an example of one of the many inconsistencies in the C language.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

The term declaration is used when referring to variables. It means that the compiler is told about the type, name, as well as allocated memory cell for variable of a variable.

The later action of the compiler, allocation of storage is called the definition of the variable.

Example:

Declaration of variable:

extern int a;

Definition of a variable:

int a=5;

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

Although the terms are often used interchangeably, they have distinct meanings in both C and C++. To be more precise, they have distinct meaning to the compiler and linker.

A declaration is simply a statement to the compiler that a type exists while a definition fully describes a type to both the compiler and the linker. It is important to note that every definition is itself a declaration, but a declaration need not be a definition. In other words, it is impossible to define something that hasn't been declared, but you can declare something without defining it. A declaration without a definition is known as a "forward declaration"; a declaration of something that will (eventually) be defined somewhere.


It is also important to note that a type can only be defined once but can be declared more than once, provided those declarations are in separate translation units and are consistent with each other. Since there can only be one definition, that definition must reside in just one translation unit. To ensure all declarations of a type remain consistent, simply place the declaration in a header file and include that header file wherever it is required. It doesn't matter where the definition resides so long as there is exactly one definition in your program. Since headers may also contain definitions (and may include other headers with definitions), it is vital that you use "header guard" macros to ensure those definitions are only included once in every compilation.


The "one definition rule" (ODR) can be broken in one of two ways: either by not providing the definition of a type at all, or by providing two or more definitions for the same type. Both will be picked up by the linker.


Unlike C++, C generally doesn't care if a type is forward declared or not, just so long as it is declared somewhere in the translation unit(s) that use them.


The built-in types do not require either a declaration or a definition; they are defined by the language itself (hence they are built-in). This includes all the fundamental types: primitive data types (int, char, float and double); all modified primitive types (types declared with the signed, unsigned, long and short modifiers); all primitive array types and; all primitive pointer types.


The only type that need not be defined before being used is a function. The declaration of a function is enough for the compiler to determine the function's prototype: the type of the function (the return type) and the type of its argument (if any). So long as the declaration is visible to the translation unit(s) that use the function, the compiler can determine if calls to that function are well-formed. The definition of the function provides the implementation of that function and so long as a definition exists somewhere, the compiler will (eventually) find it and the linker will be able to link it.


Union, struct and enum types have no implementation details in C so a declaration suffices. (In C++, we must define struct methods, if any are declared, just as we must with all class types. A struct with no declared methods is treated the same as a C-style struct).


Typedefs are, by their very nature, always defined. They are type definitions after all. This includes type definitions provided by the C standard library (such as wchar_t) as well as all user-defined aliases.


Although we quite often use the term "declare a variable", variables are not declared they are simply instantiated. To be precise, a variable is an instance of an already declared type and memory is physically allocated to the variable according to that type. When a variable is instantiated, it is said to be in an uninitialised state. In reality, the value will be whatever value happens to reside at the memory address allocated to the variable, however most compilers will warn you if you attempt to use a variable that has not been explicitly initialised.


We also quite often use the term "define a variable" when we assign a value to the variable. However we are not defining the variable, we are either initialising the (uninitialised) variable or we are assigning a new value to the (initialised) variable. The type of the value must be the same as the variable type, or of a type that can be implicitly converted to that type by the compiler.


The same can be said of constants, the only difference being that we initialise constants at the point of instantiation since there is no such thing as an uninitialised constant.


Note that although we often use the terms "declare" and "define" interchangeably and there's nothing actually wrong with using terms such as "declare a variable", they have specific meanings with respect to the compiler.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

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

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

A typedef is a type definition.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is difference between define and typedef in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

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.


User defined data type in c plus plus?

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


What is the difference between private stafford and plus student loans?

What is the difference between private stafford and plus student loans?


What is the difference between pointers in c and c plus plus?

Nothing.


What is the difference between be plus ing and get plus ing?

There are no such terms in C++.


What is the difference between statements in c plus plus charconstp and char constp?

There is no difference. Both statements are invalid.


What is the difference between Nike plus and Nike plus Active?

They have different names


What is the difference between plus 8 and -12?

-4


What is the difference between DVD plus r and DVD plus rw?

rightable and rewightable


Difference between void and devoid in c plus plus?

There is no such thing as devoid in C++.


What is the difference between minus 4 and plus five?

9.


Calculate the difference between -2.00 and plus 1.75?

3.75 is the answer.