1) What is difference between Declaration and Definition in Embedded C?
Declaration – It declares the variable/function. Declaration tells us about the name of variable, the type of value it holds etc.
Definition – It declares and allocates memory for the variable/function.
#include "stdio.h"
extern int b; //Declaration of variable
void func (int a, int b); //Declaration of function
int main ()
{
int num = 10; //Definition of variable
}
void func (int a, int b) //Definition of function
{
a=b;
b=a;
}
2) Write a program for returning more than one values in a function.
#include "stdio.h"
void func(int x,int y, int *ps, int *pp, int *pd)
{
*ps = x+y;
*pp = x*y;
*pd = x-y;
}
void main()
{
int a,b,sum,prod,diff;
a=6,b=4;
func(a,b,&sum,&prod,&diff);
printf("Sum=%d \nProd=%d \nDiff=%d", sum, prod, diff); //Output is 10,24,2
}
3) Write a program to display binary pattern of a integer in Embedded C.
#include "stdio.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(' ');
}
//break;
}
}
void main()
{
displayBits(0x12345678); //Output is
00010010 00110100 01010110 01111000
}
4) Write a program to determine if a number is Even or Odd using bitwise operator in Embedded C.
#include "stdio.h"
int main()
{
int num;
int mask = 0x1;
printf("Enter a number");
scanf("%d",&num);
if( (num&mask)==0) //Even - Rightmost bit=0 , Odd - Rightmost bit=1
printf("Number is Even");
else
printf("Number is Odd");
}
5) What is Structure Padding and Packing?
Structure padding is adding extra bit to the members of the structure. The compiler inserts extra bytes between members for alignment, these extra bytes are called padding bytes.
The padding increases performance of processor at the penalty of memory.
#include "stdio.h"
struct Base{
char a; // size 1 byte
char b; // size 1 byte
int i; // size 4 bytes
char c; // size 1 byte
}; // Total size of structure is 7 bytes
int main ()
{
struct Base B;
printf("Size of structure Base is: %d",sizeof(B)); //Output should be 7, but output is 12 because of Padding
// Here is how the structure members are packed
// ab-- iiii c--- (12 bytes in total)
}
Structure Packing is used to avoid the Structure Packing phenomenon seen above.
#include "stdio.h"
#pragma pack(1) // This forces compiler to use 1 byte packaging
struct Base{
char a; // size 1 byte
char b; // size 1 byte
int i; // size 4 bytes
char c; // size 1 byte
}; // Total size of structure is 7 bytes
int main ()
{
struct Base B;
printf("Size of structure Base is: %d",sizeof(B)); //Output should be 7
}
6) Write a program to reverse a number.
#include <stdio.h>
int main()
{
int num=123;
int rev=0;
int rem=0;
while(num>=1)
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}
printf("%d",rev); //Output is 321 i.e. reverse of 123
return 0;
}
7) Write a program to print Fibonacci series using recursion.
#include "stdio.h"
// Function for Fibonacci Series
int fib (int x)
{
if(x==0)
return 0;
if(x==1)
return 1;
return(fib(x-1)+fib(x-2));
}
void main()
{
for(int i=0;i<=10;i++)
{
printf("Series is:%d\n",fib(i)); // Output is 0 1 1 2 3 5 8 13 21 34 55
}
}
8) What is Dynamic Memory Allocation in Embedded C?
Dynamic memory allocation basically means to allocate memory during execution of program.
This is used when we do not know how much memory is needed by the program during runtime.
9) What is malloc in Embedded C?
The malloc is used to allocate memory for specified number of bytes and on successful allocation returns a pointer to first byte of allocated memory.
We can see how it works in the example below-
#include "stdio.h"
#include "stdlib.h"
void main()
{
int *ptr;
// This allocates 12 continous bytes of memory space and address of first byte is stored in ptr.
ptr = (int*) malloc(12);
}
10) What is calloc in Embedded C?
The calloc is similar to malloc and is used to allocate memory for specified number of bytes and on successful allocation returns a pointer to first byte of allocated memory.
We can see how it works in the example below-
#include "stdio.h"
#include "stdlib.h"
void main()
{
int *ptr;
// This allocates 20 continous bytes of memory space and address of first byte is stored in ptr.
ptr = (int*) calloc(5, sizeof(int));
}