-
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";
-
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.
-
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.
-
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