Variable and Constants in C

Variable

In any programming language a variable is container(Storage Place) that holds the data. To reserve a storage we need to define the variable using a unique name. Variables are a way to refer to the memory location in our system. A simple syntax for defining variable is given below

  type variable_name;
    or for multiple variables:
  type variable1_name, variable2_name, variable3_name;

for example

int price=100;

Above line of code reserves a spot for a variable named which can hold integer type of data. Name of variable is prie and it holds the value of 100.

Value of any variable can be updated at any point in the program

char website='Geekycodes'
website='google'

In the above code first we define the variable website with value (Geekycodes) and then update it with (google) in the second line.

Rules for Naming a variable

  1. A variable can have alphabets, digits, and underscore.
  2. A variable name can start with the alphabet, and underscore only. It can’t start with a digit.
  3. No whitespace is allowed within the variable name.
  4. A variable name must not be any reserved word or keyword, e.g. int, goto , etc.

Type of a variable can not be changed once it’s defined in the program. but value can be changed.

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

Above first we defined the type of number as integer. But we can’t allocate a decimal to an integer type variable. And in third line we redefined number as double data type. It’s gonna give us error. To store a decimal we need to define a variable which has data type of float or double.

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

Important Notice

If you’re a college student and have skills in programming languages, Want to earn through blogging? Mail us at geekycomail@gmail.com

For more Python related blogs Visit Us Geekycodes . Follow us on Instagram.

Leave a Reply

%d bloggers like this: