Skip to content

Commit

Permalink
prettify
Browse files Browse the repository at this point in the history
  • Loading branch information
nihalxkumar committed Apr 23, 2024
1 parent db6e6c9 commit 2597e0a
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 66 deletions.
25 changes: 13 additions & 12 deletions src/Learning-C/Datatypes.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ Type of data which user wants to store in memory
- long
- 4 bytes are allocated. Range $2^{31}$ to $2^{31}-1$
- short
- smallest -ve number -32768 ($-2^{15}$)
- Range $-32768$ to $32767$
- smallest -ve number -32768 ($-2^{15}$)
- Range $-32768$ to $32767$
- Generalize nbits $-2^{n-1}$ to $2^{n-1} -1$

⭐ bound checking is not present in C
⭐ bound checking is not present in C

### float

Expand Down Expand Up @@ -46,6 +45,7 @@ Type of data which user wants to store in memory
- Character constant can initialize variable having char data
- `char x='a';` here `a` is a character constant
- `char x=a;` here `a` is a variable
- Character constant can be single or double ch long. It can’t be of triple characters.

### void

Expand All @@ -57,12 +57,13 @@ Type of data which user wants to store in memory
- union
- enum

> C uses extended ASCII code of 8bits.
>
> because of escape sequences:
> `char x = '97'` = `x = '9`
>
> `char x='ab'` = `x = 'a`
~~~admonish note
C uses extended ASCII code of 8bits.
because of escape sequences:
```c
char x = 97 // x = 9
> Character constant can be single or double ch long. It can’t be of triple characters.
char x = 'ab' // `x = 'a`
```
~~~
31 changes: 17 additions & 14 deletions src/Learning-C/Operators/Arithmetic.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,28 @@

`+`, `-`, `*`, `/` (quotient operator), `%` (remainder operator or modulus)

## Divison and Modulus operator

> in $n/d$ if $n< d$ then output is $0$
>
> in $n \% d$ if $n<d$ then output is $n$
Rules for arithmetic operators:-

- int/int is always int

- Modulus operator cannot be used with floating point numbers as by default every float is treated as double. C is an
expanding language. `5.2%2` => Error
## Rules for arithmetic operators:-

If more than 1 operator is present in an expression then precedence rule will be applied.
1. Integer Divison
- int $/$ int is always int

PEMDAS stands for
2. Modulus with Integers Only
- Modulus operator cannot be used with floating point numbers as by default every float is treated as double. C is an
expanding language.
```c
// Example of incorrect usage:
float result = 5.2 % 2; // Error
```

- Parentheses
- Exponents
- Multiplication
- Division
- Addition and Subtraction
1. Operator Precedence
- If more than 1 operator is present in an expression then [precedence rule](https://nihalxkumar.tech/Learning-C/Operators/Operators.html#general-order-of-precedence-from-high-to-low) will be applied.

If multiple operations with the same precedence appear, they are typically performed from left to right.
4 Associativity
- If multiple operations with the same precedence appear, the [associativity](https://nihalxkumar.tech/Learning-C/Operators/Operators.html#associativity) rule determines the order of evaluation.
- Arithmetic Operators are left associative.
31 changes: 24 additions & 7 deletions src/Learning-C/Operators/Assignment.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
# Assignment

`=`, `+=`, `-=`, `*=`, `/=`, `%=`, `<<=`, `>>=`, `&=`, `^=`, `|=`
Assignment operators are used to assign values to variables. They not only perform the assignment but also allow for combining arithmetic or bitwise operations in a single step.

- `=` Assign
- `+=` Add and Assign
- `-=` Subtract and Assign
- `*=` Multiply and Assign
- `/=` Divide and Assign
- `%=` Modulus and Assign
- `<<=` Left Shift and Assign
- `>>=` Right Shift and Assign
- `&=` Bitwise AND and Assign
- `^=` Bitwise XOR and Assign
- `|=` Bitwise OR and Assign

These operators allow for combining arithmetic or bitwise operations with assignment in a single step.

- 2 types of assignment
- Simple
- Compound
## Types of Assignment Operators

Rules for assignment operators
1. Simple Assignment:
- In simple assignment, a single variable is assigned a value using the = operator.

2. Compound Assignment:
- Compound assignment operators combine arithmetic or bitwise operations with assignment. They operate on the variable itself, modifying its value in place.

## Rules for assignment operators

1. Single variable is allowed in LHS of assignment operator
2. Cascading of assignment operator

- `int x=y=z=2;`
- `int x=y=z=2;`
- Associativity of assignment operator is from Right to Left

---
Expand All @@ -23,12 +38,14 @@ Q. Swap values without introducing a third variable. `x = 5` and `y = 2`
Sol. There are many ways

```C
// Method 1: Using Arithmetic Operations
x = x + y; // x = 5 + 2 => 7
y = x - y; // y = 7 - 2 => 5
x = x - y; // x = 7 - 5 => 2
```

```C
// Method 2: Using Multiplication and Division
x = x * y; // x = 10
y = x / y; // y = 10/2 => 5
x = x / y; // x = 10/5 => 2
Expand Down
54 changes: 42 additions & 12 deletions src/Learning-C/Operators/Logical.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
# Logical
Logical operators are used to combine two or more conditions to determine the logic between values

- Not
- exclamation !
- AND
- Double ampersand &&
- OR
- Double piped ||
- Short-circuiting
> Short-circuiting refers to a feature of logical operators in programming languages that prevents unnecessary computation when the result of an expression becomes determinable based on earlier parts of the expression. Specifically, for the `OR` operator (`||`), short-circuiting means that once a true value is found among the operands, the entire expression returns true without evaluating subsequent operands. Conversely, for the `AND` operator (`&&`), short-circuiting means that once a false value is found among the operands, the entire expression returns false without evaluating subsequent operands
Any non-zero value in case of C is considered as True

Order of precedence NOT -> AND -> OR
- NOT Operator (`!`)

- The **Not Operator** is represented by an exclamation mark (!). It reverses the logical state of its operand. It converts true to false and false to true.
- In this example, the condition x > 5 evaluates to false, but the ! operator reverses it, so the message will be printed.
```c
int x = 5;
if (!(x > 5)) {
printf("x is not greater than 5\n");
}
```
- AND Operator (`&&`)

- The AND Operator is represented by double ampersands (&&). It returns true only if both operands are true. For instance:
- In this case, both x > 5 and y < 10 are true, so the message will be printed.
```c
int x = 6, y = 8;
if (x > 5 && y < 10) {
printf("Both conditions are true\n");
}
```

- OR Operator (`||`)
- The OR Operator is represented by double pipes (||). It returns true if at least one of the operands is true.
- In this example, x > 5 is false, but y < 10 is true, so the message will still be printed.
```C
int x = 3, y = 12;
if (x > 5 || y < 10) {
printf("At least one condition is true\n");
}
```

```admonish note title = "Short-curcuiting"
Short-circuiting refers to a feature of logical operators in programming languages that prevents unnecessary computation when the result of an expression becomes determinable based on earlier parts of the expression.
Specifically, for the `OR` operator (`||`), short-circuiting means that once a true value is found among the operands, the entire expression returns true without evaluating subsequent operands.
Conversely, for the `AND` operator (`&&`), short-circuiting means that once a false value is found among the operands, the entire expression returns false without evaluating subsequent operands
```

> Any non-zero value in case of C is considered as True
>
> Order of precedence NOT -> AND -> OR
22 changes: 17 additions & 5 deletions src/Learning-C/Operators/Relational.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
# Relational

`>`, `<`, `>=`, `<=`, `==`, `!=`
Relational operators are fundamental for comparing two values. These operators evaluate the relationship between operands and return a Boolean value (1 for true or 0 for false).

Relational operators are used to compare two values.
- `>` Greater Than
- `<` Less Than
- `>=` Greater Than or Equal To
- `<=` Less Than or Equal To
- `==` Equal To
- `!=` Not Equal To

- associativity of relation is Left to Right
## Key Points

- Precedence of arithmetic is more than of Relational
- Associativity of relational operators is Left to Right

- Precedence of Arithmetic Operators is more than of Relational Operators

```C
int a = 5, b = 2, c = 1, d;
d = c + a > b;
// (c + a) > b
// (1 + 5) > b
// 6 > 2
// for True comparisons the output will be 1.
// Since the comparison is true, the output will be 1.
```

## Additional Point

- Character Comparison:
- Relational operators can also be used to compare characters in C. Characters are compared based on their ASCII values. For example, `'a' < 'b'` would evaluate to true because the ASCII value of `'a'` (97) is less than that of `'b'` (98).

3 changes: 3 additions & 0 deletions src/Learning-C/Patterns.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Patterns

## Key points
1. Count the number of lines and loops for the same
2. Find relation between line number, number of characters and spaces in that line. Apply loop for the same.

Expand Down
38 changes: 22 additions & 16 deletions src/Learning-C/Vairables.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,44 @@ Memory is allocated for a variable when it is defined, and the size of the alloc

In C, when you assign a value to a variable, you are actually copying the value to the memory location that the variable represents.


> When you assign a value to a variable, you are essentially giving that value a name. So, when you write a = b;, you are saying "give the value that b refers to the name a".
~~~admonish
When you assign a value to a variable, you are essentially giving that value a name. So, when you write a = b;, you are saying "give the value that b refers to the name a".
```C
int a = 1;
int b = 2;
a = b; // This doesn't mean b is assigned to a.
```
~~~

Variables in C are memory locations that hold values. When you assign a value to a variable in C, you are actually copying the value to the memory location that the variable represents.

#### 3 basic terms related to variables
#### Basic terms related to variables

1. Definition
1. means memory allocation. Whenever a variable is defined, memory is allocated for it.
For example, in `int x;` 2 bytes are allocated for x.
- Allocates memory for a variable
- For example, in `int x;` 2 bytes are allocated for x.

2. Declaration
1. Means providing information to the compiler about the data type of the variable.
2. For example, in `int x;`, the information is given to the compiler that the data type of x is int.
- Means providing information to the compiler about the data type of the variable.
- For example, in `int x;`, the information is given to the compiler that the data type of x is int.


> runtime -> dynamic initialization
>
>compile time -> static initialization
3. Initialization
- Assigns an initial value to a variable at the time of declaration.
- `data_type variable_name = value;`
- For example, `int x = 1;`
- Note:
- Static initialization happens at compile time.
- Dynamic initialization happens at runtime.

### Define and Declare

| Define | Declare | Possible |
|--------|---------|----------------------------------------------------------|
| - [x] | - [x] | Obviously |
| - [ ] | - [x] | Yes possible, you can declare without memory allocations |
| - [x] | - [ ] | Impossible |
| - [ ] | - [ ] | Very nice |
| Define | Declare | Possible |
| ------ | ------- | ------------ |
| - [x] | - [x] | Obviously |
| - [ ] | - [x] | Possible |
| - [x] | - [ ] | Not possible |
| - [ ] | - [ ] | Very nice |

### Sign bit and range

Expand Down

0 comments on commit 2597e0a

Please sign in to comment.