- Precedence of Evaluation of expression
| → |
|
() |
|
% |
|
/ |
|
* |
|
- |
|
+ |
When parenthesis are nested, innermost parenthesis first evaluated.
- Implicit Type conversation
- Automatic conversion of variables and constants of different types in an expression to proper type.
Conversion Hierarchy
- Explicit conversion
(type-name) expression
e.g. x = (int)(y+0.8)
C Operator Precedence Table
This page lists C operators in order of precedence (highest to lowest). Their associativity indicates in what order operators of equal precedence in an expression are applied.
|
Operator |
Description |
Associativity |
|
( ) |
Parentheses (function call) (see Note 1) |
left-to-right |
|
++ -- |
Prefix increment/decrement |
right-to-left |
|
* / % |
Multiplication/division/modulus |
left-to-right |
|
+ - |
Addition/subtraction |
left-to-right |
|
<< >> |
Bitwise shift left, Bitwise shift right |
left-to-right |
|
< <= |
Relational less than/less than or equal to |
left-to-right |
|
== != |
Relational is equal to/is not equal to |
left-to-right |
|
& |
Bitwise AND |
left-to-right |
|
^ |
Bitwise exclusive OR |
left-to-right |
|
| |
Bitwise inclusive OR |
left-to-right |
|
&& |
Logical AND |
left-to-right |
|
| | |
Logical OR |
left-to-right |
|
? : |
Ternary conditional |
right-to-left |
|
= |
Assignment |
right-to-left |
|
, |
Comma (separate expressions) |
left-to-right |
|
Note 1: Parentheses are also used to group sub-expressions to force a different precedence; such parenthetical expressions can be nested and are evaluated from inner to outer. Note 2: Postfix increment/decrement have high precedence, but the actual increment or decrement of the operand is delayed (to be accomplished sometime before the statement completes execution). So in the statement y = x * z++; the current value of z is used to evaluate the expression (i.e., z++ evaluates to z) and z only incremented after all else is done. |
||
|
Source : http://www.difranco.net/ |
||
- Reading a character
variable_name = getchar();
e.g.
printf(“Type (Y/N) :”);
answer = getchar();
- getchar() function accepts any character keyed in, including RETURN / ENTER and TAB.
-
Character test functions:
isalnum(c), isalpha(c), isdigit(c), islower(c), isprint(c), ispunct(c), isspace(c), isupper(c) - Writing a character
putchar(variable_name);
e.g. answer = ‘y’;
putchar(answer);
- Reading formatted input
scanf(“control string”, arg1, arg2, … argn);
e.g. scanf(“%2d %5d”, &num1, &num2);
- Reading character strings using specified character width (number of characters)
%ws or %wc
e.g.
scanf(“%d %15c”, &no, name1);
- %s specifier can’t be used to read strings with blank spaces. But, blank spaces can be read using %[] specification.
- Scanf format codes / Applicable to printf statements also.
|
Format |
To read |
|
%c |
Single character |
|
%d |
Decimal integer |
|
%e |
Floating point value |
|
%f |
Floating point value |
|
%g |
Floating point value (either e type or f type) |
|
%h |
Short integer |
|
%i |
Decimal / hexadecimal or Octal Integer |
|
%o |
Octal integer |
|
%s |
String |
|
%u |
Unsigned decimal integer |
|
%x |
Read a hexadecimal integer |
|
%[..] |
String of words |
Prefix:
h – for short integers
l – for long integers or double
L – for long double
- Important points to remember while using scanf:
- All function arguments except the control string (s) must be pointers to variables.
- Control string format specification should match with arguments order
- Input data items separated by spaces in the same order