C Programming Practice 4 on Operators
For Complete YouTube Video: Click Here
In this class, we will discuss C Programming Practice 4 on Operators.
The discussion on many programs on operators is provided below.
- Programming Practice 1 on Operators.
- Programming Practice 2 on Operators.
- Programming Practice 3 on Operators.
In this class, we will discuss three programs.
Table of Contents
C Programming Practice 4 on Operators
Program 1
The image below is program 1.
Before we start our discussion, please take some time and guess the output.
The above is going to generate an error.
Why is an error generated?
When we have discussed identifiers, we have discussed the rules to declare a variable.
According to those rules, a variable declaration should not start with a digit.
It should always start with an alphabet or underscore.
As the variable definition 2_two starts with a digit in the above program, the compiler will generate an error.
Program 2
The image below shows program 2.
Guess the output of the program?
The program generates an error.
In the above program, we have defined the variable name as default.
As per our discussion on the rules of defining a variable, the name should not be a keyword.
Program 3
The image below shows program 2.
The second line of code declares the variable a = (1, 2, 3).
Guess the output of the program? Will such kinds of declarations, are accepted by the compiler?
The output of the program is 3. Yes, such kinds of declarations are accepted by the compiler.
How will the compiler work?
For this to understand we should have the operator precedence and associativity table.
In the operator precedence table, the parentheses have higher precedence than assignment operators than the comma operator.
As the parentheses have the highest precedence, the content in the parentheses will get executed first.
All the operators in the parentheses are comma operators. All have the same precedence, and the associativity is from left to right.
As we move from left to right, the value with the last comma operator is three, so the variable is assigned three.