in , ,

C Variables, Constants and Literals

C Variables, Constants and Literals

In this tutorial, you will find out about variables and rules for naming a variable. You will likewise find out about various literals in C programming and how to make constants.

Variables

In programming, a variable is a holder (stockpiling territory) to hold information.

To show the storage area, area, each variable should be given a unique name (identifier). Variable names are only the emblematic portrayal of a memory area. For instance:

int playerScore = 95;

Here, playerScore is a variable of int type. Here, the variable is assigned an integer value 95.

The value of a variable can be changed, hence the name variable.

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

Rules for naming a variable


A variable name can just have letters (both uppercase and lowercase letters), digits, and underscore.

The first letter of a variable ought to be either a letter or an underscore.
There is no rule on to what extent a variable name (identifier) can be. In any case, you may run into issues in certain compilers if the variable name is longer than 31 characters.

Note: You ought to consistently attempt to give significant names to factors. For instance: firstName is a superior variable name than fn.

C is a strongly typed language. This implies the variable sort can’t be changed once it is pronounced. For instance:

int number = 5;      // integer variable
number = 5.5;        // error
double number;       // error

Here, the type of number variable is int. You cannot assign a floating-point (decimal) value 5.5 to this variable. Also, you cannot redefine the data type of the variable to double. By the way, to store the decimal values in C, you need to declare its type to either double or float.


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 is a numeric literal(associated with numbers) with no fragmentary or exponential part. There are three kinds of number 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

In C programming, octal starts with a 0, and hexadecimal starts with a 0x.


2. Floating-point Literals

A Floating-point literal is a numeric exacting that has either a partial structure or an example structure. For instance:

-2.0
0.0000234
-0.22E-5

Note: E-5 = 10-5


3. Characters

A character literal is created by enclosing a single character inside single quotation marks. For example: ‘a’, ‘m’, ‘F’, ‘2’, ‘}’ etc.


4. Escape Sequences


Here and there, it is important to use characters that can’t be composted or has unique significance in C programming. For instance: newline(enter), tab, question mark, and so on.

So as to use these characters, get away from successions are used.

Escape Sequences
Escape Sequences Character
\b Backspace
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\' Single quotation mark
\" Double quotation mark
\? Question mark
\0 Null character

For example, \n is used for a newline. The backslash \ causes escape from the normal way the characters are handled by the compiler.


5. String Literals

A string literal is a sequence of characters enclosed in double-quote marks. For example:

"good"                  //string constant
""                     //null string constant
"      "               //string constant of six white space
"x"                    //string constant having a single character.
"Earth is round\n"         //prints string with a newline

Constants

If you want to define a variable whose value cannot be changed, you can use the const keyword. This will create a constant. For example,

const double PI = 3.14;

Notice, we have added keyword const.

Here, PI is a symbolic constant; its value cannot be changed.

const double PI = 3.14;
PI = 2.9; //Error

Please feel free to give your comment if you face any difficulty here.

For more Articles click on the below link.

https://www.worldofitech.com/c-introduction/

salman khan

Written by worldofitech

Leave a Reply

C Keywords and Identifiers

C Keywords and Identifiers

C Data Types

C Data Types