SegFault after scanf?
scanf requires the addresses of the variables to be passed to it. Replace your scanf by scanf(“%d %d”,&hour,&min); You should be good to go.
scanf requires the addresses of the variables to be passed to it. Replace your scanf by scanf(“%d %d”,&hour,&min); You should be good to go.
If you read the specification for scanf() carefully, most format specifiers skip leading white space. In Standard C, there are three that do not: %n — how many characters have been processed up to this point %[…] — scan sets %c — read a character. (POSIX adds a fourth, %C, which is equivalent to %lc.) … Read more
I wasn’t happy with any of these solutions, so I researched further, and discovered GNU GCC macro stringification which can be used as: #define XSTR(A) STR(A) #define STR(A) #A #define MAX_STR_LEN 100 scanf(“%”XSTR(MAX_STR_LEN)”[^\n]s”, sometext) Maybe VS2010 offers something similar?
The first problem is that the scanf() reads two characters, but not the newline afterwards. That means your fgets() reads the newline and finishes. You are lucky your program is not crashing. You tell fgets() that it is getting an array of 35 characters, but rather than passing an array, you pass the character (not … Read more
The a modifier to scanf won’t work if you are compiling with the -std=c99 flag; make sure you aren’t using that. If you have at least version 2.7 of glibc, you can and should use the m modifier in place of a. Also, it is your responsibility to free the buffer.
Make sure the scanf discards the newline. Change it to: scanf(” %c”, &loop); ^
Using scanf() is usually a bad idea for user input since failure leaves the FILE pointer at an unknown position. That’s because scanf stands for “scan formatted” and there is little more unformatted than user input. I would suggest using fgets() to get a line in, followed by sscanf() on the string to actually check … Read more
The difference between scanf(“%c”, &c1) and scanf(” %c”, &c2) is that the format without the blank reads the next character, even if it is white space, whereas the one with the blank skips white space (including newlines) and reads the next character that is not white space. In a scanf() format, a blank, tab or … Read more
Breakdown of scanf(“%*[^\n]%*c”): %*[^\n] scans everything until a \n, but doesn’t scan in the \n. The asterisk(*) tells it to discard whatever was scanned. %*c scans a single character, which will be the \n left over by %*[^\n] in this case. The asterisk instructs scanf to discard the scanned character. Both %[ and %c are … Read more
The following line will start reading a number (%d) followed by anything different from tabs or newlines (%[^\t\n]). sscanf(“19 cool kid”, “%d %[^\t\n]”, &age, buffer);