By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,889 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

Regarding scanf and fgets

P: 4
Hi. The following is my code of an example:
#include <stdio.h> /*line 1*/
main() /*line 2*/
{char *command = (char *)calloc(10, sizeof(char)); /*line 3*/
char *middle = (char *)calloc(10, sizeof(char)); /*line 4*/
char *name = (char *)calloc(20, sizeof(char)); /*line 5*/
char *after = (char *)calloc(10, sizeof(char)); /*line 6*/
/*line 7*/
scanf("%s", command); /*line 8*/
scanf("%s\n",middle); /*line 9*/
fgets(name,20,stdin); /*line 10*/
scanf("%s",after); /*line 11*/
/*line 12*/
printf("Command:%s\n", command); /*line 13*/
printf("Middle:%s\n", middle); /*line 14*/
printf("Name:%s", name); /*line 15*/
printf("After:%s\n",after); /*line 16*/
/*line 17*/
free(command); free(middle); free(name); free(after); } /*line 18*/

The goal of this example is to take 1 "argument" on each input line (So,this program should take 4 lines in total.), and then print them out.

My questions are:
- What are the meaning of \n, [\n], and [^\n] in scanf()?
- Only using of \n in line 9 will take 1 "argument" on each input line, but not [\n]
or [^\n] (In the Name field, it seems to be NULL / nothing). Why it happen like
like this?

Thanks.
Nov 3 '06 #1
Share this Question
Share on Google+
1 Reply


P: 4
I apologized that I did not show the inputs. The inputs are:

Add
the
Sam
key

Thanks
Nov 3 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.