Is it right or wrong ?(solved) | Sololearn: Learn to code for FREE!

+9

Is it right or wrong ?(solved)

int add(int a,int b) { return a+b; } void main() { int w,x,y,z,sum; w=3; x=4; y=5; z=6; sum = add(a,add(b,add(c,d)); printf("%d",sum); }

c

12/5/2021 5:17:45 PM

ЀÊpă[email protected]ï

14 Answers

New Answer

+8

You can see in playground . Are you tried it? edit: //may you not understanding, errors. here is corrected one. ЀÊpă[email protected]ï #include <stdio.h> int add(int a,int b) { return a+b; } int main() { int w,x,y,z,sum; w=3; x=4; y=5; z=6; sum = add(w,add(x,add(y,z))); //error correction : you declared w,x,y,z variables but using a,b,c,d. Correct way is above. printf("%d",sum); return 0; } /* variables declared in a function, is local variables to function and exits within function. ex: **w, x, y, z, are local variables in main() function **a, b are local variables in add() function. **reply if anything is not understood.. hope it helps... */

+8

Talking about the execution of your code first thing If you will use modern compiler gnu gcc ,llvm , clang then it will give you errors because implicit main was removed in c99 as long years ago its deprecated and cpp is more strict than so may be in c it can work but in cpp its completely error . You can write int main() instead of void main if you using clang then u can write int main with command line arguments or simply int main it will also work if you are on mac then U can write like _tmain(command line arguments) With return type main u need to use return statement at the end of program its depend on your return type for more details you can search on Google In cpp there u can define like this auto main() ->int this will also work fine. Then inside body you have defined four variables w ,x ,y,z and when u calling your add function here you passing two arguments c and d which are undefined variable instead of this u need to pass previously decleared variable ,z ,x ,y, or z then the add

+6

ЀÊpă[email protected]ï wrong Since ' a , b, c, d' is not define

+6

Function will call and inside the function body u calculating sum which will return a value then this return value will treat as another function calling parameters add(b, return value) Same thing will happen again and your function will call total three times

+2

W x y z should be used in void main during call instead of a b c d and include<stdio.h>

+1

If someone considers this 'right' if it just runs - replace the sum line with: sum = add(w, add(x, add(y, z))); Note not just the variable changes from a,b,c,d to w,x,y,z but also the extra bracket!

+1

Parth Chauhan Bro there is no need to use int because here main function is not returning any value.

+1

Wrong A, b, c, d is not defined in this code

+1

Parth Chauhan The function type is the returned value type. Printing, or outputting in any way, is not returning. The only statement that returns is "return" itself. If the function doesn't return a value, its type should be void. That said, it's a good practice for the main function to return an integer indicating execution status. Always useful for the caller.

0

ЀÊpă[email protected]ï you have to pass only declared variable in main function, and here a,b,c and d are not declared in main function. Instead of a,b,c,d u can pass w,x,y and z . then it will work.

0

Is it right or wrong ?(solved) int add(int a,int b) { return a+b; } void main() { int w,x,y,z,sum; w=3; x=4; y=5; z=6; sum = add(a,add(b,add(c,d)); printf("%d",sum); } no the above code you done it will be give you error becouse the variables like (a,b) is local scope variable and also the variables of (c,d) are not declored you can fix this problems using this way Is it right or wrong ?(solved) int add(int a,int b) { return a+b; } void main() { int w,x,y,z,sum; w=3; x=4; y=5; z=6; sum = add(w,add(x,add(y,z)); printf("%d",sum); }

-1

So at first I wish you to use meaningful names, what is w x y z, what is a b? Why it is so hard to add some sense in names? You should understand what you are doing now, this is not good code, you should add comments and use variables with arguments. U can write int w = 3; Intead of that strange defining them all as int first. That's why you can lose it in your next code, and, of course, why so strange structure? Why your add function is above then main? What do you want to do? That's all is strange :D so people before me had said what is wrong with your code yet, but you should learn to use structure and meaningful variables, this is my advice~

-2

No bro in main function you are outputting the integer value so you have to put int in place of void

-2

#include<stdio.h> int main() { int marks[11] = { 1,2,3,4,5,6,6,7,7,8}; int i; for (i=0; i<10; i++) printf("%d\n", marks[i]); return 0; }