Declaration of string variables in Data Structure

In C, strings are represented as arrays of characters. To declare a string variable, you need to declare an array of characters and initialize it with a string literal.

Here is an example of declaring a string variable in C:

```c
char greeting[] = "Hello, world!";
```

In this example, `greeting` is a character array that can hold a string of up to 13 characters (including the terminating null character `\0`). The string "Hello, world!" is used to initialize the array.

You can also declare a string variable using the `char*` pointer type. In this case, you would declare a pointer to a character and assign it the address of the string literal:

```c
char* greeting = "Hello, world!";
```

In this example, `greeting` is a pointer to a character that points to the beginning of the string "Hello, world!". Note that when using this method, you cannot modify the contents of the string. If you need to modify the string, you should declare a character array instead.

You can also declare an empty string by declaring a character array with a size of 1 and setting the first character to the null character:

```c
char emptyString[1] = {'\0'};
```

In this example, `emptyString` is an empty string with a size of 1 (which can only hold the terminating null character).



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext