- 1
Pls anyone help me fix this code in c
#include <studio.h> int add (x,y); int x,y,z; { z = x+y; return(z) } int main() { int a,b,c; a = 5; b = 10; c = add(a,b) printf("Sum of %d and %d is %d",a,b,c); }
12 odpowiedzi
+ 3
#include <stdio.h>
//Function declaration
int add(int x,int y);
int main()
{
int a,b,c ;
a=5;
b=10;
c=add(a,b);
printf("Sum of %d and %d is %d\n",a,b,c);
}
//Function definition
int add(int x,int y)
{
int z;
z = x+y ;
return(z);
}
+ 6
1. why there's a semicolon in function add? and also you didnt put the data types in parameters.
2. redefinition of x and y variable and the place of declaration is invaild. so remove "int x,y,z" and put int z inside the brackets.
3. no semicolon in update c variable! and also in return(z) in function add.
4. in the prinf, rearrange the variable order to c, a, b
+ 6
Varchas ,
sorry to say, but there are too many issues in this code. i would recommend you to go back some steps, work through the tutorial and do some simple exercises. you also should learn to debug and fix issues, which is a key knowledge for each coder. if you have problems with a code, break down the task in small pieces and try to solve the problems.
happy coding and good success!
+ 2
Bro see his profile he is a beginner so i think we have to correct his code (by which he understand where he is doing mistake) instead of showing the complex form of that program.
I know it is important for him to understand complex programs but because he is beginner that's why i think so.
+ 1
This is just a misunderstanding. I got kinda overzealous in my answer, but the major bulk of it came from comments not code. Here's the program that's unique to me:
//
include <stdio.h>
int main()
{
int a = 5, b = 10;
printf("Sum of %d and %d is %d", a, b, a + b);
}
//
It's nothing that he didn't already display knowledge of in his answer.
+ 1
Where is method body? Why you put semicolon after defining method? And you called method in main without semicolon. Int main() should return something.
+ 1
/*#include <stdio.h>
//Function declaration
//int add(int x,int y);
int add(int x,int y)
{
int z;
z = x+y ;
return(z);
}
int main()
{
int a,b,c ;
a=5;
b=10;
c=add(a,b);
printf("Sum of %d and %d is %d\n",a,b,c);
}
*/
//Another way
#include <stdio.h>
int add(int x, int y);
int main()
{
int a,b,c ;
a=5;
b=10;
c=add(a,b);
printf("Sum of %d and %d is %d\n",a,b,c);
}
int add(int x,int y)
{
int z;
z = x+y ;
return(z);
}
These are the correct code you can use first which is commented and second which is not, there were some issues in your code like semicolon, parameters passing and function add not defined properly.
0
Varchas
You have to practice more and more on this language.
By the way your program after removing issue is look like this
#include <stdio.h>
int add (int x,int y)
{
int z;
z = x+y;
return(z);
}
int main()
{
int a,b,c;
a = 5;
b = 10;
c = add(a,b);
printf("Sum of %d and %d is %d",a,b,c);
}
0
Are you trolling? If you're not, here's three versions of the code that will compile and work as intended.
#include <stdio.h>
int add (int x, int y)
{
return x + y; /* A semicolon is required after every expression and statement in C, plus this function only needs to be one line. */
}
int main()
{
int a = 5, int b = 10, int c = add(a,b); /* A semicolon is required after a function call, plus this statement only has to be on one line. */
printf("Sum of %d and %d is %d", a, b, c); /* I saw someone say you ordered this statement wrong, but you were right here. */
}
Or you could drop the add function all together and get the exact same result with less lines.
#include <stdio.h>
int main()
{
int a = 5, b = 10; /* No add() function is needed. You don't even need to declare an extra variable. */
printf("Sum of %d and %d is %d", a, b, a + b); /* Just add the two over here. */
}
0
Thank You For Your Help Everyone
Here is my final product 🤗
Hope you give it an upvote, it would mean a lot
thnx
https://code.sololearn.com/ca23a18A16A1
- 1
ကိ
- 2
kkzs