#include <stdio.h>

int main() {
        unsigned short i = 0;
        scanf("%u", &i);
        printf("%u", i);
int main() {
        unsigned short i = 0, j = 0;
        scanf("%u", &i);
        printf("%u", i);

hey try the above prog, cc short.c -o short
then ./short
enter a number, you ll end up in seg fault.

you can comment the first n try d second main(), it works fine.
what is wrong? is it wid my code? or is it wid d compiler? or is it somewhr else?

Thanx for checkin on it.!

10 Years
Discussion Span
Last Post by Belrog

Nothing wrong with the compiler, just bugs in your code.

$ gcc -W -Wall -ansi -pedantic -O2 foo.c
foo.c: In function `main':
foo.c:5: warning: unsigned int format, different type arg (arg 2)
foo.c:7: warning: control reaches end of non-void function

If you fixed the scanf format to be correct for an unsigned short, then it would work properly.

Your second program only works because the memory trashed by %u is the 'j' variable, and not something critically important to the running of the program.

> guys it works fine in VC++. but not wid GCC. Y is it??
That's just dumb luck on your part, nothing more.

Think of it in terms of running across a minefield. Just because you fail to cause an explosion, do you immediately conclude there are no mines?




thanx for telling me wats wrong.

gcc -W -Wall -ansi -pedantic -O2 foo.c

why do i have to use this to compile tat prog? ow do u do it otherwise?

n abt tat mine stuff, well i was askin if it was wrong wid compiler, i dint conclude its wrong wid gcc. may be i was being over optimistic wid it. now i know, i know nothin in c.


He added those compile tags to list the warnings. Without them it compiles, but doesn't list them. He has a good point about "abt", "tat", etc. when he listed the link. It's hard to take your question seriously when you write like that.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.