0% found this document useful (0 votes)
6 views

C programming interview prep

Uploaded by

Farah Fliss
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

C programming interview prep

Uploaded by

Farah Fliss
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

C programming interview prep

Command Line Arguments


Memory layout of C Programs
A program can be made to read cmdline
arguments which will be the input values
that change the runtime behavior of the
program.
These arguments are passed to the main
function as parameter values.
int main (int argc, char **argv) { ...
argc = count of args including the program
name
./a.out 10 12 will have argc = 3
argv is the argument vector which is an
array of strings (array of char arrays) which
contains argc+1 elements (the last element
is NULL to signal the end of the cmdline arg
list)

argv is immutable which compels the program to manipulate copies and as they are
strings, conversion functions may be needed like atoi() (ASCII to integer)

Text: Instructions void * Type


Initialized data: global and static variables initialized by the programmer. ( ∈ The void* type is a generic pointer: a pointer that can reference any type which is
Virtual Mem.)
static int i = 0; possible because the stored address size doesn’t differ from one type to the other.
int j =0; // outside of main(), global often used as the return value of a function or as a parameter for functions that
const char* str = “Hello world”; can take any type.

Uninitialized data (bss): data with no explicit init are there, init by the kernel
to arith. 0
static int i;
read-only : constants
read-write: global variables
bss : block started by symbol

Heap: dynamic memory allocation happens here, managed by: malloc, calloc,
realloc & free.
shared by all shared libraries and dyn. loaded modules.

Stack: contains the program stack, the top is tracked by the Stack Pointer
(SP), adjusts each time a value (stack frame) is pushed to the stack
A function frame contains at least the return address. Besides, it can contain
arguments and local (automatic) variables as well as some of the machine
registers.

Recursive function in stack:


each function call adds a new stack frame and each completed execution is
removed from the stack.
Pointer variables: useful for ->
implementing functions that can modify values in the caller’s stack
frame
dynamic allocation and deallocation at runtime
efficient passing of large data structures
creating linked dynamic data structures (linked lists for example)
interpret bytes of program memory in different ways
ptr refers to the variable of value 12
int * ptr;
int x;
*ptr = x;
NULL pointers are pointers that do not refer to any memory location. They
should never be dereferenced (to dereference : to access the value stored
at the pointed memory location)
int val = 5;
int *ptr = &val;
int derefptr = *ptr;

Advanced C features
Constants: alias for a literal value
#define N 50 // preprocessor macros
#define AREA(a) (5.18 * a * a) // preprocessor macros with args
enum days { MON=3, TUE, WED, THU,FRI, SAT, SUN} is equivalent to:
#define MON 3
#define TUE 4
#define WED 5

You might also like