Wednesday, October 10, 2012

sizeof() Operator

sizeof() Operator :

sizeof operator is a unary operator.

Unary operator: Unary operation is an operation with only one operand that is which takes single input.

sizeof operator is used to know the exact size of any data type (In bytes).

sizeof (Data type);

Example_1 with code:
1:  #include<stdio.h>  
2:  void main(void)  
3:  {  
4:      int i;  
5:      float j;  
6:      char k;  
7:      printf ("int size = %d\n",sizeof(int));  
8:      printf("float size = %d\n",sizeof(float));  
9:      printf("char size = %d\n",sizeof(char));  
10:      printf("int size = %d\n",sizeof(i));  
11:      printf("float size = %d\n",sizeof(j));  
12:      printf("char size = %d\n",sizeof(k));  
13:  }  
Output:
1:  int size = 4  
2:  float size = 4  
3:  char size = 1  
4:  int size = 4  
5:  float size = 4  
6:  char size = 1  


Example_2 with code:

1:  #include<stdio.h>  
2:  struct a  
3:  {  
4:      int a;  
5:      float b;  
6:      int c;  
7:  };  
8:  void main(void)  
9:  {  
10:    struct a obj;   
11:    printf("struct a size = %d\n",sizeof(struct a));  
12:    printf("obj size = %d\n",sizeof(obj)); 
13:  }  
Output:
1:  obj size = 12  
2:  struct a size = 12  


From the output of example 1 we can clearly see that the size of int and size of float is 4 bytes and size of char is 1 byte.

 In example 2 we have declared a structure "a" having members

int a;
float b;
int c;

what would be the size of structure a?

From example one we observed that int and float is of 4 bytes so the size of structure is
4 + 4 + 4 = 12 bytes
so the output of second example of first "printf" is 12.
In the example we have declared a variable of type struct a
struct a obj;
obj has reserved 12 bytes of memory so when we will print the sizeof (obj) then it will also yield 12 bytes.

Stray Pointer

Stray pointer: 

Stray pointers are some times very dangerous because with stray pointer your program can crash. As we can guess from the name "stray" stray pointers are really stray having no home. Let me clear you with an example.

Suppose in your program you have dynamically allocated some memory (Using malloc( )) and then after using that memory you freed that memory (Using free( )).

Now stray pointer will be created if you will access the deleted memory or you will assign some value at that address.

Example with C program:

1: #include<stdio.h>  
2: void main(void)  
3:{  
4:  int i;  
5:  int *ptr;  
6:  ptr = (int *)malloc(sizeof(int) * 5); // Dynamically allocating 20 Bytes of memory  
7:  for (i = 0 ; i < 5 ; i++)  
8:  {  
9:      *(ptr+i) = i;            // Assing values  
10: }  
11: for (i = 0 ; i < 5 ; i++)  
12: {  
13:     printf("value = %d\n",*(ptr+i));  // printing values on console  
14: }  
15: free(ptr);               // Freeing memory   
16: for (i = 0 ; i < 5 ; i++)   
17: {  
18:     *(ptr+i) = i;            //uh oh, This was deleted!!  
19: }  
20:}  

Explanation: 

In the 6th line we have dynamically allocated 20 Bytes (int is of 4 bytes) of memory.

Now pointer "ptr" has the base address of allocated 20 bytes. 
From line 7 to 10 we are assigning values 
From line 11 to 14 we are printing assigned value at console 
In 15th line we freed the memory by calling free function 
From line 16 to 19 That's the main problem here we are assigning the value which is unknown. We are using that memory which does not belong to us.

Monday, October 1, 2012

Fibonacci Series using Recursion

Write a program to print Fibonacci series using recursion?

1:  #include <stdio.h>  
2:  void fibo(int,int,int);  
3:  void main(void)  
4:  {  
5:    int x,y,c = 10;  
6:    x = 0;  
7:    y = 1;  
8:    printf("1 ");  
9:    fibo(x,y,c);  
10:  }  
11:  void fibo(int x,int y,int c)  
12:  {  
13:    int z;  
14:    if (c < 1)  
15:      return;  
16:    else  
17:    {  
18:      z = x + y;  
19:      printf("%d ",z);  
20:      fibo(y,z,c - 1);  
21:     }  
22:  }  

Output:


 1 1 2 3 5 8 13 21 34 55 89