Midterm Practice

Data Types and Variables

1. What is the range of values that can be represented with a 16-bit signed short integer, 16-bit short unsigned integer data type?

Signed = (-215) – (215–1)

Unsigned = 0 – (216–1)

2. What is the range of values that can be represented with a 32-bit signed integer, 32-bit unsigned integer data type?

Signed = (-231) – (231–1)

Unsigned = 0 – (232–1)

3. What is the range of values that can be represented with a 64-bit signed long integer, 64-bit unsigned long integer data type?

Signed = (-263) – (263–1)

Unsigned = 0 – (264–1)

4. Integer Overflow: What happens when we attempt to store a value outside the range of an integer data type?

Unsigned number – predictable (wraps around)

Signed number: Undefined - C doesn’t specify (something random)

5. Command Line Argument: When we pass multiple arguments from the command line, What’s the argument at index 0?

The name of the program.

6. When command line arguments are passed as strings, how can they be converted to integers?

atoi(): converts string to int (ASCII to integer)
#include <stdio.h>






Functions and Arrays



1. Do changes made to a local variable in one function affect the value of the same variable in another function?

No, changes made to a local variable in one function do not affect the value of the same-named variable in another function.



2. How are 1D and 2D arrays stored in memory, and what does contiguous memory mean in this context?

A 1D array (one-dimensional array) is stored in contiguous memory locations. The elements are placed one after the other in a continuous block of memory.



A 2D array (two-dimensional array) is essentially a 1D array of arrays. In memory, it is also stored in contiguous locations.The elements are placed one after the other in a continuous block of memory.



3. How do we calculate the number of bytes needed to store an array?

The size of each element: The size of each element in the array depends on the data type (e.g., int, float, char). Times the total number of elements: This is determined by the dimensions of the array.

Element x Size



4. When we pass an array name to a function, What is actually passed to the function?

The address of the first element in the array.



5. How a program gets compiled and executed? What are the steps?
Compile then Run

Preprocessor directives (like #include, #define, #ifdef) are processed in this stage.

The compiler converts the preprocessed source code into assembly code for the target architecture.

The assembler translates the assembly code generated by the compiler into machine code (binary instructions specific to the CPU architecture).

In this step, the linker takes one or more object files and links them together into a complete executable.




6. What are the responsibilities of a preprocessor, compiler, linker, and assembler?

1. Preprocessing: Processes directives like #include and #define.

2. Compilation: Translates preprocessed code to assembly.

3. Assembly: Converts assembly to machine code (object files).

4. Linking: Links object files into an executable, resolving external references.