Why does my C program give different outputs in different compilers?

Shubh Roy

The result from my program is not what I expect and it is different on different compilers.

I have tried it on three compilers, two of which give the same result. But I want a combination of the two results.

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

int main()
{
    int iRandomNum = 0;
    int iUserInput = 0;
    const int VIP = 7;
    srand(time(NULL));
    iRandomNum = (rand() % 10) + 1;

    printf("\nGuess a number between 1 to 10.\n Make a wish and type in the number.\n");

    scanf("%d", &iUserInput);

    if(isdigit(iUserInput))
    {
         printf("\n\nYou did not enter a digit between 1 to 10.\n\n");
    }
    else
    {
        if(iUserInput==iRandomNum || iUserInput==VIP)
            printf("\n\nYou guessed it right!\n\n\n");
        else
            printf("\n\nSorry the right answer was %d.\n\n\n", iRandomNum);
    }
    return 0;
}

When I choose any number, the program should only alert me if I have not chosen the correct number in this number guessing game. But in the case of 7, we always have the right answer. This happens in the two online compilers. But in clang, when I do this & does not work. The isdigit function does not work then

Blaze

Using the %d format specifier, you're reading an int into iUserInput. That's correct, but then you use isdigit to try to see whether the number is between 1 and 10. However, this function is for finding out whether a char is between '0' and '9'. That's not the same - assuming ASCII those chars are equal to to 48 and 57 respectively. So You isdigit check most likely finds out whether the inputted number is between 48 and 57 (although there's no guarantee that ASCII is used, so a different encoding might lead to different results).

Instead, the check should be:

if((iUserInput >= 1) && (iUserInput <= 10)) {...}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does the sizeof operator give different outputs

Why does this program give different outputs on ubuntu windows desktop app and on ubuntu virtual box?

%n format specifier program giving different outputs on different compilers. Why?

Simple C++ code returns different outputs on different compilers

why these two codes give different outputs

Why does my program give me the excess output? C program

Why building same model in 2 different ways give different outputs?

Why does my Go solution give a different result from C++?

Why does the same algorithm result in different outputs in C++ & Python?

Why does same code in C++ and Python generate different outputs?

Why does document.write and console.log give different outputs for getElementById?

Different outputs from the same C program

Why do these two similar recursive C-codes give different outputs?

Why does this code outputs two different results?

Why does gzip -c give different results than gzip?

Why does the sizeof a class give different output in C++?

why does following code give different output in C , Python?

Why do the following statements give different outputs in Java?

Why does this code give no output on online C++ compilers?

Multithreading: why does my program terminate with different results?

Why does my program call out a different code?

Why does my Ruby program takes different times to execute?

Why does parameter pack expansion work differently with different C++ compilers?

Why are these outputs different?

Why outputs are different?

Why does my extremely basic CSS code produce different outputs on jsFiddle and jsBin?

Why ruby StringIO does not give different encodings

why does the "overflow" property give the different result?

My code gives different results on different compilers