×

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:

  1. Integer literals
  2. Float or real literals
  3. Character literals
  4. 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 literalCharacter 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 characterprintf (‘\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.

Related Topics

What is Linked List in C

Linked list Similar to an array, a linked list is a linear data structure that stores a chain of nodes with two fields of memory in each node. Where first memory...

7 minutes read.

Deadlock Detection Program in C

What is Deadlock? A deadlock is a circumstance where a group in the process is halted because they are each holding onto resources while waiting for other methods to obtain them. Think...

5 minutes read.

Bigint (BIG INTEGERS) in C with Example

The maximum number of digits that a long, long int can have in C/C++ is 20. The issue is how to store the 22-digit number, which is difficult to do...

9 minutes read.

Infinite loop in C

Definition of infinite loop The infinite loop is a loop that does not get dismissed because of its looping construct. A continuous output or no output are the two possible outcomes...

3 minutes read.

Flow Chart of Do while loop in C

This is a flowchart that represents the process of executing the Do while loop in the C programming language Generally, as we know there are three main components of Do While...

3 minutes read.

Entry Control Loop in C

Initially, an entry control loop verifies the termination condition at the entry point. After that, its control passes to the main body of the while or for loop if the...

3 minutes read.

Runtime vs compile time in C

In the C programming language, the often used terms in every step consist of compile time and runtime. The compile time is referred to the source code that will be...

4 minutes read.

Expressions in C

Expressions in C: In the C programming language, an expression defines a formula in which the operands are linked to each other by using operators to compute the value. The operand...

4 minutes read.

#error #pragma in C

#error, #pragma in C #error, also known as the error directive in the C language. It will not allow you to make any compilation fail and immediately issues a statement which...

4 minutes read.

Find median of 1D array using function in C

Introduction: Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted...

3 minutes read.

Why C is a middle level language

The C programming language is generally referred to as a high level language but we glance through the backend of C, i.e the working of C as a programming language...

4 minutes read.

Sum of digits in C++

Sum of digits in C++ There are various ways to find the sum of the digits of a number in C++. We can use containers like arrays or other simple cases...

4 minutes read.

Palindrome Number in C

What is a Palindrome? A number that doesn't change when reversed is known as a palindrome. We reverse a number and compare it to the original number to determine whether it is...

4 minutes read.

Bit Fields in C

The size of a structure in C is specified in bits. The primary goal of it is to use memory efficiently, once we understand that a bit's value must fall...

3 minutes read.

memcpy() in C

Introduction: Here we discuss about the memcpy() function in C. The memcpy() function is also called the Copy Memory Block function. Used to create a copy of a specific drawing...

4 minutes read.

Memory layout in C

Memory layout in C The C language is designed so that it becomes easier for a programmer to decide the amount of memory they want to use in a program. C program...

4 minutes read.

Find a subarray with a given sum.

Find a subarray with a given sum. The simple solution is to recognize all subarrays one by one and to check each subarray's sum. The quick solution follows the following program. Algorithm: From...

4 minutes read.

Calloc in C

The calloc() is a library function in C which is used for memory allocation. The calloc() function dynamically allocates multiple blocks of memory to complex data structures. This includes data structures...

3 minutes read.

Else If Ladder in C

What is Else If Ladder: When there are multiple options and a user has to decide from the options, we use Else If Ladder. Basically, it is an extension of if...

3 minutes read.

How to find square root in C Language

Before understanding the square root program in C language, one must know about the square root of a number. A square root is a mathematical term. The square root of a...

5 minutes read.