Hey, I am really new to C programming. So far I am make really basic stuff. Anyway, how do you make a variable to save a whole word or sentence. When I tried, it only saved the C in Chris. Thanks.
Hey, I am really new to C programming. So far I am make really basic stuff. Anyway, how do you make a variable to save a whole word or sentence. When I tried, it only saved the C in Chris. Thanks.
Use an array of characters, a so-called C string. Here's a tutorial about it. http://www.cprogramming.com/tutorial/c/lesson9.html
Basically:
All of that code for removing the newline is necessary because when you enter something likeCode:char *p; /* a pointer used for removing newlines */ char name[50]; /* allow a 49-character name maximum */ fgets(name, sizeof name, stdin); /* read name from the user */ p = strchr(name, '\n'); /* search for the terminating newline */ if(p != NULL) *p = '\0'; /* replace the terminating newline with a NULL char, if it was found */ printf("Hello, %s!\n", name); /* use the variable name somehow */
you're really entering the string "Hello, World!\n". If you want to use the string, e.g. print it to the screen, you usually don't want the trailing newline; hence why the above code removes it.Code:Hello, World!
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Thanks, I was using %d.
Hey!
I thought you might like to take a look at Beej's guide to C programming.
Also, if you need a book, the forum members have a really extensive list of books in the sticky post at the top of the forum list.
Hope you find something that helps.