+ 1

I can't understand how the above code works .

can anyone explain me ? because I didn't declared the function at the beginning https://code.sololearn.com/coT4ptg1K4Zo/?ref=app

17th Nov 2018, 6:05 AM
M A Mohmad Ashik
M A Mohmad Ashik - avatar
8 Réponses
+ 6
#include <stdio.h> // ggg needs to have a prototype here void ggg(int *, int *); int main() { // main() must return int int p = 3, s = 5; ggg(&p, &s); // the formal parameters of ggg must be (int *) printf("p = %d s = %d", p, s); // getch(); Mustn't add to any code! return 0; } void ggg(int *a, int *g) { // ggg's return type is set implicitely to (int) -- it must be (void) *a = *a + *a; // for performing arithmetic operations, pointers need to be dereferenced first *g = *g + *g; }
17th Nov 2018, 6:24 AM
Babak
Babak - avatar
+ 5
We are in 2018, baby! Leave the damn thing! 8D
17th Nov 2018, 6:41 AM
Babak
Babak - avatar
+ 5
The function prototype is a function declarator which informs the compiler "beforehand" about existing a "blueprint" of the actual definition of the function. The need for having a prototype can be eliminated by transferring the whole body of the definition "before" the main like so #include <stdio.h> void ggg(int *a, int *g) { *a = *a + *a; *g = *g + *g; } int main() { int p = 3, s = 5; ggg(&p, &s); printf("p = %d s = %d", p, s); return 0; } the C11 standard says: "A function prototype is a declaration of a function that declares the types of its parameters. [...] " the above quote conveys that pre-standard compilers -- which don't conform with the new one -- had a different kind of treatment for function prototypes. That's why to avoid such confusions you have to reconsider your taste!
17th Nov 2018, 7:15 AM
Babak
Babak - avatar
+ 4
Whoa do you know how old turbo compiler is ? Its discontinued in 1994 😂 ( Even after revived in 2006 the project stopped again in 2009(cmiiw) The original turbo are more than 2 decades old And the new one almost a decade C/c++ standard are keep updated overtime
17th Nov 2018, 6:36 AM
Taste
Taste - avatar
+ 4
Code works in Dcoder on Android. It times out in SL (not-so) Playground
17th Nov 2018, 7:07 AM
David Carroll
David Carroll - avatar
+ 2
Yes you're right, main cant see your function like that. And you forget the return value in your function like void ggg(int a,int b)
17th Nov 2018, 6:15 AM
Taste
Taste - avatar
+ 2
Bro without declaring the prototype this code is running in my turbo c compiler
17th Nov 2018, 6:30 AM
M A Mohmad Ashik
M A Mohmad Ashik - avatar
+ 1
To make it clear, we're not talking about turbo like a dead meme or outdated fashion. But its about the technology, it's growing so fast that every language need to update their standard and features overtime, including removing something that was used a lot, so its getting faster and better for any new and old computer architecture
17th Nov 2018, 7:00 AM
Taste
Taste - avatar