4

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?

liori
  • 40,917
  • 13
  • 78
  • 105
  • If it were to be a declaration of a variable to store a function pointer, shouldn't it be int (*take_one_number)(void)? I think this is just declaration of function prototype inside a function. – Jay Mar 03 '13 at 03:59
  • @Jay: Yeah, I missed that a star is needed for a pointer-to-function variable. – liori Mar 03 '13 at 04:02
  • @lion: your comment "it's not a variable definition, star is missing!" for the accepted answer is not accurate at all, nor is your comment "a star is needed for a pointer-to-function variable". To be a pointer to a function, you'd need `int (*take_one_number)(void);`, which requires two parentheses and a star. Then you'd have problems trying to define the same variable multiple times,even though the variable is unused. The code is sub-optimal, shall we say. It should check that the `scanf()` calls succeed, for example. The braces are superfluous after the `take_one_number()` declarations. – Jonathan Leffler Mar 03 '13 at 05:03
  • And the range of possible return values on Unix is 0..255 (1 byte), so `return all;` will often not return the status you think it will return (and non-zero return statuses are normally regarded as 'command failed'). – Jonathan Leffler Mar 03 '13 at 05:05
  • @JonathanLeffler: yes, yes, i know, i know. This is not my code, it's code from a student a friend is teaching. We know it has other faults, we were stumped on the declaration vs. definition issue. Now we wonder what led the student to writing this code… – liori Mar 03 '13 at 05:18
  • That is the great unanswerable — why? Confusion, but it is very difficult to guess what the source of the confusion is. It can probably only be addressed by asking the student 'why', and then working from that answer. It would be a good idea to do the asking. – Jonathan Leffler Mar 03 '13 at 05:21

4 Answers4

4

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

Mark Taylor
  • 1,843
  • 14
  • 17
3

In the above code ,

 int take_one_number (void);

is not a function pointer , it is function prototype or declaration , a function can be declared more than once , but must be defined only once.

Barath Ravikumar
  • 5,658
  • 3
  • 23
  • 39
2
int take_one_number(void);

It's a function declaration whose return type is int. It's not a definition of a variable. And the scoping you did for variables has little meaning here as no variable declaration is happening in it.

Mahesh
  • 34,573
  • 20
  • 89
  • 115
2

int take_one_number(void); is a function prototype to tell the compiler that there is a function implemented somewhere with this name and property. Compiler does not complain because you are neither defining new function nor using that function.

You should also note that the blocks that follow after this prototype are not part of the take_one_number as they are separated with semi-colon. Making the blocks as independent scope blocks. To make things more clearer there would be compilation error if there were no semi-colon between the function prototype and the code block next to it.

hmatar
  • 2,437
  • 2
  • 17
  • 27
  • More informantion on function prototypes and whether you must declare refer here http://stackoverflow.com/questions/2575153/must-declare-function-prototype-in-c – hmatar Mar 03 '13 at 03:58