C Counter Bug - Syntax Problem

Asked

Viewed 93 times

-3

Guys, I was doing some basic programming exercises and I came across a bug that I didn’t quite understand:

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


//Program prints the odd numbers until a determined limit. 
int main()
{
    int numeric_limits;
    int count = 1;

    printf("Enter the limit number.\n");
    scanf(" %d", &numeric_limits);

    while(count <= numeric_limits)
    {
        printf(" %d\n", count);
        count+2;
    }

    return 0;
}

This code above is in an infinite loop "Uns". While this one below works perfectly:

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


//Program prints the odd numbers until a determined limit. 
int main()
{
    int numeric_limits;
    int count = 1;

    printf("Enter the limit number.\n");
    scanf(" %d", &numeric_limits);

    while(count <= numeric_limits)
    {
        printf(" %d\n", count);
        count++;
        count++;
    }

    return 0;
}

I’m coding in codeblocks... I went to test it on some online compiler to see if it was a compiler problem, but the online compiler gave it an infinite loop too... Does anyone know why this happens?

  • 5

    Think about it, the language was created 45 years ago, the compilers have been there for decades, millions of people have done very complicated projects all these years and no one thought bug compiler. What are the chances of the problem not being in your extremely simple code?

1 answer

3


count+2;

I think you meant it:

count += 2;

I mean, you forgot about =.

  • In this case Count is not being incremented, it is only a logic error...

  • Thank you very much!! It helped immensely!! It will be a mistake that I probably won’t make again =D!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.