Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Char and String | Data
C Basics

bookChar and String

In C, there's no dedicated data type for strings (text). Instead, text is stored as an array of char, where each element holds a single character. For example, the text c<>definity would be represented as a sequence of characters stored in such an array.

char array[12] = {'c','<','>','d','e','f','i','n','i','t','y','\0'};

You might notice that the text "c<>definity" has 11 characters, but the array contains 12 elements. That extra element stores the null terminator \0, which marks the end of a string in C.

It's not the number zero, it's a special, non-printable character (ASCII code 0) that tells the program where the string ends.

Note
Note

Always make sure your character array is one element larger than the text it holds to accommodate this terminator.

It's crucial to recognize that 'x' and "x" aren't the same thing.

A string consists of characters. Always remember to cap off a string with the \0 character.

  • 'x' is simply a character constant of the char type;
  • "x" is a string, an array of char values, or, in this case, {'x', '\0'}.
Note
Note

Don’t confuse '\0', '0', and "0". '\0' is the null character (ASCII 0), '0' is the digit zero (ASCII 48), and "0" is a string containing the digit zero plus the terminator \0.

String Output

In C, when you want to display strings (arrays of char values), you use the %s specifier. This specifier guides the printf() function on what type of information it should expect. By using %s, we inform the printf() function that it will be receiving a string.

main.c

main.c

copy
12345678910
#include <stdio.h> int main() { char array[] = { 'c','<','>','d','e','f','i','n','i','t','y','\0' }; printf("%s\n", array); // use %s format specifiers return 0; }

The C language includes a standard library for string manipulation, <string.h>. Within this library, there's a function that counts the number of characters in a string:

main.c

main.c

copy
123456789101112
#include <stdio.h> #include <string.h> int main() { char array[] = { 'c','<','>','d','e','f','i','n','i','t','y','\0' }; printf("%d\n", strlen(array)); return 0; }
Note
Note

Remember, even a space is a character and it requires its own spot in the array. For example: char array[10] = { 'u','s','e',' ','s','p','a','c','e','\0' }.

By using an index, you can access the string's elements. Since strings are essentially char arrays, you can also modify individual characters through their indices.

Main.c

Main.c

copy
123456789101112
#include <stdio.h> int main() { char str[] = "c<>definity"; str[5] = '#'; // Change the sixth character `f` to `#` printf("%s\n", str); return 0; }
question mark

Which of the following correctly declares a string in C?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 6

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Awesome!

Completion rate improved to 2.63

bookChar and String

Svep för att visa menyn

In C, there's no dedicated data type for strings (text). Instead, text is stored as an array of char, where each element holds a single character. For example, the text c<>definity would be represented as a sequence of characters stored in such an array.

char array[12] = {'c','<','>','d','e','f','i','n','i','t','y','\0'};

You might notice that the text "c<>definity" has 11 characters, but the array contains 12 elements. That extra element stores the null terminator \0, which marks the end of a string in C.

It's not the number zero, it's a special, non-printable character (ASCII code 0) that tells the program where the string ends.

Note
Note

Always make sure your character array is one element larger than the text it holds to accommodate this terminator.

It's crucial to recognize that 'x' and "x" aren't the same thing.

A string consists of characters. Always remember to cap off a string with the \0 character.

  • 'x' is simply a character constant of the char type;
  • "x" is a string, an array of char values, or, in this case, {'x', '\0'}.
Note
Note

Don’t confuse '\0', '0', and "0". '\0' is the null character (ASCII 0), '0' is the digit zero (ASCII 48), and "0" is a string containing the digit zero plus the terminator \0.

String Output

In C, when you want to display strings (arrays of char values), you use the %s specifier. This specifier guides the printf() function on what type of information it should expect. By using %s, we inform the printf() function that it will be receiving a string.

main.c

main.c

copy
12345678910
#include <stdio.h> int main() { char array[] = { 'c','<','>','d','e','f','i','n','i','t','y','\0' }; printf("%s\n", array); // use %s format specifiers return 0; }

The C language includes a standard library for string manipulation, <string.h>. Within this library, there's a function that counts the number of characters in a string:

main.c

main.c

copy
123456789101112
#include <stdio.h> #include <string.h> int main() { char array[] = { 'c','<','>','d','e','f','i','n','i','t','y','\0' }; printf("%d\n", strlen(array)); return 0; }
Note
Note

Remember, even a space is a character and it requires its own spot in the array. For example: char array[10] = { 'u','s','e',' ','s','p','a','c','e','\0' }.

By using an index, you can access the string's elements. Since strings are essentially char arrays, you can also modify individual characters through their indices.

Main.c

Main.c

copy
123456789101112
#include <stdio.h> int main() { char str[] = "c<>definity"; str[5] = '#'; // Change the sixth character `f` to `#` printf("%s\n", str); return 0; }
question mark

Which of the following correctly declares a string in C?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 6
some-alt