C Programming Flashcards
Does C have keywords?
Yes but it only has 32 unlike other languages that have hundreds.
How does C identify what is a function?
If a line of code follows the format: functionReturnType FunctionIdentifier (argument) {} C will recongnise it as a function e.g: int main (void) { do_this; }
What command would you use to compile a C program called hey.c to hello.exe?
gcc hello.c -o hello.exe
Why does C need stdio.h?
stdio.h is the library for all the standard input and output functions in C. Without it the program cannot receive input or display output.
What command would you use to run a program called hello.exe?
You would navigate to it’s directory in Cygwin and use:
./hello.exe
How would you declare a constant long float variable called pi with the value 3.1415926535?
const long float pi = 3.1415926535;
With the variables: int age = 27; char bang = !; How would you print the statement: "I am going to be 27 years old this year!"
The command you would use is:
printf(“I am going to be %d years old this year%c”, age, bang);
What are the format specifiers of: int: char: float: double: string:
int: %d
char: %c
float: %f
double: %lf
string: %s
Fill out the table of bitwise operators for the following: P Q P&Q P|Q P^Q 0 0 0 1 1 1 1 0
P Q 0 0 0 1 1 1 1 0
P&Q 0 0 1 0
P|Q 0 1 1 1
P^Q 0 1 0 1
How would you receive an integer input and store it in a variable called age?
int age;
scanf(%d, &age);
Use the binary sequence x = 0100 0110 for the following and calculate the new binary value:
x «_space;2;
x»_space; 2;
x = 0100 0110
x «_space;2 = 0001 1000
x»_space; 2 = 0001 0001
Write an if statement that prints out a users tax percentage based on the 2021 ATO standards and their income.
int income;
if(income < 18200) { printf("You don't have to pay tax!"); } else if(income >= 18200 && income < 45000) { printf("You pay 19% tax"); } else if(income >= 45000 && income < 120000) { printf("You pay 32.5% tax"); } else if(income >= 120000 && income < 180000) { printf("You pay 37% tax"); } else { printf("You pay 45% tax"); }
C doesn’t have booleans as a data type. How do we get around this?
We can define true and false at the start of the code. E.g #define TRUE 1 #define FALSE 0
What are 4 things all switch statements need?
- A controlling expression e.g switch(job)
- A case label e.g case ‘Carer’:
- Statements e.g do_this;
- A break keyword e.g break;
All together:
switch(job){
case ‘Carer’ :
{_do_this;}
break;
case ‘Boxer’:
{_do_that;}
break;
What would we use to find the address of the first byte of int variable t?
int t;
&t;