Embedded C Programs for Interview
1. Implement below using C program.
Given int *data= [x1,x2,x3,x4….xn];
write a function such that
data= [x1,x1*x2,x1*x2*x3,....]
#include<stdio.h>
int main()
{
int data[]={1,2,3,4,5,6,7,8,9};
int i,j=2,mul[9];
mul[0]=1;
mul[1]=2;
int size=9;
printf("\n Given array is\n");
for (i=0;i<size;i++)
printf("%d\n",data[i]);
for (i=1;i<size;i++)
{
mul[i]=data[i]*mul[i-1];
j++;
}
printf("\n Mult array\n");
for (j=0;j<size;j++)
printf("%d\n",mul[j]);
}
1,2,3,4,5,6,7,8,9
1.2,6,24,120,740
2. Using C program Implement realloc using malloc - Dynamic memory allocation.
char *my_realloc(Char *ptr, int size)
{
if(ptr)
free(ptr);
char *new_ptr = malloc(ptr,size);
return new_ptr;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define malloc(_x) mymalloc((_x))
#define free(_x) myfree((_x))
void myfree(void * p) {
size_t * in = p;
if (in) { --in; free(in); }
}
void * mymalloc(size_t n) {
size_t * result = malloc(n + sizeof(size_t));
if (result) { *result = n; ++result; memset(result,0,n); }
return result;
}
size_t getsize(void * p) {
size_t * in = p;
if (in) { --in; return *in; }
return -1;
}
void *reallocation(void *ptr,size_t size) {
void *newptr;
int msize;
msize=getsize(ptr);
printf("msize=%d\n",msize);
if (size <= msize) return ptr;
newptr=malloc(size);
memcpy(newptr,ptr,msize);
free(ptr);
return newptr;
}
int main() {
char * aa = malloc(50);
char * bb ;
printf("aa size is %d\n",getsize(aa));
strcpy(aa,"my cookie");
bb = reallocation(aa,100);
printf("bb size is %d\n",getsize(bb));
printf("<%s>\n",bb);
free(bb);
}
3.Write a program to initialize array using malloc, to check nth count of array.
void main()
{
int *a;
printf("enter the number of variables\n");
scanf("%d",&o);
printf("Variable is %d\n",o);
a=(int*)malloc(o*sizeof(int));
printf("Enter Elements of First List \n");
for (i = 0; i < o; i++) {
scanf("%d", a + i);
}
int s=sizeof(a)/sizeof(int);
printf("\n%d\t%u\n",s,&s);
for(m=0;m<=3;m++)
printf("%d\n",a[m]);
}
4. Write a Program to print sizeof data types- sizeof(); operator.
#include <stdio.h>
int main()
{
printf("Size of data type for Char-%d!\n",sizeof(char));
printf("Size of data type for Short-%d!\n",sizeof(short));
printf("Size of data type for Int-%d!\n",sizeof(int));
printf("Size of data type for Long-%d!\n",sizeof(long));
printf("Size of data type for Double-%d!\n",sizeof(double));
return 0;
}
5.Write a program to reverse the given number.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,cnt=0,o,count=0,l=0,cnt1=0,j=0,k=0,m;
int *a;
char b[5];
printf("enter the number of variables\n");
scanf("%d",&o);
printf("Variable is %d\n",o);
a=(int*)malloc(o*sizeof(int));
printf("Enter Elements of First List \n");
for (i = 0; i < o; i++) {
scanf("%d", a + i);
}
printf("Reversed String is\n");
while(o!=0)
{
printf("%d\n",a[o-1]);
o--;
}
}
6.Write syntax to initialize the pointer to given memory location.
#include <stdio.h>
int main()
{
int a=9;
int *p;
*p=0x012;
printf("%d\n%u\n%u\n",*p,p,&p);
*p=a;
printf("\n%d,%u\n",*p,&p);
return 0;
}
7.Write a program to Reverse the string.
#include <stdio.h>
int main()
{
int k=0,j,i=0,count=0;
char a[]="abhishek",b[10];
while(a[i]!='\0')
{
count++;
i++;
}
for(j=count;j>=0;j--)
{
b[k++]=a[j];
}
for(i=0;i<=count;i++){
printf("%c",b[i]);
}
printf("\n");
return 0;
}