Powered By Blogger

Thursday 28 April 2011

Guess why the output is different from the initialized value. Its due to a simple change done while initialization nd with a definite logic.

#include<stdio.h>

#include<conio.h>
int main(void)
{
  int i=012;
  int j=046;
  int k=056;
  printf("i=%d\n",i);
  printf("j=%d\n",j);
  printf("k=%d\n",k);

  getch();

  return 0;
}


A tricky C program to print the string "Hi i'm Subhabrata Chakraborty" without using any semicolon(;) in the function.

#include<stdio.h>
#include<conio.h>
void main(void)
{
  if(printf("Hi I'm Subhabrata Chakraborty"))
}

A tricky C program where a negetive no is shown as greater than positive one. But its not by mistake, there is a definite computing logic.

#include<stdio.h>
#include<conio.h>
int main(void)
{
  unsigned int y = 12;
  int x = -2;
 
   if(x>y)
     printf   (" x is greater");
  else
     printf (" y is greater");
   getch();
 
return 0;
}

Create a 2D array dynamically in called function then editing its value and then it's passed to main() using double pointer. Finally updated matrix is printed from main().

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
int ** passed(int,int);
int main(void)
{
    int **vam,i=0,j=0,order[2];
    printf("\n**************** Addition to matrix by passing pointer ****************\n\n");
   
    printf("Input the order of the matrix (m n) ::\n");
    for(i=0;i<2;i++)
        scanf(" %d",&order[i]);
    //Function called to get the input and to edit
    vam=passed(order[0],order[1]);
    //Matrix edited and is stored into vam
    printf("\n\nMatrix after ::\n\n");

Create a 2D array dynamically then passing it to another function using Call by Reference and then updating the matrix by using pointers and without copying it to any other dummy matrix and without using generel matrix accesing logic. Then its printed from the Main() function.

#include<stdio.h>
#include<conio.h>
int passed(int **,int,int);
int main(void)
{
    int **matrix,order[2],i=0,j=0;
    printf("\n**************** Addition to 2D matrix by pointer arithmetic  ****************\n\n");
    printf("Input the order of the matrix (m n) ::\n");
    for(i=0;i<2;i++)
        scanf(" %d",&order[i]);
    matrix=(int**)malloc((order[0]+2)*sizeof(int*));
    for(i=0;i<order[0];i++)
        matrix[i]=(int*)malloc((order[1]+2)*sizeof(int));
    printf("\n\nEnter the elements of the matrix (Row Wise)::\n");
    for(i=0;i<order[0];i++)
    {
        for(j=0;j<order[1];j++)
        {
            scanf("%d",&matrix[i][j]);
        }
    }