[go: up one dir, main page]

0% found this document useful (0 votes)
12 views9 pages

Lecture Note CSC104

Uploaded by

tonyimoh9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views9 pages

Lecture Note CSC104

Uploaded by

tonyimoh9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

COMPUTER SCIENCE DEPARTMENT

CSC 104 LECTURE NOTE ON C PROGRAMMING LANGUAGE

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:

1. int: Integer data type for storing whole numbers.

int myInteger;

2. float: Floating-point data type for storing numbers with decimals.

float myFloat;

3. char: Character data type for storing a single character.

char myChar;

4. double: Double-precision floating-point data type for more precision.

double myDouble;

5. Bool: Boolean data type for representing true or false values.

Bool myBool;

```

6. void: Special data type representing the absence of a type.

void myVoid;

7. Arrays: Collection of elements of the same data type.

int numbers[5]; // Integer array with 5 elements

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];

};

10. Enumerations: User-defined data type consisting of named integer constants.

enum Days {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};

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

// Syntax: <data_type> <variable_name>;

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;

char letter = 'A';

Variables can also be declared first and then initialized later in the program.
int quantity; // Declaration

quantity = 10; // Initialization

Example Program

#include <stdio.h>

int main() {

// Variable declaration and initialization

int num1 = 5;

float num2 = 3.14;

char symbol = 'A';

// Output values

printf("Integer: %d\n", num1);

printf("Float: %f\n", num2);

printf("Character: %c\n", symbol);

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.

Here is an example of using one of the compound assignment operators:


x += y;

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;

// Assign values to variables

x = 10;

y = x + 5;

pi = 3.14;

letter = 'A';

// Display the values

printf("x: %d\n", x);

printf("y: %d\n", y);

printf("pi: %f\n", pi);

printf("letter: %c\n", letter);

// Updating variable values

x = 20;

y = x * 2;

// Display updated values

printf("Updated x: %d\n", x);

printf("Updated y: %d\n", y);

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 `printf()` function is used to display the values of these variables.

- 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

y = x * 2; // Multiply x by 2 and assign to y

Additionally, compound assignment operators provide a shorthand for combining an arithmetic


operation with assignment:

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):

int sum = 5 + 3; // sum will be 8

2. - (Subtraction):

int difference = 7 - 4; // difference will be 3

3. * (Multiplication):

int product = 2 * 6; // product will be 12


4. / (Division):

float quotient = 10.0 / 3; // quotient will be approximately 3.3333

5. % (Modulus):

int remainder = 10 % 3; // remainder will be 1

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

2. != (Not equal to):

```c

int x = 10, y = 12;

if (x != y) {

// Condition is true

3. > (Greater than):

```c

int p = 8, q = 5;

if (p > q) {

// Condition is true
}

```

4. < (Less than):

int m = 3, n = 6;

if (m < n) {

// Condition is true

5. >= (Greater than or equal to):

int num1 = 10, num2 = 5;

if (num1 >= num2) {

// Condition is true

6. <= (Less than or equal to):

int value1 = 7, value2 = 7;

if (value1 <= value2) {

// Condition is true

Logical Operators

Logical operators are used to perform logical operations:

1. && (Logical AND):

```c

int x = 5, y = 10;

if (x > 0 && y > 0) {

// Both conditions are true

}
2. || (Logical OR):

int a = 3, b = 0;

if (a > 0 || b > 0) {

// At least one condition is true

3. ! (Logical NOT):

int flag = 1;

if (!flag) {

// Condition is true if flag is 0

You might also like