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
How do we manually convert one data type to another?
What function allows us to take input from a user? How does it do that?
scanf()
e.g. scanf(“%d”, &age);
%d - format specifier that represents the type of input data
&age - represents the memory address of the age variable
The scanf() function takes int input (specified by %d) from the user and stores it in the memory location of the age variable.
How can use booleans in our code in C?
import true and false using #include <stdbool.h></stdbool.h>
create boolean using bool
In C, boolean values are represented in terms of which integer values?
true = any values except 0
false = 0
What format specifier do we use to represent boolean value?
%d
How do we write a boolean statement in C comparing
int age = 25;
Does age equal 25?
Does age not equal 25?
Name the 3 logical operators in C programming
&& - AND
(age > 17) && (salary > 4000)
|| - OR
(age > 17) || (salary > 4000)
! - NOT
!(age > 17)
How are if..else statements written in C?
How are if..else if .. else statements executed in C?
Create an array numbers containing integers 1, 2, 3, 4, 5
How to access first element of this array?
int numbers[5] = {1, 2, 3, 4, 5};
numbers[0];
or more generally, numbers[index]
How do you write a switch statement?
What does the while loop do?
repeat a block of code until the boolean expression of the loop is false
What does the for loop do? Identify parts of a for loop
run a block of code for a certain number of times
while vs for
Based on my experience with Python problems, it seems while
loops are usually used to make an infinite loop and is best for a changing data source. We can specify when to break the loop and restart it from the top
A for
loop hardcodes conditions which is maybe more suited for an unchanging data source
Can we omit an array size?
Yes, C will automatically keep track of it
int array[] = {1, 2, 3, 4, 5};
What happens if you assign Less Elements Than Size of the Array?
The remaining spaces will be filled with a random number
What happens if we do this?
declare array first then initialize all elements together after declaration
We’re not allowed to do this
How do we print something in C?
What is the output of this print code?
The cube of 3 is 27.
The first %d is for the arg1
the second %d is arg2
How to code this output in C?
Your “login ID” will be in the form of z1234567.
recall how to write a do … while loop
do … while vs while loop?
do … while ensures the code body will be run before checking the condition
while loop code body will only run if the condition is true
what metaphor can we relate the while
and do ... while
loop to?
Fixing equipment
I fix equipment, then check whether it’s actually fixed.
If it’s not fixed, I continue fixing
While
Is this device safe to use?
If a tool is safe to use I continue using it
return-type functionName(void) {
// code block
}
what does void in the argument mean?
function has no arguments
float vs double data types
float is a lower precision decimal number (less number of digits to the right of the decimal place), double is precise
what is this called (at the beginning of a C program)?
symbolic constant
what is a symbolic constant
typically defined at the top of a C program where the variable (MAX
) is followed by associated value (20
)
what happens when the symbolic constant is read by the compiler’s pre-processor?
all occurrences of variable is replaced by associated value
when will the symbolic constant NOT be replaced?
(1) occurs inside string
(2) as part of another name
when would it be useful to use symbolic constants
- avoid burying “magic numbers” in the code (slide 58)
- make the code easier to understand and maintain (slide 57)
what styles would be useful in COMP9024
Style considerations that do matter for your COMP9024 assignments:
- consistent indentation (3 spaces throughout, or 4 spaces throughout, do not use TABs)
- keep functions short and break into sub-functions as required
- use meaningful names (for variables, functions etc)
- use symbolic constants to avoid burying “magic numbers” in the code
- comment your code “as much as possible”
how many spaces would the string “hello” take up in memory? why?
6 because the last character is the end of string delimiter \0
what are 2 different ways we can initialise a string array?
how do we use the construct atoi()?
include <stdlib.h></stdlib.h>
say it as “a to i”
n = atoi(str)
what does atoi() do?
converts string to integer to use in computation
what does typedef do?
allows us to give meaningful names to data types
what are 2 ways to initialise multi-dimensional arrays?
What is a structure in C?
collection of variables (can be of different types) under a single name
how do we define a structure?
use struct
How do we access components of the structure?
Using dot operator
Person.name
When we defining a structured data type, is any memory allocated?
No
After we’ve defined a structured data type, how do we allocate memory?
Declare a variable
How can we use a structure that’s passed as a parameter to a function?
E.g. a function that prints the date using DateT structure
When we define a struct, we are basically creating a ___
data type like double and int
What are two ways we can create struct variables?
Is this valid code to create struct variables?
Why/why not?
With typedef
keyword, person1
and person2
become aliases instead of struct variables
How can you create struct variables using typedef
What does typedef do?
Creates an alias name for struct
Why do we use structs?
if we have to store multiple attributes of related data, structs make our code easier to write and understand, improves readability