-3

I am quite new to C and I come from a Java background.

So, I wanted to declare a String and immediately tried this:

String text;

Then it tells me that the term "String" is not defined. I searched trough the internet and found this:

char text[16] = { 'E','i','n',' ','l','a','n','g','e','r',' ','T','e','x','t','\0' };

But this isn't very nice and too much work. There must be an other, better method. Maybe with importing something. Does anyone has a good solution to this?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
maracuja-juice
  • 994
  • 2
  • 14
  • 33
  • 2
    You can simply `char text[] = "whatever..."`. – Lingxi Nov 03 '15 at 07:11
  • 4
    This question may be useful: [Declaring and modifying strings in C](http://stackoverflow.com/questions/30654242/declaring-and-modifying-strings-in-c) – awesoon Nov 03 '15 at 07:12
  • 1
    I recommend you check out [The Definitive C Book Guide and List](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list), and read some beginners books and tutorials. – Some programmer dude Nov 03 '15 at 07:12
  • 1
    C is way lower level than Java, and more deviously unforgiving, you are not going to go far by trying random stuff and hoping it works (especially since many many errors in C are "undefined behavior", which means that your program may appear to work, randomly crashing once in a while). Learn the language with a proper book. – Matteo Italia Nov 03 '15 at 07:25
  • 2
    It doesn't work because C is not Java... Also "isn't very nice and too much work" sums up C strings pretty well, there's a reason why all languages invented since C have their own string class. – Lundin Nov 03 '15 at 07:39

4 Answers4

3

There is no string type in C.

A string variable is a 1-d array of ASCII characters terminated by a null character.

The method you tried to declare a string is right.

char text[16] = { 'E','i','n',' ','l','a','n','g','e','r',' ','T','e','x','t','\0' };

But the easy one is to simply

char str[]="Ein Langer Text"

This is initialization is same as previous one but in this case compiler automatically inserts the null character at the end.

A simple example:

#include <stdio.h>
int main(int argc, char const *argv[])
{
char str[]="Ein Langer Text";
int i;
for (i = 0; str[i]!='\0' ; ++i)
{
   printf("%c",str[i]);

}
printf("\n");
return 0;
}

You can even have a limited size string such as:

char[40]="whatever you want to keep here up to fourty characters";
Rusty
  • 168
  • 6
2

In C, there is no standard data type called String. It is either a string literal or a char array.

FWIW,

char text[16] = { 'E','i','n',' ','l','a','n','g','e','r',' ','T','e','x','t','\0' };

can be shortened as

char text[ ] = { "Ein langer Text"};   //modifiable, but size limited to
                                       // the initalizer

or

char text[128] = { "Ein langer Text"};  // modifiable, with larger size than initializer

or

char *text = "Ein langer Text";  //not modifiable
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • actually `const char *const text = "Ein langer Text";` would be non modidiable (and prevents compiler to complain) – OznOg Nov 03 '15 at 07:39
  • @OznOg I'm not saying you're wrong, but by definition, string literals are immutable. Attempt to change them will result in UB, anyways. – Sourav Ghosh Nov 03 '15 at 07:41
  • 3
    You can also leave out the parantheses, `char text[] = "Ein langer Text";` works just fine. – fuz Nov 03 '15 at 08:03
0

It's even simpler:

char text[]="test 123"; 

or

char text[9]="test 123"; 
Gopi
  • 19,784
  • 4
  • 24
  • 36
-2

in c, there is no string type:

in c you can try :

char *mystring = "Hello world";

i c++ you can try:

#include <iostream>

std::string mystring = "Hello";
Akhil Thayyil
  • 9,263
  • 6
  • 34
  • 48