+ 4

Why this code is working without any error and without any function definition.

https://code.sololearn.com/c654DnNsvaWY/?ref=app

15th Oct 2020, 2:48 AM
Ashutosh k.
Ashutosh k. - avatar
11 Réponses
+ 6
hello 👋 my question is why this code working " WITHOUT FUNCTION DEFINITION " ???????
15th Oct 2020, 3:43 AM
Ashutosh k.
Ashutosh k. - avatar
+ 6
hello coffee ☕ cup girl excuse me
15th Oct 2020, 3:44 AM
Ashutosh k.
Ashutosh k. - avatar
+ 5
but by the rules it should be above the main function coffee girl
15th Oct 2020, 3:50 AM
Ashutosh k.
Ashutosh k. - avatar
+ 3
U have define function properly see step by step where u write main function here u calling function in next steps after Variables decleration see this line a=func1();
15th Oct 2020, 3:48 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 3
Ashutosh k. The thing you write above the main function is the "prototype" and it is not required, while the "declaration" with the relative body is necessary. The compiler do its job from the top, down to the bottom of the code. The prototype gives the compiler a way to check if you are using your function in the proper way. Let's say that you do not write any prototype before the main. Now if you declare a function (after the main) that recieves an int as argument, but in the main you used this function giving it a char value, then the compiler wouldn't tell you anything, and yet your program would not function properly. So you use prototypes for your own sake, not because the program requires them in order to be executed. If you use the prototype, it needs to be coherent with the declaration in number, type and order of arguments and in returned type. Otherwise the compiler will give you errors.
15th Oct 2020, 8:21 PM
Davide
Davide - avatar
+ 2
Checkout i fixed your bug may be it will work without any errors #include<stdio.h> #define row 3 #define col 4 int main() { int i,j,*a,*fun1(); a=fun1(); printf("array a[][] in main():\n"); for(i=0;i<row;i++) { for(j=0;j<col;j++) { printf("%d ",*(a+i*col+j)); printf("\n"); } } return 0; }////---main int *fun1() { static int a[4][3]={{1,2,3,},{4,5,6},{7,8,9},{0,1,6}}; int i,j; printf("Array a[][] in fun 1():\n"); for(i=0;i<row;i++) { for(j=0;j<col;j++) printf("%d ",a[i][j]); printf("\n"); } return (int*)a; }///----fun 1
15th Oct 2020, 3:25 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
Ashutosh k. Rule says "declare it before you using". In your code you declared int *fun1(); before you using so no error. If you write before main function and since you always use it main so it's already declared and defined so no problems in linking... If you write after main, then atleast you have add prototype before main because compilation happens from top to bottom and all declaration pushed to stack then usages are bind to declaration at compile time so if no prior declaration then it give you warning eventhough binding happens at runtime but not give surity if ambiguity.. So rule tells add prototype or declare before using it. You have function pointer declaration so it is similar as prototyping. So it is error free. Edit : (you have other warning and given all removed warnings code already by @♨️TEJAS MK-82♨️. So I adding to that one)
15th Oct 2020, 9:51 PM
Jayakrishna 🇮🇳
+ 2
ranim you need to create a new question rather than ask something in the comment section of another question. And you are more likely to get answers if you write it in English.
16th Oct 2020, 2:47 PM
Davide
Davide - avatar
+ 1
the compiler generates the proper code, seems to be
16th Oct 2020, 1:33 PM
Kiwwi#
Kiwwi# - avatar
+ 1
Hi! This is my first day in that app , so hoping that you are guys be nice to me . I have a question , comment écrire un programme qui permet de saisir une capacité CD en Octet puis la convertir en GO et en MO
16th Oct 2020, 2:33 PM
ranim
+ 1
#include<stdio.h> #define row 3 #define col 4 int main() { int i,j,*a,*fun1(),*p; a=fun1(); printf("array a[][] in main():\n"); for(i=0;i<row;i++) { for(j=0;j<col;j++) printf("%d ",*(a+i*col+j)); printf("\n"); } return 0; }////---main int *fun1(include ) { static int a[row][col]={1,2,3,4,5,6,7,8,9,0,1,6}; int i,j; printf("Array a[][] in fun 1():\n"); for(i=0;i<row;i++) { for(j=0;j<col;j++) printf(fun1 ",a[i][j]); printf("\n"); } return (int*)a; }///----fun 1
16th Oct 2020, 4:32 PM
gaming Faruk
gaming Faruk - avatar