How can I assign a word to a variable name?

0 john johnny · August 24, 2015
Assigning numbers to a variable is easy. But I wonder how I can do this with a word or maybe sentence eg.
char drinks[50];

I want to specify a word for that variable without letting people input the word I want to appear. Like I want drinks to input "juice", each time I use a printf("%s", drinks);  or in the next line I change the the equivalent of drinks to "milk". Then the next printf ("%s", drinks); would output "milk".

Is it possible? If so can you give example? Please. Thanks in advance.

Post a Reply

Replies

Oldest  Newest  Rating
+1 c student · August 25, 2015
char drinks[50] = "this is a string";
+1 Franz Schmidt · August 25, 2015
c students solution is only possible when you initialise the variable. If you want to change the value of a string during the program is running you need the "string.h" library.

Some functions are:
strcpy(str_dest, str_src);

Assigns the source string to the destination string.

strcat(str_dest, str_src);

Adds the source string to the end of the destination string.

strcmp(str_1, str_2);

Compares the two strings and returns 0 if there is no difference.

strlen(str);

Returns the number of characters in a string (length of the string)


These are the functions I used most of this lib.
0 john johnny · August 29, 2015
Thanks to both of you. The string.h was more fitted for what I'm doing.
Thanks.
  • 1

C

107,174 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator