0

Here's my program and I am getting syntax errors that I don't understand. This is my main that will accept my function data:

int main() //main program
{
    float num1, num2, total; 
    printf("Enter first number:  ");
    scanf("%f", &num1);
    printf("Enter Second number:  ");
    scanf("%f", & num2);
    total = multiNumbers(num1, num2);
    printf("sum is %2.f", total);   
    return 0;
}

int multiNumbers(int num1, int num2) {
    int sum;
    sum = num1 * num2;
    return sum;
}

Here are the compilation errors:

In function 'int main()':
[Error] 'printf' was not declared in this scope
[Error] 'scanf' was not declared in this scope
[Error] 'multiNumbers' was not declared in this scope
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    Is this your whole source-code file? where IS "multiNumbers"? –  May 11 '17 at 23:49
  • i put my function below.. Thats not the lines with the syntax – April Renee May 11 '17 at 23:52
  • You should post the whole code as explained in [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) -- nevertheless, it seems obvious now –  May 11 '17 at 23:54
  • Side note - you should change all types in `multiNumbers` to `float` instead of `int` if you want to have the correct multiplication result.. – SHG May 12 '17 at 00:14
  • @SGH, nice catch, but then just go for `double` (for a better chance of correct results). `float` is probably the most nonsensical datatype in C on most platforms. –  May 12 '17 at 00:21
  • [Printf was not declared in this scope](http://stackoverflow.com/q/38560419/1441), [Function not declared in this scope](http://stackoverflow.com/q/23327630/1441) (...I could keep going) – crashmstr May 12 '17 at 14:48

4 Answers4

3

[Error] 'printf' was not declared in this scope

[Error] 'scanf' was not declared in this scope

Add stdio library in your code

#include <stdio.h>

[Error] 'multiNumbers' was not declared in this scope

Add a declaration before main function

 int multiNumbers(int num1, int num2);

Code Snippet:

#include <stdio.h>
int multiNumbers(int num1, int num2);
int main() //main program
{
    float num1, num2, total; 
    printf("Enter first number:  ");
    scanf("%f", &num1);
    printf("Enter Second number:  ");
    scanf("%f", & num2);
    total = multiNumbers(num1, num2);
    printf("sum is %2.f", total);   
    return 0;
}
int multiNumbers(int num1, int num2) {
    int sum;
    sum = num1 * num2;
    return sum;
}
Community
  • 1
  • 1
ayusha
  • 474
  • 4
  • 12
2

[Error] 'printf' was not declared in this scope
[Error] 'scanf' was not declared in this scope

printf and scanf are declared in stdio.h, so you need to include this file first:

#include <stdio.h>

[Error] 'multiNumbers' was not declared in this scope

If this function isn't declared or defined above your main function, you must at least add a declaration like this above main:

int multiNumbers(int num1, int num2);

(just this line)

1

You must at least declare the function before you call it. A function definition also serves as a declaration, so the easiest thing to do is move the body of multiNumbers before main.

Edit

int multiNumbers(int num1, int num2) {
  int sum;
  sum = num1 * num2;
  return sum;
}

int main() //main program
{
    float num1, num2, total; 
    printf("Enter first number:  ");
    scanf("%f", &num1);
    printf("Enter Second number:  ");
    scanf("%f", & num2);
    total = multiNumbers(num1, num2);
    printf("sum is %2.f", total);   
    return 0;
}

One problem that the compiler will complain about - your types don't match. You've declared multiNumbers to take int parameters and return an int value, but you're passing float values and assigning the result to a float.

That's not going to work.

If you intend multiNumbers to work with float values, then you need to change the types of num1, num2, and sum to float (although honestly, I'd recommend using double instead of float).

John Bode
  • 119,563
  • 19
  • 122
  • 198
1

You must first

#include <stdio.h>

and you must also put the functions prototype before main().

#include <stdio.h> // You forgot this

int multiNumbers(int num1, int num2); // This is the function's prototype

int main() //main program
{
    float num1, num2, total; 
    printf("Enter first number:  ");
    scanf("%f", &num1);
    printf("Enter Second number:  ");
    scanf("%f", &num2);
    total = multiNumbers(num1, num2);
    printf("sum is %2.f", total);   
    return 0;
}

int multiNumbers(int num1, int num2) {
    int sum;
    sum = num1 * num2;
    return sum;
}
Archmede
  • 1,592
  • 2
  • 20
  • 37