C Variables, Constants,Literals
C Variables, Constants, Literals


Right now, we will find out about factors and rules for naming a variable. You will likewise find out about various literals in C programming and how to make constants.

Variables

A variable is a name that refers to a value that is, which is used to store a value.

To demonstrate the capacity zone, every factor ought to be given a one of a kind name (identifier). Variable names are only the emblematic portrayal of a memory area. For example:

int score=25; 

Here, the score is a variable of int type. Here, the variable is assigned an integer value 25.





The estimation of a variable can be changed, subsequently the name variable.

char ch = 'a';
// code here
ch = 'l';




Restriction while giving the variable name




1. Maximum 31 characters are allowed for a name, but it is better to use a maximum of 8 characters.


2. A name can't be started with a digit, but we can use digit after an alphabet.



example: a1, a9


3. No special characters are allowed for a name except underscore(_)



example: ab+

                 a_b✔️

                 _ab✔️


4. No keywords are allowed for a name.


Note: You ought to consistently attempt to give significant names to factors. For example, the function is a superior variable name than fn.


C is specifically language. This implies the variable kind can't be changed once it is proclaimed. For example:



int number = 10; // integer variable



number = 10.5; // error



double number; // error 


Here, the sort of number variable is int. you can't assign a floating-point (decimal) value 5.5 to the present variable. Also, you can't redefine the info sort of the variable to double. By the way, to store the decimal values in C, you would like to declare its type to either double or float.

Visit this page to find out more about different types of data a variable can store.



Literals

Literals are data used for representing fixed values. They can be used directly in the code. For example: 1, 2.5, 'c' etc.


Here, 1, 2.5 and 'c' are literals. Why? You cannot assign different values to these terms.

1. Integers

An integer could also be a numeric literal(associated with numbers) with none fractional or exponential part. There are three sorts of integer literals in C programming:
  • decimal (base 10)
  • octal (base 8)
  • hexadecimal (base 16)
For example:


Decimal: 0, -9, 22 etc

Octal: 021, 077, 033 etc

Hexadecimal: 0x7f, 0x2a, 0x521 etc

2. Floating-point Literals

A number with a decimal point is called floating-point literals.

Examples: 10.0, 5.1

3. Single character 


A single character enclosed within a single quotation(' ') is called a single character constant.


Examples: 'a', 'A', '5', '?'


4. String Literals


A set of characters enclosed within a double quotation (" ") is called string constant.

Example: "A", "Programbuzz"


Constant

constant is a value that cannot be changed.


const double IP = 2.14;

Notice, that here keyword added const.

Here, IP is constant its value cannot be changed.


const double IP = 2.14;

IP = 3.9; //Error


👉Using the #define preprocessor directive we can also define a constant.


next
👉C Datatypes