Curriculum
Course: C basic
Login

Curriculum

C basic

C Introduction

0/1

C Get Started

0/1

C Comments

0/1

C Constants

0/1

C Operators

0/1

C Break and Continue

0/1

C User Input

0/1

C Memory Address

0/1

C Structures

0/1
Text lesson

Strings

Strings

Strings serve the purpose of storing text or sequences of characters.

For instance, “Hello World” constitutes a string of characters.

Unlike numerous other programming languages, C lacks a built-in String type for straightforward creation of string variables. Instead, you must utilize the char type and establish an array of characters to represent a string in C:

 

char greetings[] = “Hello World!”;

It’s important to use double quotes ( “” ) when defining strings.

To output the string, you can employ the printf() function along with the format specifier %s, indicating to C that we are dealing with strings:

Example

char greetings[] = “Hello World!”;
printf(“%s”, greetings);

Access Strings

As strings are essentially arrays in C, you can access a string by referencing its index number within square brackets [ ].

The following example outputs the first character (index 0) in the variable greetings:

Example

char greetings[] = “Hello World!”;
printf(“%c”, greetings[0]);

 

It’s worth noting that we must utilize the %c format specifier to print a single character.

 

Modify Strings

To modify the value of a specific character within a string, reference its index number and employ single quotes:

Example

char greetings[] = “Hello World!”;
greetings[0] = ‘J’;
printf(“%s”, greetings);
// Outputs Jello World! instead of Hello World!

Loop Through a String

You can iterate through the characters of a string by utilizing a for loop:

Example

char carName[] = “Volvo”;
int i;

for (i = 0; i < 5; ++i) {
  printf(“%c\n”, carName[i]);
}

As mentioned in the arrays chapter, you can utilize the sizeof formula (rather than manually specifying the size of the array in the loop condition, such as i < 5) to enhance the sustainability of the loop:

Example

char carName[] = “Volvo”;
int length = sizeof(carName) / sizeof(carName[0]);
int i;

for (i = 0; i < length; ++i) {
  printf(“%c\n”, carName[i]);
}

Another Way Of Creating Strings

In the preceding examples, we employed a “string literal” to generate a string variable, which is the simplest approach in C.

It’s important to recognize that you can also create a string with a predefined set of characters. The following example will yield the same outcome as the initial example on this page:

Example

char greetings[] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘ ‘, ‘W’, ‘o’, ‘r’, ‘l’, ‘d’, ‘!’, ‘\0’};
printf(“%s”, greetings);

 

Why do we include the \0 character at the end? This character, known as the “null terminating character,” must be included when constructing strings using this method. It informs C that the string has reached its conclusion.

Differences

The distinction between the two methods of creating strings lies in the ease of writing. With the first method, you don’t need to include the \0  character manually, as C handles it for you.

It’s important to recognize that the size of both arrays remains the same: They each consist of 13 characters, including the \0  character (note that even spaces count as characters).

Example

char greetings[] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘ ‘, ‘W’, ‘o’, ‘r’, ‘l’, ‘d’, ‘!’, ‘\0’};
char greetings2[] = “Hello World!”;

printf(“%lu\n”, sizeof(greetings));   // Outputs 13
printf(“%lu\n”, sizeof(greetings2));  // Outputs 13

Real-Life Example

Utilize strings to craft a basic welcome message:

Example

char message[] = “Good to see you,”;
char fname[] = “John”;

printf(“%s %s!”, message, fname);