Exforsys

Home arrow Technical Training arrow C Tutorials

C String Constants

Page 3 of 5
Author: Exforsys Inc.     Published on: 2nd Mar 2006    |   Last Updated on: 6th Jul 2011

C Programming - Constants and Identifiers

String Constants

A string is one or more characters. String constants (also called literals) use a double quote instead of single quote to delimit the start and end of the string. Each character in the string constant can be any character allowed as a character constant. You can use all the escape sequences for character constants with strings too. Let us see some examples:

Ads

Sample Code
  1. char * menu_name = "Main Menu";
  2. int edit_count = 0;
  3. if (strcmp(menu_name, "Edit Menu") == 0)
  4. {
  5.      printf("Edit menu counter = %d\n", ++edit_count);
  6.      edit();
  7. }
Copyright exforsys.com


In this code "Main Menu", "Edit Menu" and "Edit menu counter = %d\n" are all string literals.

If you want to include a double quote character inside a string use the escape sequence backslash double quote like this:

Sample Code
  1. const char * s = "This string has \" double quotes\" ";
Copyright exforsys.com


If you have a very long string literal there are a couple of ways to break it up. Two or more string constants next to each other will be treated as one string constant, so you could break up the long string literal into smaller ones and put them next to each other.

Another way is to use a backslash at the end of a line inside a string literal; then the string can be continued on the next line. Here is an example:

Sample Code
  1.    #include <stdio.h>
  2.    
  3.    void main()
  4.    {
  5.            printf( "Once upon a midnight "
  6.                    "dreary,\nwhile I "
  7.                    "pondered weak and weary,\n" );
  8.            printf( "Over many a quaint and curious\nvolume of \
  9.   forgotten lore,\n" );
  10.   }
Copyright exforsys.com


Here is the output of program:

Sample Code
  1. Once upon a midnight dreary,
  2. while I pondered weak and weary,
  3. Over many a quaint and curious
  4. volume of forgotten lore,
Copyright exforsys.com


On lines 5, 6 and 7 the string constants are added together since they are right next to each other without any other syntax between them. You can see in the output that the line is only broken for the newline character between dreary and while. As far as printf() is concerned it is passed one long string , not 3 separate strings.

On line 8 you can see that the last character of the line is a backslash. Then the string continues on line 9. Because of the backslash at end end, the string on line 9 is added to the end of the one on line 8 (without a backslash or newline). With the backslash method you do not need to use the double quote to start the string again on line 9. In the output, the line is only broken for the newline between curious and volume.

Just as there are wide characters to hold extended characters, there are wide string literals to hold strings containing extended characters. You indicate a wide string literal by prefixing it with an L just as for wide characters:

Sample Code
  1. wchar_t widestring[] = L"This is a wide string literal.";
Copyright exforsys.com


The string above does not contain any extended characters but wide strings can hold basic characters as well as extended characters.

Strings in C are terminated by the null character, which has a numeric value of 0. String literals are also automatically terminated by the null character. This program shows that the null character is added automatically:

Sample Code
  1.    #include <stdio.h>
  2.    #include <string.h>
  3.    #include <stddef.h>
  4.    #include <wchar.h>
  5.    #include <locale.h>
  6.    
  7.    void main()
  8.    {
  9.            setlocale( LC_ALL, "" );
  10.  
  11.           char hello[] = "Hello";
  12.           wchar_t whello[] = L"H\x113ll\x151";
  13.  
  14.           printf( "hello = \"%s\"\n", hello);
  15.           printf( "whello = \"%ls\"\n", whello);
  16.  
  17.           printf( "length of hello = %d characters\n", strlen( hello ));
  18.           printf( "length of whello = %d characters\n", wcslen( whello ));
  19.  
  20.           printf( "size of hello = %d bytes\n", sizeof( hello ));
  21.           printf( "size of whello = %d bytes\n", sizeof( whello ));
  22.   }
Copyright exforsys.com


The output of this program follows:

Sample Code
  1. hello = "Hello"
  2. whello = "H&#275;ll&#337;"
  3. length of hello = 5 characters
  4. length of whello = 5 characters
  5. size of hello = 6 bytes
  6. size of whello = 24 bytes
Copyright exforsys.com


Line 11 declares character array hello and initializes it with the string literal "Hello". Line 12 does the same for wide character array whello but uses extended characters for e and o. The strlen() function is called on line 17 to get the length of the string, which is 5 characters. Functions like strlen() that work on plain character strings generally will not work on wide character strings. The wcslen() is the wide character counterpart to strlen(); it also returns 5 characters as is normal. Also note on line 15 the format specifier for a wide character string is %ls instead of %s.

Ads

However on line 20 sizeof() returns 6 bytes, not 5. This is because the compiler automatically added an extra null byte at the end of the string. The sizeof() on line 21 returns 24 bytes. That is because on this system a wide character takes 4 bytes. The 5 wide character string plus the wide null character is 6, times 4 bytes for each character gives 24 bytes.



 
This tutorial is part of a C Tutorials tutorial series. Read it from the beginning and learn yourself.

C Tutorials

 

Comments