0

I am writing my first c Program in Visual studio and getting very basic error because of the format specifier.

#include <stdio.h>
int main()
{
    int x,y;
    int area = x*y;
    printf("Enter the value of x  \n");
    scanf("%d", &x);

    printf("Enter the value of y  \n");
    scanf("%d", &y);

    printf("Area of the rectangle is %d\n", area);
    return 0;
}

error: Enter the value of x
2 Enter the value of y
1 Area of the rectangle is -293795784 // This is the error. I am expecting for Output 2 i.e product of x and y Not sure why its taking the address of the value instead of value.

4
  • 5
    The instructions are executed sequencially. Aug 4, 2020 at 19:39
  • 1
    ⟼Remember, it's always important, especially when learning and asking questions on Stack Overflow, to keep your code as organized as possible. Consistent indentation helps communicate structure and, importantly, intent, which helps us navigate quickly to the root of the problem without spending a lot of time trying to decode what's going on.
    – tadman
    Aug 4, 2020 at 19:41
  • 1
    C does not work like a spreadsheet, where all the cells are interrelated. It only knows the values that have already been assigned. What you have done with int area = x*y; is undefined behaviour because int x,y; has no initialisation of the values. Aug 4, 2020 at 19:45
  • 1
    This isn't a spreadsheet, nor a VHDL or something, When you type that int area = x*y; you aren't hooking up wires into some async logic gates circuitry, this actually translates into sequentially running CPU instructions, and takes whatever happened to be in x and y at that moment, multiplies it, and loads the result into area. And thats exactly what ends up being printed. Aug 4, 2020 at 20:26

3 Answers 3

5

We can not find the area before x and y are initialised!

#include <stdio.h>
int main()
{
    int x,y;

    printf("Enter the value of x  \n");
    scanf("%d", &x);

    printf("Enter the value of y  \n");
    scanf("%d", &y);
    //Put this area formula here
    int area = x*y;
    printf("Area of the rectangle is %d\n", area);

    return 0;

}

Also you need to check the return value from scanf. That is - check that scanf actually scanned an integer. In other words: When you want to read exactly 1 integer, check that scanf returns 1. If not, there is an error.

This goes like:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int x,y;

    printf("Enter the value of x  \n");
    if (scanf("%d", &x) != 1)            // Notice this line
    {
        // Wrong input - stop program
        fprintf(stderr,"Input error - input must be a number\n");
        exit(1);
    };

    printf("Enter the value of y  \n");
    if (scanf("%d", &y) != 1)            // Notice this line
    {
        // Wrong input - stop program
        fprintf(stderr,"Input error - input must be a number\n");
        exit(1);
    };
    
    //Put this area formula here
    int area = x*y;
    printf("Area of the rectangle is %d\n", area);

    return 0;

}
0
0

You defined the variable area before taking the input of x and y instead you should also assign a value for your area variable which is x*y.

what you did


int x,y;
int area= x*y; // you dont have the value of x and y yet //

so after that, if you print the value of area it actually doesn't have any value.

what you should do assigning the value of area after you have the value of x and y.

The correct way


int x,y;

printf("Enter the value of x  \n");
    scanf("%d", &x);

    printf("Enter the value of y  \n");
    scanf("%d", &y);

int area=x*y; // now you have the value of x & y //
  
-1

I fight with IDE all the time because I like spaces and blank lines but no line waste. I stand off operands, operators most places. I like to NOT bury the lead on if or while of call returns, either:

printf("Enter the value of x: ");

while ( 1 != scanf( "%d", &x )){
    fprintf( stderr, "Invalid x input, must be a number!\n" );
}

printf("Enter the value of y: ");

while ( 1 != scanf( "%d", &x )){
    fprintf( stderr, "Invalid y input, must be a number!\n" );
}

This reduces eyestrain for all, makes errors easier to see, improves grades and the boss's opinion, conserves vertical white space. I stack one liners but stand off all blocks. I do the {} always in case I want to add lines, like debug printouts. I say 'else' rarely, preferring to exit(), return, break, continue or use the ternary operator, in parentheses for operator precedence: ( <boolean_value> ? <true_value> : <false_value> ). I try to use switch whenever possible for more than 2 outcomes, as it is a wonderful place to hang comments, like SQL CASE, with no extra indent for cases!

int retries = 0, ret ;
char buf[4096];

do {
    switch ( ret = read( fd, buf, sizeof( buf ))){
    case -1: // report error
        perror( "read(fd)" );
        exit( 1 );
    case 0: // nominal EOF; sockets, pipes return 0 for 0 length messages.
        if ( ++retries > 100 ){
            return 0 ; // assume normal EOF behavior for some function
        }

        continue ;
    default: // we read something
        retries = 0 ;
        break;
    }
} while ( ! ret );

// process the read() data

Of course, opinions vary! :D

4
  • How might this answer the question?
    – Gerhardh
    Aug 5, 2020 at 8:10
  • Proper way to do user input. Aug 6, 2020 at 19:46
  • True, but there is no question about user input. And formatting preferences are even more off.
    – Gerhardh
    Aug 6, 2020 at 19:56
  • I did not put formatting on the table, I just chimed in. I hope some IDE people rethink what beautified C/C++/C#/JAVA looks like. I had to use 'answer' as 'comment' destroys formatting. Aug 6, 2020 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.