0

i am enthusiast and new in programming trying this simple c language code and compile it with gnu compiler. here is my code:

/*simple program:trying to printf string and get a letter*/

#include "stdio.h"

int main()
{
    int i=0;
    char c;

    while(i++<100){
        if(i % 2)
            printf("%C this is even number",i);
        if(i==50){
            c=getchar();
            printf("you enter %c letter",c);
            }
    }
    return 0;
}

however when i compile it with gcc and run in terminal, it doesnt show anything,no error and no warning. i tried to figure it out by changing the way my code behave but still not work. is it the code that wrong or gcc have bug or may be i miss about something?

4

3 に答える 3

3

Try to put a newline at the end of printf text:

printf("%C this is even number\n",i);

Also i is an int so use the %d format:

printf("%d this is even number\n",i);

Edit: I just tested this with the changes and it works for me.

于 2012-06-27T15:37:51.693 に答える
0

You should call fflush(stdout) after your display.

于 2012-06-27T15:41:06.123 に答える
0

It ran for me using cygwin under Windows Vista.

I pasted your code into a file called test.c and ran gcc test.c that output a file called a.exe which I then ran.

johnma@johnma-PC ~
$ ./a.exe
 this is even number♥ this is even number♣ this is even number this is even numb
 this is even numbern this is even number◄ this is even number this is even numb
er this is even number this is even number↓ this is even numberthis is even numb
er this is even number this is even number! this is even number# this is even nu
mber% this is even number' this is even number) this is even number+ this is eve
n number- this is even number/ this is even number1 this is even number
you enter
 letter3 this is even number5 this is even number7 this is even number9 this is
even number; this is even number= this is even number? this is even numberA this
 is even numberC this is even numberE this is even numberG this is even numberI
this is even numberK this is even numberM this is even numberO this is even numb
erQ this is even numberS this is even numberU this is even numberW this is even
numberY this is even number[ this is even number] this is even number_ this is e
ven numbera this is even numberc this is even number
johnma@johnma-PC ~
于 2012-06-27T15:42:14.090 に答える