Why is scanf() causing infinite loop in this code?

scanf consumes only the input that matches the format string, returning the number of characters consumed. Any character that doesn’t match the format string causes it to stop scanning and leaves the invalid character still in the buffer. As others said, you still need to flush the invalid character out of the buffer before you … Read more

scanf() leaves the newline character in the buffer

The scanf() function skips leading whitespace automatically before trying to parse conversions other than characters. The character formats (primarily %c; also scan sets %[…] — and %n) are the exception; they don’t skip whitespace. Use ” %c” with a leading blank to skip optional white space. Do not use a trailing blank in a scanf() … Read more

Segmentation fault using scanf() [closed]

Three problems here. First scanf(ifp,”%lf %lf\n”,&theta,&Cla); You’re calling scanf when you look like you want to use fscanf: fscanf(ifp,”%lf %lf\n”,&theta,&Cla); Second is the file you’re opening: ifp=fopen(argv[0],”r”); argv[0] is the name of the running executable, which is probably not what you want. If you want the first argument passed in, use argv[1] ifp=fopen(argv[1],”r”); Last is … Read more

sscanf Beginner in C

@n.Doe, What you might be experiencing is that the variables aren’t being initialized and thus see some random values when the sscanf() doesn’t get to find the content for those missing ones. Try initializing them as following. int int1 = 0, int2 = 0, int3 = 0; Also, the sscanf() needs the variables addresses (hence … Read more