loading...

Strings in C

mikkel250 profile image mikkel250 Updated on ・3 min read

Basics of the C programming language (20 Part Series)

1) Functions in C 2) Arrays in C 3 ... 18 3) Strings in C 4) String functions in C 5) Setting up VSCode for C in linux 6) Pointer basics in C 7) Advanced pointer operations in C 8) Pointers: Const, Void, and Arrays 9) Pointers arithmetic 10) Pointers and strings 11) Passing by value, passing by reference in C 12) Dynamic memory allocation in C 13) Structures in C 14) Structures and arrays in C 15) Nested structures in C 16) Structures and pointers in C 17) Structures and functions in C 18) File input and output in C 19) Reading file contents in C 20) Writing to files 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!"};

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!"};

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!";
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

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] = '!';

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);

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

scanf("%s", input);

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;
}

Basics of the C programming language (20 Part Series)

1) Functions in C 2) Arrays in C 3 ... 18 3) Strings in C 4) String functions in C 5) Setting up VSCode for C in linux 6) Pointer basics in C 7) Advanced pointer operations in C 8) Pointers: Const, Void, and Arrays 9) Pointers arithmetic 10) Pointers and strings 11) Passing by value, passing by reference in C 12) Dynamic memory allocation in C 13) Structures in C 14) Structures and arrays in C 15) Nested structures in C 16) Structures and pointers in C 17) Structures and functions in C 18) File input and output in C 19) Reading file contents in C 20) Writing to files in C

Posted on Dec 20 '19 by:

mikkel250 profile

mikkel250

@mikkel250

I've been tinkering with computers since I was a teen. I'm currently pivoting from my current role as Tech Support manager to Full Stack Web Developer. I'm actively seeking employment in the field.

Discussion

markdown guide