Variables,Operators and Expressions in 'C' Language

Variables: Variable in C Programming is also called as container to store the data. Variable name may have different data types to identify the type of value stored. Suppose we declare variable of type integer then it can store only integer values.Variable is considered as one of the building block of C Programming which is also called as identifier.

What is Variable in C Programming?

  1. Initially 5 is Stored in memory location and name x is given to it
  2. After We are assigning the new value (3) to the same memory location
  3. This would Overwrite the earlier value 5 since memory location can hold only one value at a time
  4. Since the location ‘x’can hold Different values at different time so it is refered as ‘Variable
  5. In short Variable is name given to Specific memory location or Group.
Constant and Variable in C

Rules For Constructing Variable Name

  1. Characters Allowed :
    • Underscore(_)
    • Capital Letters ( A – Z )
    • Small Letters ( a – z )
    • Digits ( 0 – 9 )

  1. Blanks & Commas are not allowed
  2. No Special Symbols other than underscore(_) are allowed
  3. First Character should be alphabet or Underscore
  4. Variable name Should not be Reserved Word
Valid Examples: 

num
Num
Num1
_NUM
NUM_temp2
Invalid Examples:

number 1
num 1
addition of program

Remember following Tricks


  1. Do not Create unnecessarily long variable name
  2. Do not use underscore as first character to avoid confusion between System Variable & user defined variables because many system variables starts with undescore
  3. Variable names are case-Sensitive . i.e sum,Sum,SUM these all three are different variable names.
  4. Reserve words with one/more Capital letters allowed eg. Int,Float,chAr are allowed but try to skip them.

Keywords in C Programming Language :

  1. Keywords are those words whose meaning is already defined by Compiler
  2. Cannot be used as Variable Name
  3. There are 32 Keywords in C
  4. C Keywords are also called as Reserved words .

32 Keywords in C Programming Language


autodoubleintstruct
breakelselongswitch
caseenumregistertypedef
charexternreturnunion
constfloatshortunsigned
continueforsignedvoid
defaultgotosizeofvolatile
doifstaticwhile


C Programming Operators and Expressions

In this Section, you will learn about Operators in C Programming (all valid operators available in C), expressions (combination of operators, variables and constants) and precedence of operators (which operator has higher priority and which operator has lower priority).
  1. C Operators
  2. Expressions in C
  3. C Operator Precedence
Operators are the symbols which tell the computer to execute certain mathematical or logical operations.  A mathematical or logical expression is generally formed with the help of an operator. C programming offers a number of operators which are classified into 8 categories viz.
  1. Arithmetic operators
  2. Relational operators
  3. Logical operators
  4. Assignment operators
  5. Increment and Decrement operators
  6. Conditional operators
  7. Bitwise operators
  8. Special operators

1. Arithmetic Operators

C programming language provides all basic arithmetic operators: +, -, *, / and %.
Arithmetic Operators in C
Note: ‘/’ is integer division which only gives integer part as result after division. ‘%’ is modulo division which gives the remainder of integer division as result.
Some examples of arithmetic operators are:
  • a + b
  • a – b
  • a * b
  • a  / b
  • a % b
In these examples, a and b are variables and are called operands.
Note: ‘%’ cannot be used on floating data type.

2. Relational Operators

Relational operators are used when we have to make comparisons. C programming offers 6 relational operators.
Relational Operators in C
Relational expression is an expression which contains the relational operator. Relational operators are most commonly used in decision statements like ifwhile, etc. Some simple relational expressions are:
  • 1 < 5
  • 9 != 8
  • 2 > 1+3
Note: Arithmetic operators have higher priority than relational operators meaning that if arithmetic expressions are present on two sides of a relational operator then arithmetic expressions will be calculated first and then the result will be compared.

3. Logical Operators

Logical operators are used when more than one conditions are to be tested and based on that result, decisions have to be made. C programming offers three logical operators. They are:
Logical Operators in C
For example:
a < 18 || a> 60
An expression which combines two or more relational expressions is known as logical expression.
    Note:  Relative precedence of relational and logical operators are as follows
Highest precedence!

> >= < <=
== !=
&&
Lowest precedence||

4. Assignment Operators

Assignment operators are used to assign result of an expression to a variable. ‘=’ is the assignment operator in C. Furthermore, C also allows the use of shorthand assignment operators. Shorthand operators take the form:
var op = exp;
where var is a variable, op is arithmetic operator, exp is an expression. In this case, ‘op=’ is known as shorthand assignment operator.
The above assignment
var op = exp;
is the same as the assignment
var = var op exp;
Consider an example:
x += y;
Here, the above statement means the same as
x = x + y;
Note: Shorthand assignment can be used with all arithmetic operators.

5. Increment and Decrement Operators

C programming allows the use of ++ and  operators which are increment and decrement operators respectively. Both the increment and decrement operators are unary operators. The increment operator ++ adds 1 to the operand and the decrement operator – subtracts 1 from the operand. The general syntax of these operators are:
Increment Operator: m++ or ++m;
Decrement Operator: m--or --m;
In the example above, m++ simply means m=m+1; and m-- simply means m=m-1;
Increment and decrement operators are mostly used in for and while loops.
++m and m++ performs the same operation when they form statements independently but they function differently when they are used in right hand side of an expression.
++m is known as prefix operator and m++ is known as postfix operator. A prefix operator firstly adds 1 to the operand and then the result is assigned to the variable on the left whereas a postfix operator firstly assigns value to the variable on the left and then increases the operand by 1. Same is in the case of decrement operator.
For example,
X=10;
Y=++X;
In this case, the value of X and Y will be 6.
And,
X=10;
Y=X++;
In this case, the value of Y will be 10 and the value of X will be 11.

6. Conditional Operator

The operator pair “?” and “:” is known as conditional operator. These pair of operators are ternary operators. The general syntax of conditional operator is:
expression1 ? expression2 : expression3 ;
This syntax can be understood as a substitute of if else statement.
For example,
a = 3 ;
b = 5 ;
Consider an if else statement as:
if  (a > b)
x = a ;
else
x = b ;
Now, this if else statement can be written by using conditional operator as:
x = (a > b) ? a : b ;

7. Bitwise Operator

In C programming, bitwise operators are used for testing the bits or shifting them left or right. The bitwise operators available in C are:
Bitwise Operators in C

8. Special Operators

C programming supports special operators like comma operator, sizeof operator, pointer operators (& and *) and member selection operators (. and ->). The comma operator and sizeof operator are discussed in this section whereas the pointer and member selection operators are discussed in later sections.

1. Comma Operator

The comma operator can be used to link the related expressions together. A comma linked expression is evaluated from left to right and the value of the right most expression is the value of the combined expression.
For example:
                x = (a = 2, b = 4, a+b)
In this example, the expression is evaluated from left to right. So at first, variable a is assigned value 2, then variable b is assigned value 4 and then value 6 is assigned to the variable x. Comma operators are commonly used in for loops, while loops, while exchanging values, etc.

2 .Sizeof() operator

The sizeof operator is usually used with an operand which may be variable, constant or a data type qualifier. This operator returns the number of bytes the operand occupies. Sizeof operator is a compile time operator. Some examples of use of sizeof operator are:
x = sizeof (a);
y = sizeof(float);
The sizeof operator is usually used to determine the length of arrays and structures when their sizes are not known. It is also used in dynamic memory allocation.

9.C Expressions

Arithmetic expression in C is a combination of variables, constants and operators written in a proper syntax. C can easily handle any complex mathematical expressions but these mathematical expressions have to be written in a proper syntax. Some examples of mathematical expressions written in proper syntax of C are:
Note: C does not have any operator for exponentiation. 

10.C Operator Precedence

At first, the expressions within parenthesis are evaluated. If no parenthesis is present, then the arithmetic expression is evaluated from left to right. There are two priority levels of operators in C.
High priority: * /  %
Low priority: + -
The evaluation procedure of an arithmetic expression includes two left to right passes through the entire expression. In the first pass, the high priority operators are applied as they are encountered and in the second pass, low priority operations are applied as they are encountered.
Suppose, we have an arithmetic expression as:
x = 9 – 12 / 3 + 3 *2 - 1
 This expression is evaluated in two left to right passes as:
First Pass
Step 1: x = 9-4 + 3 * 2 – 1
Step 2: x = 9 – 4 + 6 – 1

Second Pass

Step 1: x = 5 + 6 – 1
Step 2: x = 11 – 1
Step 3: x = 10
But when parenthesis is used in the same expression, the order of evaluation gets changed.
For example,
x = 9 – 12 / (3 + 3) * (2 – 1)
When parentheses are present then the expression inside the parenthesis are evaluated first from left to right. The expression is now evaluated in three passes as:
First Pass
Step 1: x = 9 – 12 / 6 * (2 – 1)
Step 2: x= 9 – 12 / 6 * 1
Second Pass
Step 1: x= 9 – 2 * 1
Step 2: x = 9 – 2
Third Pass
Step 3: x= 7
There may even arise a case where nested parentheses are present (i.e. parenthesis inside parenthesis). In such case, the expression inside the innermost set of parentheses is evaluated first and then the outer parentheses are evaluated.
For example, we have an expression as:
x = 9 – ((12 / 3) + 3 * 2) – 1
The expression is now evaluated as:
First Pass:
Step 1: x = 9 – (4 + 3 * 2) – 1
Step 2: x= 9 – (4 + 6) – 1
Step 3: x= 9 – 10 -1
Second Pass
Step 1: x= - 1 – 1
Step 2: x = -2
Note: The number of evaluation steps is equal to the number of operators in the arithmetic expression.

Comments

Popular posts from this blog

Built-in Data Types: In 'C' Language

For Loop in C