Home > database >  How do I add numbers in C again?
How do I add numbers in C again?

Time:03-28

Seriously, I don’t remember..

Would an integer work?? But I don’t know why its not working...

I'm trying to add integers, like this.

int main() {
   i = 1;
   b = 3;
}
 Signed int addition() {
i   b

}

CodePudding user response:

You can't use functions and variables, including local variables, parameters, etc before they have been declared first. Though, you can initialize variables at the same time you declare them. For example:

#include <iostream>

int addition(int a, int b);

int main() {
   int i = 1;
   int b = 3;
   int sum = addition(i, b);
   std::cout << sum;
}

int addition(int a, int b) {
    return a   b;
}

CodePudding user response:

You have a lot of syntax errors in that code. Until you are more comfortable with programming, just start with a simple working "Hello world" example, and add one line of code at a time, compiling after each line you add. Look carefully at the error messages the compiler gives you and fix them before adding another line. You might arrive at something like this:

int main() {
  int i = 1;
  int b = 3;
  return i   b;
}

When you run this and check the process return code (e.g. using$? in Bash) then you would get 4.

CodePudding user response:

It should not be hard.

// [...]
int Add(int x, int y) { return x   y; }

using namespace std;
int main(void) {
  int a = 1, b = 2;
  printf("%d\n", Add(a   b));
  return 0;
}
  •  Tags:  
  • c
  • Related