Lecture 5 - Introduction to C programming Flashcards
What does it mean that C is a procedural language?
Programmers think in terms of operations to be done and supply data for the operation
add(a, b)
In object-oriented languages programmers think of the objects to be operated on and then what operation to be performed
rect.area()
What are the built-in data types in C
char (8 bit)
short (16)
int (16, 32, 64)
long (32, 64)
float (32)
double (64)
What is the syntax when declaring a pointer?
int *p;
How do you put an address in a pointer variable?
p = &a;
&a: gives address of a
a must be the same type as the pointer-type
How to change a value pointed to by a pointer?
p = &a
*p = 5
The last line changes the value of a
What is two’s complement?
Example using negative short:
short a = -65;
65 = 00000000 01000001
Two’s complement:
- flip all the bits
- 11111111 10111110
- Add 1 to the flipped number
- 11111111 10111111
- This is the two complement of a
What is two’s complement used for in C memory mapping
When having negative values (for example a negative short), it is represented as the two’s complement of the positive value.
This way the signed bit is 1, indicating a negative value. We get the magnitude of the number by taking the two’s complement again.
What happens if a short is assigned to an int?
As the short is only 8 bits, to keep the sign-bit the same, the value is sign extended.
Copy the upper bits to the rest of the higher order bits in the int
What happens if an integer (32 bits) is assigned to a short (16 bits)?
If the integer is large enough to take more than 16 bits, only the 16 LSB are copied to the short, and the higher order bits are discarded.
How many bits are used to hold pointer values?
Enough bits to represent the addresses. These are the same across all pointer-types, as the addresses of ints are just as large as addresses of pointers
What does the types of the pointers do?
When writing to a value pointed to by a pointer, the pointer type tells the program how many bits to represent this value in.
short *p;
*p = 65;
Here we know 65 needs to be represented in 16 bits
int *p
*p = 65
Here 65 would be represented by i.e 32 bits
How can you cast pointers to point to different data types?
int a;
short p = (short) &a
What is the difference between these to statements:
int a = 65
short b = a
int a = 65
short b = (short) &a
In the first, the lower bytes of a is copied to b
In the second, the 2 higher order bytes are copied to b
How is structs declared?
struct structName {
int x, y;
} variableName;
struct structName varName;
How do you access struct values?
Struct variable:
s.x
Struct pointer:
s->