answersLogoWhite

0

What is typedef declaration?

Updated: 8/10/2023
User Avatar

Wiki User

13y ago

Best Answer

The typedef keyword allows the programmer to create new names for types such as int or, more commonly in C++, templated types--it literally stands for "type definition". Typedefs can be used both to provide more clarity to your code and to make it easier to make changes to the underlying data types that you use.

User Avatar

Wiki User

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

Wiki User

13y ago

It's meant as an alias for another type; it does not create a new type, however.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

typedef is used to create user defined data types

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is typedef declaration?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How typedef feature used with structure?

You use typedef to declare a synonym for an existing type. It's generally just a way of reducing a complex or cumbersome declaration outside your code to a more simplified, more easily understood declaration that you can use inside your code.Cumbersome example:void DoStuff( void (*)(int&, char& ), int&, char&); // Huh? Do what?Simplified example:typedef void (*pFunc) ( int&, char& );void DoStuff( pFunc, int&, char& ); // Aha! It's a function pointer!In relation to C structures, typedef provides a way to declare and name user-defined types, primarily so you don't have to use the struct keyword in the variable declaration. C++ structures are more flexible and the typedef keyword is optional.Structure examples for C:struct hard{ int i;double f;};// typedef is optional in C++, but required in C.typedef struct{int i;double f;} easy;int main(){struct hard hs; // Requires struct keywordeasy es; // Same as C++.}


What is the difference between a macro and typedef?

A Macro is a preprocessor directive means that before compilation the macros are replaced. Where as typedef is defining a new data type which is same as the existing data type. Syntax: typedef Existing datatype New datatype For example typedef int NUMBER; Here NUMBER (New datatype)is defined as a data type which contains the properties same as int(Existing datatype). You can declare a variable of int as NUMBER a; is same as int a; similarly typedef int* NUMBERPOINTER; NUMBERPOINTER a; Here a is a pointer of integer type.


What is meant by the storage class of a variable?

auto, extern, static, register, typedef (only formally)


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 difference between define and typedef in c plus plus?

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.

Related questions

What is the use of typedef?

The purpose of typedef is to assign alternative names to existing types, most often those whose standard declaration is cumbersome, potentially confusing, or likely to vary from one implementation to another


How typedef feature used with structure?

You use typedef to declare a synonym for an existing type. It's generally just a way of reducing a complex or cumbersome declaration outside your code to a more simplified, more easily understood declaration that you can use inside your code.Cumbersome example:void DoStuff( void (*)(int&, char& ), int&, char&); // Huh? Do what?Simplified example:typedef void (*pFunc) ( int&, char& );void DoStuff( pFunc, int&, char& ); // Aha! It's a function pointer!In relation to C structures, typedef provides a way to declare and name user-defined types, primarily so you don't have to use the struct keyword in the variable declaration. C++ structures are more flexible and the typedef keyword is optional.Structure examples for C:struct hard{ int i;double f;};// typedef is optional in C++, but required in C.typedef struct{int i;double f;} easy;int main(){struct hard hs; // Requires struct keywordeasy es; // Same as C++.}


Typedef int a a a Is this possible in C?

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


What are the Uses of typedef?

the purpose of typedef is to redefine the name of an existing variable type. e.g. typedef unsigned long int T; T v1,v2;


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 do we represent typedef in enterprise architect tool?

create class with typedef construct.and then add the base class with the name type


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.


Use of typedef?

You use typedef to give a different name to a current data type. Consider the following: typedef int integer; Now everytime you do "integer x", it'll be an int. Basically, there is no case where you _must_ use it, only cases where it might be easier to have typedefs.


What is the difference between a macro and typedef?

A Macro is a preprocessor directive means that before compilation the macros are replaced. Where as typedef is defining a new data type which is same as the existing data type. Syntax: typedef Existing datatype New datatype For example typedef int NUMBER; Here NUMBER (New datatype)is defined as a data type which contains the properties same as int(Existing datatype). You can declare a variable of int as NUMBER a; is same as int a; similarly typedef int* NUMBERPOINTER; NUMBERPOINTER a; Here a is a pointer of integer type.


What is meant by the storage class of a variable?

auto, extern, static, register, typedef (only formally)


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 difference between define and typedef in c plus plus?

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.