1,105,286 Community Members

scanf error (syntax error)

Member Avatar
vivosmith
Light Poster
41 posts since Mar 2010
Reputation Points: 27 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello
I have been using c for dummies along with bloodshed software for a day or so and I tried putting this snippet of code in from the book and I got a syntax error:

#include <stdio.h>
int main()
{
char name[20];
char color[20];
printf(“What is your name?”);
scanf(“%s”,name);
printf(“What is your favorite color?”);
scanf(“%s”,color);
printf(“%s’s favorite color is %s\n”,name,color);
return(0);
}

I figured this book was up to date (2004) and up to this point the only error I got was from using () around 0 in return. But for some reason this won't compile. And I copied and pasted it too. So if there is any way I can fix this, please reply.

Thanks
vivosmith

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 80 [?]
Team Colleague
 
0
 

The only syntax error I get with a copy/paste of your code is different characters for " than my compiler was expecting. Try the following:

#include <stdio.h>
int main()
{
    char name[20];
    char color[20];
    printf("What is your name?");
    scanf("%s",name);
    printf("What is your favorite color?");
    scanf("%s",color);
    printf("%s's favorite color is %s\n",name,color);
    return(0);
}
Member Avatar
vivosmith
Light Poster
41 posts since Mar 2010
Reputation Points: 27 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I think I know what I did wrong. Since the scanf is not going to put text on the page, there is no need \n on there. Thanks :)

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 80 [?]
Team Colleague
 
0
 

I think I know what I did wrong. Since the scanf is not going to put text on the page, there is no need \n on there. Thanks :)

On top of not being related to your original stated problem, your original code didn't use '\n' in either scanf format string. So if the problem and code were both wildly different from reality, what kind of help were you expecting to receive? :icon_rolleyes:

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: