Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

help me [i have in syntax]

syntax

  • Please log in to reply
12 replies to this topic

#1 kiddies

kiddies

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 129 posts

Posted 15 June 2009 - 04:49 AM

when i learn C for review the last lesson in my lesson,wow interesting, i ve forgotten my C code for making simple program
like yhis:
#include <stdio.h>
void main()
{
   char name[20];
   char color[20];
   
   printf("What is your name?");
   scanf("%s", & name[20]);
   printf("What is your favorite color?");
   scanf("%s",& color[20]);
   printf("%s","favorite color is %d\n",& name,& color);
}


help me for correct if any wron there....
  • 0

#2 Aereshaa

Aereshaa

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 638 posts

Posted 15 June 2009 - 04:56 AM

The [20] subscripts and referencization aren't necessary. You're passing in the whole array, not the address of a nonexistent 21st element.
#include <stdio.h>
void 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","favorite color is %d\n", name, color);
}

  • 0
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz

#3 marwex89

marwex89

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 2857 posts

Posted 15 June 2009 - 04:59 AM

Ow, you got some mistakes.

Most importantly, your scanf-s should be:

scanf("%s", name);
scanf("%s", color);

And the printf should also use "name" and "color", not "& name[20]" etc.

And to be picky, main should really return an int.

Edit: areshaa got it ;) sry, typing on a cell phone is slow..

Posted via CodeCall Mobile
  • 0

#4 Sinipull

Sinipull

    CC Addict

  • Validating
  • PipPipPipPipPip
  • 384 posts

Posted 15 June 2009 - 05:18 AM

This should work

#include <stdio.h>
int main(void)
{
   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, favorite color is %s\n", name, color);
}

If you are running it on windows, you should also include conio.h and end the program with getch();

btw, why did you want to print out your color as integer, if you inserted it as a string? It's not wrong, but looked weird..
  • 0

#5 kiddies

kiddies

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 129 posts

Posted 15 June 2009 - 05:24 AM

oh thanks but im running in linux....where the correct syntax...i cant found....plis... i wanna learn C again..
  • 0

#6 Sinipull

Sinipull

    CC Addict

  • Validating
  • PipPipPipPipPip
  • 384 posts

Posted 15 June 2009 - 05:26 AM

doesn't my syntax work? It works well in dev-cpp....
  • 0

#7 marwex89

marwex89

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 2857 posts

Posted 15 June 2009 - 05:27 AM

just do as we said, change the scanfs and the printf.

Posted via CodeCall Mobile
  • 0

#8 kiddies

kiddies

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 129 posts

Posted 15 June 2009 - 05:29 AM

i ve an error like this Posted Image
  • 0

#9 kiddies

kiddies

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 129 posts

Posted 15 June 2009 - 05:30 AM

ive done in linux
  • 0

#10 Sinipull

Sinipull

    CC Addict

  • Validating
  • PipPipPipPipPip
  • 384 posts

Posted 15 June 2009 - 05:31 AM

That's weird, works fine in windows. Guess i don't know anything about linux systems and you should listen to the other guys.
  • 0

#11 kiddies

kiddies

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 129 posts

Posted 15 June 2009 - 05:33 AM

ok guys, i will change my OS first......may be it work
  • 0

#12 marwex89

marwex89

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 2857 posts

Posted 15 June 2009 - 05:35 AM

man, use "name", not "&name". Same goes for color. And that counts for windows systems too, btw.

Posted via CodeCall Mobile
  • 0





Also tagged with one or more of these keywords: syntax

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download