My first C program [Help]

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Dex
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 3
Joined: Wed Dec 30, 2009 8:50 pm

My first C program [Help]

Post by Dex »

First off, let me say that I am only 12 years old.
With that being said, my brain power is no match to yours ;)

I am learning C as my first programming language.
The book I am reading is "Absolute Beginners Guide to C".
I am working on my first real, interactive program.
Heres the source:

Code: Select all

/* Written by Dex*/
/* -Do you like my name?- Written in C++ */ 
#include <stdio.h>
main()
{
char name[4];  /* The variable that stores the yes/no choice */

printf("Hello, i'm Dex, do you like my name? [Say yes or no] \n"); /* Asking for you
to enter 'yes' or 'no' */

scanf("%c", name[4]); /* Character input for the 'name' variable */


if(name == "yes") /* If the user enters the word 'yes' */
{
        
printf("/n Really? Thanks, I like my name, too"); /* Happy message */

}


else

printf("/n What? You don't like my name? -Sadface-"); /* Sad message */
}

But when I type my 'yes or no' response, the program crashes!
Can anybody please explain this?



Edit: Please ignore the horrible whitespace used in my program, i think i am messing up spacing in some parts.
Thanks, Dex.
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: My first C program [Help]

Post by hurstshifter »

Welcome Dex! Glad to see you are interested in learning C. A few things...

1. You should get into the habit of setting the return value and formal parameters of your functions, even main. So instead of

Code: Select all

main()
you should have

Code: Select all

int main(void)
Your compiler probably compiled the original without any issues but when you start creating your own custom functions they will all have these features.

2. You are using the scanf() function to accept a char from the end user (%c), but are actually looking for the full "yes" or "no" response. In this situation you should use %s to scan in an entire string of characters. You'll learn more about strings later.

3. main() should be returning a value (typically 0 as long as your program ended normally). Finish main with

Code: Select all

return 0;
4. Your program probably is not crashing but just running extremely quickly and closing. Most IDEs will have an option to run the application with debugging which will prevent this from occurring. Otherwise, you can place this line at the end of your code to have it pause

Code: Select all

system("pause");
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
Dex
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 3
Joined: Wed Dec 30, 2009 8:50 pm

Re: My first C program [Help]

Post by Dex »

Thank you so much for the help :)
This info was not included in my book (probably because it is a bit dated).
User avatar
Trask
ES Beta Backer
ES Beta Backer
Posts: 738
Joined: Wed Oct 29, 2008 8:17 pm
Current Project: Building a 2D Engine
Favorite Gaming Platforms: Sega Genesis and Xbox 360
Programming Language of Choice: C/C++
Location: Pittsburgh, PA
Contact:

Re: My first C program [Help]

Post by Trask »

hurstshifter wrote: 4. Your program probably is not crashing but just running extremely quickly and closing. Most IDEs will have an option to run the application with debugging which will prevent this from occurring. Otherwise, you can place this line at the end of your code to have it pause

Code: Select all

system("pause");
I really hope I don't come off as an ass, but just as a suggestion since you're making them ...about 2 years ago I broke myself out of using

Code: Select all

system("pause"); 
in favor of

Code: Select all

getchar();
simply from a mindset that assumes that your OS is not Windows, so better portability. Is it wrong to use? No, but again it was a habit that I found myself going "Huh, that's not going to work here, what do I use now?"
MarauderIIC wrote:You know those people that are like "CHECK IT OUT I just made Linux run on this piece of celery [or other random object]!!"? Yeah, that's Falco, but with ES.
Dear god, they actually ported ES to a piece of celery!
Martin Golding wrote: "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
Dex
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 3
Joined: Wed Dec 30, 2009 8:50 pm

Re: My first C program [Help]

Post by Dex »

Thank you, Trask, I will keep that in mind. :bow:
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: My first C program [Help]

Post by hurstshifter »

Trask wrote: I really hope I don't come off as an ass, but just as a suggestion since you're making them ...about 2 years ago I broke myself out of using

Code: Select all

system("pause"); 
in favor of

Code: Select all

getchar()
"
Excellent point
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
Master Jake
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 69
Joined: Sat Dec 12, 2009 8:43 pm
Programming Language of Choice: C/C++
Location: United States
Contact:

Re: My first C program [Help]

Post by Master Jake »

Correct me if I'm wrong; but, aside from the errors mentioned above:

Along with %s as mentioned above, use "name" without an index to specify the full string

Code: Select all

scanf("%s", name);
C doesn't have string comparison available because it is not an object. This being said, "name == something" is invalid. You need the string comparison
function strcmp

Code: Select all

if(strcmp(name, "yes") == 0)
Side note: scanf is a bad function for reading in characters from the console as it can lead to buffer overflows and had little protection. As soon as you can, stray away from that.
Post Reply