Reputation: 2788
I know simple definition of typedef :
typedef is a keyword in C to assign alternative names to types.
Following this definition I tried to implement typedef as following :
int main()
{
typedef long mylong; //as per my knowledge after this statement mylong will be treated as long
int long b; // this works fine
int mylong c; // but this gives error
}
I tried this on gcc. And following is the error
I know this error means I didn't get actual concept of typedef. Can anybody please tell me where I am wrong?
Upvotes: 4
Views: 3581
Reputation: 310950
According tp paragraph 2 (Constraints) of section 6.7.2 Type specifiers of the C Standard
Each list of type specifiers shall be one of the following multisets...
— void
— char
— signed char
— unsigned char
— short, signed short, short int, or signed short int
— unsigned short, or unsigned short int
— int, signed, or signed int
— unsigned, or unsigned int
— long, signed long, long int, or signed long int
— unsigned long, or unsigned long int
— long long, signed long long, long long int, or
signed long long int
— unsigned long long, or unsigned long long int
— float
— double
— long double
— _Bool
— float _Complex
— double _Complex
— long double _Complex
— atomic type specifier
— struct or union specifier
— enum specifier
— typedef name
As you see the C Standard does not allow to combine typedef name
with other type specifiers.
Upvotes: 3
Reputation: 170065
When you omit the type in C
, it's assumed int
So typedef long mylong;
is the same as typedef long int mylong;
.
Making the offending line be something like this:
int long int c;
Hence the error.
The typedef is a new type* (not text substitution for long
). So you don't need to add an int
to make a variable of that type. A simple mylong c;
will suffice.
*Well, it's a bit more involved than that. The new type is in fact the same as a regular long int
, and the two are interchangeable. But for sound domain logic, you should treat it as a new seperate type
Upvotes: 3
Reputation: 726529
Unlike #define
, typedef
is a mechanism for introducing a new name for a type, as opposed to a textual substitution.
Recall that types long int
, int long
, and long
are three synonyms that refer to the same C type. When you use typedef
, you make another synonym referring to that same type.
When you use it like this
mylong x = 123;
the usage is correct: mylong
is used like a name of a type. However, when you try using it in combination with int
, like this
int mylong x = 123;
the compiler reports an error because int mylong
does not name a valid type. To the compiler it looks the same as if you wrote, say int float x = 5
or struct mystruct int z = ...
.
Upvotes: 7
Reputation: 10417
You misunderstand long
/ short
.
typedef long mylong;
You said "typedef is a keyword in C to assign alternative names to types." long
is actually not type - long int
is type. C just allows you to leave out int
when you're using short
, long
or long long
So your statement is equal to
typedef long int mylong;
Upvotes: 2