tags:

views:

80

answers:

1

this program hangs after taking first argument:-

#include <stdio.h>
#include <conio.h>
void ellip(char*,...);
int main(int argc,char* argv[]){
    printf("a");
    ellip("first argument",99,"second arg","thirdarg");
    _getch();
return 0;
}
void ellip(char* m,...)
{   char com='c';
    for(;;)
        {   
            auto g=0;
            while(com=='c')
            {

                printf("%d\nMatched Continue:-",g++);
                scanf("%c",&com);


            }
        }
}

while the same program with a subtle modification(Addition of space)

scanf("%c ",&com);

Works Fine!

Is this some sort of bug in vc or a problem in my computer?

+5  A: 

When the new line is read from stdin and placed into com, then com is now '\n' and the for(;;) loop will loop forever while the while(com=='c') will never be entered.

scanf("%c ",&com); fixes the problem because the space character will cause scanf to skip over all white space.

See the MSDN article. FIrst bullet point explains what the space charcter is doing.

gwell