You are on page 1of 24

VARIABLES, CONSTANTS

Introduction to OPERANDS
C/C++
DECISION MAKING
Variables • Declaration and
&
Constants definitions

Topics Operators

Decision • If/switch
making
Variables
Definition
Variables are defined with the type followed by the list of variable
Type tells the compiler how much memory to allocate

type variable_list;

Examples: int i, j, k;
char c, ch;
float f, salary;
double d;

Initialized: type variable_name = value;


int d = 3, f = 5; // definition and initializing d and f.
byte z = 22; // definition and initializes z.
char x = 'x'; // the variable x has the value 'x'.
Variables
Declaration
Tells the compiler the variable name and that it exists somewhere
Type is still needed so the compiler knows how to use it
Useful when using multiple C files to share global variables

// Variable declaration:
extern int a, b;
extern int c;
extern float f;
Literals
Integer
212 /* Legal */
215u /* Legal */
0xFeeL /* Legal */
078 /* Illegal: 8 is not an octal digit */
032UU /* Illegal: cannot repeat a suffix */

85 /* decimal */
0213 /* octal */
0x4b /* hexadecimal */
30 /* int */
30u /* unsigned int */
30l /* long */
30ul /* unsigned long */
Literals
Floating point
3.14159f /* Legal float*/
3.14159 /* Legal double*/
314159E-5L /* Legal */
510E /* Illegal: incomplete exponent */
210f /* Illegal: no decimal or exponent */
.e55 /* Illegal: missing integer or fraction */
Literals
Strings
"hello, dear"

"hello, \
dear"

"hello, "
"d"
"ear"
Constants
Precompiler
Constants replaced by the precompiler
Defined using #define
#define identifier value

Precompiler replaces the identifier with actual value


#include <stdio.h>

#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n’

int main() {
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
return 0;
}
Constants
In memory
Constants in memory are typed as variables
const type variable = value;

Compiler throws an error if you assign a value to a constant


#include <stdio.h>

int main() {
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n’;

int area; area = LENGTH * WIDTH;

printf("value of area : %d", area);


printf("%c", NEWLINE);
return 0;
}
Storage classes
Auto
◦ Default for local variables
◦ Automatic allocation at runtime to stack or register

Static
◦ Default for global variables
◦ Allocation in data memory at compile time

Extern
◦ Variable is defined in another file (global variables)

Register
◦ Variable should be placed in register (max size is the size of the register)

Volatile
◦ Reload from memory
Variables • Declaration and
&
Constants definitions

Topics Operators

Decision • If/switch
making
Operators
Arithmetic
Operat Description Example
or

+ Adds two operands. A+B


− Subtracts second operand from the first. A−B

* Multiplies both operands. A*B

/ Divides numerator by de-numerator. B/A

% Modulus Operator and remainder of after an B%A


integer division.

++ Increment operator increases the integer value A++


by one.

-- Decrement operator decreases the integer A--


value by one.
Operators
Relational
Operator Description Example

== Checks if the values of two operands are equal or not. If yes, then (A == B)
the condition becomes true.
!= Checks if the values of two operands are equal or not. If the (A != B)
values are not equal, then the condition becomes true.

> Checks if the value of left operand is greater than the value of (A > B)
right operand. If yes, then the condition becomes true.

< Checks if the value of left operand is less than the value of right (A < B)
operand. If yes, then the condition becomes true.

>= Checks if the value of left operand is greater than or equal to the (A >= B)
value of right operand. If yes, then the condition becomes true.

<= Checks if the value of left operand is less than or equal to the (A <= B)
value of right operand. If yes, then the condition becomes true.
Operators
Logical
Operator Description Example

&& Called Logical AND operator. If both the operands are (A && B)
non-zero, then the condition becomes true.

|| Called Logical OR Operator. If any of the two operands (A || B)


is non-zero, then the condition becomes true.

! Called Logical NOT Operator. It is used to reverse the !(A && B)


logical state of its operand. If a condition is true, then
Logical NOT operator will make it false.
Bit wise operations
p q p&q p|q p^q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Operators
Binary
Operator Description Example

& Binary AND Operator copies a bit to the result if it exists in (A & B)
both operands.
| Binary OR Operator copies a bit if it exists in either operand. (A | B)

^ Binary XOR Operator copies the bit if it is set in one operand (A ^ B)


but not both.
~ Binary One's Complement Operator is unary and has the (~A )
effect of 'flipping' bits.
<< Binary Left Shift Operator. The left operands value is moved
left by the number of bits specified by the right operand. A << 2

>> Binary Right Shift Operator. The left operands value is moved
right by the number of bits specified by the right operand. A >> 2
Operators
Assignment
Operator Description Example
= Simple assignment operator. Assigns values from right side operands to left side operand C = A + B will assign the value
of A + B to C
+= Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand. C += A is equivalent to C = C +
A
-= Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left C -= A is equivalent to C = C -
operand. A
*= Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left C *= A is equivalent to C = C *
operand. A
/= Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left C /= A is equivalent to C = C /
operand. A
%= Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. C %= A is equivalent to C = C
%A
<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2

>>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2

&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2


^= Bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2
|= Bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2
Operators
MISC
Operat Description Example
or

sizeof() sizeof(a), where a is integer, will return 4.


Returns the size of a variable.

& &a; returns the actual address of the variable.


Returns the address of a variable.

* Pointer to a variable. *a;

?: If Condition is true ? then value X : otherwise


Conditional Expression. value Y
Variables • Declaration and
&
Constants definitions

Topics Operators

Decision • If/switch
making
Decision making
if/else
Conditional execution
Test condition and execute statement if condition is true – if
Optional execute other statement if condition is false

if (condition) if (condition) {
statement_true; statement1_true;
else statement2_true;
statement_false; } else {
statement1_false;
statement2_false;
}
Decision Making
Nested if
if (condition) {
if (condition) { if (condition1) {
statement1_true; statement1_true;
statement2_true; statement2_true;
} else { } else if (condition2) {
statement1_false; statement3_true;
statement2_false; statement4_true;
} } else {
} else { statement1_false;
statement3_false; statement2_false;
statement4_false; }
}
Decision making
Switch
Test on integer variable switch (variable) {
case value1:
Different execution depending on value
statement1;
statement2;
break;
case value2: case value3:
statement3;
break;
case value4:
statement4;
return;
default:
statement_default;
}
Homework
Write a program that
◦ Reads 2 integer numbers H and W
◦ Multiplies the numbers to calculate the area
◦ Prints the Area
◦ Print Area is small if less than 100, medium if less than 200 and large if more than 200

Write a program that


◦ Reads one integer
◦ Prints if value is 0: “S”, 1: “M”, 2: “L”, 3: “XL”, 4: “XXL”, 5: “3XL”
◦ All other values print “No size.”
Materials
C Tutorial:
https://www.tutorialspoint.com/cprogramming/index.htm
C/C++ Library description
https://www.cplusplus.com/

You might also like