Hey peoples, this program is supposed to add two numbers n1 and n2 and output it to res
Code:
#include <stdio.h>
#include <stlib.h>
int addNumbers(int n1, int n2, int res);
int main(void)
{
int number1;
int number2;
int result = 0;
printf("Please enter the first number: ");
scanf("%d", &number1);
printf("Please enter the second number: ");
scanf("%d", &number2);
addNumbers(number1, number2, result);
printf("The result = %d\n", result);
return 0;
}
int addNumbers(int n1, int n2, int res)
{
return res = n1 + n2
}
I've checked the code with another example and it seems all is correct? however, res always ends up in equaling 0? am i missing a line of code, or is my program incorrect?
Supposedly there are two ways of solving a program like this:
1. Fix this by using pass by pointer variable instead of pass by value.
2. Change the addNumbers function so that it returns the result instead of passing it back via the pointer variable.
Also what do you guys use to code in c, I am using putty and i find it annoying to use.