Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 209
liori Avatar asked Dec 21 '22 09:12

liori


1 Answers

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 { }

like image 110
Mark Taylor Avatar answered Dec 24 '22 02:12

Mark Taylor