Skip to content

Updated operator.md #4289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion docs/C/operator.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,42 @@ Operators in C are special symbols used to perform operations on variables and v
printf("a: %d, b: %d, c: %d\n", a, b, c); // Output: a: 5, b: 3, c: 5
```

### Type Conversion in C

A variable's data type can be changed from one data type to another by type conversion.Type conversion is mainly of two types:
- Implicit Type Conversion
- Explicit Type Conversion

1. **Implicit Type Conversion**:
- The type conversion that is done automatically by the compiler is known as implicit type conversion. This usually occurs when variables of different data types are used together in an expression in order to avoid loss of data. All the variables are upgraded to the data type of variable with largest data type.
- Example:
```c
#include <stdio.h>
int main() {
int a = 5;
float b = 4.5;
float result;
result = a + b; // Implicit conversion of 'a' to float before the addition
printf("Result: %f\n", result);
return 0;
}
```

2. **Explicit Type Conversion**:
- Explicit type conversion is when the user explicitly specifies the type to which a variable should be converted. This is done using the cast operator `(type)`.
- Example:
```c
#include <stdio.h>
int main() {
int a = 5;
float b = 4.5;
int result;
result = a + (int)b; // Explicit conversion of 'b' to int before the addition
printf("Result: %d\n", result);
return 0;
}
```

### Key Points about C Operators

1. **Versatility**:
Expand All @@ -137,4 +173,4 @@ Operators in C are special symbols used to perform operations on variables and v

### Conclusion

Operators are fundamental components of the C programming language, enabling developers to perform a wide range of operations efficiently. From basic arithmetic to advanced bitwise manipulations, C operators provide the tools necessary for building robust and performant applications. Understanding the different types of operators, their functionalities, and their correct usage is essential for writing effective C code. Mastery of operators also contributes to better optimization and control over system resources, making C a powerful language for both low-level and high-level programming tasks.
Operators are fundamental components of the C programming language, enabling developers to perform a wide range of operations efficiently. From basic arithmetic to advanced bitwise manipulations, C operators provide the tools necessary for building robust and performant applications. Understanding the different types of operators, their functionalities, and their correct usage is essential for writing effective C code. Mastery of operators also contributes to better optimization and control over system resources, making C a powerful language for both low-level and high-level programming tasks.
Loading