2

This code is supposed to read certain amount of numbers and then print how many of them are divisible by the divisor variable, however when i write it like this there seems to be some kind of problem.

#include <stdio.h>

int main()
{
    long repeat;
    int divisor;
    long long t;
    long result = 0;

    scanf("%d", &repeat);
    scanf("%d", &divisor);


    for (long i = 0; i < repeat; i++)
    {
        scanf("%d", &t);
        if (t % divisor == 0)
        {
            result++;
        }
    }

    printf("%d",result);
    return 0;
}

But when I set all of the variables at the start to 0 it works fine. Can somebody tell me what´s the problem with the first implementation?

#include <stdio.h>

int main()
{
    long repeat = 0;
    int divisor = 0;
    long long t = 0;
    long result = 0;

    scanf("%d", &repeat);
    scanf("%d", &divisor);


    for (long i = 0; i < repeat; i++)
    {
        scanf("%d", &t);
        if (t % divisor == 0)
        {
            result++;
        }
    }

    printf("%d",result);
    return 0;
}
12
  • 5
    long long t; .... scanf("%d", &t); should generate a warning. Insure all warnings are enabled and report the results. This will save you time. Commented May 28, 2018 at 19:38
  • 2
    "...there seems to be some kind of problem." That isn't very descriptive. What sort of problem? An error? Unexpected output? It's hard to identify what might be wrong, especially when you aren't showing any examples of the values you are feeding in. Commented May 28, 2018 at 19:40
  • The result is always 0. Commented May 28, 2018 at 19:41
  • Perhaps read the manual page for scanf. It returns a value that you should check Commented May 28, 2018 at 19:42
  • 1
    What are you using to compile? Commented May 28, 2018 at 19:43

1 Answer 1

3

Not someone, but something - GCC 7.2 can:

% gcc div.c -Wall -Wextra
div.c: In function ‘main’:
div.c:10:13: warning: format ‘%d’ expects argument of type ‘int *’, 
          but argument 2 has type ‘long int *’ [-Wformat=]
     scanf("%d", &repeat);
            ~^   ~~~~~~~
            %ld
div.c:16:17: warning: format ‘%d’ expects argument of type ‘int *’, 
          but argument 2 has type ‘long long int *’ [-Wformat=]
         scanf("%d", &t);
                ~^   ~~
                %lld
div.c:23:14: warning: format ‘%d’ expects argument of type ‘int’, 
          but argument 2 has type ‘long int’ [-Wformat=]
     printf("%d",result);
             ~^
             %ld

It also tells below what you should use instead: %ld, %lld and %ld. Since you didn't use correct length modifiers, the behaviour was undefined, which made you think that you "fixed" the program by initializing the variables with zeroes.


Also, remember to check the return value of scanf and that you need to discard for example the rest of the line if something invalid has been input; just entering hello into your program will make it hang.

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

Comments

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.