×

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

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

Recursion in C

Recursion in C: In the C programming language, the concept known as recursion exists that is a technique in which a function calls itself either directly or indirectly. It allows...

4 minutes read.

#define in C

#define in C In the C programming language, the preprocessor directive acts an important role within which the #define directive is present that is used to define the constant or the...

3 minutes read.

Armstrong Number in C

The Armstrong number is defined as the sum of each of its digits to the power of the number base for the each given number with any given number base....

3 minutes read.

C printf and Scanf

Input-Output functions in C Programming In C Language, the printf() and scanf() are inbuilt library functions that used for input and output. It is defined in the header file“<stdio.h>”. printf() Function: In C...

2 minutes read.

Control statement in C

What is a control statement? A control statement helps us to control the flow of the program. The control statement helps us to execute the program's instructions in a user-defined order....

8 minutes read.

Bank Account System in C using File Handling

This tells about the creation of bank account system using the C language and handling of files in C. Approach Let us see the approaches and the functions how they are covering...

5 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

Loop Questions in C

Question 1: What is For loop syntax? Ans: The syntax that has been used for the ‘for’ loop in C programming language is: for (initialization statement /*for providing a value to variables*/; test...

19 minutes read.

C vs Java Strings

String in C In C, we can define a string as a bunch of characters. A character array is distinguished from a string by the presence of the special character '\0'...

5 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

Selection sort in C

In the C standard, the selection sorting technique exists where the smallest among the unsorted elements of the array is selected at each time and is then inserted into its...

3 minutes read.

Goto and Labels in C

Introduction Goto in C: The goto statement is known as the jump statement in C. The goto is used to transfer the control of a program to a predefined label. The goto...

3 minutes read.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes read.

Built-in functions in C

The function is a set of instructions and statements enclosed in the "{}" delimiter. In c, there are two types of functions. Pre-define functions/ Built-in functionsUser define function. Built-in functions in C:- These...

8 minutes read.

Commenting in C

Commenting in C Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that...

3 minutes read.

Static in C

Static is a keyword that is used in the C programming language. It can be used both as variables and as functions. In other words, it can be declared both...

3 minutes read.

OpenGL in C

OpenGL stands for Open Graphics Library. It is a low-level, widely supported modeling and rendering software package that is widely available across all the platforms. It is an API used...

4 minutes read.

Fibonacci series program in C using Recursion

In this tutorial, we’ll explore how to utilise recursion to build the Fibonacci sequence in C language. What does the term "Fibonacci Series" mean? The following number in a Fibonacci number series is...

2 minutes read.

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

4 minutes read.