Rissam Asghar
Rissam Asghar

Reputation: 3

Warning this code has no effect

The getchar(); line gives me the warning: this code has no effect.

#include <stdio.h>

int main()
{
    int this_is_a_number;

    printf( "Please enter a number: " );
    scanf( "%d", &this_is_a_number );
    printf( "You entered %d", this_is_a_number );
    getchar();
    return 0;
}

is there any solution?

Upvotes: 1

Views: 4712

Answers (2)

Roman Hocke
Roman Hocke

Reputation: 4239

As @nicomp answered, there is unused return value. You can get rid of it by casting result to void:

(void)getchar();

Upvotes: 2

nicomp
nicomp

Reputation: 4647

getchar() reads a key from the keyboard and returns the key. You would have to assign the value returned from the function to a variable:

int key;
key = getchar();

The warning is telling you that no variables will be changed when you call it the way you did. Certainly the code has some effect -- it reads from the keyboard, but that's all it does.

I taught Turbo C/C++ a long time ago so I won't say anything snarky about it. That was an amazing product back in its' day.

Upvotes: 4

Related Questions