Constants vs Variables in C language - C tutorials

                          

Variables and Constants in C language

In this section i am clearing concept about  Constants vs Variables in C language to become expert in determining or assigning of variables and constants. Variables and constant are the main identity of C programming language or any other language because without variables and constant we don't make any program or softwares so these things are important in this..

Constants vs Variables in C language 

C tutorials - Variables :

In the C programming language, variables are the designated memory regions where the user can store various values of the same datatype while running a program. In other words, a variable is the name given to a memory area where different values of the same data type can be stored. To put it another way, a variable can be thought of as a storage space for values of the same data type while a program is running. The following is a formal definition of a data type:

 

Variable is the name given to a region in memory where different values of the same datatype can be stored while a program is running.

A  storage location paired with associated symbolic name which has value.

Before being utilized, every variable in the C programming language must be stated in the declaration section. Every variable needs to have a datatype that specifies the types and ranges of values that may be stored as well as the amount of memory that can be allocated.


The underscore symbol, characters, and numbers can all be found in variable names

The guidelines for choosing a variable name are as follows:

1. The name of a variable shouldn't begin with a digit.

2. It's not a good idea to name variables with keywords.

3. A variable name should only contain the underscore (_) symbol.

 

4. Although a variable name can be any length, the compiler only takes into account the first 31 characters.

Variables Rules :-

a. Variables are case sensitive
b. 1st character is alphabet or '_'
c. no comma/blank space
d. No other symbol other than '_'


Constants vs Variables in C language - Announcement of Variable :

A variable's declaration instructs the compiler to allocate the necessary amount of memory with the supplied variable name and restricts the types of values that can be stored there to those that match those datatypes. Declarative statements can be made inside any block or function in the C programming language, as well as before the function as global variables. But it must come first in a block or function.


Datatype variableName in a declaration; example

Integer number; The aforementioned declaration instructs the compiler to allocate 2 bytes of memory with the name number and only allow integer values to be stored there.


Ex .  

#include <stdio.h>
int main()
{
// declaration and definition of variable 'a123'
char a123 = 'a';

// This is also both declaration
// and definition as 'b' is allocated
// memory and assigned some garbage value.
float b;

// multiple declarations and definitions
int _c, _d45, e;

// Let us print a variable
printf("%c \n", a123);

return 0;
}

Constants vs Variables in C language - Constants:

A constant in the C programming language is comparable to a variable but can only hold one value while the programme is running. It follows that once a value is assigned to a constant, it cannot be altered while the programme is running. The value is fixed throughout the programme once it is set to the constant. The following is a definition of a constant:

A constant is a designated memory address that only ever has one value while a program is running.

It is similar to a variable but it cannot be modified by the program once defined.

Values that don't change(fixed).
Types:-
  Integer Constants      :-   1, 2, 3, 0
  Character Constants :-  'a', 'b', 'A', '#', '&'
  Real Constants           :- -1, -2 1.0, 2.0, 3.14, -24


Integer constants :-

A decimal, octal, or hexadecimal integer constant can also be an octal or hexadecimal integer. A decimal integer value is supplied as a direct integer value, while octal and hexadecimal values are prefixed by "o" and "OX," respectively.
Long or unsigned integer constants are two additional types of integer constants. Long and unsigned long integer constant values are both prefixed with "l," but unsigned long integer constant values are prefixed with "ul."

Example

O76 is an octal integer constant, 
 125 is a decimal integer constant.



Floating Point constant:-

The integer and decimal components of a floating-point constant must both be present. It occasionally might additionally include the exponentiation. A floating-point constant's value must be prefixed with "e" or "E" when it is written in exponent form.


Example

The exponent form of the floating-point number 3.14 is 3E-14.

Character Constant :-

A symbol that is encased in a single quote marks is a character constant. A character constant can only be one character long.


Example :-

'A' \s'2' \s'+'



There are certain predefined character constants called escape sequences in the C programming language. Every escape sequence begins with the symbol "," and each one has a unique specific functionality. This escape syntax is used in the printf() output function.


String Constant :-

Double quote marks around a group of letters, numbers, unique symbols, and escape sequences form a string constant.


The following one line defines the string constant.
Btechsmartclass, this is.

As shown below, we can define a string constant using multiple lines.
"This is btechsmartclass," it says.

The following definition of a string constant uses white space to separate the elements:
The "btechsmartclass" is "this."

The same string constant is defined by the three items above.

C constant creation :-

Constants in the C programming language can be made using two ideas.


using the term "const"
Using the preprocessor '#define'
using the term "const"
Using the keyword "const," we can create a constant of any datatype. We add the term "const" before the variable definition to create a constant.
The following general syntax is used to create constants using the keyword "const."

ConstantName: const datatype;

Alternatively, const datatype constantName = value;


Using the preprocessor '#define' :-

Using the preprocessor directive "#define," we may also create constants. When using this preprocessor directive to construct constants, they must be defined at the beginning of the programme (because all the preprocessor directives must be written before the global declaration).
The '#define' preprocessor directive is used to generate constants using the following syntax...


value #define CONSTANTNAME

Ex.      
                   
#include <stdio.h>
  
// Constants
#define val 10
#define floatVal 4.5
#define charVal 'G'

No comments

This website is only for educational purpose, it is not related to any organization. This website helps to beginners in programming to learn fast.

Powered by Blogger.