Skip to main content

How to declare Variable and Constant in C| What is Data Types and Keywords in C | C Programming Language

What is Data type, variable and constant?

Before we start talking about data type, variable and constant
It is good to have the basic knowledge of Bits & Bytes

Now, what is Bit?

Bit the smallest unit of data in a computer memory. A bit has a single binary value. It is either 0 or 1 (True or False)

What is Data type?

When we create any variable we have to specify data type of that variable to store value in it.
Data type is use to tell computer which type of data is going to be stored in memory, so the computer will allocate the space accordingly.

Types of Data type:

1. int Data type
int Datatype is used to store integer numbers into computer memory. int data type allocates 4 bytes in a memory to store an integer value. For example 6. Number 6 is an integer number. Integer does not contain point. 
2. float Data type
float data type is used to store real numbers into computer memory. float data type allocates 8 bytes in a memory to store real number. For example 6.500000. Number 6.500000 is a real/ decimal number which contains point.
3. char Data type
char data type is used to store a single character into computer memory. char data type allocates 1 byte in a memory to store a single character. Every character that we want to store into computer memory should be written by enclosing it with a single quote. For Example, 'a' or '1' or '$'. Character can be any alphabet, digit or any special symbol.

What is variable & constant?

Variable or Constant is a name given to allocated memory. Variables or constant is used to store and retrieve data from memory. When a variable or constant is declared, some memory is allocated to store the data of variable according to its data type and address of variable is also assigned to allocated memory, so the computer can locate the data of variable using its address.

Syntax of variable declaration

int num = 6;

Here,
First, we have to write data type of the variable.
Second, we have to give a name of variable.
Third, assign a value to a variable. It is optional at the time of variable declaration, but it is compulsory to assign value at the time of constant declaration.
Last, we have to add semicolon ";" at the end. This means the procedure of variable declaration is over.
Variable should be declared within the body of main function.

Difference Between Constant and Variable

Constant: We have to add “const” keyword before data type of variable.

Variable: We don’t want to add any keyword before data type of variable.

Constant: We have to assign the value to a constant only at the time of its declaration.

VariableWe can assign value to a variable at the time of its declaration or after declaration.

ConstantOnce the value is initiated to constant, then it cannot be changed.

VariableValue assigned to a variable is changeable.

What is Keyword in C language?

  • Keywords are the reserved words that compilers can only understand.
  • Every keyword has a special meaning to compiler.
  • We cannot use keywords as the name of any variable or constant.

The following are 32 keywords of C programming language.

int, float, char, short, double, long, unsigned, enum, signed, for, do, while, continue, break, goto, if, else, switch, case, return, static, void, auto, struct, const, register, typedef, default, extern, union, sizeof, volatile.

Rules for constructing variable names

1) The first character in the variable name must be underscore or alphabet.

2) No coma or blanks allowed within a variable name.

3) We cannot use any special symbols accept underscore within a variable name.





Comments