C Flashcards
Recall the basic structure of a C program
int main function
How to make a comment in C
//
What’s the formal name for fixed data (values) that we can use directly in a C program?
literals
What are the data types in C (literals) - give examples
Basic data types (refer to image)
arrays
string (i.e. array of characters)
How do we declare variables in C?
Depends on the data type of the variable we want to declare
(1) Integer Literals
int age;
(2) Floating-point Literals
double salary;
(3) Character Literals
char letter;
How do we properly end a C statement?
Use semi-colon ;
How do we print
printf(“”);
prints anything present inside the quotation marks
“%d”, age
prints anything present inside the quotation mark
What is %d in this C statement?
printf(“%d”, age);
%d - format specifier for int data types
value of age replaces %d
What are the respective format specifiers for each of the literal data types?
(1) Integer Literals
%d
(2) Floating-point Literals
%lf
(‘L’ f not 1)
(3) Character Literals
%c
How do you print this:
How many characters can the character variable store?
a single character inside single quotes
char letter = ‘s’;
NOT char letter = “s”;
How do we change the value stored in a variable?
E.g. if we initially declare int age = 25 but want to change age to 35
age = 35;
When we change a value stored in a variable, what should we note?
The new value must match the data type we initially declared for that variable.
How do you name variables made up of 2 words?
camelCase
Give some examples of illegal variable names
❌ spaces
❌ use of symbols (except alphabets, numbers and underscore)
❌ variable name starts with a number
❌ names already part of C syntax e.g. if, else etc
How can we create multiple variables together in a single line?
What do pointers allow us to do?
Work with memory addresses
After creating and storing value in variable, how can we access the memory address of that value?
e.g.
int var = 13;
&var
What is a pointer?
Use variable ‘pt’ as an example
a special symbol that stores the address of a variable rather than a value
int* pt;
How can you assign an address to a Pointer?
Use int number = 36; as example
Label the different parts of this C function
What are some assignment operators?
=
+= (same as accumulator pattern in Python)
-=
*=
/=
%=
How to limit the number of 0s when formatting floating points?
printf(“%.2lf”)
2 is the number of zeros we want to pad out the floating point with
What result is returned when we use division operator on int and floating points?
e.g. 12 / 8
12.0 / 8.0
Integers - returns only the quotient (1)
Floating - returns exact result (1.50)
What are the increment and decrement operators?
increment: ++
decrement: - -
How would you use increment/decrement operators on say variable ‘var’?
It is recommended in COMP9024 to put the operator after var
var++
var–
What data type can be used with increment and decrement operators?
only integers
What is precedence and associativity?
Operators with higher precedence are executed first and operators with lower precedence are executed last
(), * and / are calculated first before + and -
How can we find out how much storage each data is occupying in the memory?
Use sizeof(variable) operator
How do we print sizeof() calculations?
Use format specifier “%zu”
What is the measurement does sizeof() return?
Bytes
What kinds of type conversions are there?
(1) Implicit Type Conversion
(2) Explicit Type Conversion - manually convert one type to another
What happens if we try to assign a double value to an int variable?
E.g.
int number = 8;
number = 35.48
We’ll get only 35
Double values are larger (8 bytes) than integer values (4 bytes) so we’ll lose data