Thread: Set string variable

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    15

    Set string variable

    I am very new to C programming. All I want to do is define a string variable and pass text to it. I have tried many things. I thought one of the following would work but am having no luck. Thank you for looking and for your help. My code is below.

    Code:
    char str[20];
    str = "Canada Dollars";
    Code:
    char str[20];
    str == "Canada Dollars";

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    firstly, the tag end character in this forum, and in all html as far as i know, is / not \ as you have used. secondly, you cant assign a string a value like that after it has been declared but you can initialise it when you are declaring it, change code to this.

    Code:
    char str [20] = "Canada Dollars";
    also, just so you know, == is a comparative operator, it will never be used to assign values to variables.
    Last edited by Richie T; 01-28-2006 at 06:13 PM.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    one more thing... if your program depends on assigning a ilteral value to a string later in your program, you can do it like this:

    Code:
    char str [20];
    
    /*now some program*/
    
    strcpy (str, "Canada Dollars");
    strcpy is defined in string.h. some ppl may say to use strncpy, to avoid buffer overflows, that should probably be fine too.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    15
    Thank you. I did change my HTML end tag but you were so quick to responding that you saw my tag before I had time to change it. Thank you again!

    D

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. C help for network animator
    By fastshadow in forum Tech Board
    Replies: 7
    Last Post: 03-17-2006, 03:44 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM