Monday, February 7, 2011

C Programming - Part 4

Working with C Operators 

There are some basic but important things that every learner of C should know . 
These are--             
• Add plenty of comments using  (/*     */    or     // )
• Good layout,
  it should be not like this :
      main( ) {printf("Hello World\n");}
• Use meaningful variable names
• Initialize your variables
• Use parentheses to avoid confusion as:
      a=(10.0 + 2.0) * (5.0 - 6.0) / 2.0




       Common variable types

int -      stores integers
(-32767 to +32768)-- for 16 bit machine
 (-2147483647 to + 2147483648)--  for 32-bit machine
 (-9223372036854775807 to 9223372036854775808)- for 64 bit machine

unsigned int – range upto 0 to 65535 for 16 bit machine
 0 to  4294967296-- for 32- bit machine
 0 to 18446744073709551616--   for 64 bit machine

char – holds 1 byte of data (-127 to 128)
unsigned char – holds 1 byte (0 to +255)
long – usually double int  (signed)
unsigned long – positive double int
float – floating point variable
double – twice of a floating point variable
Note: local, global and static variables


                                 printf  function arguments

• printf (“%d”,i);

Usual variable    type                   Display
%c                      char                   single character
%d (%i)             int                      signed integer
%e (%E)           float or double      exponential format
%f                      float or double      signed decimal
%g (%G)           float or double      use %f or %e as required
%o                     int                        unsigned octal value
%p                     pointer              address stored in pointer
%s                     array of char           sequence of characters
%u                     int                       unsigned decimal
%x (%X)           int                      unsigned hex value



                                     Operators

+         addition
-          subtraction
*         multiplication
/          division
%        mod or remainder (e.g., 2%3 is 2), also called 'modulo'
<<      left-shift (e.g., i<<j is i shifted to the left by j bits)
>>      right-shift
&        bitwise AND
|          bitwise OR
^        bitwise exclusive-OR
&&     logical AND (returns 1 if both operands are non-zero; else 0)
||         logical OR (returns 1 if either operand is non-zero; else 0)
<        less than (e.g., i<j returns 1 if i is less than j)
>        greater than
<=     less than or equal
>=     greater than or equal
==     equals
!=      does not equal

Increment and Decrement Operators

  ++ Increment operator
   -- Decrement Operator

  k++ or k--          (Post-increment/decrement)
  k = 10;
  x = k++;            // sets x to 10, then increments k to 11

  ++k or --k         (Pre-increment/decrement)
  k = 10;
  x = ++k;            // increments k to 11 and then sets x to the
                            // resulting value, i.e., to 11


We can check these increment and decrement operator into our small tricky programs and can check the value .Till next time try it into the programs .
In next post we will try these programs. 


Thanks
Er.Surender Sharma
linux Kernel Developer

1 comment: