Search code examples
cvariablesfunction-pointersidentifier

Duplicated identifier for a variable in function local scope


So, this is code from a student of my friend…

#include <stdio.h>

int main(){
    int hours;
    int take_one_number(void);{
        scanf("%d",&hours);
    }
    int minutes;
    int take_one_number(void);{

        scanf("%d",&minutes);
    }
    int seconds;
    int take_one_number(void);{

        scanf("%d",&seconds);
    }
    int all;
    printf("%d",all=hours*3600+minutes*60+seconds);
    return all;

}

Well, it… compiles… and… uhm, works… as requested by the teacher…

My question: if I understand correctly, take_one_number here is a definition of a variable to store a function pointer. Why does neither GCC nor LLVM complain about a duplicated identifier in these definitions?


Solution

  • The function take_one_number is declared 3 times, but never defined. In each case, the ; after the (void) ends the declaration. The scanf statement is then just a regular statement inside of main(), surrounded by a meaningless scope { }