String literals in C
Constants refer to the fixed values of the program that may not alter during the compiling ad execution. These fixed values are known as literals. In other words, the values assigned to each constant variable are known as literals.
Integer constants, floating constants, character constants, string literals, and enumeration constants are all instances of basic data types that may be used as constants.
Constants are considered equal to regular variables, and that therefore their values cannot be changed after they've been declared and declared.
Constants and literals are interchangeable terms, such as, consider an example “const int = 5;” is a constant variable and the value five is referred to as a constant integer literal.
There are four types of literals in the C programming language:
- Integer literals
- Float or real literals
- Character literals
- String literals
Integer literals: an integer literal can be a decimal, octal, or hexadecimal constant. The prefix specifies the base or radix: 0 represents octal, 0x or 0X means hexadecimal, and nothing for decimal. It can also have a suffix that can be a combination of U or L, U for unsigned integer, and L for long.
The suffix can be in any position and can be uppercase or lowercase. An integer portion, a decimal point, a fractional part, and an exponent part make up a floating point literal. It can also be represented either as decimal form or exponential form.
Whenever the decimal form is represented, a decimal point or exponent should be included. When representing an exponential form, integer part or fractional part should be included.
Character literals: these are enclosed within single quotes; for example, ‘X Y Z’ can be stored as a simple variable of character literal. It can also be a plain character (‘X Y Z’), an escape sequence (‘\n’), or a universal character (‘0213’).
String literals: string literals or string constants are the types of constants enclosed within double quotes (“ ”). Plain characters, escape sequences, and global characters are one of the characters in the string that are identical to character literals.
By splitting them with white spaces, a large string may be broken down into multiple lines. String literals can store numerous characters and can accommodate the special characters and escape sequence. String constant occupies byte space in memory, that is, the total number of the characters + 1.
In addition, byte space used to store the constant includes a NULL; that is, it will be used for \0 character. The NULL value is added explicitly at the end of the string to specify the termination of the string.
A programmer can also concatenate string literals using the “+” operator.
E.g.:
“I love C tutorials!”
“I love Tutorials and Examples \n” + “I love to code.”
The given examples are best suited for string literals that can be written in different forms and then can be concatenated.
‘X Y Z’ and “X Y Z” both look alike but, in reality, are different from each other.
‘X Y Z’ is a character constant occupying only one byte of space in the memory location, while “X Y Z” is a string constant, and it occupies 2 bytes in the memory space.
For instance, consider a string that can be written down in different ways:
“Tutorials and examples”
“Tutorials, \
examples”
“Tutorials,” “and” “examples”
The above examples showcase how a sentence can be further be split into three different ways, all having the same meaning.
Defining literals:
Two ways can be followed while defining C constants or literals -
- Usage of #define preprocessor.
- Usage of the const keyword.
Syntax:
char stringValue[] = “C tutorial on sting literals”;
Eg:
#include <stdio.h>
int main()
{
const char str[] = “C tutorial \n on \n String \t literals” ;
printf( “%s” , str);
return 0;
}
Output:
C tutorial
on
String literals
Difference between string literal and character constant:
String literal | Character constant |
String literals are enclosed within double quotes (“X Y Z ”) | Character literals are enclosed within single quotes (‘X Y Z ’) |
A pointer represents it to the first character. | An integer characterizes it; that is, it incurs ASCII values. |
printf( “\n”) is acceptable by the C language as it represents an escape character | printf (‘\n’) is not acceptable since it will be considered as an integer. |
String literals represents alphabets (both cases), numbers (0 to 9), special characters (@,?, & etc.) and escape sequences like \n, \b etc. | A character constant like '!' represents a single character. |