×

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

#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.

If Statement in C

Decision Control Statements We frequently desire one set of instructions to be carried out in one circumstance while another set of instructions to be carried out in a completely different circumstance....

3 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.

C Program to Find Largest Number Using Dynamic Memory Allocation

This tutorial will teach us how to locate the highest number a user has ever input into a dynamically allocated memory. C Program: #include <stdio.h> #include <stdlib.h> int main ()  {   int no;   double *data_n;   printf...

1 minute 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.

Diffie-Hellman Algorithm in C

Background: A method of public-key encryption known as elliptic curve cryptography (ECC) is based on the algebraic structure of elliptic curves over finite fields. To ensure equal security, ECC encryption requires fewer...

4 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.

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.

Factorial Program in C Using While Loop

Before looking into the mechanism of factorial program, first, you have to understand about the working of a while loop. While Loop The syntax that has been used for the “while” loop...

4 minutes read.

C program to compare the two strings

C program to compare the two strings Strings can be compared either by using the string function or without using string function. First, we will look at how we can compare the strings with...

4 minutes read.

Write a program that produces different results in C and C++

In this tutorial, we'll explore several programs that, depending on whether they are developed using C or C++ compilers, will produce varying outputs. There are numerous similar programs, but we will just...

2 minutes read.

Snake Game in C

Snake game in C is basic desktop console program which was created in 1970’s. It is an old classic game which was played by every child across the world. This...

4 minutes read.

String Handling functions in C

String :- The String is the collection of characters. Every String ends with the null character, and the String is enclosed in the double quotations .ie, "javaTpoint". If we see any...

5 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.

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes read.

Random function in C

Random function in C In the C programming language, the rand() is a function used for Pseudo Random Number Generator (PRNG). The random number generated by the rand() function is not...

4 minutes read.

C Program to find the Roots of a Quadratic Equation

What is Quadratic Equation: Equations of degree 2 in polynomial form are called quadratic equations. A, B, and C are the coefficient variables in the equation, which is written as ax2...

3 minutes read.

Multithreading in C interview questions

Q1. What exactly is a thread? Ans: A thread is a brief sequence of instructions intended to be planned and carried out by the CPU apart from the parent process. Q2. Can...

4 minutes read.

How to use sine() function in C

What is Function? The function is a set of statements. It takes input and performs some computation to produce output. The process is a set of codes only achieved when it is...

3 minutes read.

Results of Comparison Operations in C and C++

In this tutorial, we will explore comparison operators and how the system should compare an incoming value supplied as a context parameter to a given value or range of values. A...

2 minutes read.