So here's my code:
main()
{
char *iochar;
printf(" Enter a standard line: ");
scanf ( "%s", &iochar);
if (iochar != NULL)
{
printf(" Here's what you entered: %s ", &iochar);
}
else
{
printf(" Oops! Looks like you forgot to enter something! ");
}
}
My problem is that I can scan the user entry and store it and if something exists it puts out the correct message. But if I just hit return to test for no input (null, empty, etc) the program neither quits nor outputs my error message. It just hangs until I input something.
I've been programming in Java for the last two semesters so C is totally lost on me right now. So any help would be much appreciated.
scanf(ever). Usefgets, or, if you have it,getline. Then "just hitting return" will produce the string"\n"and typing control-D (which forces an EOF condition) will produce the string"". Incidentally, the spaces at the ends of your second and thirdprintfstrings should be\ninstead.char *iochar;. Not checking the function return value fromscanf. Using&for string arguments toscanfandprintf... I suggest the man pages for functions you use, and enabling full compiler warnings.%sconversion specification forscanf()skips white space, including newlines and blanks and tabs, and doesn't start saving anything until it comes across a non-white-space character. If you care about newlines, you can't usescanf()because it doesn't care about newlines (unless you work very hard — so hard that it isn't worth doing). If you care about lines, use a line-based input function (e.g.fgets()) and then maybesscanf()to parse the line that it reads.