C – Program Code – Basics Part III

Submitted by Karthikeyan on
  1. Precedence of Evaluation of expression
       

()

%

/

*

-

+

 

 

When parenthesis are nested, innermost parenthesis first evaluated.

  1. Implicit Type conversation
  • Automatic conversion of variables and constants of different types in an expression to proper type.

Conversion Hierarchy

 

 

  1. 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)
Brackets (array subscript)
Member selection via object name
Member selection via pointer
Postfix increment/decrement (see Note 2)

left-to-right

++ --
+ -
! ~
(type)
*
&
sizeof

Prefix increment/decrement
Unary plus/minus
Logical negation/bitwise complement
Cast (convert value to temporary value of type)
Dereference
Address (of operand)
Determine size in bytes on this implementation

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
Relational greater than/greater 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
Addition/subtraction assignment
Multiplication/division assignment
Modulus/bitwise AND assignment
Bitwise exclusive/inclusive OR assignment
Bitwise shift left/right 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/

 

 

  1. Reading a character

variable_name = getchar();

 e.g.

printf(“Type (Y/N) :”);
answer = getchar();

 

  1. getchar() function accepts any character keyed in, including RETURN / ENTER and TAB.
  2. Character test functions:
    isalnum(c), isalpha(c), isdigit(c), islower(c), isprint(c), ispunct(c), isspace(c), isupper(c)
  3. Writing a character

putchar(variable_name);

e.g.      answer = ‘y’;

            putchar(answer);

  1. Reading formatted input

scanf(“control string”, arg1, arg2, … argn);
e.g.  scanf(“%2d %5d”, &num1, &num2);

 

  1. Reading character strings using specified character width (number of characters)

%ws    or %wc

e.g.
scanf(“%d %15c”, &no, name1);

 

  1. %s specifier can’t be used to read strings with blank spaces. But, blank spaces can be read using %[] specification.
  2. 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

 

  1. Important points to remember while using scanf:
  1. All function arguments except the control string (s) must be pointers to variables.
  2. Control string format specification should match with arguments order
  3. Input data items separated by spaces in the same order