TechGeeks

TOPIC 5: Fundamental Data Types

Every language is equipped with some basic elements & grammatical rules. Before starting with programming, we should know the basic elements that build the language. In the C programming language, data types are a domain of allowed values & the operations that can be performed on those values. The type of a variable determines how much space is occupied in storage and how the interpretation of bit pattern stored happens.

There are basically 4 fundamental data types in C, which are: char, int, float & double.

Char

It is used to store a single character

int

It is used to store any integer value

float

It is used to store any single precision floating point number

double

It is used to store any double precision floating point number

TOPIC 6: Operators and Expressions

An operator is a symbol that informs the compiler to perform a specific logical or mathematical operation.

C programming language is rich in built-in operators and provides the following types of operators:

Arithmetic Operators

The mathematical calculations like addition, subtraction, multiplication, division and modulus are performed by these operators.Assuming that variable A holds 10 and B holds 20 then:

Operator : (+)

Description : Adds two operands

Example : A + B will give 30

Operator : (-)

Description : Subtracts second operand from the first

Example : A - B will give -10

Operator : (*)

Description : Multiplies two Operands

Example : A * B will give 200

Operator : (/)

Description :Divides numerator by de-numerator

Example : B / A will give 2

Operator : (%)

Description :Modulus Operator gives the remainder after an integer division

Example : B % A will give 0

Operator : (++)

Description :Increments operator increases integer value by one

Example : A++ will give 11

Operator : (--)

Description :Decrements operator decreases integer value by one

Example : A-- will give 9

Relational Operators

The value of two variables is compared by these operators.

The relational operators supported by C language are as follows.Assuming that variable A holds 10 and B holds 20, then:

Operator : ( == )

Description : Checks for the equality of the values of the two operands, if yes then condition becomes true.

Example : (A == B) is not true.

Operator : ( != )

Description : Checks for the equality of the values of the two operands, if values are not equal then condition becomes true.

Example : (A != B) is true.

Operator : ( > )

Description : when the left operand value is greater than the value of right operand,condition is true.

Example : (A > B) is not true.

Operator : ( < )

Description :when the left operand value is less than the right operand value, condition becomes true.

Example : (A < B) is true.

Operator : ( >= )

Description : when the left operand value is greater than or equal to the value of right operand, condition is true.

Example : (A >= B) is not true.

Operator : ( <= )

Description :when the left operand value is less than or equal to the value of right operand, condition is true.

Example : (A <= B) is true.

Operator : (--)

Description :Decrements operator decreases the integer value by one

Example : A-- will give 9

Operator : (--)

Description :Increments operator increases the integer value by one

Example : A++ will give 10

Logical Operators

The operators that are used to perform logical operations on the given two variables are called logical operators.

Following shows all the logical operators supported by C language. Assuming variable A holds 1 and B holds 0, then:

Operator : ( && )

Description: It is called Logical AND operator.When both the operands are nonzero, then condition becomes true.

Example: (A && B) is false.

Operator : ( || )

Description:It is called Logical OR Operator.When any of the two operands is nonzero, then condition becomes true.

Example: (A || B) is true.

Operator : ( ! )

It is called Logical NOT Operator.If a condition is false then Logical NOT operator will make it true.

Bitwise Operators

The operator that works on bits and performs bit-by-bit operation are referred to as Bitwise operators. Bitwise operators are used in bit level programming.

These operators can perform operations on int and char but not on float and double.

Showbits( ) function can be used to display the binary representation of any integer or character value. Bit wise operators in C language are:& (bitwise AND),| (bitwise OR), ~ (bitwise OR), ^ (XOR), < (left shift) and > (right shift).

The Bitwise operators supported by C language are explained in the following lines.Assuming that variable A holds 60 (00111100) and B holds 13 (00001101), then:

Operator : ( & )

Description:Binary AND Operator is used to copy a bit to the result if it exists in both operands.

Example: (A & B) will give 12, i.e., 0000 1100

Operator : ( | )

Description:Binary OR Operator copies a bit if it exists in either operand.

Example: (A | B) will give 61, i.e., 0011 1101

Operator : ( ^ )

Description:Binary XOR Operator is used to copy the bit if it is set in one operand but not both.

Example: (A ^ B) will give 49, i.e., 0011 0001

Operator : ( ~ )

Description: Binary Ones Complement Operator is unary and has the effect of ‘flipping’ bits.

Example: (~A ) will give -61,i.e., 1100 0011 in 2’s complement form.

Operator : ( << )

Description: Binary Left Shift Operator. The value of the left operand is moved left by the number of bits specified by the right operand.

Example: A << 2 will give 240 i.e.,1111 0000

Operator : ( >> )

Description: Binary Right Shift Operator. The value of the left operand is moved right by the number of bits specified by the right operand.

Example: A >> 2 will give 15 i.e.,0000 1111

<< Back Next >>
>