Variable Help

This is a discussion on Variable Help within the C Programming forums, part of the General Programming Boards category; Hey, I am really new to C programming. So far I am make really basic stuff. Anyway, how do you ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    5

    Variable Help

    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.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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:
    Code:
    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 */
    All of that code for removing the newline is necessary because when you enter something like
    Code:
    Hello, World!
    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.
    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.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    5
    Thanks, I was using %d.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 11:07 AM
  2. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 07:32 AM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 03:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21