Lecture 2 - Primitive Types and Basic Constructs Flashcards
What are the primitive data types in C?
char: Character type, 8 bits.
short: Short integer, typically 16 bits.
int: Integer, at least 16 bits.
long: Long integer, at least 32 bits.
float: Single precision floating-point, around 6 decimal places.
double: Double precision floating-point, around 10 decimal places.
How are signed and unsigned types different in C?
Signed types can represent both positive and negative numbers, while unsigned types can only represent non-negative numbers.
What is sizeof()
in C, and how is it used?
sizeof()
is an operator that returns the size (in bytes) of a data type or variable. For example, sizeof(int)
returns the number of bytes used by an integer.
What is the purpose of the typedef
keyword in C?
typedef
is used to create an alias for a data type, making the code more readable and easier to maintain.
How are Boolean values represented in C?
In C, Boolean values are represented using integers. Zero (0
) is considered false
, and any non-zero value is considered true
.
What are some common C operators and their functions?
+
: Addition-
: Subtraction*
: Multiplication/
: Division%
: Modulus (remainder)
What are the key differences in handling Booleans between C and Java?
Java has a dedicated boolean
type (true
or false
), while C uses integers (zero for false
, non-zero for true
).
What is the purpose of relational operators in C?
Relational operators are used to compare two values. Examples include >
, <
, >=
, <=
, ==
, and !=
.
How do selection constructs like if
and else work in C?
The if
construct executes a block of code if a condition evaluates to true. The else
block is executed if the condition evaluates to false.
if (condition) { // true block } else { // false block }
What is the syntax for a for
loop in C?
for (initialization; condition; increment/decrement) { // loop body }
Example
~~~
for (int i = 0; i < 10; i++) {
printf(ā%d\nā, i);
}
~~~
What is the switch
statement in C?
The switch
statement is used for multi-way branching based on the value of a variable. It replaces multiple if-else
statements and works with int
and char
types.
switch (variable) { case 1: // code for case 1 break; case 2: // code for case 2 break; default: // default case }
What is the difference between break
and continue
in loops?
break: Exits the loop entirely.
continue: Skips the rest of the current loop iteration and continues with the next one.
What is a do-while
loop, and how is it different from a while
loop?
A do-while
loop guarantees that the loop body will execute at least once because the condition is checked after executing the body.
do { // code } while (condition);
How does C handle type casting?
C allows both implicit and explicit type casting. Explicit type casting is done using the cast operator:
int x = (int) 5.67; // x becomes 5
What are compound assignment operators in C?
Compound assignment operators combine an operation with assignment. Examples include +=
, -=
, *=
, /=
, and %=
.