Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Char and String | Tipos de Dados e Variáveis
Fundamentos de C

Char and String

Deslize para mostrar o menu

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 arr[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.

char and string

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

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

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

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?

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 6

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Seção 2. Capítulo 6
some-alt