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


Oldest  Newest  Rating
0 john johnny · August 29, 2015
Thanks to both of you. The string.h was more fitted for what I'm doing.
+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.


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

These are the functions I used most of this lib.
+1 c student · August 25, 2015
char drinks[50] = "this is a string";
  • 1



One of the most popular languages of all time.

Bucky Roberts Administrator