Lecture Note CSC104
Lecture Note CSC104
Data Type |Variable |Declaration |Initialization |Assignment |Operator: Arithmetic, Logical and
relational
Data Types in C
Data types in C specify the type of data that a variable can store. Here are some basic data types in C:
int myInteger;
float myFloat;
char myChar;
double myDouble;
Bool myBool;
```
void myVoid;
8. Structures: User-defined data type that groups related data under one name.
struct Point {
int x;
int y;
};
9. Unions: Similar to structures but members share the same memory location.
union Value {
int intValue;
float floatValue;
char stringValue[20];
};
Variables in C
A variable is a named location in memory that holds a value. To use a variable in C, you need to declare it
first.
Declaration of Variables
int age;
float temperature;
char grade;
Initialization of Variables
Initialization is the process of assigning an initial value to a variable at the time of declaration.
int count = 0;
float pi = 3.14;
Variables can also be declared first and then initialized later in the program.
int quantity; // Declaration
Example Program
#include <stdio.h>
int main() {
int num1 = 5;
// Output values
return 0;
In this example, we declare and initialize variables of different data types and print their values using
`printf()`.
Assignment
In C programming, assignment is the process of storing a value in a variable. The assignment operator
(`=`) is used for this purpose. Here's an explanation of how assignment works in C:
The standard assignment operator = simply stores the value of its right operand in the variable specified
by its left operand. As with all assignment operators, the left operand (commonly referred to as the
“lvalue”) cannot be a literal or constant value.
int x = 10;
float y = 45.12 + 2.0;
int z = (2 * (3 + function () ));
Note that, unlike the other assignment operators described below, you can use the plain assignment
operator to store values of a structure type.
Compound assignment operators perform an operation involving both the left and right operands, and
then assign the resulting expression to the left operand. Here is a list of the compound assignment
operators, and a brief description of what they do:
+=
Adds the two operands together, and then assign the result of the addition to the left operand.
-=
Subtract the right operand from the left operand, and then assign the result of the subtraction to the left
operand.
*=
Multiply the two operands together, and then assign the result of the multiplication to the left operand.
/=
Divide the left operand by the right operand, and assign the result of the division to the left operand.
%=
Perform modular division on the two operands, and assign the result of the division to the left operand.
<<=
Perform a left shift operation on the left operand, shifting by the number of bits specified by the right
operand, and assign the result of the shift to the left operand.
>>=
Perform a right shift operation on the left operand, shifting by the number of bits specified by the right
operand, and assign the result of the shift to the left operand.
&=
Perform a bitwise conjunction operation on the two operands, and assign the result of the operation to
the left operand.
^=
Performs a bitwise exclusive disjunction operation on the two operands, and assign the result of the
operation to the left operand.
|=
Performs a bitwise inclusive disjunction operation on the two operands, and assign the result of the
operation to the left operand.
Since there are no side effects wrought by evaluating the variable x as an lvalue, the above code
produces the same result as:
x = x + y;
#include <stdio.h>
int main() {
// Declare variables
int x, y;
float pi;
char letter;
x = 10;
y = x + 5;
pi = 3.14;
letter = 'A';
x = 20;
y = x * 2;
return 0;
}
In this example:
- Variables (`x`, `y`, `pi`, `letter`) are declared with their respective data types.
- Values are assigned to these variables using the assignment operator (`=`).
- The values of `x` and `y` are updated, and the updated values are displayed.
It's important to note that the right-hand side of the assignment operator is evaluated first, and then its
value is stored in the left-hand side variable.
x = x + 5; // Increment x by 5
x += 5; // Equivalent to x = x + 5;
y *= 2; // Equivalent to y = y * 2;
These compound assignment operators are convenient for concise and readable code.
Arithmetic Operators
C provides operators for standard arithmetic operations: addition, subtraction, multiplication, and
division, along with modular division. Usage of these operators is straightforward; here are some
examples:
Arithmetic operators are used for mathematical operations:
1. + (Addition):
2. - (Subtraction):
3. * (Multiplication):
5. % (Modulus):
Relational Operators
You use the relational operators (also called comparison operators) to determine how two operands
relate to each other: are they equal to each other, is one larger than the other, is one smaller than the
other, and so on. Relational operators are used for comparing values:
1. == (Equal to):
```c
int a = 5, b = 7;
if (a == b) {
// Condition is true
```c
if (x != y) {
// Condition is true
```c
int p = 8, q = 5;
if (p > q) {
// Condition is true
}
```
int m = 3, n = 6;
if (m < n) {
// Condition is true
// Condition is true
// Condition is true
Logical Operators
```c
int x = 5, y = 10;
}
2. || (Logical OR):
int a = 3, b = 0;
if (a > 0 || b > 0) {
3. ! (Logical NOT):
int flag = 1;
if (!flag) {