vs06 vs06 - 1 year ago 179
C Question

scanf reading "Enter" key

Why scanf doesn't work when I type "Enter" in the code below?

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char**argv)
char *msg = malloc(100*sizeof(char));
} while(strcmp(msg,"")!=0);

Answer Source

Because of scanf() wait char-string, separated by whitespaces, enters, etc. So, it just ignores ENTERs, and waiting for "real non-empty string". If you want to get empty string too, you need to use

fgets(msg, 100, stdin);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download