Opentopia Directory Encyclopedia Tools

Typedef

Encyclopedia : T : TY : TYP : Typedef


The correct title of this } is }}}. The initial letter is capitalized due to [Naming conventions #Lower case first lettertechnical restrictions].
typedef is a keyword in the C and C++ programming languages. It is used to give existing datatypes new names to make a program more readable to the programmer.''

Consider this code:

#include
void main()

Now consider this :

#include
void main()

Both sections of code do the same thing: creates an int datatype (marks) and gives it a value of 100. The method of doing this in section two makes it easier to read because the typedefine statement causes student_mark_t to mean the same thing as int. In these examples, the variable marks stores the "marks" (or grades) of a student, so defining marks as a variable of type student_mark_t gives the variable name context.

One more example:

struct var
;
Here a USER DEFINED datatype var has been defined. Thus, to create a variable of the type var, the following code is required:

struct var a; 

Let's add the following line to the end of this example:

typedef struct var newtype;
Now, in order to create a variable of type var, the following code will suffice:

newtype a;
This is much easier to read because the keyword struct does not need to precede every variable of type var.

It is also possible to declare typedefs for arrays.

typedef BaseType NewType [arrSize]
Doing this it is possible to declare a new array of type BaseType and size arrSize by writing:

NewType array;

External links

 


From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.

Search Titles
0123456789
ABCDEFGHIJ
KLMNOPQRST
UVWXYZ?

E-mail this article to:

Personal Message: