0% found this document useful (0 votes)
6 views

BCSC 1102 Introduction to Programming Lecture 2 PPT

Uploaded by

kintaxima
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

BCSC 1102 Introduction to Programming Lecture 2 PPT

Uploaded by

kintaxima
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

BCSC 1102 : Intro To Programming Week 2

Dr. Shem Mbandu Angolo, PhD

The Co-operatetive University of Kenya


September - December 2024

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 1 / 65
Outline
1 Basic Structure of a C Program
Preprocessor Directives
Global Declarations
The main() Function
Variable Declarations
Function Definitions
Statements and Expressions
Comments
Example
Explanation of the Example
2 Data Types
Basic Data Types
Integer Types
Floating-Point Types
Character Type
Derived Data Types
User-Defined Data Types
3 Variables in C The Co-operatetive University of Kenya Sep
Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 2 / 65
Basic Structure of a C Program
1 Basic Structure of a C Program
Preprocessor Directives
Global Declarations
The main() Function
Variable Declarations
Function Definitions
Statements and Expressions
Comments
Example
Explanation of the Example
2 Data Types
Basic Data Types
Integer Types
Floating-Point Types
Character Type
Derived Data Types
User-Defined Data Types
3 Variables in C The Co-operatetive University of Kenya Sep
Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 3 / 65
Preprocessor Directives

Commands processed before the actual compilation.


Include file inclusion and macro definitions.
Begin with a # symbol.
Example: include ¡stdio.h¿

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 4 / 65
Global Declarations

Variables or functions accessible from any part of the program.


Placed outside all functions.
Example: int globalVariable;

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 5 / 65
The main() Function

Entry point of every C program.


Execution starts here.
Typically returns an integer value.
Example: int main() return 0;

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 6 / 65
Variable Declarations

Must be declared before use.


Specify the type and name of the variable.
Example: int a, b, sum;

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 7 / 65
Function Definitions

Blocks of code that perform specific tasks.


Called from the main() function or other functions.
Example: int add(int x, int y) return x + y;

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 8 / 65
Statements and Expressions

Statements: Instructions executed by the program.


Expressions: Combinations of variables, operators, and values.
Example: int a = 5, b = 10; int sum = a + b; printf(”Sum:

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 9 / 65
Comments

Non-executable parts of the code.


Provide explanations or annotations.
Ignored by the compiler.
Example: // Single-line comment /* Multi-line comment */

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 10 / 65
Example

Complete Example: include ¡stdio.h¿


int globalVariable = 10;
int add(int x, int y);
int main() int a = 5, b = 15; int sum;
sum = add(a, b);
printf(”Sum:
return 0;
int add(int x, int y) return x + y;

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 11 / 65
Explanation of the Example

1 Preprocessor Directive: include ¡stdio.h¿


2 Global Variable: int globalVariable = 10;
3 Function Declaration: int add(int x, int y);
4 main() Function: int main() int a = 5, b = 15; int sum; sum =
add(a, b); printf(”Sum: return 0;
5 Function Definition: int add(int x, int y) return x + y;

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 12 / 65
Data Types
1 Basic Structure of a C Program
Preprocessor Directives
Global Declarations
The main() Function
Variable Declarations
Function Definitions
Statements and Expressions
Comments
Example
Explanation of the Example
2 Data Types
Basic Data Types
Integer Types
Floating-Point Types
Character Type
Derived Data Types
User-Defined Data Types
3 Variables in C The Co-operatetive University of Kenya Sep
Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 13 / 65
Basic Data Types

Integer Types
Floating-Point Types
Character Type

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 14 / 65
Integer Types

int: Typically 4 bytes.


short: Typically 2 bytes.
long: Typically 4 or 8 bytes.
long long: Typically 8 bytes.
Example: int a = 10; short b = 20; long c = 100000L; long long d =
10000000000LL;

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 15 / 65
Floating-Point Types

float: Typically 4 bytes.


double: Typically 8 bytes.
long double: Typically 10, 12, or 16 bytes.
Example: float x = 3.14f; double y = 3.141592653589793; long double z
= 3.141592653589793238L;

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 16 / 65
Character Type

Used to store single characters.


Typically 1 byte.
Example: char ch = ’A’;

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 17 / 65
Derived Data Types

Arrays: Collection of elements of the same type.


Pointers: Variables that store memory addresses.
Functions: Blocks of code that perform specific tasks.
Example: int numbers[5] = 1, 2, 3, 4, 5; int *p = numbers[0];

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 18 / 65
User-Defined Data Types

struct: Group of variables of different types.


union: Group of variables sharing the same memory location.
enum: Enumeration of named integer constants.
Example: struct Point int x; int y; ;
union Data int i; float f; char str[20]; ;
enum Weekday SUNDAY, MONDAY, TUESDAY;

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 19 / 65
Variables in C
1 Basic Structure of a C Program
Preprocessor Directives
Global Declarations
The main() Function
Variable Declarations
Function Definitions
Statements and Expressions
Comments
Example
Explanation of the Example
2 Data Types
Basic Data Types
Integer Types
Floating-Point Types
Character Type
Derived Data Types
User-Defined Data Types
3 Variables in C The Co-operatetive University of Kenya Sep
Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 20 / 65
Variable Declaration

Specify the type and name of the variable.


Example: int number; float salary; char grade;

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 21 / 65
Variable Initialization

Initialize variables at the time of declaration.


Example: int number = 10; float salary = 5000.50; char grade = ’A’;

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 22 / 65
Variable Scope

Scope refers to the region of the program where the variable can be
accessed.
Types: Local Scope, Global Scope, Block Scope.

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 23 / 65
Local Scope

Declared inside a function or block.


Accessible only within that function or block.
Destroyed when the function or block exits.
Example: void function() int localVar = 10; // Local variable
printf(”Local Variable:

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 24 / 65
Function Scope

Local variables declared inside a function.


Accessible only within that function.
Example: void function() int localVar = 10; // Local variable
printf(”Local Variable:

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 25 / 65
Block Scope

Declared inside a block (within curly braces).


Accessible only within that block.
Example: int main() int a = 10;
if (a ¿ 5) int b = 20; // Block scope printf(”Block Variable:
return 0;

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 26 / 65
Global Scope

Declared outside all functions.


Accessible from any function within the program.
Example: int globalVar = 20; // Global variable
void function() printf(”Global Variable:
int main() function(); printf(”Global Variable: return 0;

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 27 / 65
Static Variables

Retain their value between function calls.


Can be declared with local or global scope.
Stored in the static memory area.

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 28 / 65
Static Local Variables

Declared inside a function with the static keyword.


Retain their value between function calls.
Example: void function() static int count = 0; // Static local variable
count++; printf(”Count:
int main() function(); // Output: Count: 1 function(); // Output:
Count: 2 function(); // Output: Count: 3 return 0;

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 29 / 65
Static Global Variables

Declared outside all functions with the static keyword.


Accessible only within the file where they are declared.
Example: static int globalVar = 20; // Static global variable
void function() printf(”Global Variable:
int main() function(); printf(”Global Variable: return 0;

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 30 / 65
Register Variables

Stored in the CPU registers for faster access.


Declared with the register keyword.
Limited to local scope.
Example: void function() register int i; for (i = 0; i ¡ 10; i++) printf(”
printf(””);
int main() function(); return 0;

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 31 / 65
Extern Variables

Declared with the extern keyword.


Indicate that their definition is in another file.
Used to share variables across multiple files.
Example:
// File1.c extern int globalVar; // Extern variable declaration
void function() printf(”Global Variable:
// File2.c int globalVar = 20; // Global variable definition
int main() function(); printf(”Global Variable: return 0;

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 32 / 65
Data Type Modifiers
1 Basic Structure of a C Program
Preprocessor Directives
Global Declarations
The main() Function
Variable Declarations
Function Definitions
Statements and Expressions
Comments
Example
Explanation of the Example
2 Data Types
Basic Data Types
Integer Types
Floating-Point Types
Character Type
Derived Data Types
User-Defined Data Types
3 Variables in C The Co-operatetive University of Kenya Sep
Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 33 / 65
Signed and Unsigned Modifiers

Signed modifiers allow variables to hold both positive and negative


values.
Unsigned modifiers restrict variables to hold only non-negative
values, effectively doubling their maximum value.

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 34 / 65
Signed Integers

By default, integer types (int, short, long) are signed.


They can store both positive and negative values.
Example: int a = -10; // Signed integer short b = 30000; // Signed
short integer long c = -100000L; // Signed long integer

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 35 / 65
Unsigned Integers

The unsigned modifier specifies that a variable can only hold


non-negative values.
This effectively doubles the maximum value that the variable can
store.
Example: unsigned int a = 10; // Unsigned integer unsigned short b =
60000; // Unsigned short integer unsigned long c = 100000UL; //
Unsigned long integer

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 36 / 65
Short and Long Modifiers

The short modifier reduces the storage size of the integer type.
The long modifier increases the storage size of the integer type.
When used with double, long increases the precision.

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 37 / 65
Short Integers

The short modifier is used to reduce the storage size of the integer
type.
Example: short int a = 1000; // Short integer short b = 2000; // Short
integer (int can be omitted)

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 38 / 65
Long Integers

The long modifier increases the storage size of the integer type.
When used with double, it increases the precision.
Example: long int a = 100000L; // Long integer long b = 200000L; //
Long integer (int can be omitted) long long c = 100000000000LL; //
Long long integer
double x = 3.14; long double y = 3.141592653589793238L; // Long
double for higher precision

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 39 / 65
Combining Modifiers

Data type modifiers can be combined to achieve the desired storage


size and value range.
Example Combinations: unsigned short int a = 50000; // Unsigned
short integer long long int b = 100000000000LL; // Long long integer
unsigned long int c = 4000000000UL; // Unsigned long integer

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 40 / 65
Storage Size and Value Ranges

The storage size and value ranges of modified data types depend on
the system architecture (32-bit vs. 64-bit) and the compiler.
Overview:
short: 2 bytes, range: -32,768 to 32,767 (signed), 0 to 65,535
(unsigned)
int: 4 bytes, range: -2,147,483,648 to 2,147,483,647 (signed),
0 to 4,294,967,295 (unsigned)
long: 4 or 8 bytes, range: depends on the system
long long: 8 bytes, range: -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807 (signed), 0 to
18,446,744,073,709,551,615 (unsigned)
float: 4 bytes, range: 1.2E-38 to 3.4E+38
double: 8 bytes, range: 2.3E-308 to 1.7E+308
long double: 10, 12, or 16 bytes, range: depends on the system
The Co-operatetive University of Kenya Sep
Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 41 / 65
Constants and Literals
1 Basic Structure of a C Program
Preprocessor Directives
Global Declarations
The main() Function
Variable Declarations
Function Definitions
Statements and Expressions
Comments
Example
Explanation of the Example
2 Data Types
Basic Data Types
Integer Types
Floating-Point Types
Character Type
Derived Data Types
User-Defined Data Types
3 Variables in C The Co-operatetive University of Kenya Sep
Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 42 / 65
Constants

Variables whose value cannot be changed once defined.


Provide a way to define immutable values.

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 43 / 65
const Keyword

Used to declare constant variables.


Example: const int MAXS IZE = 100; constfloatPI = 3.14;

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 44 / 65
#define Directive

Used to define constant values and macros.


Example: define MAXS IZE 100definePI 3.14

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 45 / 65
Enumerations

User-defined data types consisting of named integer constants.


Example: enum Days SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY;

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 46 / 65
Constant Pointers

Pointers can also be declared as constants.


Example: const int *ptr1; // Pointer to a constant integer int *const
ptr2 = var; // Constant pointer to an integer

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 47 / 65
Literals

Fixed values used directly in the code.


Can be of various types: integer, floating-point, character, string,
boolean.

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 48 / 65
Integer Literals

Represent whole numbers without any fractional part.


Can be specified in different bases: decimal, octal, hexadecimal.
Example: int decimal = 100; int octal = 0144; // Octal representation
of 100 int hexadecimal = 0x64; // Hexadecimal representation of 100

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 49 / 65
Floating-Point Literals

Represent real numbers with a fractional part.


Can be written in decimal form or exponential (scientific) notation.
Example: float decimalFloat = 3.14; double scientificFloat = 1.23e4; //
1.23 * 104

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 50 / 65
Character Literals

Represent single characters enclosed in single quotes.


Can also represent escape sequences.
Example: char letter = ’A’; char newline = ”; // Newline character char
 // Tab character
tab = ’’;

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 51 / 65
String Literals

Represent sequences of characters enclosed in double quotes.


Stored as arrays of characters with a null terminator (\0).
Example: char greeting[] = ”Hello, World!”;

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 52 / 65
Boolean Literals

Represent truth values: true or false.


In C, represented by integers: 0 (false) and 1 (true).
stdbool.h header file can be included to use bool data type.
Example: include ¡stdbool.h¿
bool flag = true;

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 53 / 65
Operators
1 Basic Structure of a C Program
Preprocessor Directives
Global Declarations
The main() Function
Variable Declarations
Function Definitions
Statements and Expressions
Comments
Example
Explanation of the Example
2 Data Types
Basic Data Types
Integer Types
Floating-Point Types
Character Type
Derived Data Types
User-Defined Data Types
3 Variables in C The Co-operatetive University of Kenya Sep
Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 54 / 65
Arithmetic Operators

Perform basic mathematical operations.


Types: Addition, Subtraction, Multiplication, Division, Modulus,
Increment, Decrement.

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 55 / 65
List of Arithmetic Operators

Addition (+): Adds two operands.


Subtraction (-): Subtracts the second operand from the first.
Multiplication (*): Multiplies two operands.
Division (/): Divides the first operand by the second.
Modulus (%): Returns the remainder of the division.
Increment (++): Increases an integer value by one.
Decrement (–): Decreases an integer value by one.
Example: int a = 10, b = 5, result;
result = a + b; // Addition: result is 15 result = a - b; // Subtraction:
result is 5 result = a * b; // Multiplication: result is 50 result = a / b; //
Division: result is 2 result = a
a++; // Increment: a is now 11 b–; // Decrement: b is now 4

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 56 / 65
Relational Operators

Used to compare two values.


Return either true (1) or false (0).

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 57 / 65
List of Relational Operators
Equal to (==): Checks if two operands are equal.
Not equal to (!=): Checks if two operands are not equal.
Greater than (¿): Checks if the left operand is greater than the
right operand.
Less than (¡): Checks if the left operand is less than the right
operand.
Greater than or equal to (¿=): Checks if the left operand is
greater than or equal to the right operand.
Less than or equal to (¡=): Checks if the left operand is less than
or equal to the right operand.
Example: int a = 10, b = 5; int result;
result = (a == b); // Equal to: result is 0 (false) result = (a != b); //
Not equal to: result is 1 (true) result = (a ¿ b); // Greater than: result is
1 (true) result = (a ¡ b); // Less than: result is 0 (false) result = (a ¿=
b); // Greater than or equal to: result is 1 (true) result = (a ¡= b); //
Less than or equal to: result is 0 (false)
The Co-operatetive University of Kenya Sep
Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 58 / 65
Logical Operators

Perform logical operations and return true or false.

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 59 / 65
List of Logical Operators

Logical AND (&&): Returns true if both operands are true.


Logical OR (——): Returns true if at least one operand is true.
Logical NOT (!): Returns true if the operand is false and vice versa.
Example: int a = 1, b = 0; int result;
result = (a b); // Logical AND: result is 0 (false) result = (a —— b); //
Logical OR: result is 1 (true) result = !a; // Logical NOT: result is 0
(false) result = !b; // Logical NOT: result is 1 (true)

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 60 / 65
Bitwise Operators

Perform bit-level operations on integer types.


Manipulate individual bits of the operands.

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 61 / 65
List of Bitwise Operators

Bitwise AND (&): Performs a bitwise AND operation.


Bitwise OR (—): Performs a bitwise OR operation.
Bitwise XOR (ˆ): Performs a bitwise XOR operation.
Bitwise NOT (˜): Performs a bitwise NOT operation.
Left shift (¡¡): Shifts bits to the left.
Right shift (¿¿): Shifts bits to the right.
Example: int a = 5, b = 3; // a = 0101, b = 0011 in binary int result;
result = a b; // Bitwise AND: result is 1 (0001 in binary) result = a — b;
// Bitwise OR: result is 7 (0111 in binary) result = a
b ; //BitwiseXOR : resultis6(0110inbinary )result = a; //BitwiseNOT :

resultis − 6(1111...1010inbinary )result = a << 1; //Leftshift :


resultis10(1010inbinary )result = a >> 1; //Rightshift :
resultis2(0010inbinary )

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 62 / 65
Operator Precedence and Associativity

Operator precedence determines the order in which operators are


evaluated.
Associativity determines the order in which operators of the same
precedence are evaluated.

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 63 / 65
Precedence Table
Highest: () [] -> .
Unary: + - ++ -- ! ~
Multiplicative: * / %
Additive: + -
Shift: << >>
Relational: < <= > >=
Equality: == !=
Bitwise AND: &
Bitwise XOR: ^
Bitwise OR: |
Logical AND: &&
Logical OR: ||
Conditional: ?:
Assignment: = += -= *= /= %= &= ^= |= <<= >>=
Lowest: ,
Example: int a = 10, b = 5, c = 2; int result;
result = a + b * c; // Multiplication has higher precedence than
The Co-operatetive addition:
University of Kenya Sep
Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 64 / 65
Conclusion

Understanding the structure and components of a C program is


crucial for writing efficient code.
Knowing the various data types, variables, constants, literals, and
operators helps in effective programming.
Proper use of scope and data type modifiers optimizes memory usage
and enhances code readability.

The Co-operatetive University of Kenya Sep


Dr. Shem Mbandu Angolo, PhD BCSC 1102 : Intro To Programming Week 2 65 / 65

You might also like