Thread: Storing Words in a Variable?

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    26

    Storing Words in a Variable?

    How can I store an actual word with a variable? My ideal would be as thus:

    Code:
    int main ()
    
    {
    //introduce variable
    whatvariable a;
    
    //make user input a word
    printf("Enter a word here:");
    
    //store the word as a var
    scanf("%d", &whatvariable);
    
    //Print what user input
    printf("Your word was %d\n", a");
    
    //return that ........
    return 0;
    
    
    }

    Sorry if this c code is horrible, I just started learning today

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, in C, a word is a string (at least if by word we mean "hello", "world", "big", "small", "tea-cosy" and such things).

    C represents strings as an array of characters, where the end is marked with a zero (the value zero, not the digit '0').

    To read/write a string with scanf/printf, you use %s.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Hopefully you have some stuff to read on this, here's an example:
    Code:
    char myexample[16]="hello world";
    Notice that myexample is 16 elements/bytes/characters long. It could be as short as 12 here, 11 for "hello world" and 1 for the '/0' zero byte (null terminator) mentioned by matsp.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Correction: '\0', not '/0'.
    Different things in C.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-26-2007, 08:50 AM
  2. storing an unknown amount of variable
    By sreetvert83 in forum Game Programming
    Replies: 5
    Last Post: 09-17-2005, 05:42 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 10:01 AM
  5. storing different variable types in arrays.........
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 06-28-2002, 11:45 AM