C Programming

Literals in C

You must have heard about the constant variables, whose value cannot be changed. The constant values allocated towards the relentless variables are known as Literals. The literals could be a set of values that could not be changed. It still has memories. However, it has no links in the form of variables. As an illustration, Const int =10; of a constant variable statement where 10 is an int literal. C literals are mostly cast-off to simplify code to get around situations by declaring a variable is not a preference. There are different kinds of literals used in the C programming language. We will discuss each one of them separately.

Integer Literal

A value made up of integers or digits is known as an integer variable. Even throughout the process of carrying out a code, such values remain constant. It does not reflect the value in proportional or exponential form. A numerical constant may have a positive or negative value. The variable must be contained by the defined data type’s scope. Inside a numerical constant, no blanks or asterisks are permitted. Open the shell-terminal in the Linux system using Ctrl+Alt+T. Now you have to make a new c type of file to write a C language code. So, use the “nano” command to create a file “test.c”.

$ nano test.c

After the opening of the nano file editor, type the below script of C language in it. This script is showing a single header library included in it. After that, the main function starts. The main method contains a constant type integer variable named “x” with integer value “54” in it. At the very next line, the print statement is printing out the constant value of variable “x”:

Save the file using the “Ctrl+S” command and quit. Now, it’s running for the compilation of the code. For this, perform the below query in the terminal:

$ gcc test.c

It’s time to run the file using the “a.out” query. The output image is simply presenting the constant value “54” of integer type variable “x” using a print statement.

$ ./a.out

Floating-point Literal

This is a kind of literal that just includes the floating-point value or true numbers. Such actual numbers have a variety of components, including numeric, real, and exponential bits. The numerical or exponential representation of the floating-point literal should be defined. They’re often referred to as true constants. A decimal point or an exponential may be found in a true constant. It could be either positive or negative. Inside a true constant, commas and blank space aren’t permitted. Let’s have a simple example of floating-point literal. Open the same file “test.c” to use it for floating-point literal discussion.

$ nano test.c

Now we have to update the code as shown in the image. One header and one main function are still there. In the main method, we have defined three float type variables “x”, “y”, and “z”. While two of them have been defined as literals using “const” at the start of them. Both of the float literals have floating-point values. The third float variable is not literal but simple. The third variable “z” has been using the sum of both the literal variables. A print statement has been used in the last line of code to output the sum of both the floating-point literals within the “z” variable. And the main method will be closed.

Compilation of the test.c file has been done using the stated-below “gcc” query in the console.

$ gcc test.c

As the compilation of a file doesn’t show any error, this means the code is correct. Now run the file “test.c” using the “a.out” instruction below. The output shows a floating-point result as the sum of both literal variables “x” and “y”.

$ ./a.out

Character Literal

Just one single quote character is referred to as a character constant. It is 1 unit in size and could only hold one character. A letter might have been any alphabet (x, c, D, Z, etc.), any distinct character (&, $, #, @, etc.), or even a solitary digit (0–9). It could correspondingly be any escape series symbol, such as space “ ”, a blank or null character “o”, or even a new line “n”, and so on.

Let’s have a character literal example. So open the same file “test.c”.

$ nano test.c

Now type out the same code with a little update in it. Just change the “float” with the “char” keyword and specify a new variable “a” with a value “Aqsa”. This value is not a single character value, so the output must show some error upon compilation.

Compilation of the file “test.c” using the “gcc” compiler command shows an error of “multi-character constant.”

$ gcc test.c

Now update the code again. This time, we have been using a single special character as the value of a character literal “c”. Save and quit the document.

Compile again via “gcc” as:

$ gcc test.c

Now on running the code, you will get an output perfectly.

$ ./a.out

String Literal

Double quotes have been used to wrap string literals. Simple words, escape series, and standard characters are among the characters in the string literal. String literals can be used to split an extensive string into several lines. Also, whitespace could be used to divide them. Here is a simple illustration of a string literal. Open the same file once again.

$ nano test.c

Now the same code has been updated with a string literal. This time we have used the “name” variable array as a string literal with a value “Aqsa”.

Compilation with a “gcc” command has proceeded correctly.

$ gcc test.c

Upon execution, we can see the output of a string literal as below:

$ ./a.out


The concept of literals is relatively easy to understand and simple to implement on any Linux system. All the above queries work fine with all the distributions of Linux.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.