Literals in C Language
×


Literals in C Language

355

In C programming, literals are fixed values that are directly written into the source code.

They represent fixed data values that do not change during Program Execution.

Types of Literals in C 

1Integer Literals

2Floating Point Literals

3Character Literals

4String Literals

5Boolean Literals

Integer Literals 

 These are a Whole number of literals without any fractional part.

 They can be represented in different number systems.

 Decimal Literals (base 10): The default format for integer literal is given as.

int decimal = 10;

 Octal Literals (base 8): In C, octal literals, which are base 8 numbers, start with '0' followed by digits from 0 to 7, allowing representation of values using only these digits.

int oct=012;

 Hexadecimal Literals (base 16): Hexadecimal is represented by starting with "0x" or "0X" followed by a sequence of hexadecimal digits.

 Binary Literals: Binary Literals are Represented starting with 0b or 0B followed by binary digits.

int binary= 0b1010;

For example:

#include<stdio.h>

int main()
{

	int decimal =10;
	int oct= 012;
	int hex=0xA;
	int binary =0b1010;

	printf("Decimal Literal : %d\n", decimal);
	printf("Octal Literal : %d\n", oct);
	printf("Hexadecimal Literals : %d\n", hex);
	printf("Binary Literals : %d\n", binary);
	
	return 0;
	
}
Output:

Decimal Literal: 10
Octal Literal: 10
Hexadecimal Literal : 10
Binary Literal: 10

Floating Point Literals 

Floating Point Literals in C represent numbers in fractional parts.

 Floating-point literals in C allow you to express numbers with decimal points in your code.

For example:

float pi =3.14;

In C, the number '3.14' is a floating-point literal, representing the constant value of pi, which includes decimal.

#include<stdio.h>

int main()

{
	float pi=3.14;
	printf(" Value of pi : %f", pi);
	
	return 0;
}
Output:

Value of pi: 3.1400

Character Literals 

 Character Literal in C represents an Individual character.

 In simple words, they allow you to specify a single character within your code.

For example:

#include<stdio.h>

int main()
{
	char letter = 'R';
	printf("Value of letter = %c\n", letter);

	return 0;
}
Output:

Value of letter = R

String Literals 

 String Literals in C are a sequence of Characters enclosed within double quotes.

 They represent text or string of characters.

For example:

char *message = "Hello C Programming";

using Pointer:

// Program using Pointer
#include<stdio.h>
 int main()
{
	char *message ="Hello C Programming";
	printf("Message = %s\n", message);
	return 0;
}
Output:

Hello C Programming
Using Array:

// Program using Array
#include<stdio.h>
int main()
{
	char message[] ="Hello C Programming";
	printf("Message = %s\n", message)
	return 0;
 }
Output:
Hello C Programming

Boolean Literals 

Boolean literals in C represent the two truth values: true and false.

In simple terms, they allow you to express whether something is true or false in your code.

Boolean literals in C are represented using integers value, with 0 signifying false, while any non-zero value stands for true.

For example:

#include<stdio.h>
int main()
{
	int is_hot = 1;
	int is_cold =0;
	printf("Is it hot? = %d\n",is_hot);
	printf("Is it cold? = %d\n",is_cold); 
	return 0;
}
Output:

Is it hot? = 1

Is it cold? = 0



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments