C Knowledge Flashcards

1
Q

Standard ‘MIN’ Macro

A

define MIN(A,B) ((A) <= (B) ? (A) : (B))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Conditional expression with ternary operator

A

expr1 ? expr2 : expr3
expr1 is evaluated first
If true (non-Zero), then expr2 is evaluated.
Otherwise, expr3 is evaluated.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Code an infinite loop

A

while(1)
for(;;) //KnR preferred method, necessary for Lint
Loop:
goto Loop;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the three uses of the keyword static?

A

a. A variable declared static within the body of a function maintains its value between function calls
b. A variable declared static within a module, but outside a function, is accessible by all functions within that module. (localized global)
c. Functions declared static w/in a module may only be called by other functions within that module. The scope of the function is localized to the module in which it was declared.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the keyword const mean?

A

essentially “ready-only”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

const int a;
or
int const a;

A

read-only integer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

const int *a;

A

pointer to a read-only integer. (integer isn’t modifiable, but pointer is)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

int * const a;

A

read-only pointer to a modifiable integer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

int const * a const;

A

constant pointer to a constant integer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does the keyword volatile mean?

A

( volatile == a qualifier) A volatile variable is one that can change unexpectedly. Tells optimizer that the variable must reload the variable every time it is used instead of holding a copy in a register.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Give three examples of using the volatile keyword.

A
  1. Hardware registers in peripherals (e.g., status registers)
  2. Non-stack variables referenced within an interrupt service routine.
  3. Variables shared by multiple tasks in a multi-threaded application
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Can a parameter be both const and volatile?

A

Yes. Example: read-only status register. Volatile because it can change unexpectedly, const because the program should not attempt to modify it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can a pointer be volatile?

A

Yes. Example: when an interrupt service routine modifies a pointer to a buffer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is wrong with following function:

int square(volatile int *ptr)
{
return *ptr * *ptr;
}

A

Since *ptr is volatile and could change unexpectedly, it could produce a return value that is not a square. Function should set a local variable with the value of *ptr and then square the local variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Efficient way to produce Bit manipulation?

A

define SET_BIT(x,y) (x |= (1«y))

Set Bit |=
Clr Bit &= ~

#define CLR_BIT(x,y) (x &= ~(1«y))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Set integer value at absolute address 0x67a9 to the value of 0xaa55 on pure ANSI compiler

A

int *ptr;
ptr = (int *) 0x67a9;
*ptr = 0xaa55;

17
Q

Can an interrupt service routine return a value?

A

no

18
Q

Can ISRs be passed a value?

A

no

19
Q

What is the goal of the body of an ISR?

A

short and sweet (less than five lines of code?)

20
Q

Re-entrant?

A

One function is said to be a reentrant function if there is a provision to interrupt that function in the course of execution, then service the ISR (Interrupt Service Routine) and then resume the task

21
Q

Expressions involving signed and unsigned types __________

A

have all operands promoted to unsigned types

22
Q

tilde ~

A

creates complement of operand (0s become ones, ones become 0s)

23
Q

What is #error?

A

A preprocessor directive that causes compilation to terminate and prints the user defined error message to stderr.
#error <writer></writer>

Filename (line_number): Error!
Ennnn: <writer></writer>

24
Q

When would a programmer use #error?

A

if (INT_MAX != 32767)

Incomplete code: add to section of code that is incomplete to guide user after return to project

Compiler dependent code: use to check if compiler meets minimums for certain aspects
#if (LDBL_DIG < 12)
#error ** long doubles need 12 digit resolution **
#endif

#error This file only works with 16 bit int
#endif

Conditionally compiled code:

25
Q
A