0

I am very new to C programming, trying to adjust going from Python to C where the I/O syntax is very different. I would like to ask the user to enter two integers, then return to the user the two integers they had entered followed by the two integers multiplied together. My code follows:

#include <stdio.h>

int main(void) {

    int a,b,c;
    printf("Enter a: \n");
    scanf("%d", &a);
    printf("Enter b: \n");
    scanf("%d", &b);

    c = a*b;
    printf("a = %d, b = %d, a*b = %d", a,b,c);
    return 0;
}

The program does not allow the user to input a value, but rather chooses its own seemingly random integer, and the multiplication is also not correct. My output is as follows:

Enter a: 
Enter b: 
a = 2665720, b = -2147319336, a*b = -76879552
9
  • 1
    What's the output you get? I don't see issue with it? Commented Dec 28, 2014 at 6:45
  • 1
    what values are you typing? Commented Dec 28, 2014 at 6:48
  • 1
    I'm using Eclipse, just tried my code with CodeBlocks and it worked fine so I suppose it's Eclipse's problem somehow. Commented Dec 28, 2014 at 6:57
  • 3
    Always check return values of scanf functions, especially when reading integers. It's not even worth debugging before adding that. Commented Dec 28, 2014 at 7:13
  • 1
    Your program should work well provided you give it valid input, but eclipse's terminal emulator is very limited as far as I can remember, so I recommend you change the IDE, just that. You can try Geany, it's great and it launches a custom terminal emulator for you so you can test your program correctly. Still, you can read my answer to see a different approach to what you are trying to do. Commented Dec 28, 2014 at 7:19

1 Answer 1

2

Even if the problem is with your IDE not passing the input to your program, the observed behavior is because of a and b being uninitialized when you call printf, to avoid that you must verify that scanf succeeded reading the integer. Here I suggest a way of doing that, it might not be the best solution, but it takes care of inavlid input, looping until valid input is recieved.

You have to check for valid input since scanf wont initialize the passed parameters on invalid input, a better way to do that is with the fgets function in combination with strtol that way you can check the validity of the input data

#include <stdio.h>
#include <stdlib.h>

int main(void)
{

    int   a,b,c;
    char  string[32];
    char *endptr;
    int   valid;

    valid = 0;
    while (valid == 0)
    {
        printf("Enter a: ");

        if (fgets(string, sizeof string, stdin) == NULL)
            return -1;
        if (*string == '\n') /* invalid input */
        {
            printf("Invalid Input\n");
            continue;
        }

        a = strtol(string, &endptr, 10);
        if ((*endptr != '\0') && (*endptr != '\n')) /* invalid input */
        {
            printf("Invalid Input\n");
            continue;
        }

        printf("Enter b: ");
        if (fgets(string, sizeof string, stdin) == NULL)
            return -1;
        if (*string == '\n') /* invalid input */
        {
            printf("Invalid Input\n");
            continue;
        }

        b = strtol(string, &endptr, 10);
        if ((*endptr != '\0') && (*endptr != '\n')) /* invalid input */
        {
            printf("Invalid Input\n");
            continue;
        }
        valid = 1;
    }
    c = a * b;

    printf("a = %d, b = %d -> a * b = %d\n", a, b, c);
    return 0;
}

the uninitialized values will contain garbage, explaining the behavior you are observing.

With your program if you type any invalid string scanf will fail and you will try to print uninitialized values, you should at least check that it matched the correct number of arguments, like this

if (scanf("%d", &value) != 1)

something is wrong and you should either restart the program or, abort it.

Sign up to request clarification or add additional context in comments.

10 Comments

@chux no, because I already take care of that here if (*string == '\n').
Relatively minor to the point at hand, but: 1) You're missing fflush(stdout); after printf("Enter a: "); and after printf("Enter b: "); 2) You don't check the return from fgets(), which will lead to trouble if the user does something like CTRL-D at the prompt. 3) string is a reserved identifier in C.
string is not a reserved identifier in c, it is a c++ class. And thank you for pointing that out.
string certainly is a reserved identifier in C, along with every other identifier starting with str and another lowercase letter.
@PaulGriffiths can you post a link for an external resource to support that? I'm pretty sure string is not reserved in c, it is in c++ though.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.