C Interview Questions–to be solved

These programs were asked in various colleges/universities across India. These Questions were in my email dated 1999. So please let me know if any copyright information, so that I will remove it. But these questions are very much helpful for budding Student (engineers and interviewees).


//  Carefully study the given program

#include <stdio.h>
#include <alloc.h>

void main(void);

typedef struct NType
{
    int i;
    char c;
    long x;
}NewType;

void main(void)
{
    NewType *c;
    c=(NewType*)malloc(sizeof(NewType));
    c->i=100;
    c->c='C';
    (*c).x=100l;

    printf("%d,%c,%4ld",c->i,c->c,c->x);
}

/* What is the output of this program ?
a)  It will produce a variable redefinition error
b)  It will print some junk
c)  It will print 100,C,100
d)  It will print 100,C,l100
*/

// Carefully go through the following program
#include <stdio.h>

void main(void);

const int k=100;

void main(void)
{
   int a[100];
   int sum=0;

   for(k=0;k<100;k++)
    *(a+k)=k;
    sum += a[--k];
   printf("%d",sum);
}

/*
What will be the out put of this program ?
a) It will print the sum of all the elements
b) It will print 99
c) It will produce a runtime error
d) None of the above
*/


// Read the following program carefully
#include <stdio.h>

void main(void);
int printf(const char*,...);

void main(void)
{
   int i = 100,j=10,k=20;
   int sum;
   float ave;
   char myformat[]="ave = %.2f";
   sum=i+j+k;
   ave=sum/3;

   printf(myformat,ave);
}

/*
What will the above program do ?
a) It will print 43.33
b) It will print 43.34
c) It will produce a compilation error
d) None of the above
*/

//   Consider the following program code
#include <stdio.h>
void main(void)
{
    int a[10];
    printf("%d",*(a/(a+9)));
}

/* What will the program do ?
a)  It will print some junk as array a is not initialised.
b)  It will result in a compilation error.
c)  It will produce a runtime error as array a is not initialised.
d)  None of the above. */

//   Carefully go through the following code
#include <stdio.h>
void main(void);

void main(void)
{
    struct s{
        int x;
        float y;
        }s1 = {25,45.00};

    union u{
        int x;
        float y;
        }u1;

        u1=(union u)s1;

        printf("%d and %f",u1.x,u1.y);
}

/* What will this program point ?

a)  25 and 45.00
b)  Produce a compilation error
c)  45 and 45.00
d)  Produce a runtime error  */


//  Consider the following C program.
#include <stdio.h>
void main(void)
{
    unsigned int c;
    unsigned x=0x0003;
    scanf("%u",&c);

    switch(c&x)
    {
        case 3 : printf("Hello! \t");
        case 2 : printf("Welcome \t");
        case 1 : printf("To All \t");
        default: printf("\n");
}
}

/* If the value input for the variable c is 10, what will be the output
of the program ?

a)  The program will generate a compile time error as there is no break
    statement for the various case choices.
b)  The program will print Hello!
c)  The program will print Welcome    To All
d)  None of the above  */

// 16)
#include <stdio.h>
// print the sum of series 1/5 + 1/4 + ...
static int i=5;

void main(void)
{
    int sum = 0;
    do
    {
        sum+=(1/i);
    }while (0 < i--);
}

/* What will this program output ?
a)  It will point the sum of the series 1/5 + 1/4 + ... + 1/1
b)  It will produce a compilation error
c)  It will produce a runtime error
d)  None of the above
*/

//

#include <stdio.h>

void main(void);

void main(void)
{
    int i = 100,j=20;

    i++=j;
    i*=j;

    printf("%d\t%d\n",i,j);
}

/*  What will this program output ?
a)  It will print 400    20
b)  It will produce a compilation error
c)  It will print 401    21
d)  It will print some garbage
*/

// 18) What will the following program do ?
#include <stdio.h>
int fn(void);
void print(int,int(*)());

int i = 10;

void main(void)
{
    int i = 20;
    print(i,fn);
}

void print(int i,int (*fn1)())
{
    printf("%d\n",(*fn1)());
}

int fn(void)
{
    return(i-=5);
}

/*
a) There will be a compilation error.
b) It will print 5
c) There will be a linkage error as there is no fn1() function definition
d) both (a) and (c).
*/

//    1)    Consider the following program
#include <stdio.h>
void main()
{
    int y,z;
    int x=y=z=10;
    int f=x;
    float ans=0.0;

    f=x*y;
    ans=x/3.0 + y/3;
    printf("%d,%.2f",f,ans);
}

/*    What will be the output of this program ?
    a)    It will print 1000,6.66;
    b)    It will give a type mismatch error;
    c)    It will generate a compile-time error;
    d)    None of the above;
*/

//    2)    Study the following code carefully
#include <stdio.h>
void main(void);
double dbl=20.4530,d=4.5710,dblvar3;

void main()
{
    double dbfn(void);
    dblvar3=dbfn();
    printf("%.2f\t%.2f\t%.2f\n",dbl,d,dblvar3);
}

double dbfn(void)
{
    double d,dblvar3;
    dbl=d=dblvar3=4.5;
    return(dbl+d+dblvar3);
}

/*    The output of the above program will be
    a)    4.50    4.57    29.52;
    b)    4.50    4.57    13.50;
    c)    4.57    4.57    29.52;
    d)    4.57    4.50    13.50; */

//    3)    Consdier the following program
#include <stdio.h>
int SumElement(int *,int);

void main(void)
{
    int x[10];
    int i=10;
    for(;i;)
    {
        i--;
        *(x+i)=i;
    }
    printf("%d",SumElement(x,10));
}

int SumElement(int array[],int size)
{
    int i=0;
    float sum=0;
    for(;i<size;i++)
        sum+=array[i];
    return sum;
}

/*    What will be the output of this program ?
    a) It will print 45;
    b) It will produce a type mismatch error as SumElement's return
        statement returns a float;
    c) It will produce a compilation error in statement for(;,i,;);
    d) Both (b) and (c); */


  // 4) Carefully study the following
#include <stdio.h>

void main(void)
{
    int oldvar=25, newvar=-25;
    int swap(int,int);
    swap(oldvar,newvar);
    printf("Numbers are %d \t %d",newvar,oldvar);
}

int swap(int oldval,int newval)
{
    int tempval= oldval;
        oldval = newval;
        newval = tempval;
}

/*     The output of this program will be
    a)  Numbers are 25  -25
    b)    Numbers are 25   25
    c)  Numbers are -25  25
    d)    Numbers are -25 -25  */


// 5) Consider the following program
#include <stdio.h>

void main(void);
int newval(int);

void main(void)
{
    int ia[]={12,24,45,0};
    int i,sum=0;

    for(i=0;ia[i];i++)
    {
        sum+=newval(ia[i]);
    }
    printf("Sum = %d",sum);
}

int newval(int x)
{
    static int div = 1;
    return (x/div++);
}

/*     The output of this program will be
    a)  Sum = 61;
    b)    Sum = 39;
    c)  Runtime error
    d)    Compilation error */


// 6)  Study the following program
#include <stdio.h>
void main(void);

void main(void)
{
    int var1=5,var2=5,var3=6,minmax;
    minmax = (var1 > var2) ? (var1 > var3) ? var1:var3:(var2 > var3) ? var2:var3;
    printf("%d\n",minmax);
}

/*  This program will
    a)  Produce a runtime error
    b)  Produce a compilation error
    c)  Print 5
    d)  Print 6 */


// 7) Consider the following program
#include <stdio.h>
void main(void);

void main(void)
{
    void pa(int *a,int n);
    int arr[5]={5,4,3,2,1};
    pa(arr,5);
}

void pa(int *a,int n)
{
    int i;
    for(i=0;i<n;i++)
        printf("%d\n",*(a++)+i);
}

/*  Which of the following is correct ?
    a)  The Program prints the alternate elements of array.
    b)  It will not compile as 'array' cannot be incremented.
    c)  It will print 6,5,4,3,2 on individual lines.
    d)  It will print 5 five times 6 */


// 8)  Consider the program in two files.
// The content of the file1.c is
#include <stdio.h>
void main(void);
void print(void);

void main(void)
{
    print();
}

void f1(void)
{
    printf("\n f1();");
}

// and the content of the file2.c is
#include <stdio.h>

void print(void)
{
    extern void f1(void);
    f1();
}
static void f1(void)
{
    printf("\n static f1();");
}

/*  Which will be the output of the program ?
    a)  It will print f1().
    b)  It will print the value returned by f1().
    c)  It will produce a compile-time error as f1() is defined twice.
    d)  None of the above */

// 9) Read the following code
#include <stdio.h>
void main(void);

static int i=50;
int print(int i);

void main(void)
{
    static int i=100;
    while (print(i))
    {
        printf("%d\n",i);
        i--;
    }
}
int print(int x)
{
    static int i = 2;
    return(i--);
}

/* What will be the output of this program ?
a)  It will print from 100 to 0 in steps of -1
b)  It will print 100 and 99 on two steps
c)  It will print 100,99 and 98 and stop
d)  None of the above
*/

-----------------


//  10)  Carefully study the given program

#include <stdio.h>
#include <alloc.h>

void main(void);

typedef struct NType
{
    int i;
    char c;
    long x;
}NewType;

void main(void)
{
    NewType *c;
    c=(NewType*)malloc(sizeof(NewType));
    c->i=100;
    c->c='C';
    (*c).x=100l;

    printf("%d,%c,%4ld",c->i,c->c,c->x);
}

/* What is the output of this program ?
a)  It will produce a variable redefinition error
b)  It will print some junk
c)  It will print 100,C,100
d)  It will print 100,C,l100
*/

//  11) Carefully go through the following program
#include <stdio.h>

void main(void);

const int k=100;

void main(void)
{
   int a[100];
   int sum=0;

   for(k=0;k<100;k++)
    *(a+k)=k;
    sum += a[--k];
   printf("%d",sum);
}

/*
What will be the out put of this program ?
a) It will print the sum of all the elements
b) It will print 99
c) It will produce a runtime error
d) None of the above
*/


// 12) Read the following program carefully
#include <stdio.h>

void main(void);
int printf(const char*,...);

void main(void)
{
   int i = 100,j=10,k=20;
   int sum;
   float ave;
   char myformat[]="ave = %.2f";
   sum=i+j+k;
   ave=sum/3;

   printf(myformat,ave);
}

/*
What will the above program do ?
a) It will print 43.33
b) It will print 43.34
c) It will produce a compilation error
d) None of the above
*/

// 13)  Consider the following program code
#include <stdio.h>
void main(void)
{
    int a[10];
    printf("%d",*(a/(a+9)));
}

/* What will the program do ?
a)  It will print some junk as array a is not initialised.
b)  It will result in a compilation error.
c)  It will produce a runtime error as array a is not initialised.
d)  None of the above. */

// 14)  Carefully go through the following code
#include <stdio.h>
void main(void);

void main(void)
{
    struct s{
        int x;
        float y;
        }s1 = {25,45.00};

    union u{
        int x;
        float y;
        }u1;

        u1=(union u)s1;

        printf("%d and %f",u1.x,u1.y);
}

/* What will this program point ?

a)  25 and 45.00
b)  Produce a compilation error
c)  45 and 45.00
d)  Produce a runtime error  */


// 15) Consider the following C program.
#include <stdio.h>
void main(void)
{
    unsigned int c;
    unsigned x=0x0003;
    scanf("%u",&c);

    switch(c&x)
    {
        case 3 : printf("Hello! \t");
        case 2 : printf("Welcome \t");
        case 1 : printf("To All \t");
        default: printf("\n");
}
}

/* If the value input for the variable c is 10, what will be the output
of the program ?

a)  The program will generate a compile time error as there is no break
    statement for the various case choices.
b)  The program will print Hello!
c)  The program will print Welcome    To All
d)  None of the above  */

// 16)
#include <stdio.h>
// print the sum of series 1/5 + 1/4 + ...
static int i=5;

void main(void)
{
    int sum = 0;
    do
    {
        sum+=(1/i);
    }while (0 < i--);
}

/* What will this program output ?
a)  It will point the sum of the series 1/5 + 1/4 + ... + 1/1
b)  It will produce a compilation error
c)  It will produce a runtime error
d)  None of the above
*/

// 17)
#include <stdio.h>

void main(void);

void main(void)
{
    int i = 100,j=20;

    i++=j;
    i*=j;

    printf("%d\t%d\n",i,j);
}

/*  What will this program output ?
a)  It will print 400    20
b)  It will produce a compilation error
c)  It will print 401    21
d)  It will print some garbage
*/

// 18) What will the following program do ?
#include <stdio.h>
int fn(void);
void print(int,int(*)());

int i = 10;

void main(void)
{
    int i = 20;
    print(i,fn);
}

void print(int i,int (*fn1)())
{
    printf("%d\n",(*fn1)());
}

int fn(void)
{
    return(i-=5);
}

/*
a) There will be a compilation error.
b) It will print 5
c) There will be a linkage error as there is no fn1() function definition
d) both (a) and (c).
*/


//    1)    Consider the following program
#include <stdio.h>
void main()
{
    int y,z;
    int x=y=z=10;
    int f=x;
    float ans=0.0;

    f=x*y;
    ans=x/3.0 + y/3;
    printf("%d,%.2f",f,ans);
}

/*    What will be the output of this program ?
    a)    It will print 1000,6.66;
    b)    It will give a type mismatch error;
    c)    It will generate a compile-time error;
    d)    None of the above;
*/

//    2)    Study the following code carefully
#include <stdio.h>
void main(void);
double dbl=20.4530,d=4.5710,dblvar3;

void main()
{
    double dbfn(void);
    dblvar3=dbfn();
    printf("%.2f\t%.2f\t%.2f\n",dbl,d,dblvar3);
}

double dbfn(void)
{
    double d,dblvar3;
    dbl=d=dblvar3=4.5;
    return(dbl+d+dblvar3);
}

/*    The output of the above program will be
    a)    4.50    4.57    29.52;
    b)    4.50    4.57    13.50;
    c)    4.57    4.57    29.52;
    d)    4.57    4.50    13.50; */

//    3)    Consdier the following program
#include <stdio.h>
int SumElement(int *,int);

void main(void)
{
    int x[10];
    int i=10;
    for(;i;)
    {
        i--;
        *(x+i)=i;
    }
    printf("%d",SumElement(x,10));
}

int SumElement(int array[],int size)
{
    int i=0;
    float sum=0;
    for(;i<size;i++)
        sum+=array[i];
    return sum;
}

/*    What will be the output of this program ?
    a) It will print 45;
    b) It will produce a type mismatch error as SumElement's return
        statement returns a float;
    c) It will produce a compilation error in statement for(;,i,;);
    d) Both (b) and (c); */


  // 4) Carefully study the following
#include <stdio.h>

void main(void)
{
    int oldvar=25, newvar=-25;
    int swap(int,int);
    swap(oldvar,newvar);
    printf("Numbers are %d \t %d",newvar,oldvar);
}

int swap(int oldval,int newval)
{
    int tempval= oldval;
        oldval = newval;
        newval = tempval;
}

/*     The output of this program will be
    a)  Numbers are 25  -25
    b)    Numbers are 25   25
    c)  Numbers are -25  25
    d)    Numbers are -25 -25  */


// 5) Consider the following program
#include <stdio.h>

void main(void);
int newval(int);

void main(void)
{
    int ia[]={12,24,45,0};
    int i,sum=0;

    for(i=0;ia[i];i++)
    {
        sum+=newval(ia[i]);
    }
    printf("Sum = %d",sum);
}

int newval(int x)
{
    static int div = 1;
    return (x/div++);
}

/*     The output of this program will be
    a)  Sum = 61;
    b)    Sum = 39;
    c)  Runtime error
    d)    Compilation error */


// 6)  Study the following program
#include <stdio.h>
void main(void);

void main(void)
{
    int var1=5,var2=5,var3=6,minmax;
    minmax = (var1 > var2) ? (var1 > var3) ? var1:var3:(var2 > var3) ? var2:var3;
    printf("%d\n",minmax);
}

/*  This program will
    a)  Produce a runtime error
    b)  Produce a compilation error
    c)  Print 5
    d)  Print 6 */


// 7) Consider the following program
#include <stdio.h>
void main(void);

void main(void)
{
    void pa(int *a,int n);
    int arr[5]={5,4,3,2,1};
    pa(arr,5);
}

void pa(int *a,int n)
{
    int i;
    for(i=0;i<n;i++)
        printf("%d\n",*(a++)+i);
}

/*  Which of the following is correct ?
    a)  The Program prints the alternate elements of array.
    b)  It will not compile as 'array' cannot be incremented.
    c)  It will print 6,5,4,3,2 on individual lines.
    d)  It will print 5 five times 6 */


// 8)  Consider the program in two files.
// The content of the file1.c is
#include <stdio.h>
void main(void);
void print(void);

void main(void)
{
    print();
}

void f1(void)
{
    printf("\n f1();");
}

// and the content of the file2.c is
#include <stdio.h>

void print(void)
{
    extern void f1(void);
    f1();
}
static void f1(void)
{
    printf("\n static f1();");
}

/*  Which will be the output of the program ?
    a)  It will print f1().
    b)  It will print the value returned by f1().
    c)  It will produce a compile-time error as f1() is defined twice.
    d)  None of the above */

// 9) Read the following code
#include <stdio.h>
void main(void);

static int i=50;
int print(int i);

void main(void)
{
    static int i=100;
    while (print(i))
    {
        printf("%d\n",i);
        i--;
    }
}
int print(int x)
{
    static int i = 2;
    return(i--);
}

/* What will be the output of this program ?
a)  It will print from 100 to 0 in steps of -1
b)  It will print 100 and 99 on two steps
c)  It will print 100,99 and 98 and stop
d)  None of the above
*/

C programs–For practice (to be solved)

#include <stdio.h>
#define A     2
#define B     3
#define C     A - B
#define D    4 - C
main()
{
    int x = D;
   
    printf("x = %d\n", x);
}
-------------------
#include <stdio.h>
main()
{
    int i =3;
    printf("%d\n", i++);
    printf("%d\n", i++);
}
---------------
#include <stdio.h>
#define A      RAM
#define RAM     ram
main()
{
    char *ram = "hello ram!";
    printf("%s\n", A);
}
-------------
#include <stdio.h>
main()
{
    int i = 5, sum = 0;
   
    do {
        sum += 1/i;
       } while(0 < i--);
    printf("%d\n", sum);
}
-------------
#include <stdio.h>
#define max(a,b)    ((a) > (b) ? a : b)
main()
{
    int i=5, j=5;
    printf(" max = %d\n", max(++i, j++));
    printf("i = %d, j = %d\n", i, j);
}
-------------
#include <stdio.h>
char a[5][6] = { "one", "two", "three", "four", "five" };
main()
{
    printf("%s %d\n", a[1], &a[0][0]);
}
-------------
#include <stdio.h>
main()
{
    int j = 5;
    if(j++ != 6 && j++ != 7 && ++j != 8)
        printf("No : %d\n",j);
    printf("Yes : %d\n", j);
}
------------
#include <stdio.h>
main()
{
    int j = 5;
    if(j++ != 6 && j++ == 7 && ++j != 8)
        printf("No : %d\n",j);
    printf("Yes : %d\n", j);
}
--------------
#include <stdio.h>
fn(a)
int *a;
{
    *a = 20;
}
main()
{
    int *a = (int *) malloc(4);
    *a = 10;   
    fn(a);
    printf("a = %d\n", *a);
}
------------
#include <stdio.h>
struct t {
    int a;
    int b;
    } te[3] = { 1, 2, 3, 4, 5, 5 };
main()
{
    struct t  *b;
    b = te;
    printf("val : %d\n", ++b++->a);
}
   
-------------
#include <stdio.h>
main()
{
    char a[5];
    int  b[5];
    printf("%s\n", a);
    printf("%d\n", b);
}
____------------
#include <stdio.h>
main()
{
    int  i=0;
    int x,y,z=0;
    for(; i < 5; i++) {
        int x;
        x = i * y +  z;
        y = z = i;
    }
    printf("%d %d \n", i,x);
}
----------------
#include <stdio.h>
int a[20];
main()
{
    int i ;
    a = (int *) malloc(20 * sizeof(int));
    for(i=0; i<20; i++) {
        *a = i;
        a++;
    }
    if(printf("%d\n", a) ) printf("Yes\n");
}
---------------
#include <stdio.h>
main()
{
    int a = 5;
    int i, count = 0;
    for(i=0; i<32; i++)
        if( a & (1 << i) ) count ++;
    printf("count = %d\n", count);
}
----------------
#include <stdio.h>
char *fn()
{
    char buf[100];
    scanf("%s", buf);
    return buf;
}
main()
{
    char *a;
    a = fn();
    printf("%s\n", a);
}
   
---------------
#include <stdio.h>
main()
{
    int a = 1, i=0;
    for(; i<10; i++)
        printf("2 ** %d = %d\n", i, a << i);
}
---------------
#include <stdio.h>
typedef int *(*abc)();
int *f1();
void f2();
main()
{
    abc  fn1, fn2;
    int *ret;
    printf("B4 fncall ...\n");
     fn1 = f1;   
    ret = f1();
    printf("Over ret = %d .,......\n", *ret);
}
int *f1()
{
    int *a;
   
    a = (int *) malloc(20);
    *a = 100;
    printf("inside f1\n");
    return(a);
}
void f2()
{
    printf("inside f2\n");
}

------------
#include <stdio.h>
/* void     (*fn) (); */
void *f1()
{
    printf("f1\n");
}
main()
{
    void * (*fn)();
    fn = f1;
    fn();
}

---------------
#include <stdio.h>
typedef struct {
    void (*fn1)();
    void (*fn2)();
}fnptr;
void f1()
{
    printf("Inside fn1\n");
}
void f2()
{
    printf("Inside fn2\n");
}
main()
{
    fnptr a;
    a.fn1 = f1;
    a.fn2 = f2;
    (*a.fn1)();
    (*a.fn2)();
    a.fn2 = f1;
    (*a.fn2)();
}
-------------
#include <stdio.h>
char *fn();
main()
{
    char  *a = "Good", *b = "ddd";
   
    a = fn();
    b = fn();
    printf("%s\n", a);
}
char *fn()
{
    static char  b[10];
    strcpy(b, "super");
    return (b);
}
----------------
#include <stdio.h>
main()
{
    int temp = 1;
   
    temp = (temp <<= 1 % 2);
    printf("temp =  %d\n", temp);
}
----------------
#include <stdio.h>
int test();
struct point {
    int x;
    int y;
      } u[] = { 1,2,  10,4,  45,6,  27,8};

main()
{
    struct point  *a;
    test();
/*
    a = u;
    printf("%d \n", ((a++)->x)++);
    printf("a-> = %d\n", a->x);
    printf("u. = %d\n", u->x);
*/
}
int test()
{
    struct point *a, *b, *c, *d;
    a = u; b=u; c=u; d=u;
    printf(" ++a->x %d\n", ++a->x);
    printf("b++->x  %d\n", b++->x);
    printf("c->x++  %d\n", c->x++);
    printf("(++d)->x %d\n", (++d)->x);
}
----------------
#include <stdio.h>
typedef union {
    int no;
    char nm;
      } u;
main()
{
    u a;
     char b[5];   
   
    a.no = 10;
    a.nm ='4';
    printf("%d %d\n", a.nm, b);
    a.nm = '2';
    a.no = 20;
    printf("%d\n", a.nm);   
}
------------------
#include <stdio.h>
change(str)
char *str;
{
    char *b = "lakshman";
    str = b;
}
main()
{
    char *a = "ram";
    change(a);
    printf("%s\n", a);
}
----------------
#include <stdio.h>
main()
{
    int a,b;
    a = 100; b = 50;
    printf("%d\n", a-- - --b);
    a = 100; b = 50;
    a = a-- - --b;
    printf("%d\n", a);
}

---------------
#include <stdio.h>
fn();
main()
{
    int i = 10;
    fn(i, i++);
}
fn(a,b)
int a,b;
{
    printf("val : %d %d\n", a,b);
}
_-----------------
#include <stdio.h>
struct x {
    int a;
    char b;
}  ex[4] = { 1, '1', 2, '2', 3, '3', 4, '4' };
main()
{
    struct x *y;
    y = ex;
    ++y++->a;
    printf("ex : %d %c    y: %d %c \n", ex->a, ex->b, y->a, y->b);
}
   
-------------------
#include <stdio.h>
enum a{
    b = 0,
    c, d, e, f, g
};
main()
{
    enum a aa;
    aa = e;
    if(aa) printf("yes : %d\n", aa); else printf("no");
}
----------------
#include <stdio.h>
main()
{
    int a = 10, b = 20;
    printf("a = %d %d %d\n",a,a = a++ + ++b, a);
}
-----------------------------------------------------------------------------
#include <stdio.h>
#define A     2
#define B     3
#define C     A - B
#define D    4 - C
main()
{
    int x = D;
   
    printf("x = %d\n", x);
}
-------------------
#include <stdio.h>
main()
{
    int i =3;
    printf("%d\n", i++);
    printf("%d\n", i++);
}
---------------
#include <stdio.h>
#define A      RAM
#define RAM     ram
main()
{
    char *ram = "hello ram!";
    printf("%s\n", A);
}
-------------
#include <stdio.h>
main()
{
    int i = 5, sum = 0;
   
    do {
        sum += 1/i;
       } while(0 < i--);
    printf("%d\n", sum);
}
-------------
#include <stdio.h>
#define max(a,b)    ((a) > (b) ? a : b)
main()
{
    int i=5, j=5;
    printf(" max = %d\n", max(++i, j++));
    printf("i = %d, j = %d\n", i, j);
}
-------------
#include <stdio.h>
char a[5][6] = { "one", "two", "three", "four", "five" };
main()
{
    printf("%s %d\n", a[1], &a[0][0]);
}
-------------
#include <stdio.h>
main()
{
    int j = 5;
    if(j++ != 6 && j++ != 7 && ++j != 8)
        printf("No : %d\n",j);
    printf("Yes : %d\n", j);
}
------------
#include <stdio.h>
main()
{
    int j = 5;
    if(j++ != 6 && j++ == 7 && ++j != 8)
        printf("No : %d\n",j);
    printf("Yes : %d\n", j);
}
--------------
#include <stdio.h>
fn(a)
int *a;
{
    *a = 20;
}
main()
{
    int *a = (int *) malloc(4);
    *a = 10;   
    fn(a);
    printf("a = %d\n", *a);
}
------------
#include <stdio.h>
struct t {
    int a;
    int b;
    } te[3] = { 1, 2, 3, 4, 5, 5 };
main()
{
    struct t  *b;
    b = te;
    printf("val : %d\n", ++b++->a);
}
   
-------------
#include <stdio.h>
main()
{
    char a[5];
    int  b[5];
    printf("%s\n", a);
    printf("%d\n", b);
}
____------------
#include <stdio.h>
main()
{
    int  i=0;
    int x,y,z=0;
    for(; i < 5; i++) {
        int x;
        x = i * y +  z;
        y = z = i;
    }
    printf("%d %d \n", i,x);
}
----------------
#include <stdio.h>
int a[20];
main()
{
    int i ;
    a = (int *) malloc(20 * sizeof(int));
    for(i=0; i<20; i++) {
        *a = i;
        a++;
    }
    if(printf("%d\n", a) ) printf("Yes\n");
}
---------------
#include <stdio.h>
main()
{
    int a = 5;
    int i, count = 0;
    for(i=0; i<32; i++)
        if( a & (1 << i) ) count ++;
    printf("count = %d\n", count);
}
----------------
#include <stdio.h>
char *fn()
{
    char buf[100];
    scanf("%s", buf);
    return buf;
}
main()
{
    char *a;
    a = fn();
    printf("%s\n", a);
}
   
---------------
#include <stdio.h>
main()
{
    int a = 1, i=0;
    for(; i<10; i++)
        printf("2 ** %d = %d\n", i, a << i);
}
---------------
#include <stdio.h>
typedef int *(*abc)();
int *f1();
void f2();
main()
{
    abc  fn1, fn2;
    int *ret;
    printf("B4 fncall ...\n");
     fn1 = f1;   
    ret = f1();
    printf("Over ret = %d .,......\n", *ret);
}
int *f1()
{
    int *a;
   
    a = (int *) malloc(20);
    *a = 100;
    printf("inside f1\n");
    return(a);
}
void f2()
{
    printf("inside f2\n");
}

------------
#include <stdio.h>
/* void     (*fn) (); */
void *f1()
{
    printf("f1\n");
}
main()
{
    void * (*fn)();
    fn = f1;
    fn();
}

---------------
#include <stdio.h>
typedef struct {
    void (*fn1)();
    void (*fn2)();
}fnptr;
void f1()
{
    printf("Inside fn1\n");
}
void f2()
{
    printf("Inside fn2\n");
}
main()
{
    fnptr a;
    a.fn1 = f1;
    a.fn2 = f2;
    (*a.fn1)();
    (*a.fn2)();
    a.fn2 = f1;
    (*a.fn2)();
}
-------------
#include <stdio.h>
char *fn();
main()
{
    char  *a = "Good", *b = "ddd";
   
    a = fn();
    b = fn();
    printf("%s\n", a);
}
char *fn()
{
    static char  b[10];
    strcpy(b, "super");
    return (b);
}
----------------
#include <stdio.h>
main()
{
    int temp = 1;
   
    temp = (temp <<= 1 % 2);
    printf("temp =  %d\n", temp);
}
----------------
#include <stdio.h>
int test();
struct point {
    int x;
    int y;
      } u[] = { 1,2,  10,4,  45,6,  27,8};

main()
{
    struct point  *a;
    test();
/*
    a = u;
    printf("%d \n", ((a++)->x)++);
    printf("a-> = %d\n", a->x);
    printf("u. = %d\n", u->x);
*/
}
int test()
{
    struct point *a, *b, *c, *d;
    a = u; b=u; c=u; d=u;
    printf(" ++a->x %d\n", ++a->x);
    printf("b++->x  %d\n", b++->x);
    printf("c->x++  %d\n", c->x++);
    printf("(++d)->x %d\n", (++d)->x);
}
----------------
#include <stdio.h>
typedef union {
    int no;
    char nm;
      } u;
main()
{
    u a;
     char b[5];   
   
    a.no = 10;
    a.nm ='4';
    printf("%d %d\n", a.nm, b);
    a.nm = '2';
    a.no = 20;
    printf("%d\n", a.nm);   
}
------------------
#include <stdio.h>
change(str)
char *str;
{
    char *b = "lakshman";
    str = b;
}
main()
{
    char *a = "ram";
    change(a);
    printf("%s\n", a);
}
----------------
#include <stdio.h>
main()
{
    int a,b;
    a = 100; b = 50;
    printf("%d\n", a-- - --b);
    a = 100; b = 50;
    a = a-- - --b;
    printf("%d\n", a);
}

---------------
#include <stdio.h>
fn();
main()
{
    int i = 10;
    fn(i, i++);
}
fn(a,b)
int a,b;
{
    printf("val : %d %d\n", a,b);
}
_-----------------
#include <stdio.h>
struct x {
    int a;
    char b;
}  ex[4] = { 1, '1', 2, '2', 3, '3', 4, '4' };
main()
{
    struct x *y;
    y = ex;
    ++y++->a;
    printf("ex : %d %c    y: %d %c \n", ex->a, ex->b, y->a, y->b);
}
   
-------------------
#include <stdio.h>
enum a{
    b = 0,
    c, d, e, f, g
};
main()
{
    enum a aa;
    aa = e;
    if(aa) printf("yes : %d\n", aa); else printf("no");
}
----------------
#include <stdio.h>
main()
{
    int a = 10, b = 20;
    printf("a = %d %d %d\n",a,a = a++ + ++b, a);
}
---------------

Linux Basics for NS2

At many places, I happen to see researchers and students wanted to use Windows for NS2 rather than Linux. The reason may be,

  • Linux is tough

  • It occupies more space and more memory in my Computer

  • Linux uses lot of commands and i am not aware of it

  • Tough to handle dual boot OS (windows and Linux)

  • always wanted to be in the cakewalk. (never try a new thing)

  • etc


But here are some basic tips for using NS2 under a Linux Machine.

  • It is enough to know some basic Linux commands to work with NS2 (hardly not more than 20 commands)

  • One should have a basic knowledge of how tree structure in Linux/Unix look like

  • Finally, where to set the PATH information in Linux


With all the above three, one can confidentally work on Linux. As for as windows is concerned, upto NS2.27 version is tested under windows and recent versions there is no hope (however it will work fine, but "Use at your Own Risk")
Please see this post to install Fedora 12 in your Computer
Now we will see some basic Linux commands that are needed for NS2.

  1. ls => listing the file contents

  2. ls -l => listing the file in long format, it tells you about the ownership and permissions

  3. chmod => Changing the mode of the file (syntax is chmod 755 <filename>

  4. echo => it echo the value to the screen


    1. Example: echo name

    2. echo $SHELL (it will print the name of the shell)

    3. echo $USER (it will print the name of the user)

    4. echo $PATH (it display the values contained in the PATH variable)

    5. echo $HOME (it displays the home folder of the user) (Now you can understood that the variable names are denoted using a $ symbol)


  5. pwd => it is just present working directory

  6. make => (it executes the instructions given in the Makefile which is available under that directory)

  7. gcc filename.cxx => A command to run the gcc compiler (This is not necessarily needed for NS2, but as a developer, you should know)

  8. ./install  => (See a dot in the beginning, it is just executing the install file which is available under a folder, any executable can be executed with the help of this ./)

  9. passwd => changing the password of the current user

  10. cd => change the directory

  11. mkdir => make a directory

  12. gedit => it is similar like a notepad editor in windows(syntax is gedit filename)

  13. vi => it is vi editor


Path setting
The path can be set in a file which is available for each user of the Linux OS.  For example, a user called  "pradeep" will have the home folder as /home/pradeep and another user "kumar" has home folder as /home/kumar
Each user "pradeep" and "kumar" will have a file called .bash_profile (in case of redhat and fedora) and .bashrc (Ubuntu, etc) in their home folder. This file is a hidden file which can be opened using a vi or gedit editor
The command being
vi /home/pradeep/.bash_profile
vi /home/pradeep/.bashrc

or
gedit /home/pradeep/.bash_profile
gedit /home/pradeep/.bashrc
Similarly the same for user "kumar" (if you set the path for "pradeep" and running under "kumar", it will not work, for that to work, there is a separate customisation is available), for beginners, these steps are enough.