Skip to content

Commit b4685d8

Browse files
authored
Merge pull request #4289 from suhanipaliwal/suhanipaliwal-patch-1
Updated operator.md
2 parents 97f3c0d + 1b2f146 commit b4685d8

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

docs/C/operator.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,42 @@ Operators in C are special symbols used to perform operations on variables and v
115115
printf("a: %d, b: %d, c: %d\n", a, b, c); // Output: a: 5, b: 3, c: 5
116116
```
117117

118+
### Type Conversion in C
119+
120+
A variable's data type can be changed from one data type to another by type conversion.Type conversion is mainly of two types:
121+
- Implicit Type Conversion
122+
- Explicit Type Conversion
123+
124+
1. **Implicit Type Conversion**:
125+
- 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.
126+
- Example:
127+
```c
128+
#include <stdio.h>
129+
int main() {
130+
int a = 5;
131+
float b = 4.5;
132+
float result;
133+
result = a + b; // Implicit conversion of 'a' to float before the addition
134+
printf("Result: %f\n", result);
135+
return 0;
136+
}
137+
```
138+
139+
2. **Explicit Type Conversion**:
140+
- 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)`.
141+
- Example:
142+
```c
143+
#include <stdio.h>
144+
int main() {
145+
int a = 5;
146+
float b = 4.5;
147+
int result;
148+
result = a + (int)b; // Explicit conversion of 'b' to int before the addition
149+
printf("Result: %d\n", result);
150+
return 0;
151+
}
152+
```
153+
118154
### Key Points about C Operators
119155

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

138174
### Conclusion
139175

140-
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.
176+
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.

0 commit comments

Comments
 (0)