+ 1

Hoisting in Ansi-C?

In JavaScript I can do something like this: console.log(hi()); function hi(){ return "hello" } So you can call the function before declaration because of the engines hoisting system. Here I saw, that this feature in C also work. printf("%d", sum(3,4)); int sum(int a, int b){ return a+=b; } So C is use hoisting?

25th Sep 2018, 9:37 AM
BraveHornet
BraveHornet - avatar
1 Odpowiedź
+ 1
C is a compiled language. Your code can reference external functions, but they are linked in a separate linker stage after all files are compiled. The code you provided can be compiled by the ancient K&R dialect of C. K&R C assumes that any function encountered without a prior declaration, returns an int. No error checking is done on the parameters you pass to the function, so they better match what the actual function expects. EDIT: The subject line says ANSI C, but I'm pretty sure this code would trigger warning on any respectable ANSI C compiler. EDIT 2: According to code playground, ANSI C accepts your code without any warnings. It simply does an implicit declaration of any new function as returning an int.
26th Sep 2018, 9:18 PM
Udi Finkelstein
Udi Finkelstein - avatar