Jump to content

help me [i have in syntax]

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1
kiddies

kiddies

    Programmer

  • Members
  • PipPipPipPip
  • 130 posts
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....

#2
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
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);
}

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

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
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

#4
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
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..

#5
kiddies

kiddies

    Programmer

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

#6
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
doesn't my syntax work? It works well in dev-cpp....

#7
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
just do as we said, change the scanfs and the printf.

Posted via CodeCall Mobile

#8
kiddies

kiddies

    Programmer

  • Members
  • PipPipPipPip
  • 130 posts
i ve an error like this Posted Image

#9
kiddies

kiddies

    Programmer

  • Members
  • PipPipPipPip
  • 130 posts
ive done in linux

#10
Sinipull

Sinipull

    Programming Expert

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

#11
kiddies

kiddies

    Programmer

  • Members
  • PipPipPipPip
  • 130 posts
ok guys, i will change my OS first......may be it work

#12
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
man, use "name", not "&name". Same goes for color. And that counts for windows systems too, btw.

Posted via CodeCall Mobile