1) What is Call by Value and Call by Reference in Embedded C?
In Call by value, actual values are not changed in between functions, since memory location is different in between function calls.
In Call by reference, actual values are changed in between functions, since memory location is same in between function calls.
#include "stdio.h"
void callByValue(int x, int y);
void callByReference(int *x1, int *y1);
void main ()
{
int a=5,b=8;
printf("a= %d, b= %d", a,b);
callByValue(a,b);
printf("\nCall by Value: a= %d, b= %d", a,b); // No change in Values
callByReference(&a,&b);
printf("\nCall by Value: a= %d, b= %d", a,b); // Change in Values
}
void callByValue(int x, int y)
{
x++;
y++;
printf("\nx= %d, y= %d", x,y);
}
void callByReference(int *x1, int *y1)
{
(*x1)++;
(*y1)++;
printf("\nx1= %d, y1= %d", *x1,*y1);
}
2) How to change value of a constant variable in Embedded C.
The value of a constant variable can be changed using a pointer.
#include "stdio.h"
void main ()
{
const int num=5;
int *ptr=#
*ptr=12;
printf("num=%d",num);
}
3) Write a program to print a pyramid with 5 rows.
#include "stdio.h"
void main ()
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5-i;j++)
{
printf(" ");
}
for(int j=1;j<=i;j++)
{
printf("* ");
}
printf("\n");
}
}
4) Explain the compilation process in Embedded C.
Compilation process takes place in following steps-
1.Pre-Processing – The C pre-processor performs the inclusion of header files, replaces macros with its values and removes the comments from the C-code.
Input – Hello.c Output – Hello.i
2. Compilation – The compiler performs the compilation of pre-processed code to assembly code.
Input – Hello.i Output – Hello.s
3. Assembler – The assembler performs conversion for assembly code to object file (containing 0s and 1s).
Input – Hello.s Output – Hello.o
4. Linker – The linker performs the linking of object files generated after compilation of different C files and combines them into a single output i.e. hex/elf etc file.
Input – Hello.o Output – Out.hex
5) Write a C program for factorial using recursion.
#include "stdio.h"
int fact(int n);
void main ()
{
int num=5;
printf("Factorial =%d",fact(num));
}
int fact(int n)
{
if(n==0)
{
return 1;
}
return(n * fact(n-1));
}
6) What is realloc() in embedded C?
The realloc is used to increase or decrease the memory allocated by malloc or calloc without loosing the data that was stored using malloc or calloc.
#include "stdio.h"
#include "stdlib.h"
void main()
{
int *ptr;
// This allocates 20 continuous bytes of memory space and address of first byte is stored in ptr.
ptr = (int*) calloc(5, sizeof(int));
// This reallocates memory of 20 bytes to 8 bytes
ptr = (int*) realloc(ptr,8);
}
7) What is the difference between malloc() and calloc() in embedded C?
The first difference is that, there are two arguments in calloc and only one argument in malloc.
This can be seen in syntax below-
#include "stdio.h"
#include "stdlib.h"
void main()
{
int *ptr, *ptr1;
// This allocates 20 continous bytes of memory space and address of first byte is stored in ptr.
ptr = (int*) calloc(5, sizeof(int));
// This allocates 4 continous bytes of memory space and address of first byte is stored in ptr.
ptr1 = (int*) malloc(sizeof(int));
}
The second difference is that the memory allocated by malloc() contains garbage value while the memory allocated by calloc() is initialized by 0.
8) What is the use of free() in embedded C?
The free() is used to clear or free the memory allocated dynamically using the malloc() or calloc(). This is required since the memory allocated by malloc() or calloc() is not freed up even after the end of program.
#include "stdio.h"
#include "stdlib.h"
void main()
{
int *ptr;
ptr = (int*) malloc(sizeof(int)); // Used to allocate Memory
free(ptr); // Used to free-up memory allocated by malloc
}
9) Write a program to clear rightmost “set” bit of a given number
#include "stdio.h"
#include "stdlib.h"
void displayBits (int x)
{
int i, mask;
for(i=31;i>=0;i--)
{
mask = 1<<i;
putchar((x&mask)?'1':'0');
if(i%8==0)
{
putchar(' ');
}
}
printf("\n");
}
void main()
{
unsigned n =140;
displayBits(n); //Output is 00000000 00000000 00000000 10001100
n = n & (n-1);
displayBits(n); //Output is 00000000 00000000 00000000 10001000
}