3

If I enter a number that's more than 5 digits long, it shows the wrong number. For example:

Enter Integer: 123456
-7616 is EVEN.-7616 is ODD.

My teacher wants us to use Turbo C++ but it sometimes freezes for me after I run a program so I used OnlineGDB (https://www.onlinegdb.com) (language: C (TurboC)) instead. Here is my code:

#include <stdio.h>
#include <conio.h>

int number;

int main()
{
    clrscr();
    
    printf("Enter Integer: ", number);
    scanf("%d", &number);
    
    if ((number%2)==0)
    {
        printf("%d is EVEN.", number);
    }
    
    printf("%d is ODD.", number);
    
    getch();
    return(0);
}
Ken White
  • 123,280
  • 14
  • 225
  • 444
Banana
  • 39
  • 4
  • 1
    Your code will always print that the number is odd, since that `printf` is being executed unconditionally – UnholySheep Nov 14 '21 at 14:25
  • 6
    Wow, the implementation of C you are using has a 16-bit `int`. It cannot handle numbers larger than 32767 in an `int` type. Try using `long`. When you change `int` to `long`, change `%d` to `%ld` in `scanf` and `printf`. – Eric Postpischil Nov 14 '21 at 14:26
  • 1
    Since it's Turbo **C++** - you could use a `std::string` and just check the last `char`. – Ted Lyngmo Nov 14 '21 at 14:28
  • You say you use OnlineGDB, but that program does not compile in OnlineGDB because `clrscr` is not defined. When `clrscr();` and `getch();` are removed, the program handles 123456 as expected; it does not show the output you report. Edit the question to contain a [mre], including the actual source code being executed and the specific compiler and version being used. – Eric Postpischil Nov 14 '21 at 14:28
  • Does this answer your question? [Is the size of C "int" 2 bytes or 4 bytes?](https://stackoverflow.com/questions/11438794/is-the-size-of-c-int-2-bytes-or-4-bytes) – J... Nov 14 '21 at 14:28
  • [Does the size of an int depend on the compiler and/or processor?](https://stackoverflow.com/q/2331751/327083) – J... Nov 14 '21 at 14:29
  • @TedLyngmo You're making the very bold assumption that [Turbo C++ supports the STL](https://discuss.codechef.com/t/c-stl-standard-template-library-in-turbo-c/696). :-) – Adrian Mole Nov 14 '21 at 14:29
  • [What does the C++ standard state the size of int, long type to be?](https://stackoverflow.com/q/589575/327083) – J... Nov 14 '21 at 14:29
  • 3
    If your teacher wants you to use Turbo C, your teacher will not expect your program to work for `int` values larger than 32767, so you're fine. (Does your teacher also want you to eat Wooly Mammoth for lunch?) – Steve Summit Nov 14 '21 at 14:30
  • @UnholySheep What does "executed unconditionally" mean? edit: I think I got it. I added else to my code. – Banana Nov 14 '21 at 14:34
  • 1
    @Dana "Unconditionally" means that the last `printf` call is not controlled by `if` or `else`. It executes no matter what. – Steve Summit Nov 14 '21 at 14:37
  • @EricPostpischil If I removed clrscr(); and getch();, it still shows the wrong output. But changing int to long solved the problem even if I didn't remove clrscr(); and getch();. – Banana Nov 14 '21 at 14:42
  • @Dana Those comments of Eric's referred solely to your claim that you'd switched from Turbo C to OnlineGDB. You couldn't have run a program containing `getch` and `clrscr` on OnlineGDB. So Eric was wondering what you *did* run there. – Steve Summit Nov 14 '21 at 14:46
  • @Dana Oh! I would never have imagined that OnlineGDB had a "Turbo C"setting! Thanks for that clarification. – Steve Summit Nov 14 '21 at 14:51
  • @SteveSummit I run the exact code on my question. – Banana Nov 14 '21 at 14:51
  • 1
    You forgot “else “ , so all numbers will be written as ODD (for odd numbers) or EVEN+ODD (for even number) – Ptit Xav Nov 14 '21 at 16:33
  • If by any possibility, run away from teachers urging you to use Turbo C++. It's multiple decades old... – the busybee Nov 14 '21 at 19:36

1 Answers1

1

It looks like int is a 16-bit integer. 123456 is greater than the 16-bit signed integer limit (32767), so scanf() can’t put the whole number into number. Instead, it gets truncated. 123456 is represented in binary as 11110001001000000. That’s 17 bits, but only the last 16 can fit in number. With the leftmost bit gone, we have 1110001001000000, which is -7616 in two’s complement form (which is what an integer uses).

Try using a larger integer type, like long.

As others suggested in the comments, you may want to put the printf() for odd numbers in an else.

tjcaul
  • 383
  • 1
  • 9