DEV Community

mikkel250
mikkel250

Posted on • Updated on

Strings in C

Defining strings

To define a char, single quotes are used, and it defines a single character.
Anything inside of double quotes is a string. It can be any number of characters. If you want to include double quotes inside of a string, they must be escaped with a backslash \, or the double quotes will end the string.
To include a backslash, it must be escaped with a backslash in front of it printf("this is a backslash \\");
A string in C is ended with what is called a null character \0.
The null character is a string terminator, not to be confused with NULL, which is a symbol or keyword that represents a memory address that doesn't reference anything (i.e. a symbol that represents 'nothing' or 'nothing here').

A string appears in memory as below:
An image of how a string is stored in memory

Ways to define (aka initialize) strings

You can specify the size of the string explicitly, and define it like an array.

char word[7] = {"Hello!"};
Enter fullscreen mode Exit fullscreen mode

But you must remember to leave enough 'space' (one array index) for the null terminator (in this case, it is length 7, even though only 6 characters are present, to account for the null terminator). The compiler will not complain that the size doesn't match or that there is no null terminator, and you will end up with bugs in your program and may not know why.
Another way to create strings is to make the size larger than necessary, and then only fill in part of the array:

char word[20] = {"Hello!"};
Enter fullscreen mode Exit fullscreen mode

The easiest way to do it is to use the shorthand notation, and the compiler will automatically compute the number of elements in the array, and will automatically add the null terminator.

char word[] = "Hello!";
Enter fullscreen mode Exit fullscreen mode
Assigning Values to a string after initializing

Since you cannot assign arrays in C, you cannot assign strings either. The following will produce an error:

char s[100];  //declaration
s = "hello";  // produces ('lvalue required') error
Enter fullscreen mode Exit fullscreen mode

If you performing an assignment, you cannot assign one array of characters to another after it has been initialized. Use the strncpy() function to assign a value to a char array after it has been declared (covered in the next installment in this series).
You can, however, assign single array indices, but this is cumbersome for more than a few at a time:

s[0] = 'h';
s[1] = 'i';
s[2] = '!';
Enter fullscreen mode Exit fullscreen mode

Displaying and using a string as input, and some caveats

To print out a string, use the %s format specifier:

printf("The string is: %s", s);
Enter fullscreen mode Exit fullscreen mode

The same format specifier is used to get user input via scanf():

scanf("%s", input);
Enter fullscreen mode Exit fullscreen mode

Note that no ampersand is needed for the variable name.
Another thing to note is that scanf() will only read up until the first space, so another function is used to read in strings with spaces.

Another thing to note is that the equality operator == cannot be used to directly compare strings - the "string compare" strcmp() function is used for this (covered in the next installment in this series).
You can, however, compare string character by character if you wish.

String constants using the define statement

Constants can be defined as string at the beginning of a program to act as placeholders for values that you don't want to change, and are available globally throughout the program. They are declared using the pound symbol, and by convention are uppercase, followed by the data it will hold, and do not need a semicolon.
It can be used for numbers and also to hold strings or characters.

C99 also added a second way to add constants, using the const keyword, which allows you to define the data type, and is more flexible, but in older programs you may still see define being used. Using a const is a good way to handle standard messages because the compiler will throw an error if the definition is changed after declaration.

The third way is to create symbolic constraints using enums, which won't be covered here, but is another handy way to do so that constrains the data type that can be assigned to them.

For example:

#include <stdio.h>
#include <string.h>

#define TAXRATE 0.15
#define OOPS "Oh noes! Something went wrong!"

int main()
{
  float myIncome = 85000.25;
  myTaxes = myIncome * TAXRATE;

  return 0;
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)