ECE 485 Lectures 2 & 3 Pop Quiz Review (Quiz 1)

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Using the #define statement, how would you declare a manifest constant that returns the number of seconds in a year? Disregard leap years in your answer.

#define SECONDS_PER_YEAR (60UL * 60UL * 24UL * 365UL)

Define a memory-mapped switches which is mapped to CPU 0x22001200 address

#define SWITCHES ((volatile char *) 0x22001200)

Using the variable a, write down definitions for the following: (c) A pointer to a pointer to an integer

(c) int **a; // A pointer to a pointer to an integer

void fun(int *p) { int q = 10; p = &q; } int main() { int r = 20; int *p = &r; fun(p); printf("%d", *p); return 0; } A. 10 B. 20 C. Compile error D. Runtime Error

B. 20

Consider the following two C lines int var1; extern int var2; A. Both statements only declare variables, don't define them. B. First statement declares and defines var1, but second statement only declares var2 C. Both statements declare define variables var1 and var2

the purpose of int var1 is to declare AND define var. since int var2 is extern, it is only declared in the file, but it is retrieved from an external source. B. First statement declares and defines var1, but second statement only declares var2

Write one line C code to define the data type of int8_t. (Only use one space as delimiter, for example, struct abct ABC, no space starting)

typedef signed char int8_t;

What is the size (the number of bytes) of myvar with the following declarations: Typedef struct os_tcb { unsigned char *px; struct os_tcb *pt; char dir; char p[5]; } OS_TCB; OS_TCB myvar;

unsigned char *px; -> 4 bytes struct os_tcb *pt; -> 4 bytes char dir; -> 1 byte char p[5]; -> 6 bytes Total: 15 bytes

Using the variable a, write down definitions for the following: (d) An array of ten integers

(d) int a[10]; // An array of 10 integers

Using the variable a, write down definitions for the following: (e) An array of ten pointers to integers

(e) int *a[10]; // An array of 10 pointers to integers

Using the variable a, write down definitions for the following: (f) A pointer to an array of ten integers

(f) int (*a)[10]; // A pointer to an array of 10 integers

Using the variable a, write down definitions for the following: (g) A pointer to a function that takes an integer as an argument and returns an integer

(g) int (*a)(int); // A pointer to a function a that takes an integer argument and returns an integer

Using the variable a, write down definitions for the following: (h) An array of ten pointers to functions that take an integer argument and return an integer.

(h) int (*a[10])(int); // An array of 10 pointers to functions that take an integer argument and return an integer

What is the size (the number of bytes) of myvar with the following declarations: signed char myvar;

1 byte

What is the size (the number of bytes) of myvar with the following declarations: Assuming that the Keil compiler will assign a 32-bit integer for each enumerate value (RED, BLUE or GREEN) typedef enum color {RED=0, BLUE=1,GREEN=2 } color_type; color_type myvar = BLUE

4 bytes

What is the size (the number of bytes) of myvar with the following declarations: signed char *myvar;

4 bytes

What is the size (the number of bytes) of myvar with the following declarations: char myvar[] = "555555";

7 bytes

What is a volatile keyword?

A volatile keyword prevents the compiler from applying optimizations on objects that can change in ways that the complier cannot determine. Objects declared as volatile are omitted from optimization. Their values can be changed by code outside the scope of the current code at any given time.

Output of the program #include <stdio.h> extern int var = 0; int main() { var = 10; printf("%d ", var); return 0; } A. 10 B. Compile Error: var is not defined C. 0

A. 10

What is the output of the program? #include <stdio.h> int main() { int a[5] = {1,2,3,4,5}; int *ptr = (int*)(&a+1); printf("%d %d", *(a+1), *(ptr-1)); return 0; } A. 2 5 B. Garbage value C. Compile error D. Segmentation fault

A. 2 5

Please list the advantage(s) of using the defined datatype uint8_t instead of char? (list 3)

Advantages: - Only 1 header file needs to be changed - Uint8 fits with aliasing of data types better - Uint8 is a distinct extended integer type

1. Assume that float takes 4 bytes, predict the output of following program. #include <stdio.h> int main() { float arr[5] = {12.5, 10.0, 13.5, 90.5, 0.5}; float *ptr1 = &arr[0]; float *ptr2 = ptr1 + 3; printf("%f ", *ptr2); printf("%d", ptr2 - ptr1); return 0; } A. 90.500000 3 B 90.500000 12 C 10.000000 12 D 0.500000 3

For printf *ptr2, start at 12.5 and move 3 spots over. The number you land on is 90.5 ptr2 refers to the integer 13.5 ptr1 refers to the integer 10.0 ptr2 - ptr1 = 13.5 - 10.0 = 3.5 = 3 (no float) Answer: A A. 90.500000 3

What is 'stack overflow' error in C?

Stack overflow is an error which occurs if the program tries to access memory beyond its max available limit. This error can also occur if the pointer exceeds a stack boundary. A program will terminate once there is a stack overflow. Any instructions are also terminated as well and will nto execute.

What is wrong with this C macro definition? #define MUL(a,b) a*b

The problem with this macro definition is the order of operations. This may produce a wrong answer based on how it is written.

What is the difference between Call by Value and Call by Reference?

These are ways in which functions are invoked. The difference is that with CBValue parameter passing method, values of parameters are copied to function's formal parameters. The two types of parameters are stored in different memory locations. Changes made inside functions are NOT reflected in the actual parameters of the caller. With CBReference, changes made inside the function are actually reflected in actual parameters of the caller. The actual and formal parameters refer to the same locations.

What does the following C statement declare? int * f [ ] ( ) A. A function returning a pointer to an array of integers. B. Array of functions returning pointers to integers. C. A function returning an array of pointers to integers. D. An illegal statement

This is an array of functions returning pointers to integers Answer: B) Array of functions returning pointers to integers

What is wrong with this C macro definition? #define MIN(a,b) ((a) <(b))? (a): (b)) Hint: considering this example, int a=1, b=2; printf("%d\n",MIN(a,b)); printf("%d\n",MIN(a++,b++));

This macro definition has the potential to result in a double evaluation. Using Inline would be better. Inline Function are those function whose definitions are small and be substituted at the place where its function call is happened. Function substitution is totally compiler choice.

Can a variable be both const and volatile?

Yes, a variable can be both a constant and volatile. Const means the variable cannot be assigned a new value. This value can be changed through the use of a code or pointer.

Using the variable a, write down definitions for the following: (b) A pointer to an integer

int *a // A pointer to an integer

Write a declaration for 'an array of pointers to an integer'. For this question make it an array of pointers to an integer.

int *ptr[10];

Using the variable a, write down definitions for the following: (a) An integer

int a; // An integer

What is the output of this C code? #include void main() { int k = 5; int *p = &k; int **m = &p; printf("%d %d %d", k, *p, **m); } A. 5 5 5 B. 5 5 junk C. 5 junk junk D. compile time error

k = 5 *p = &k = 5 **m = &p = 5 Answer: A. 5 5 5

Below is a sample of a C file foo.c uint32_t myvar1 = 0; static uint8_t myvar2; extern int16_t myvar3; extern func1(uint8_t c); static func2(uint8_t c) { } void func3(uint8_t c) { } Please describe the scopes of variables myvar1, myvar2, myvar3 and functions func1(), func2(), func3().

myvar1: global variable (accessible anywhere) myvar2: local static variable (no access from outside of file) myvar3: declared externally func1(): declared in external file func2(): local static variable (no access from outside file) func3(): accessible anywhere, but can NOT return a value

Consider a compiler where int takes 4 bytes, char takes 1 byte and pointer takes 4 bytes. #include <stdio.h> int main() { int arri[] = {1, 2 ,3}; int *ptri = arri; char arrc[] = {1, 2 ,3}; char *ptrc = arrc; printf("sizeof arri[] = %d ", sizeof(arri)); printf("sizeof ptri = %d ", sizeof(ptri)); printf("sizeof arrc[] = %d ", sizeof(arrc)); printf("sizeof ptrc = %d ", sizeof(ptrc)); return 0; } A. sizeof arri[] = 3 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 4 B. sizeof arri[] = 12 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 1 C. sizeof arri[] = 3 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 1 D. sizeof arri[] = 12 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 4

sizeof arri[ ] = 12 sizeof ptri = 3 sizeof arrc[ ] = 3 sizeof ptrc = 4 correct answer: D D. sizeof arri[] = 12 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 4

Exercises: assuming unsigned char xyz is 1 byte unsigned char xyz; Set the variable xyz bit 4, bit 5 and bit 6 LOW without modifying the other bits. Use only one line C-code.

xyz = xyz & ~(1<<4 | 1<<5 | 1<<6);

Exercises: assuming unsigned char xyz is 1 byte unsigned char xyz; Set the variable xyz bit 7, 6, 5 and 4 as the value 0101 (binary) without modifying other bits, using only one line C-code.

xyz = xyz &~(1<<4 | 1<<5 | 1<<6 | 1<<7) | (1<<6 | 1<<4);

Exercises: assuming unsigned char xyz is 1 byte unsigned char xyz; Set the variable xyz bit 6 HIGH without modifying the other bits, using one C-code only.

xyz = xyz | (1<<6);


Kaugnay na mga set ng pag-aaral

Unanticipated Problems and Reporting Requirements in Social and Behavioral Research module 14

View Set

CISSP-Topic 6, Physical Security

View Set

Archaeology & the Human Past Exam 3

View Set

FINC 318 Chp 8: Net Present Value SmartBook 2.0

View Set

Chapter 4- Documentation and Interprofessional Communication

View Set

Chapter 2 Collaboration, Interpersonal Communication, and Business Etiquette

View Set