开发者

strange behaviour in visual c

开发者 https://www.devze.com 2023-01-11 23:22 出处:网络
this program hangs after taking first argument:- #include <stdio.h> #include <conio.h> void ellip(char*,...);

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消