CPSC2310 - Exam 1
What is the output of the following code? #include <stdio.h> int main() { float arr[5] = {18.5, 90.5, 13.5, 10.8, 0.5}; float *ptr1 = &arr[0]; float *ptr2 = ptr1 + 3; printf("%.2f\n", *ptr2); printf("%ld\n", ptr2 - ptr1); printf("%.2f \n", ptr1[(ptr2 - ptr1)]); int i = 0; return 0; }
10.80 3 10.80
printf ("%o\n", 100); This should print 100 (base 10) in octal format. The output would be: ______
144
Write the code to open a file pointer for reading in binary mode. You can choose the name of the file pointer. The name of the file being opened will be given through the command line.
FILE *inFile; inFile = fopen(argv[1], "rb"); if (inFile == NULL) { printf("The file could not be opened."); exit(-1); }
(T/F) size_t in C guarantees the value is always going to be negative.
False
T/F - Cache memory is smaller, cost less, and is faster that main memory.
False Cache memory is faster and smaller, but much more expensive.
When using malloc must also _______ the memory after you are finished with the memory
free
We discussed a function that returns the current position of a file pointer. What is that function?
ftell()
Define a structure that represents a Car that contains variables that represent the make of the car, model of the car, year of the car and Color of the car. Assume the name, model and color have no more than 20 characters. Use an initialization list to create an instance of the struct initializing the struct to Honda, CR-V, 2021, Silver.
struct Car { char make[20]; char model[20]; int year; char color[20]; } hondaInstance = { 'Honda', 'CR-V', 2021, 'Silver' };
Is this a 1D or 2D malloc example? int *arr = (int*)malloc(row*col*sizeof(int));
1D
What character is interepreted by the preprocessor as a preprocessor command?
# ie. #define, #include
What are hte part of the print format specifier prototype?
%[flags][width][.precision][length]specifier
We discussed Caches memory and why it matters that we have it. Explain why caches memory was developed - how it has helped computer systems.
***
With respect to memory, what is cache memory and describe why cache memory is useful.
***
What do the following print specifier flags mean: - + (space) # 0
- : left justify + : force print + sign with positives (space) : add space if no sign before value # : preced x, X or o with 0 0 : left pad number with 0s
The preprocessor produces _____ files
.i
The assembler produces _____ files from _____ files
.o from .s
The compiler produces ____ or _____ files
.s or .o
printf("%010d\n", 2023); The output would be: ________
0000002023
printf ("%#o\n", 100); This should print 100 (base 10) in octal format. The output would be: _____
0144
printf ("%#X\n", 15); This should print 15 (base 10) in hexadecimal format. The output would be: ______
0xF
What are three different ways to dynamically allocate memory for a 2D array?
1) Using a single pointer and a 1D array with pointer arithmetic 2) Using an array of pointers 3) Using pointer to a pointer 4) Using double pointer and one malloc call 5) Using a pointer to Variable Length Array. 6) Using a pointer to the first row of VLA
What are the four phases of the compilation system?
1. Preprocessing 2. Compilation 3. Assembly 4. Linking
Given the following struct: struct test{ char a; int b; short c; int d; }; How many bytes of memory will be needed for this struct?
1/3 4 2/2 4 So, we will need 16 bytes of memory for this struct.
Consider the following struct: typedef struct test { char a; int b; char c; short int d; } test_t; How much memory would need to be set aside to store this struct? In other words, if I printed out the size of the struct, what would the size be?
12
Is this a 1D or a 2D malloc example? int **arr = (int**)malloc(row*sizeof(int*)); for(i=0; i < row; i++) { arr[i] = (int*)malloc(col*sizeof(int)); }
2D
Consider the following struct: typedef struct test2 { int a; short int b; char c; char d; } test2_t; How much memory would need to be set aside to store this struct? In other words, if I printed out the size of the struct, what would the size be?
8
What is a function pointer?
A pointer to a function!
Assume the file was compiled using the following: gcc -save-temps hello.c -o hello What file would this phase produce?
An executable file called "hello"
What is the third phase of the compilation process? Explain what this phase does.
Assembly An assembler is used to translate the assembly instructions to machine language instructions (or object code). The output consists of actual instructions to be run by the target processor.
What is the output of the following code? #include <stdio.h> int main() { char cArr[] = {64, 'A' , 98}; //64 = @ 98 = b 65 = A char *cPtr = cArr; printf("CArr[0] = %c \n", *cArr); printf("cPtr[1] = %c \n", *(cPtr+1)); printf("cArr[1] + 1 = %c \n",*(cArr+1)+1); printf("cPtr[2] = %c \n", *(cPtr+2)); printf("cArr[2] = %c \n", *(cArr+2)); return 0; }
CArr[0] = @ cPtr[1] = A cPtr[1] + 1 = B cPtr[2] = b cPtr[2] = b
What is the second phase of the compilation process? Explain what this phase does.
Compilation The preprocessed code is translated to assembly instructions specific to the target processor architecture. It also allows for C code to contain inline assembly instructions and for different assemblers to be used
File handling in 'C' has 4 basic operations. What are the 4 operations you can do with files?
Create a new file Open an existing file Close a file Read from and write information to a file COCRW!
(T/F) You can print the address of a bit field member
False!
(T/F) The following struct is a valid use of bit fields. struct test{ unsigned int i : 5; unsigned char c : 8; unsigned float f : 8; };
False! Floats are not a part of SCLIB and cannot be used with bit fields
Why use pointers?
Gives the ability to work with particular memory locations Necessary for programming microcontrollers Necessary for arrays and strings(C++) Allows us to effectively represent complex data structures (linked list, stacks, queues, trees, etc) Change values passed as arguments to functions Optimization of programs -pass pointer to a function does not have to duplicate the data
Explain what happens in the following line of code: #define num 10
It's telling the preprocessor that num is a macro set to 10 (similar to a global variable, but it cannot be redefined without doing #undef) Macros are basically a text substitution that are handled during the pre-processing step of the compilation process.
Explain what happens in the following line of code: #include <stdio.h>
It's telling the preprocessor to include the standard input/output library
What is the name of the last phase of the compilation process? Explain what this phase does? What file would this phase produce?
Linking The linker will arrange the pieces of object code so that functions in some pieces can successfully call functions in other ones. It will also add pieces containing the instructions for library functions used by the program. Linking produces an executable file
What are macros?
Macros are basically a text substitution that are handled during the pre-processing step of the compilation process
What are function macros?
Often basic functions like min or max that can be predefined as a macro and won't change throughout the program **NOTE: you cannot have a space after you define your function macro or else the compiler will treat the function as a variable and might also cause errors!
What is the first phase of the compilation process? Explain what this phase does.
Preprocessing Before interpreting commands, the preprocessor does some initial processing. This includes jointing lines, removing comments, and looking at macros (#)
What does the function rewind do and what is the disadvantage of using rewind?
Rewind moves the file pointer to the beginning of the file or buffer. An advantage is that it's easy to call, but a disadvantage is that you cannot check if it's successful because it clears the error indicator.
What are the only integral types that can use bit fields?
SCLIB: short, char, long, int, bool
There are three constants use with fseek. Name the three constants and describe what they do.
SEEK_CUR: indicates the current position of the pointer of the file SEEK_END: moves the pointer of the file to the end of the file SEEK_SET: moves the file pointer to the beginning of the file
Why use bit fields? (2 main reasons)
Saves memory Reduces the size of the struct
In 'C' we typically use 2 types of files with respect to file handling (reading and writing). Name the two types and the advantages each type has.
Text based files (.txt): they are readable and editable but hold a smaller amount of data Binary files (.bin): they are not easily readable, but they are more secure and hold a higher amount of data
The following is an unusual print statement. Explain what this is doing. printf("Unusual trick: %*d\n", 5, 10);
The asterisk is allowing us to specify the width of the output field, so it's printing 10 with a width of 5 characters ( 10)
Given the following struct: struct test{ char a; int b; short c; int d; }; Describe the layout of memory. In other words, explain where each member of the struct will be placed in memory, how many bytes that member will consume including alignment padding.
The first block will have 1 bytes for the char, 3 bytes of padding. The second block will have 4 bytes for the int, 0 bytes for padding. The third block will have 2 bytes for the short, 2 bytes for padding. The fourth block will have 4 bytes for the int, 0 bytes of padding.
What does the 'C' function fseek do?
The function fseek is similar to rewind; it moves the file pointer from its current position to a new position, forward or backward, specified by the number of bytes.
The following structure has a problem. typedef struct{ int a; struct NODE *B; int c; }NODE_T; What is the problem? Fix it.
The problem is again on the third line, this time where B is declared. struct NODE is not defined, so we'd need to define it before assigning *B to be a NODE type. typedef struct NODE{ int a; struct NODE *B; int c; }NODE_T;
The following C structure has a problem. struct NODE{ int a, b, c; struct NODE d; char e; }; What is the problem? Fix it.
The problem is on the third line, where d is declared as a structure of the same type as the mother struct, NODE. This creates an infinity loop and is likely a stack overflow, and could be corrected by changing d to be a pointer struct NODE{ int a, b, c; struct NODE * d; char e; };
(T/F) During compile time Header Guards make sure files are not included multiple time.
True
What are macros used for?
Used to define something that is used repeatedly in a program
What are 2 reasons to use double pointers?
Used to handle multi-dimensional arrays (ie matrix from lab 4) Used when passing a pointer by reference
What are function pointers used for?
Used when you have to switch between various versions of one implementation
What are examples of some predefined macros?
_DATE_ gives you the date you compiled _FILE_ gives u the file name of the current source file _LINE_ gives you the line number within the current course file _TIME_ gives you the time compiled _func_ prints the name of the current function
Assume you have opened a file pointer to read a file. The file pointer variable is called "inputFP". Fill in the blank with the "C" provided function that can be used to test the assumption that the file opened successfully. __________________(inputFP != NULL);
assert
A ____________ function is a reference of a function passed to another function as an argument to be called.
callback
What's the difference between malloc and calloc?
calloc also initializes the data, whereas malloc does not
What do the following print specifiers mean: d or i u o x or X e or E g or G a or A c s p
d or i - signed int u - unsigned int o - unsigned octal x or X - unsigned hexadecimal e or E - floating point g or G - shortest representation a or A - hexadecimal floating point c - character s - string of characters p - pointer address
The linker produces _____ files
executable
printf ("%x\n", 15); This should print 15 (base 10) in hexadecimal format. The output would be: ______
f
What is the name of the file that contains a backup of your home folder on the SoC?
snapshot
Given the following struct: struct test{ char a; int b; short c; int d; }; Rewrite the struct above in a way that will minimized the amount of padding needed. How many bytes will be needed for the new improved struct.
struct test { int b; int d; short c; char a; }; This struct will have 12 bytes of memory, and only 1 byte of padding.
Write the command to tar and compress the following files: driver.c functions.c functions.h. The name of your tarball must be HW1.tar.gz.
tar -czvf HW1.tar.gz driver.c functions.c functions.h
Define a structure that represents a student that contains variables for the student's first name, last name, identification number(no characters), and class standing. Use a number for the class standing. (Meaning: 1 would be a freshman). Assume the first and last names have no more than 20 characters. Use typedef to allow me to create a struct in the following ways: struct Student stu; or student_t stu;
typedef struct Student { char firstName[20]; char lastName[20]; int idNum; int classStanding; } student_t;