C Variables, Constants and Literals - C Programing - Rana Tutorials

Rana Tutorials

Saturday, May 23, 2020

C Variables, Constants and Literals - C Programing

In this tutorial, you have  learn about variables and their  rules for naming a variable. You will also learn about the  different literals in C programming and how to create constants. in C programing.

Variables

In programming, a variable is a container(Container means storage area in program) (storage area) to hold data.

To indicate the storage area, each and every variable should be given a uniqueand diffrent  name (identifier). Variable names are only the symbolic representation of a memory location. For example:

int playerScore = 95;

Here,  playerScore is a variable of  int type. Here, the variable is assigned with a integar value  95

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

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

Rules for naming a variable
A variable name has only have letters (both uppercase and lowercase letters), digits and underscore.
The first letter of a variable should be a letter or a underscore 
in C programing there is no rule on how long a variable name (identifier) can be.otherwise , you may run into problems in some compilers if the variable name isgreater than than 31 characters.

Note: You should always try to give meaningful names to variables. For example:  firstNameis a very good  variable name than fn

C is a strongly interseting  typed language. it means when  variable type declared  it cannot be changed once it is . For example:

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

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

Literals

Literals are data used for assign fixed values. These can be used directly in the code. For example: 12.5'c'

Here, 1, 2.5 and 'c' are literals. Why You can not take or assign different values to these terms?

1. Integers

An integer is a numeric exact (associated with numbers) without any fractional or exponential part. In C programing three types of integer exact  in C programming:

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 number having fractional part is called a floating point number.Afloat Data type occupies 4 byte of memory spacefloat value can store values with 6 to 7  digit of precision. For example:

-2.0
0.0000234
-0.22E-5

Note: E-5 = 10-5

3. Characters


character data type is used to represent single character. A character data type occupies 2 byte of space in memory.
For example: 'a''m''F''2''}' etc.


4. Escape Sequences


Sometimes, it is necessary to use  those characters which  cannot be typed or has special meaning in C programming. For example: newline (enter), tab, question mark etc.

In order to use these characters, escape sequences are used.

Escape Sequences
Escape SequencesCharacter
\bBackspace
\fForm feed
\nNewline
\rReturn
\tHorizontal tab
\vVertical tab
\\Backslash
\'Single quotation mark
\"Double quotation mark
\?Question mark
\0Null 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


For storing more than one character a sting is used e.g char name[11]will allocate 11 bytes to hold a string >In case of a string the last byte is always used to store the string terminator known as null character.

"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

.Constant are those tokens whose value do not change during the program execution

const double PI = 3.14;

If you want to define a variable whiach value can not be changed. fot it is we use  constantkeyword.This will create a const. For example,


Notice, we have added keyword  const

Here,  PI(3.14)it is a symbolic constant; its value cannot be changed.

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



No comments:

Post a Comment