Variables And Constants In C Programming Language | LearntHub

1. Variable:-

A variable is a data name that may be used to store a data value, Unlike constants that remain unchanged during the execution of a program,a variable may take different values at different times during execution.
Variables and constants
A variable name can be chosen by the programmer in a meaningful way so as to reflect its function or nature in the program.
Some example of such names:
  • Average
  • height
  • Counter_1
  • class_strenght etc.

Conditions Of Variables:-

Variable names may consist of letters , digits , and the underscore character, subject to the following condition:
1. They must start with a letter. Some systems permit underscore as the first character.
  1. Union
  2. Pointers
  3. File Handling
2. ANSI standard recognizes a length of 31 characters, however length should not be normally more than eight characters, since only the first eight characters are treated as significant by many compiler.
3. Uppercase and lowercase are important. For example the variable Total is not the same as total or TOTAL.
4. It should not be a keyword.
5. White space is not allowed.

Variables Declaration:-

The variable represents the memory location and with its help, we can also change the access and access to memory location in programming, we can do this procedure in Variable Declaration.

Scope Of Variables:-

How far a variable can work in a program.  This is its scope. According to Scope the variables are divided into two categories:
1. Local Variables
2. Global Variables

Local Variables:-

Local Variables inside the function. Local Variables are the function within which they are visible only. The default value of Local Variables is 'garbage value'.
In other word, Local variables are variables that are defined in a small block of a program such as a function or control statement can be blocks or any other block. The use of such variables is limited only to this block. For example, if you have created a variable in a function, then you cannot access the variable outside the function.

Global Variables:-

Global variables are those variables whose scope is in the whole program. 
Anybody can access these variables anywhere in the entire program.

2. Constant:-

Constants in C refer to the fixed values that do not change during the execution of a program. Constant's value is fixed. If an attempt is made to change its value, an error occurs in the program. It can be of any data type. It is also called Literals. Constant is also Pointer.

Type Of Constants:-

There is many types of constants which written below:
  • Integer Constant
  • Floating-point / Real Constant
  • Character Constant
  • String Constant
Example:- This is a simple example of constants:

Integer Constants:-

An integer constant refers to a sequence of digits(whole numbers). There are three types of integers  namely decimal integer, octal integer and hexadecimal integer. Decimal integers consist of a set of digits, 0 through 9, preceded by an optional- or +sign.
Valid examples of decimal integer constants are: 
123 - 321 +78 O 654321 etc.
Example: Source code

Floating Point/Real Number:-

Integer numbers are insufficient to represent quantities that vary continuously, such as distances, heights, temperature, prices, and so on. These quantities are represented by numbers containing fractional parts like 17.548. Such numbers are called real ( floating point) constants.
Further examples of real constants are:
0.0083-0.75  435.36 +247.0 etc.

Character Constants:-

Character Constant acts like normal Variable Character Constant takes only single character. Character constant is also used with Escape Sequences.
Example:- a,b,c,f,h,x,z,} etc
Example: Source code

String Constants:-

A string is a sequence of characters enclosed in literal double quotation marks String Constant takes two and multiple characters. String Constant is also used with sequences.
Example: Source code

Post a comment

0 Comments