Typedef and variable names

Ignoring why I would like to do this, just trying to understand what is happening here: This code compiles:

#include <stdio.h>
typedef char byte;

int main (void) 
{
    byte var_byte;
    int byte = 10;

    printf("\n Test program: %d\n", byte);
}  

But if I change the order in which the variables are declared, it does not compile.

This does not amount to:

#include <stdio.h>
typedef char byte;

int main (void)
{
    int byte = 10;
    byte var_byte;

    printf("\n Test program: %d\n", byte);
}

Compiler Error:

b.c:7:8: error: expected ‘;’ beforevar_bytebyte var_byte;
        ^~~~~~~~

Can someone explain why order matters?

+4
source share
2 answers

In this program

#include <stdio.h>
typedef char byte;

int main (void)
{
    int byte = 10;
    byte var_byte;

    printf("\n Test program: %d\n", byte);
}

the variable name bytehides the typedef name.

From Standard C (6.2.1 Identifier Areas)

  1. ... , . , ( ) ( ). , ; ( ) .

, typedef .

typedef ( ), , , .

.

#include <stdio.h>
typedef char byte;

void f( void );

int main (void)
{
    int byte = 10;

    printf("\n Test program: %d\n", byte);

    f();
}

void f( void )
{
    byte c = 'A';
    printf( "%c\n", c );
}

main ( ) typedef .

f , typedef, , , typedef.

, ( ++)

#include <stdio.h>

size_t byte = 255;

int main(void) 
{
    typedef int byte[byte];

    {
        byte byte;

        printf( "sizeof( byte ) = %zu\n", sizeof( byte ) );
    }

    return 0;
}

sizeof( byte ) = 1020

​​ byte

size_t byte = 255;

main typedef name byte.

typedef int byte[byte];

byte . typedef

    typedef int byte[byte];

byte byte.

byte, typedef.

byte byte;

,

sizeof( byte )

, typedef.

+7

: ( )

C, typedef, , .

byte var_byte; 

int byte. , .

int byte , C. , , , , ​​

+1

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


All Articles