The C Family Flashcards

1
Q

What does the term ‘imperative’ describe in programming?

A

Describes computation in terms of statements that change a program state

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

What is the opposite of imperative programming?

A

Declarative programming

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

What are the two main programming paradigms associated with C?

A

Procedural and functional

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

Is C a compiled or interpreted language?

A

Compiled

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

What type of typing does C use?

A

Statically, weakly typed

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

What does it mean for C to be statically typed?

A

Types are checked before runtime

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

What does weakly typed mean in the context of C?

A

Supports implicit type conversions

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

On what platforms is C available?

A

Pretty much every platform

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

What is a key characteristic of C regarding portability?

A

Very fast

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

What feature does C provide for memory management?

A

Explicit memory management

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

What standards does C adhere to?

A

ANSI/ISO standards

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

True or False: C has runtime error checking.

A

False

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

What type of error handling is lacking in C?

A

Sophisticated exception handling

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

What does GNU stand for?

A

GNU stands for ‘GNU’s Not Unix’

GNU is a free, UNIX-compatible operating system started by Richard Stallman in 1983.

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

What types of software packages does the GNU system include?

A

The GNU system includes various software packages, including:
* Compilers
* Libraries
* Tools

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

What is GCC?

A

GCC stands for GNU Compiler Collection, a powerful compiler that allows compiling different programming languages including:
* C
* C++
* Objective-C
* Fortran
* Java
* Ada
* Go

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

What command is used to compile a C program with GCC?

A

gcc myProgram.c

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

What is the default name of the executable file created by GCC?

A

a.out

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

How can you run the compiled program in Linux?

A

./a.out

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

How can you specify a custom output file name when compiling with GCC?

A

Use the -o option, e.g., gcc myProgram.c -o myProgram

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

What is the purpose of compiling to an object file?

A

Object files are portable and don’t need the full source code. They allow linking multiple object files to create a final executable.

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

What command is used to compile a C program into an object file?

A

gcc -c myProgram.c -o myProgram.o

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

What command is used to link an object file and create an executable?

A

gcc myProgram.o -o myProgram

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

What is the command to compile multiple source files at once?

A

gcc circle.c radius.c -o circle -lm

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

What does the -lm option do when compiling?

A

-lm links the math library, needed for functions like sqrt()

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

What is the first step in compiling multiple files separately before linking?

A

Compile each file separately using gcc -c

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

What is the command to link object files after compiling them separately?

A

gcc circle.o radius.o -o circle -lm

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

What is the solution to the error ‘sqrt not defined’?

A

Always link the math library using -lm.

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

What is the solution to the error ‘radius.o not found’?

A

Make sure all .o files are correctly compiled before linking.

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

What does K&R C refer to?

A

K&R C is the original version of C created by Kernighan & Ritchie.

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

What is ANSI C?

A

ANSI C (C89/C90) is the first standardized version of C, widely used.

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

What version of C was introduced in 1999?

A

C99

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

What is the latest update of the C language as of 2011?

A

C11

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

What is Embedded C?

A

Embedded C is a version designed for small systems (microcontrollers).

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

What integer value does a C program return to indicate success?

A

0

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

What does a C program return to indicate failure or error?

A

Any other number

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

Fill in the blank: When a C program finishes running, it returns an integer value to the _______.

A

operating system

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

Give an example of a return statement indicating success in a C program.

A

return 0;

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

What does ‘weakly typed’ mean in C?

A

C allows implicit type conversions, which can lead to unexpected results.

Example of implicit conversions can cause issues like data loss or overflow.

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

What is a potential issue when converting larger types to smaller types in C?

A

Data loss when converting larger types to smaller types.

This can occur when a larger data type is assigned to a smaller data type, resulting in truncation.

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

What is an example of signed int overflow in C?

A

When unsigned short int x = 65535; is assigned to short int y, y becomes -1.

This occurs because the maximum value of signed short int is 32767.

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

What is the output of printf for variable c when c = x; with x = 65535?

A

Undefined behaviour.

This happens because c is a char, which cannot hold the value 65535.

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

What are the two types of arrays in C?

A

1D arrays and 2D arrays.

Example: int numbers[5] = {1, 2, 3, 4, 5}; for 1D and int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}}; for 2D.

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

What does C not do with respect to arrays?

A

C does not perform bounds checking.

This can lead to accessing out-of-bounds elements, which can cause memory issues.

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

How are strings represented in C?

A

Strings are arrays of characters ending with a null terminator (0).

Example: char greeting[] = ‘Hello!’; automatically includes 0.

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

What does the function strlen(s) do in C?

A

Returns the length of the string (excluding the null terminator).

strlen counts the characters until it hits the null terminator.

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

What is the purpose of srand(seed) in C?

A

Sets a new seed to change the sequence of random numbers.

Using srand with time(0) ensures different sequences on each run.

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

What does the function rand() generate?

A

A pseudo-random integer between 0 and RAND_MAX.

RAND_MAX is a constant defined in stdlib.h.

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

What is the shape of the Normal (Gaussian) Distribution?

A

Follows a bell-curve shape.

This distribution is commonly used in statistics and natural phenomena.

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

What does the GNU Scientific Library (GSL) provide?

A

Better random number generators than rand().

GSL offers more sophisticated methods for generating random numbers.

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

What is the purpose of the function gsl_rng_set in GSL?

A

Sets the seed for the random number generator.

This is important for ensuring reproducibility of random sequences.

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

Fill in the blank: Strings in C are just arrays of characters ending with a ______.

A

null terminator.

The null terminator is represented as 0 in C.

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

True or False: C automatically checks for buffer overflows in arrays.

A

False.

C does not provide any built-in mechanism for bounds checking.

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

What is the output of printf when using sprintf(buffer, “%s %d”, str, num)?

A

Formats and stores output into buffer.

This allows for formatted strings to be stored for later use.

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

What is a one-dimensional array in C?

A

An array that holds a fixed number of elements of the same type, initialized with specific values

Example: Char array[5] = {‘A’, ‘B’, ‘C’, ‘D’, ‘E’};

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

How can the size of an array be defined in C?

A

The size can be implicit and defined by its initialization

Example: Int a[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};

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

What is an uninitialized array in C?

A

An array declared without an initial value, containing random data

Example: Int anArray[10];

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

How is a two-dimensional array initialized in C?

A

An array of arrays initialized with specific values

Example: Int a2[2][3] = { {1,2,3} , {4,5,6} };

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

How do you access and set individual values in an array?

A

Using the index notation, e.g., anArray[0] = 42 or array[3] = ‘Z’;

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

What is a string in C?

A

A string is an array of characters terminated by a null character (0)

Example: Char string[] = ‘Hello World!’;

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

What is the significance of the null terminator in strings?

A

It signals the end of the string to prevent the program from getting stuck.

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

True or False: Strings in C are enclosed in single quotes.

A

False

Strings use double quotes, while characters use single quotes.

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

What are functions in C?

A

Uniquely named groups of program statements that may accept parameters and return values.

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

What is the syntax for defining a function in C?

A

<return> functionName(<parameter>) { /* statements */ return <value>; }
</value></parameter></return>

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

What is the main function requirement in a C program?

A

Every program must include a main() function.

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

What is a procedure in C?

A

A function that does not return a value, defined with void.

67
Q

What is the purpose of function prototypes?

A

To inform the compiler of the function’s signature before it is called.

68
Q

What is the difference between global and local scope?

A

Global scope variables are declared outside functions, while local scope variables exist only within the function.

69
Q

What are static variables in C?

A

Variables that are initialized only once and retain their value between function calls.

70
Q

Fill in the blank: Functions must have a _______ name.

71
Q

What should be avoided unless necessary in function design?

A

Using global variables.

72
Q

What are the key points regarding functions in C?

A
  • Define using a clear structure and unique names
  • Avoid overloading; separate by purpose and return type
  • Use prototypes to declare functions before use
  • Avoid global variables unless justified
  • Utilize static variables for data persistence
73
Q

What fundamentals were covered in this lecture?

A

Defining and using functions, role of prototypes, scope, and static variables.

74
Q

What is stored in memory?

A

Every variable

Memory is a linear array of bytes.

75
Q

What does each memory location have?

A

An address (a numerical label)

This address is used to identify the location of the variable in memory.

76
Q

How many bytes are typically allocated for an integer variable?

A

4 bytes

This is common on many systems.

77
Q

What is a pointer?

A

A variable that stores a memory address instead of a value.

78
Q

How do you declare a pointer to a character?

A

char *p;

This declares p as a pointer to a character.

79
Q

What does the & operator do?

A

Gives the address of a variable.

80
Q

What does the * operator do?

A

Dereferences a pointer to access the value at the stored memory address.

81
Q

What is pointer arithmetic?

A

Performing arithmetic operations on pointers.

82
Q

Fill in the blank: A void pointer (void *) is a _______.

A

Generic pointer that can store addresses of any data type.

83
Q

What function is used for dynamic memory allocation?

84
Q

What should you always check after using malloc()?

A

If malloc() succeeds.

85
Q

What function is used to release allocated memory?

86
Q

What are common pitfalls related to pointers?

A
  • Memory Leaks
  • Dangling Pointers
  • Wild Pointers
87
Q

What is a segmentation fault?

A

Occurs when trying to access memory illegally.

88
Q

What tool can be used for memory debugging?

89
Q

True or False: Pointer arithmetic follows the size of the data type.

90
Q

What happens if you forget to free memory?

A

Memory Leaks occur.

91
Q

What is a dangling pointer?

A

Using a pointer after the memory it points to has been freed.

92
Q

What is a wild pointer?

A

Using uninitialized pointers.

93
Q

What does p++ do if p is a pointer to an int?

A

Moves p to the next int (increases by sizeof(int)).

94
Q

What happens when you write beyond allocated space?

A

It can corrupt adjacent memory.

95
Q

What is the output of printf(“p = %p, *p = %c\n”, p, *p) if p points to ‘A’?

A

p= 0x1e224eff, *p= A

96
Q

Fill in the blank: The function _______ is used to allocate memory for an array of integers.

97
Q

What is Pass by Value?

A

A copy of the variable is passed to the function, keeping the original variable unchanged.

Default in C; can be inefficient for large data structures.

98
Q

What is a downside of Pass by Value?

A

Can be inefficient for large data structures.

It involves copying the entire data, which takes extra memory and processing time.

99
Q

How does Pass by Reference work?

A

A pointer to the variable is passed instead of a copy, allowing the original variable to be modified.

Great for modifying large data structures efficiently.

100
Q

What is a pointer in C?

A

A pointer stores the memory address of another variable.

Syntax example: int *p = &x; where p stores the address of x.

101
Q

What is the syntax to dereference a pointer?

A

Using the asterisk (*) before the pointer variable.

Example: printf(“%d”, *p); prints the value at the address stored in p.

102
Q

What is a function pointer?

A

A function pointer stores the address of a function.

Syntax example: int (*funcPtr)(int, int); for a function taking two ints and returning an int.

103
Q

Why use function pointers?

A

Makes code modular and reusable; enables dynamic function execution.

Useful in scenarios like callbacks and sorting.

104
Q

What is a key advantage of linked lists over arrays?

A

They do not need a predefined size and allow efficient insertions and deletions.

This makes linked lists more flexible for dynamic data storage.

105
Q

What is the basic structure of a linked list node?

A

struct Node { int data; struct Node *next; };

Each node contains data and a pointer to the next node.

106
Q

How do you insert a node at the beginning of a linked list?

A

Create a new node, set its next to the current head, and update the head to the new node.

Example: void insert(struct Node** head, int newData) {…}

107
Q

What is the purpose of the printList function?

A

To traverse and print all the nodes in the linked list.

Example: void printList(struct Node *node) {…}

108
Q

True or False: Pass by reference allows modifying original values.

A

True.

This is done by passing pointers to the function.

109
Q

Fill in the blank: _______ enables dynamic execution of functions.

A

Function pointers.

They allow the program to choose which function to execute at runtime.

110
Q

Final takeaway: What do linked lists provide compared to arrays?

A

More flexibility for dynamic data storage.

Linked lists can grow and shrink in size easily compared to static arrays.

111
Q

What is the purpose of scanf() in C?

A

Takes user input

It is like the reverse of printf()

112
Q

What does scanf() return?

A

Number of values successfully assigned

113
Q

What happens if input fails in scanf()?

A

Exits the loop

114
Q

Define a stream in the context of programming.

A

A way of handling data flow in a program

115
Q

What are the key features of a stream?

A
  • Abstracts file handling
  • Buffered
  • Two Types: Text Streams and Binary Streams
116
Q

What is the function to open a file in C?

117
Q

What is the function to close a file in C?

118
Q

What does a file pointer (FILE *) do?

A

Accesses files in C

119
Q

What does fopen() return if it fails?

120
Q

What mode is used to open a text file for reading?

121
Q

Fill in the blank: To create a binary file for writing, use mode _______.

122
Q

What is the action of the function fputs()?

A

Write a string

123
Q

What does fprintf() do?

A

Writes to a file like printf()

124
Q

What is the purpose of the function fseek()?

A

Jump to a specific position in a file

125
Q

What is the role of errno in error handling?

A

Global variable to check errors

126
Q

What are the three standard streams in C?

A
  • stdin
  • stdout
  • stderr
127
Q

What does stdout represent?

A

Standard output (console)

128
Q

What is the result of the command ‘./myprogram > output.txt 2> errors.txt’?

A

Saves stdout in output.txt and stderr in errors.txt

129
Q

What happens when you reach the end of a file (EOF)?

A

Stops reading

130
Q

What is the summary takeaway regarding file operations?

A

Always check if fopen() worked before reading/writing

131
Q

True or False: The function remove() is used to delete a file.

132
Q

What is the C Preprocessor?

A

Before your code is compiled, it passes through a preprocessor that expands macros and header files, removes comments, allows conditional compilation, and modifies source code before sending it to the compiler.

133
Q

What does the directive #include <file> do?</file>

A

Includes a system header file.

134
Q

What does the directive #include “file” do?

A

Includes a user-defined header file.

135
Q

What is the purpose of #define MACRO?

A

Defines a macro (a shortcut for values or code).

136
Q

What happens when #ifdef MACRO is used?

A

Compiles code only if a macro is defined.

137
Q

What does #ifndef MACRO do?

A

Compiles code only if a macro is NOT defined.

138
Q

What is the function of #undef MACRO?

A

Undefines a macro.

139
Q

What does the #pragma directive provide?

A

Compiler-specific instructions (rarely used).

140
Q

What are system headers and how are they included?

A

System headers are included using angle brackets < >, e.g., #include <stdio.h>.</stdio.h>

141
Q

What are user-defined headers and how are they included?

A

User-defined headers are included using quotes “ “, e.g., #include “myfile.h”.

142
Q

What does #define PI 3.14159 accomplish?

A

Replaces every PI in the code with 3.14159.

143
Q

What is a common pitfall when using macros?

A

Not using parentheses can lead to unexpected results.

144
Q

How can the MAX macro be defined correctly?

A

define MAX(a, b) ((a) > (b) ? (a) : (b))

145
Q

What does stringifying macros do?

A

Converts macro arguments into strings.

146
Q

What is the benefit of structuring code?

A

Makes code easier to read, allows reusability, and improves debugging and maintainability.

147
Q

What are the components of a structured project?

A
  • myProgram.c (Main Program)
  • myProgram.h (Header File)
  • stack.c (Stack Implementation)
  • stack.h (Stack Function Declarations)
148
Q

What do header files contain?

A

Function declarations and constants.

149
Q

What do source files contain?

A

Function definitions.

150
Q

What is the purpose of #ifndef STACK_H?

A

Prevents multiple inclusions (aka include guards).

151
Q

What are the three stages of compilation?

A
  • Preprocessing
  • Compilation
  • Linking
152
Q

What command compiles source files separately?

A

gcc -c stack.c -o stack.o

153
Q

What is the command to link object files?

A

gcc stack.o myProgram.o -o myProgram

154
Q

What is the purpose of a Makefile?

A

Saves time when recompiling large projects, automatically tracks dependencies, and reduces manual errors.

155
Q

What command is used to build a project using a Makefile?

156
Q

What is a library in C?

A

A collection of precompiled functions that can be used in multiple projects.

157
Q

What is the difference between a static library and a shared library?

A
  • Static Library (.a): Linked at compile time (larger executable)
  • Shared Library (.so): Linked at runtime (smaller executable)
158
Q

What command links the math library?

A

gcc myMaths.c -lm -o myMaths

159
Q

What does the -lm flag do?

A

Tells GCC to link the math library.

160
Q

What does the preprocessor do?

A

Expands macros, includes headers, and modifies code.

161
Q

What should you do to prevent errors when using #define?

A

Use #define wisely.

162
Q

What is important to remember about structuring code?

A

Structure code into multiple files (.c and .h).

163
Q

What is the benefit of using Makefiles?

A

Automate compilation.

164
Q

Fill in the blank: A library is a collection of _______.

A

[precompiled functions]