Bah! newb question, but how do I make a string variable in C?

Started by
3 comments, last by JavaMava 16 years, 6 months ago
I've looked through the lessons on my professors site, and it talks about variables, but no example of actually making a string. Basically I want this s string_name = "This is the string."; but whats the proper syntax, in C?
Advertisement
C uses pointers:
char string[] = "mystring";char* string = "mystring";

In the above, string[0] points to the first character in the string ('m').
If you don't mind using C++ then you can use the much friendlier version...

#include <string> // need this headerstd::string s = "This is the string.";
Quote:Original post by JavaMava
s string_name = "This is the string.";

but whats the proper syntax, in C?


char[] string_name = "This is the string.";

There is no string type in C, you have to use pointers to string literals. You also have to have seperate arrays to do any kind of string manipulation which can cause buffer overflow issues.

Dealing with text in C is not pretty and is downright ineffecient since it has to constantly count characters.
Thanks every one for the help. Worked like a charm.

I'm using C since it's my first year in college and thats what they want us to use. Don't get to touch C++ until second year(third semester).

This topic is closed to new replies.

Advertisement