Naxs
Naxs

Reputation: 11

Error was not declared in this scope

I'm making this simple code where if I put two numbers and it shows the solution. This is the code:

#include <stdio.h>

int main()
{
    printf("Enter two numbers:")
    ;scanf("&d &d", a, b)
    ;printf("=======================\n"); /*  */
    /*  */
    printf("The sum of %d and %d is %d.\n\n", 3, 4, 3 + 4)
    /*  */
    ;printf("The difference of %d and %d is %d. \n\n", 3, 4, 3-4)
    /*  */
    ;printf("The product of %d and %d is %d. \n\n", 3, 4, 3*4)
    /* sum of squares would be a*a + b*b */
    ;printf("The sum of the squares of %d and %d is %d. \n\n", 3, 4, 3*3 + 4*4)
    ;printf("**end**"); /* :-) */ 
    ;return (0);
}

and I keep getting an error that says:

[Error] 'a' was not declared in this scope 
and
[Error] 'b' was not declared in this scope

What's wrong with it?

Upvotes: 0

Views: 3691

Answers (3)

tylerism
tylerism

Reputation: 679

You need to declare the variables before you try to store anything in them.

int  a,b;

That line needs to be the first line in your main function.

EDIT: Make SURE your code looks like this?

int main()
{
    int  a,b;
    printf("Enter two numbers:")
    ;scanf("&d &d", a, b)
    ;printf("=======================\n"); /*  */
    /*  */
    printf("The sum of %d and %d is %d.\n\n", 3, 4, 3 + 4)
    /*  */
    ;printf("The difference of %d and %d is %d. \n\n", 3, 4, 3-4)
    /*  */
    ;printf("The product of %d and %d is %d. \n\n", 3, 4, 3*4)
    /* sum of squares would be a*a + b*b */
    ;printf("The sum of the squares of %d and %d is %d. \n\n", 3, 4, 3*3 + 4*4)
    ;printf("**end**"); /* :-) */ 
    ;return (0);
}

Upvotes: 0

Puneet Chawla
Puneet Chawla

Reputation: 6009

1.It's compulsory to declare variable before using. You are using a and b in below line. but you haven't declared it before. So it will give you error surely.

scanf("&d &d", a, b);

2.It's also compulsory to use '&' with a and b while getting value from user. It won't give you error but can give you incorrect results that you don't want.

scanf("&d &d", &a, &b);

3.Indentation is also not good. You need to use ';' at the end of statement instead of before stating next statement or any other coding line. It won't give you error but it easy to understand code and easily readability.

printf("Enter two numbers:")
;scanf("&d &d", a, b)

Upvotes: 0

rfreytag
rfreytag

Reputation: 1193

First of all the declaration if the two variables is missing. Declare them first, in your case int would be best, so:

int a, b;

then you also need to pass pointers to the scanf() function, so it can write the values in the variables, like so:

scanf("%d %d", &a, &b);

it's also good practice to check if the values have been read at all or not. scanf() returns the number of values it successfully parsed from the input, so in your case it should return 2 if successful, test for that.

Also it's not a bad idea to initialize your variables as well, so maybe do it like that instead:

int a = 0, b = 0;

also you don't seem to be using the variables in the rest of your program, you might want to do something about that.

Upvotes: 1

Related Questions