AD1

Wednesday, 25 January 2017

What will be the output of the following program


#include <stdio.h>
int add(int a,int b){return(a+b);}
int sub(int x,int y){return(x-y);}
int main()
{
add(10,30);
printf("%d",sub-add+main);
return 0;
}
view raw ftp.c hosted with ❤ by GitHub
output

in this program we perform math operations on the base addresses of the functions , so the output is compiler dependant 
assume the base address of sub() is 4199876 and add() 4199856 and main() is 4199894
so the output will be 4199914

Tuesday, 24 January 2017

What will be the output of the following code 


#include <stdio.h>
struct test
{
unsigned var1:5;
unsigned var2:4;
unsigned var3:2;
};
int main()
{
typedef struct test TEST;
TEST new_test={32,10,5};
printf("var1=%d\t var2=%d\t var3=%d\t ",new_test.var1,new_test.var2,new_test.var3);
return 0;
}
view raw boundry.c hosted with ❤ by GitHub

Output
var1=0    var2=10    var3=1

Explanation 

Pointers and bitfields

what will be the output of the following code

#include <stdio.h>
struct test
{
unsigned int x;
long int y: 33;
unsigned int z;
};
int main()
{
struct test t;
unsigned int *ptr1 = &t.y;
unsigned int *ptr2 = &t.z;
printf("%d", ptr2 - ptr1);
return 0;
}
view raw bitfields.c hosted with ❤ by GitHub
the code creates compilation error , as you can not create a pointer to bit fields , simply because bit fields may not start at byte boundry

Thursday, 19 January 2017

Swap structure values

Assume that you have a structure defined as follow 
struct two_integers {int n1;int n2;};
and this structure is initialized as follow
struct two_integers x={100,-100}; 
Write a C function that returns the same structure x but n1 and n2 are swapped

#include <stdio.h>
struct two_integers {int n1;int n2;};
void swap(struct two_integers *xp)// pass structure variable by reference
{
int t=xp->n1;
xp->n1=xp->n2;
xp->n2=t;
return;
}
int main()
{
struct two_integers x={100,-100};// n1=100 and n2=-100;
struct two_integers *xp=&x;
swap(xp);
printf("n1=%d\tn2=%d\n",x.n1,x.n2);
return 0;
}


Structures

In the following code , assume that the base address of the structure is 2424384 (in decimal form) , what will be the output of the program 

#include <stdio.h>
int main()
{
struct employee {int age;float salary;};
struct employee e1={40,3000};
struct employee *pointer_to_e=&e1;
printf("%d\n%d\n%d\n%d",pointer_to_e,&(e1.age),&(e1.salary),(pointer_to_e)+1);
return 0;
}
view raw struct.c hosted with ❤ by GitHub
Solution
2424384
2424384
2424388
2424392

Tuesday, 17 January 2017

Sorting 2D array

Assume you have 2D array of integers , int a[3][4]={8,7,6,5,
                                                                                                            -2,800,19,-40,
                                                                                                             1,-23,600,-500};

Write a C program to sort it in ascending order (from low to high)

Solution
#include <stdio.h>
int main()
{
int a[3][4]={8,7,6,5,
-2,800,19,-40,
1,-23,600,-500
};
int t=0;
int *p=&a[0];
for(int i=0;i<=12;i++)
{
for(int j=0;j<=11;j++)
{
if(*(p+j+1)<*(p+j))
{
t=*(p+j);
*(p+j)=*(p+j+1);
*(p+j+1)=t;
}
}
}
printf("after sorting in ascending order (from low to high) \n");
for(int i=0;i<=2;i++)
{
for(int j=0;j<=3;j++)
{
printf("%d\t",a[i][j]);
}
}
return 0;
}
view raw sort2d.c hosted with ❤ by GitHub

Monday, 16 January 2017

Increment all array values using pointers

Write C function INC(int array[5],int n) that accepts two parameters array of five integers and integer, and adds the integer n to all array elements , then it returns the new array , as example if array[5]={1,5,7,0,2} and n =5 after function call we have  array[]={6,10,12,5,7}
#include <stdio.h>
void INC(int *pointer,int n)
{
for(int x=0;x<=4;x++)
{
*(pointer+x)+=n;
}
return;
}
int main()
{
int N;printf("enter the number you want to add to all array elements\n");
scanf("%d",&N);
int array[5]={1,5,7,0,2};
INC(array,N);
printf("array after function call\n");
for(int i=0;i<=4;i++)
{
printf("%d\t",array[i]);
}
return 0;
}
view raw inc.c hosted with ❤ by GitHub






Saturday, 14 January 2017

Pointers and 2D arrays

Assume you have 2D array S[2][4]={{1234,56},
{1212,33},
{1434,80},
{1312,78}};
write a C program to print all the values and their corresponding addresses of this array
using pointers

#include "stdio.h"
int main()
{
int s[4][2] ={
{1234,56},
{1212,33},
{1434,80},
{1312,78}
};
int *p=&s[0];// pointer to array
for(int i=0;i<=3;i++)
{
printf("Start Address of %d th 1-D array = %d\n",i,&s[i]);
for(int k=0;k<=1;k++)
{
printf("address of elemnt %d in %d th 1-D array %d \t and its value = %d\n",k,i,p,*p);
p++;
}
}
return 0;
}
view raw 2d.c hosted with ❤ by GitHub

Pointer Arithmetic

What will be the output of the following program 


#include "stdio.h"
int main()
{
//Pointer Arithmetic
int i=4,*j;
j=&i ;printf("%d\n",j);
j=j+1 ;printf("%d\n",j);
j=j+9 ;printf("%d\n",j);
j=j-3 ;printf("%d\n",j);
return 0;
}

2424388
2424392
2424428
2424416

size of double data type

Write a program to calculate the size of a double data type in terms of Bytes , Do not use the sizeof() operator

Solution


#include "stdio.h"
int main()
{
double array[2]={0,0};
double *p1=array;
double *p2=p1+1;
printf("Size of double data type is %d Bytes\n",(int)p2-(int)p1);
return 0;
}
view raw sizeof.c hosted with ❤ by GitHub

Wednesday, 11 January 2017

what will be the output of the following program


#include "stdio.h"
int main()
{
int numbers[5];// integer array
for(int i=0;i<=4;i++)
{
printf("%d\n",numbers[i]);
}
return 0;
}
view raw array.c hosted with ❤ by GitHub
output will be garbage values
uninitialized array elements have garbage values

Tuesday, 10 January 2017

What will be the output of the following program


#include "stdio.h"
#define PRODUCT(x) (x*x)
int main( )
{
int i=3;
int j=0;
int k=0;
j=PRODUCT(i++) ;
k=PRODUCT(++i) ;
printf("j=%d\nk=%d",j,k) ;
return 0;
}
view raw pre.c hosted with ❤ by GitHub

j=12
k=49

From C file to exe file

Describe in detail how a C program is compiled and executed 

Answer

1) The preprocessor code searches for preprocessor directives (mainly #include or #define) and substitute them with their codes,the output of this step creates an expanded file (.I file)
2) the compiler takes .I file and coverts the high level code into assembly then into machine language (binary format) and generate .obj file
3) the linker takes .obj file and link it with C standard library functions in your code 
and generates the executable version of your C code



Monday, 9 January 2017

what will be the output of the following program


#include <stdio.h>
float x = 4.5 ;
int main( )
{
float y;
float f(float);
x*=2.0 ;
y=f(x) ;
printf("x=%f\ny=%f",x,y) ;
return 0;
}
float f(float a)
{
a+=1.3 ;
x-=4.5 ;
return(a+x) ;
}
view raw global.c hosted with ❤ by GitHub
x=4.5
y=14.8

What will be the output of the following program


#include <stdio.h>
int main()
{
static int count=5;
printf ("count = %d\n", --count);
if (count!= 0)
main() ;
return 0;
}
view raw static.c hosted with ❤ by GitHub
output is
count=4
count=3
count=2
count=1
count=0

Wednesday, 4 January 2017

what is the output of the following program


#include <stdio.h>
add(double a,double b)
{
return a+b;
}
void main()
{
printf("%d",add(12.3,98.2));
return;
}
view raw default.c hosted with ❤ by GitHub
output=110 
by default C functions return int values in case you did not specify the return type

what is the output of the following program


#include <stdio.h>
int add(int x,int y,int z)
{
printf("%d\n",x+y+z);
return;
}
int main()
{
add(2,3,5);
int x=add(2,3,5);
printf("%d",x);
return 0;
}
view raw garbage.c hosted with ❤ by GitHub
10
10
garbage value
if your function does not return any value , it must be declared as void