Constants & Variables in C

C Variables We store data in computer memory. While storing we also keep one reference to this memory to retrieve the data we store. During our script execution we may change the data or we may assign once and do not change the data throughout our program.

Variables are names given to memory locations whose data can be changed.

Constants are names given to memory locations whose data are not changed.

Types of constants

Primary constants, Secondary constants

Primary Constants are Integer Constant, Real Constant , Character Constant

Secondary Constants are Array, Pointer , Structure , Union, Enum Etc.

Declaring constants in C

Rules for declaring Integer Constants

Must have one digit
Must NOT have a decimal point
Can be both positive or negative
By default it is positive ( if no sign is given then it is considered as positive )
No blank space or commas are allowed

Range for integer constant is -32768 to 32768 ( depends on type of compiler ) Example of integer :
345
-543
+6725
-5643

Rules for declaring Real ( or floating ) constants

Floating point ( or real ) constants can be written in Fractional form and exponential form

Rules are here

Must have at least one digit
Must have a decimal point
Can be both positive or negative
By default it is positive
No blank space or commas are allowed

Example :

125.34
-45.98
567.0
-456.0
Exponential form has two parts Mantissa and exponent. Both parts are separated by ā€˜eā€™. Part before the ā€˜eā€™ is called Mantissa and part after e is called exponent.

Rules

Mantissa and exponent part are separated by e
Before Mantissa part we may have positive or negative sign
By default Mantissas is positive
The Exponent part must have one digit ( either positive or negative )

Rage is from -3.4e38 to 3.4e38
Example :
3.2e2
2.1e8
-0.7e+5
-2.1e-3

Character Constants

A single alphabet or digit or symbol ( must enclosed within quotes ) Example :
'B'
'c'
'3'
Code to create and display constants
#include <stdio.h>
int main(void){
const int my_int=45; // Integer constant
const char my_char='A'; // Char constant
const char my_string[]="Welcome"; // char constant

printf(" Integer Constant %d \n",my_int);
printf(" Char Constant  %c \n",my_char);
printf(" String Constant  %s \n",my_string);
return 0;
}

Variables

The value or data of a variable can change during program execution.

Types of variable

Integer , Floating , character variable

Each type can handle same time data, like integer variable can handle integer type of data.

Rule for construction of Variables

Name of the variable can have combination of alphabets , digits and underscore. Maximum length is 31 character ( some compiler allow more )
The first char must be alphabet or underscore ( no Digits ) .
Blank space or comma are not allowed
Only underscore is allowed no special chars are allowed. Test on constant & variables

Examples of variables

my_var
my_page1
my_page2

Sample code for variables

#include <stdio.h>
int main(void){
int my_int=45; // Integer variable 
char my_char='A'; // Char variable single quote 
char my_string[]="Welcome"; // string  variable 

printf(" Integer variable %d \n",my_int);
printf(" Char variable  %c \n",my_char);
printf(" String variable   %s \n",my_string);
return 0;
}
Global Local and static variables Basic programming structure of C

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer