HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

C Strings



Strings

C strings are fundamental elements in C programming used to represent sequences of characters. Unlike many other programming languages, C doesn't have a built-in string type. Instead, you work with character arrays that are null-terminated to signify the end of the string.

For example, "Hello World" is a string of characters.

char greetings[] = "Hello CodeLines!";


Note that you have to use double quotes ("").

To output a string, you can use the printf() function with the format specifier %s to tell C that we are now working with strings:

Example

#include <stdio.h>

int main() {

char greetings[] = "Hello CodeLines!";

printf("%s", greetings);

return 0;

}


Output

Hello CodeLines!



Access Strings

C strings are not a fundamental data type like integers or characters. Instead, they are essentially arrays of characters, typically terminated by a null character (\0). This means that accessing and manipulating strings in C involves understanding pointers and array operations.

This example prints the first character (0) in greetings:


Example

#include <stdio.h>

int main() {

char greetings[] = "Hello CodeLines!";

printf("%c", greetings[0]);

return 0;

}


Output

H


Note that we have to use the %c format specifier to print a single character.


Modify Strings

To change the value of a specific character in a string, refer to the index number, and use single quotes:


Example

#include <stdio.h>

int main() {

char greetings[] = "Hello CodeLines!";

greetings[0] = 'J';

printf("%c", greetings[0]);

return 0;

}


Output

Jello CodeLines



Loop Through a String

You can also loop through the characters of a string, using a for loop:


Example

#include <stdio.h>

int main() {

char carName[] = "Lines";

int i;

for (i = 0; i < 5; ++i) {

printf("%c\n", carName[i]);

}

return 0;

}


Output

L

i

n

e

s


And like we specified in the arrays chapter, you can also use the sizeof formula (instead of manually write the size of the array in the loop condition (i < 5)) to make the loop more sustainable:


Example

#include <stdio.h>

int main() {

char carName[] = "Lines";

int length = sizeof(carName) / sizeof(carName[0]);

int i;

for (i = 0; i < length; ++i) {

printf("%c\n", carName[i]);

}

return 0;

}


Output

L

i

n

e

s



Another Way Of Creating Strings

In the examples above, we used a "string literal" to create a string variable. This is the easiest way to create a string in C.

You should also note that you can create a string with a set of characters. This example will produce the same result as the example in the beginning of this page:

Example

#include <stdio.h>

int main() {

char greetings[] = {'H', 'e', 'l', 'l', 'o', ' ', 'C', 'o', 'd', 'e', 'l', 'i','n', 'e', 's !', '\0'};

char greetings2[] = "Hello CodeLines!";

printf("%s\n", greetings);

printf("%s\n", greetings2);

return 0;

}

return 0;

}


Output

Hello CodeLines!

Hello CodeLines!


Why do we include the \0 character at the end? This is known as the "null terminating character", and must be included when creating strings using this method. It tells C that this is the end of the string.


Differences

The difference between the two ways of creating strings, is that the first method is easier to write, and you do not have to include the \0 character, as C will do it for you.

You should note that the size of both arrays is the same: They both have 13 characters (space also counts as a character by the way), including the \0 character:

#include <stdio.h>

int main() {

char greetings[] = {'H', 'e', 'l', 'l', 'o', ' ', 'C', 'o', 'd', 'e', 'l', 'i','n','e','s!', '\0'};

char greetings2[] = "Hello CodeLines";

printf("%lu\n", sizeof(greetings));

printf("%lu\n", sizeof(greetings2));

return 0;

}


Output

16

16



Real-Life Example

Use strings to create a simple welcome message:

#include <stdio.h>

int main() {

char message[] = "Good to see you,";

char fname[] = "CodeLines";

printf("%s %s!", message, fname);

return 0;

}


Output

Good to see you, CodeLines!