Introduction to Programming with C Flashcards

(81 cards)

1
Q

What kind of language is C described as in this lecture?

A

A high-level structured programming language often used for microcontroller applications.

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

What three main things does this lecture focus on?

A

Good coding practices, C operators, and flow control structures.

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

What are the main sections of the lecture?

A

C-language basics (formatting, comments, preprocessor, statements, variables, operators) and flow control (if, switch, while, for).

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

Why is code formatting important?

A

It improves readability, maintainability, and makes programs easier to understand.

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

Name three good code formatting practices.

A

Use descriptive names, indent nested code blocks, delete redundant code.

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

What does “each function/class should have one purpose” mean?

A

Each function should do one clear job, not many unrelated things.

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

Why is readability more important than cleverness in code?

A

Readable code is easier to debug, maintain, and understand by others.

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

What is refactoring?

A

Rewriting existing code to improve structure and readability without changing behaviour.

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

Why should you use version control and backups?

A

To keep track of changes and avoid losing work.

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

What are comments in C used for?

A

To explain what the program or specific code blocks do, for future readers.

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

Are comments compiled into machine code?

A

No, the compiler ignores comments.

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

How do you write a multi-line comment in C?

A

Between /* and */.

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

How do you write a single-line comment in C?

A

Start the line or part of the line with //.

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

Give an example of a single-line comment in C.

A

A = 10; // This line sets variable A to 10

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

What is the C pre-processor?

A

A tool that runs before compilation and processes lines starting with #.

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

What does the #include directive do?

A

It includes the contents of a header file into the source file.

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

How is #include used for system header files?

A

include <header_file.h></header_file.h>

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

How is #include used for user-defined header files?

A

include “my_library.h”

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

What does the #define directive do?

A

It defines a macro so that a name is replaced by a value or code snippet.

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

Give an example of #define for a constant value.

A

define pi 3.1415

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

Give an example of #define for a macro function.

A

define cube(x) (x) * (x) * (x)

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

What is a statement in C?

A

A complete instruction ending with a semicolon.

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

What are operators and operands?

A

Operators represent actions; operands are the data they act on.

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

What is a function in C?

A

A block of code that can take inputs (arguments) and optionally return a value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What does the keyword void mean in a function declaration?
The function takes no arguments or returns no value (depending on position).
26
What is a variable in C?
A name that refers to a memory address holding a value.
27
Give an example of using variables to calculate a total.
num1 = 10; num2 = 20; total = num1 + num2;
28
What are the main categories of operators listed in the lecture?
Data transfer, arithmetic, logical, bitwise logical, relational, program control.
29
In C, how are most data transfer operations done?
Using assignment statements.
30
Give a simple example of a data transfer sequence.
a = 10; b = 20; sum = a + b;
31
What rule defines the order of arithmetic operations?
BODMAS or BIDMAS (brackets, orders, division/multiplication, addition/subtraction).
32
List the basic arithmetic operators in C.
+, -, *, /, %, ++, --.
33
What does the % operator do?
Gives the remainder (modulus) of integer division.
34
What do ++ and -- do?
Increment and decrement a variable by 1.
35
Why can brackets be useful in arithmetic expressions?
They clarify the intended order of operations and avoid mistakes.
36
What happens if integer division is used, like 5 / 3 stored in an int?
It stores only the whole number part (e.g. 1).
37
Why is 5.5 % 2 invalid in C?
The modulus operator % only works with integers.
38
What is the result of 5.5 / 2 stored in a float?
2.75 (correct fractional result).
39
What do logical operators return in C?
1 for true and 0 for false.
40
What are the logical operators in C?
&& (and), || (or), ! (not).
41
When is (A && B) true?
Only when both A and B are true.
42
When is (A || B) true?
When at least one of A or B is true.
43
What does !A do?
It flips the truth value of A (true becomes false, false becomes true).
44
What do bitwise logical operators operate on?
Individual bits of the operands.
45
List the bitwise logical operators in C.
&, |, ^, ~, <<, >>.
46
What does & (bitwise AND) do?
Produces 1 in a bit position only if both bits are 1.
47
What does | (bitwise OR) do?
Produces 1 in a bit position if either bit is 1.
48
What does ^ (bitwise XOR) do?
Produces 1 in a bit position if the bits are different.
49
What does ~ (bitwise NOT) do?
Inverts all bits (0 becomes 1, 1 becomes 0).
50
What do << and >> do?
Left shift and right shift bits.
51
Why are bitwise operators useful for I/O ports?
They allow setting, clearing, and toggling specific bits in port registers.
52
How can XOR be used to toggle all bits of a port?
portA = portA ^ 0xFF;
53
What are relational operators used for?
To compare values and produce a true or false result.
54
List the relational operators in C.
==, !=, <, <=, >, >=.
55
What does == mean in C?
Is equal to (comparison).
56
What does = mean in C?
Assignment (store a value in a variable).
57
What group of operators is “discussed in Section 3”?
Program control operators (flow control structures).
58
What are flow control statements used for?
To control the order in which statements are executed.
59
Which flow control structures are covered in the lecture?
if, if-else, if-elseif-else, switch, while, do-while, for, nested for.
60
What does an if statement do?
Executes a block of code only if a condition is true.
61
In C, what values are considered “true” in an if expression?
Any non-zero value.
62
What is the basic form of an if statement in C?
if (expression) { statements; }
63
What extra part does an if-else statement add?
An alternative block that runs if the condition is false.
64
What does an if-elseif-else chain allow you to do?
Test multiple conditions in sequence and choose one of several blocks.
65
Why might you use a switch statement instead of many if-else statements?
It can be clearer and easier to read when selecting between many constant cases.
66
What are the “cases” in a switch statement?
The possible constant values that the expression might match.
67
What is the default case in a switch statement?
The block that runs if none of the cases match.
68
Why is break usually used inside each case?
To prevent fall-through to the next case.
69
What type of values can case labels use?
Constant expressions such as numbers or characters.
70
What does a while statement do?
Repeatedly executes a block as long as a condition is true.
71
When is the condition tested in a while loop?
Before each iteration.
72
How is a while loop written in C?
while (expression) { statements; }
73
How is a do-while loop different from a while loop?
The condition is checked after the loop body, so it always runs at least once.
74
How is a do-while loop written in C?
do { statements; } while (expression);
75
What is a for loop used for?
Repeating a block of code a known number of times with a loop counter.
76
What are the three parts of a for loop header?
Initialization, condition, increment.
77
How is a typical for loop written in C?
for (init; condition; increment) { statements; }
78
How can a for loop always be rewritten?
As an equivalent while loop.
79
What is a nested for loop?
A for loop written inside another for loop.
80
What can nested loops be used for?
Multi-dimensional counting or repeated operations, like minutes and seconds.
81
What are the three main things summarised at the end of the lecture?
Clear coding and comments, operator categories, and C flow control statements with their flowchart representations.