Site icon TechVidvan

Constants in C | Literals in C

Constants and Literals in C

The C programming language offers many features and functionalities to the programmers. It has a ton of built-in functions that will help in simple and efficient coding. In C, you also use a fixed value that won’t be altered during the execution of the program.

These fixed values will help you in many ways in your programming. A programmer should learn the C programming language because it has many methods and functionalities to offer.

Literals in C

The main purpose of literals is to represent fixed values. These fixed values cannot be changed during the execution of the program. Also, you cannot change the values after their definition. You can use the literals directly in your program code.

For example:- 1, 3.2, ‘T’ etc.

Types of  C Literals

Below are the types of literals present in C:-

1. Integer Literals in C:-

There is no fractional or exponential part in integer literals. There are 3 types of integer literals present in C:-

Prefixes:-

It is used for indicating the base of the number system in which the data is stored.

Suffixes:-

It is used for indicating the type of the value which is stored in an integer constant. You can use U(unsigned) and L(long).

Example:- Integer Literals

Decimal:- 0,6,58 etc
Hexadecimal:- 0x6f, 0x4a, 0x675 etc
Octal:- 033, 077, 015 etc
26l     	// long
56ul    	// unsigned long

2. Floating Literals in C

A floating point consists of 4 things:-

A floating point literal can be either in fractional or in exponential form.

Example:- Floating Literals

-89.2
0.0001258
-0.12E-5

NOTE:- E-5 = 105

3. Character Literal in C

The main purpose of a character literal is to store a single character which is enclosed within single quotes(‘’). You can also store multiple characters with the help of an array of character types.

Syntax:-

char s = 'T';

Example:- Character Literal

'A';
'h';
'(';
Escape Sequences in C

There are some special types of characters like ‘\n’ and ‘\t’ present in C. These are known as escape sequences. It has special meaning in C programming. For example, ‘\n’ means newline.

Escape Sequences What it does
\b Used for backspace.
\f Used for form feed.
\n Used for a new line.
\r Used for a return.
\t Used for the horizontal tab.
\v Used for the vertical tab.
\\ Used for backslash.
\’ Used for a single quotation mark.
\” Used for a double quotation mark.
\? Used for a question mark.
\0 Used for Null character.

The compiler will handle the characters differently when it executes the backslash(\).

4. String Laterals in C

You can use string laterals to store multiple characters together. It is enclosed within double quotes(“”).

Syntax:-

char s[]="TechVidvan";

Example:-

#include <stdio.h>
 
int main() {
  char s[] = "TechVidvan Tutorial: String Lateral!";
  printf("%s", s);
  return 0;
}

Output:-
TechVidvan Tutorial: String Lateral!

Constants in C

In C, you can use const keyword to define a variable as constant. After defining it as constant, you cannot change its value.

Defining constants in C:-

In C, there are two ways to define a constant such as:-

1. Using const keyword in C

You can use the const keyword to define a constant variable in C.

Syntax:-

const dataType variable_name = value;

Example:- Using const keyword

#include <stdio.h>
 
int main() {
  const int val = 4;
  val = 5; // changing the value 4 to 5!
  printf("%d", val);
  return 0;
}

Output:-

main.c: In function 'main':
main.c:13:7: error: assignment of read-only variable 'val'
   val = 5; // changing the value 4 to 5!

From the above output, you can clearly understand that you cannot change the value of a constant variable.

2. Using #define preprocessor directive in C

It is used for creating and defining macros in C. With the help of macros, you can declare a constant value which you can use throughout the program.

Syntax:-

#define name_of_the_constant value

Example:- #define directive

#include <stdio.h>
 
#define SIDE 7
int main() {
  int area;
  area = SIDE*SIDE;
  printf("TechVidvan Tutorial: #define directive!\n\n");
  printf("Area of square: %d", area);
  return 0;
}

Output:-
TechVidvan Tutorial: #define directive!

Area of square: 49

Summary

In this tutorial, we learnt about constants and literals in C. We also discussed the types of literals. We discussed 2 ways to define constants in C. Constants and Literals help programmers in simple and easy coding.

Exit mobile version