Fix guide -c -operators. Fixed formatting, added examples (#22717)

* working on fixing sizeof() operator

* working on fixing sizeof() operator

* Update index.md

* Update index.md
This commit is contained in:
doug b
2018-11-25 13:42:43 -05:00
committed by Christopher McCormack
parent 5607a6092b
commit bb34761dfd

View File

@ -191,15 +191,42 @@ int main()
a %= 5; // equivalent to a = a % 5 = 21 % 5 = 1
```
Misc Operators sizeof & ternary
Besides the operators discussed above, there are a few other important operators including sizeof and ? : supported by the C Language.
## 6. Misc Operators, sizeof(), Address, Value at Address and Ternary
Operator Description Example
sizeof() Returns the size of a variable. sizeof(a), where a is integer, will return 4.
& Returns the address of a variable. &a; returns the actual address of the variable.
* Pointer to a variable. *a;
? : Conditional Expression. If Condition is true ? then value X : otherwise value Y
- `sizeof()` Returns the amount of memory allocated.
```C
int a;
sizeof(a); // result is 4
double b;
sizeof(b); // result is 8
// note the result of sizeof() may vary depending on the type of machine.
```
- `&` Address Operator, Gives the address of a variable.
```C
int a;
&a; // result an address such as 860328156
```
- `*` Value at Address Operator, gives the value at an address.
```C
int a = 50;
int *b = &a;
&a; // result is the address of 'a' such as 1152113732
*b; // result is 50, the value stored at the address
```
- `? :` Ternery Operator, simplifies a typical if-else conditional. condition ? value_if_true : value_if_false
```C
// a typical if-else statement in c
if (a > b) {
result = x;
}
else {
result = y;
}
// can be rewritten with the ternary operator as
result = a > b ? x : y;
```
## 6. Operator precedence and associativity in C
Operators with the highest precedence appear at the top of the list. Within an expression, operators