Wednesday, April 6, 2011

IF-ELSE

The simplest of the decision making in a programming language is if-else.
But before this we need to go inside the operators used for comparison.

#include<stdio.h>
int main(){
int i = 10;
printf("%d %d %d",i>20, i< 20; i ==20);
 }

Answer is:
0 1 0

This thing you won't find in most of the books. When a comparison fails then it is replaced by 0 otherwise by 1. In the above example when i>20 proves to be failed, then we have found 0 as the output. Isn't it amazing, how the things are done in a simple manner.

Working example.




#include<stdio.h>

int main(){

int i = 10;


if(i<5)
         printf("i is greater than 5\n");

else
         printf("i is less than 5\n");

 }

Output: i is greater than 5.


Tuesday, February 22, 2011

C Programming - Part 5

Increment/Decrement Operator with example.

int main(){
     int i=10,j;
     j=++i;
     printf("%d\n",j,i);
     j=i++; 
    printf("%d\n",j,i);

}

$ gcc test.c
./a.out
11 11
11 12

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

C Programming - Part 3

Structure of a ‘C’ program
This is the structure of program which shows how it looks like.

      pre-processor directives
      global declarations
      function prototypes
      main( )
      {
      local variables to function main ;
      statements associated with function main ;
      }
      f1( )
      {
      local variables to function f1 ;
      statements associated with function f1 ;
      }
      f2( )
      {
      local variables to function f2 ;
      statements associated with function f2 ;
      }


Structure of my first ‘C’ program

   /* This is
      a comment */
   // This is a one-line comment


   # include <stdio.h>     /* includes header files */
  int  main( )     /* Must have a main function. First function executed */
   {
   printf ("Hello World!");      /* stdio functions */
   }



After making this program we need to compile it.One of the compiler we are using is gcc compiler . gcc compiler is best compiler for C programs.The full form of gcc is GNU Compiler Collection .