Embedded C Questions

     

Embedded C Questions Questions


 


Question 1

 

a

Rewrite the following code fragment using the ‘?:’ ternary operator

 
  if (a == 6)
   b = 7;
else
   b = 9;
 
 
 
 

b

In the following code fragment, explain the differences between
(i) variables x and y;
(ii) variables m and n;
(iii) variables y and n.

 
  ...
/* The following two variables are declared outside of any function */
int x = 3;
static int y = 3;
...
v oid func1(void)
{
  int m = 3;
  static int n = 3;
...
 
 
 
 

c

A structure is defined as follows:

 
  typedef struct tagContent
{
  void *p_data;
  int index;
  int *s_data;
} Content;

 
 

Produce a structure that will give a singly-linked list of “Content” structures.

 
 
 
 

d

(i) Explain the effect of declaring a variable to be volatile. (ii) Where is this useful?

 
 
 
 

e

In the following code fragment, what is the expected output?

 
  unsigned char c = 0xA5;
unsigned char d = 0x3C;
d &= (c | 0x70);
printf(“c is 0x%x\n“, c);
printf(“d is 0x%x\n“, d);
 
 
 


Question 2

 

Each of the following purported implementations of a ‘C’ standard Library function contain exactly 1 logic error. Describe just that logic error and its remedy

Notes: There are no syntax errors
You should not consider issues of good v. bad style

 
 

a

The definition of the ‘C’ Library function memcmp is

int memcmp(const char *cs, const char *ct, size_t n)

Compare the first n characters of cs with the first n characters of ct.

Return < 0 if cs < ct.
Return > 0 if cs > ct.
Return 0 if cs == ct.

 
  int memcmp(const char *cs, const char *ct, size_t n)
{
  size_t i;

  for (i = 0; i < n; i++, cs++, ct++)
  {
    if (*cs < *ct)
    {
      return -1;
    }
    else if (*cs > *ct)
    {
      return 1;
    }
    else
    {
      return 0;
    }
  }
}

 
 
 
 

b

The definition of the ‘C’ Library function strcat is

char *strcat(char *s, const char *ct)

Concatenate ct to the end of s. Return s.

 
  char *strcat(char *s, const char *ct)
{
  int i, start;
  start = strlen(s);
  for (i = 0; i < strlen(ct); i++)
  {
    s[start + i] = ct[i];
  }
  return s;
}
 
 
 
 

c

The definition of the ‘C’ Library function memset is

void *memset(char *s, char c, size_t n)

Copy c to the first n characters of s. Return s.

 
  void *memset(char *s, char c, size_t n)
{
  size_t i;

  for (i = 0; i < n; i++, s++)
  {
    *s = c;
  }
  return s;
}

 
 
 


Question 3

 

The definition of library function strspn is:

size_t strspn(const char *str, const char *chars)

Return number of leading characters at the beginning of the string str which are all members of string chars.

e.g. if ‘str’ is “fecxdy” and ‘chars’ is “abcdef” then the function would return 3, since ‘f’, ’e’ and ‘c’ all appear somewhere in ‘chars’, giving 3 leading characters of ‘str’, and ‘x’ is the first character of ‘str’ which is not a member of ‘chars’.

Write an implementation of strspn in ‘C’. The only function you may call from your implementation is strlen.

 
 
 


Question 4

  Answer any parts that you can  
 

a

In ‘C’, there is no language-defined Boolean type , and an int is often used instead, as in the following code fragment:

 
  int flag = FALSE;  
  (i) Why should a float not be used?

(ii) What would typically happen if you tried to compile the above fragment? And why?
 
 
 
 

b

Name three facilities for inter-process communication that you would expect to be provided by a multi-tasking operating system.

 
 
 
 

c

Explain how a 32 bit integer 0x1234ABCD is likely to be stored (a) By a big endian processor and (b) By a little endian processor.

 
 
 
 

d

Why can the use of macros be a bad idea?

 
 
 
 

e

What is a floating point number?

 
 
 
 

f

List some useful facilities for debugging in a Set Top Box?

 
 
 
 
Contact NDS
© NDS Limited 2005
Privacy | Legal | Site Map