Why doesn't typedef work here?

I know a simple typedef definition:

typedef is a keyword in C to assign alternative names to types.

Following this definition, I tried to implement typedef as follows:

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 the next error is
enter image description here

I know that this error means that I did not get a real concept of typedef. Can someone please tell me where I am going wrong?

+4
source share
4 answers

In contrast #define, it typedefis a mechanism for introducing a new name for a type, as opposed to textual substitution.

, long int, int long long - , C. typedef, , .

mylong x = 123;

: mylong . , int, ,

int mylong x = 123;

, int mylong . , , int float x = 5 struct mystruct int z = ....

+7

C, , int

, typedef long mylong; typedef long int mylong;.

:

int long int c;

, .

typedef - * ( long). int . mylong c; .

* , , . long int, .

+3

2 () 6.7.2 C

...

voidcharsigned charunsigned charshort, signed short, short int, or signed short intunsigned short, or unsigned short intint, signed, or signed intunsigned, or unsigned intlong, signed long, long int, or signed long intunsigned long, or unsigned long intlong long, signed long long, long long int, or
signed long long intunsigned long long, or unsigned long long intfloatdoublelong double_Boolfloat _Complexdouble _Complexlong double _Complex
— atomic type specifierstruct or union specifierenum specifiertypedef name

, C- typedef name .

+3

long/short.

typedef long mylong;

You said: typedef is a keyword in C to assign alternative names to types . " longnot really a type - it's a type. C just lets you leave when using , or so, your statement is equal to long intintshortlonglong long

typedef long int mylong;
+2
source

Source: https://habr.com/ru/post/1545981/


All Articles