THE C PROGRAMMING LANGUAGE: CHAPTER 02

 


 In our last few blogs we have already seen about origin and uses of C programming language(coding language), as well as reasons for learning C programming language. Also we have learnt about the followings in C. 

■ VARIABLESCONSTANTS KEYWORD that are used in C.

■ BASIC STRUCTURE OF A PROGRAM.

■ COMPILATION AND EXECUTION.

■ LIBRARY FUNCTIONS.

■ RECEIVING INPUTS FROM THE USER.

PREVIOUS BLOGS :📝 CODING LANGUAGE : A STRANGE PARADISE OF FUTURE!

                                      📝 THE C PROGRAMMING LANGUAGE: CHAPTER 01.

NOW WE WILL CONTINUE FURTHER IN C.

// Do read chapter 01 first, link is already provided.

INSTRUCTIONS AND OPERATORS.

We can say a C program is basically a set of instructions. just like we witness a recipe - which contains instructions to prepare a particular dish.

But there are types of Instructions.

1) Type declaration Instruction

2) Arithmetic Instruction

3) Control Instruction

●TYPE DECLARATION INSTRUCTION.

type declaration is just a declaration of a type such as an interface , a function or a class . You can declare type and entity such as a variable, function, or an n object (that uses this type) at the same place (in the same file) or separate these in different files.

Like : int a;

          float b;

Other Variations:

1)

i) int i=10; int j=i; int a = 2

    int j1=a + j - i

// Won't show us any error as we first defined "i", "j", and "a" in it and then used it in j1

ii) float b= a+3; float a= 1.1 

// ii) It will show us an ERROR! as we are trying to use "a" before defining it.

2)

int a, b, c, d;

a= b= c= d= 30;

// Value of a, b, c and d will be 30 each.

● ARITHMETIC INSTRUCTIONS

 int i = ( 3 * 2 ) + 1

// Here 3, 2 and 1 represents operands

// * and + represents operators 

Operands are the quantity on which an operation is to be done. It can be int/float etc.

There are many operators but basic are these below.

  • Addition: The ‘+’ operator adds two operands. For example, x+y.
  • Subtraction: The ‘-‘ operator subtracts two operands. For example, x-y.
  • Multiplication: The ‘*’ operator multiplies two operands. For example, x*y.
  • Division: The ‘/’ operator divides the first operand by the second. For example, x/y.
  • Modulus: The ‘%’ operator returns the remainder when first operand is divided by the second. For example, x%y

NOTE:-

1) No operators is assumed to be present

int i= ab ⇾ Invalid

int i= a*b ⇾ Valid

2)There is no core operator in C to perform exponentiation. However, the <math.h> library provides the "pow()" function which takes two numbers as parametres and returns the result. 

Example:

#include<math.h>

void main(){

int a;

a= pow(2,3);

printf("%d",a);

}

TYPE CONVERSION

An arithmetic operation between 

int and int ⇾ int // 5/2 = 2

int  and float ⇾ float // 5.0/2 = 2.5, 2.0/5= 0.4

float and float ⇾ float //  4.2/2.1 = 2.0

NOTE:-

int a= 3.5;

In this case 3.5 (float) will be demoted to 3 (int) because a can not store floats. // for example: 2/5 = 0.4 (float) but will be demoted to 0 (int) as [ int and int ⇾ int ].

float a=8; // a will store 8.0 (conversion to float).

OPERATOR PRECEDENCE IN C

In C language simple mathematical rules like BODMAS are not followed.

The List for operator priority in C.

Priority          Operator

   1st                  *, /, %

   2nd                   +, -

   3rd                     =

Operator of higher priority are evaluated first in the absence of parenthesis.

OPERATOR ASSOCIATIVITY

When operator of equal priority are present in an expression, the tie is taken care by associativity.

x*y/z = (x*y)/z

x/y*z = (x/y)*z

*, / Follows Left to Right associativity.

● CONTROL INSTRUCTIONS

It determines the flow of control in a program.

There are four types of control instructions in C.

1)Sequence control instruction.     

2)Decision control instruction.

3)Loop control instruction.

4)Case control instruction.

// we will learn all these four control instructions later in the series of chapters.  

CONDITIONAL INSTRUCTIONS

It is basically the choice that has to be made, all the decision are based on conditions being met.

Instructions for decision making in C.

1) If-else statement

2) switch statement 

If-else statement

The syntax of an if-else statement in c looks like:

if (condition to be checked) 

{

   statements-if-condition-true;

}

else{

   statement-if-condition-false;

}

RELATIONAL OPERATORS IN C.



Relational operators are used to evaluate conditions (True or False) inside the if statements.

List of relational operators are:

==equals to
>=greater than or equal to
>greater than
<less than
<=less than or equal to
!=not equal to

// " = " is used for assignment. whereas, " == " is used for equality check.

LOGICAL OPERATOR

&&, ! and || are the three logical operators in C. These are read as "and", "not" and "or".

Uses of logical operators: // 1⇾ true, 0⇾ false 

1) && (AND) is true when both the conditions are true

"1" and "0" is evaluated as false

"0" and "0" evaluated as false

"1" and "1" is evaluated as true 

2) || (OR) is true when at least one of the conditions is true.

1 or 0 = 1(true)

1 or 1 = 1 (true)

0 or 0 = 0 (false)

3) ! returns true if given false and false if given true.

!(3==3) evaluates to false

!(3>30) evaluates to true

As the number of conditions increases, the level of indentation increases. This reduces readability. Logical operators come to rescue in such cases. 

Else if clause 

Instead of using multiple if statements, we can also use if along with if thus forming an if-else if-else ladder.

if(condition) 

{

//statements;

}

else if (another condition)

//statements;

}

else {

//statements;

}

Using if-else if- else reduces indents. The last "else " is optional. Also, there can be any number of "else if". 

The final else is only executed if all the conditions mentioned fails.

Operator precedence

PriorityOperator
1st !
2nd*,/,%
3rd+,-
4th<>,<=,>=
5th==,!=
6th&&
7th||
8th=

Conditional operators

A shorthand "if-else" can be written using conditional or ternary operators  

Condition ? expression- if - true : expression- if - false

Here, '?' and ':' Ternary operators.

Switch case-control instruction

Switch-case is used when we have to make a choice between the number of alternatives for a given variable.

Syntax,

Switch (integer-expression)

{

Case c1:

          Code;

Case c2:              //c1, c2, c3 are constants

          Code;       // Code represents any valid C code

Case c3:

          Code;

default:

          Code;

}

The value of integer-expression is matched against c1, c2, c3...… if it matched any of these cases, that case along with all subsequent "case" and "default" statements are executed.


THIS IS WHERE WE COMPLETE OUR CHAPTER 02. FOR ANY QUERIES DO ASK IN THE COMMENT SECTION OR EMAIL ME AT  aniket0130ak@gmail.com


Post a Comment

0 Comments

Copyright Disclaimer

Copyright Disclaimer under section 107 of the Copyright Act 1976, allowance is made for “fair use” for purposes such as criticism, comment, news reporting, teaching, scholarship, education and research.

Translate