Variables, Constant in C | Learn C | Village Programmer


This is the Third article of the series Learning C Programming Language


Happy Christmas guys, I am Vikas Patel (Village Programmer). 
In this Christmas Special C Programming  learning series another tutorial is here.
In this tutorial I'll be talking about variables, constants and expressions in C programming with example of code.
So let's start.

Content in this post: 
  • What creature a Variable is ?
  • Types of Variables .
  • How to declare a Variable ?
  • How to Assign values and use them (initialization) ?
  • What creatures are Constants ?
  • Types of Constants .
  • How to declare a Constant ?
  • How to use Constants ?

🔅What creature a Variable is: 


Variables are names that are used to store values. It can take different values but one at a time. A data type is associated with each variable & it decides what values the variable can take. When you decide your program needs another variable, you simply declare (or define) a new variable and C makes sure you get it. You declare all C variables at the top of whatever blocks of code need them. Variable declaration requires that you inform C of the variable's name and data type. 

Syntax – datatype variablename;

Learn more about Data Types here 👉 Elements of C programming .

🔅Types of Variables:

Mainly there are following types of variables : 
  1. int (Integer)
  2. float (Floating Point)
  3. char (Character)
  4. double (float with more precision points)
  5. long (Long Integer)
  6. and also void .

🔅How to declare a variable:


There are two places where you can declare a variable:
  • After the opening brace of a block of code (usually at the top of a function)
  • Before a function name (such as before main() in the program) Consider various
examples:
Suppose you had to keep track of a person's first, middle, and last initials. Because an initial is obviously a character, it would be prudent to declare three character variables to hold the three initials. 
In C, you could do that with the following statement:
1. ☝ 
 
int main(){
  int id_no, number;
  // code goes here
  return 0;
}

2. ✌
 
int main(){
  int id_no;
  int number;
  // code goes here
  return 0;
}

🔅Initializing a variable:

When a variable is declared, it contains undefined value commonly known as garbage value. If we want we can assign some initial value to the variables during the declaration itself. This is called initialization of the variable.
Example: 

 
int main(){
  int id_no = 123;
  int number = 99283;
  // code goes here
  return 0;
}

🔅What creatures are Constants:

A constant is an entity that doesn’t change whereas a variable is an entity that may change.

🔅Types of constants: 

C constants can be divided into two major categories:
  • Primary Constants
  • Secondary Constants

Here our only focus is on primary constant. For constructing these different types of constants certain rules have been laid down.

Rules for Constructing Integer Constants:
An integer constant must have at least one digit.
a) It must not have a decimal point.
b) It can be either positive or negative.
c) If no sign precedes an integer constant it is assumed to be positive.
d) No commas or blanks are allowed within an integer constant.
e) The allowable range for integer constants is -32768to 32767.
Ex.: 426, +782,-8000, -7605

Real constants are often called Floating Point constants. The real constants could be written in two forms—Fractional form and Exponential form.

Rules for constructing real constants expressed in fractional form:
a) A real constant must have at least one digit.
b) It must have a decimal point.
c) It could be either positive or negative.
d) Default sign is positive.
e) No commas or blanks are allowed within a real constant.
Ex. +325.34, 426.0, -32.76, -48.5792

Rules for constructing real constants expressed in exponential form:
a) The mantissa part and the exponential part should be separated by a letter e.
b) The mantissa part may have a positive or negative sign.
c) Default sign of mantissa part is positive.
d) The exponent must have at least one digit, which must be a positive or negative integer.
Default sign is positive.
e) Range of real constants expressed in exponential form is -3.4e38 to 3.4e38.
Ex. +3.2e-5, 4.1e8, -0.2e+3, -3.2e-5


Rules for Constructing Character Constants:
a) A character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas.
b) The maximum length of a character constant can be 1 character.
Ex.: ‘M’, ‘6’, ‘+’

🔅How to declare a constant:

Constants are declared just like variables but the contain the const keyword;
ex:

// integer constant
const int x = 123;
// float constant
const float y = 234.7467;
const float PI = 3.1415;
// char const
const char ch = 'V';

🔅How to  use constants:

You can use constants when you want some data to not be changed.
like mathematical constants PI .

float area = 2*PI*radius;


Thanks for reading. 🙏
If you liked it feel free to share with your dear ones.💗
And tell me what do you think in the comment box.💬

Post a Comment

أحدث أقدم