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;
What is *y showing us?
*y is returning the address of the variable y. * denotes that the variable is a pointer. Pointers ‘point’ to the address of the variable that follows.
Are while loops always executed?
No. A while loop has the controlling statement evaluated first so there are instances where the statement in the loop may never be triggered.
What happens if we put a switch statement within a while loop and a break occurs?
The break will only break out of the switch not the while loop. Break statements only break out of the brackets to one level up.
Write a for loop that prints out the numbers 2,…..,40 in multiples of 2.
for(i = 2, i <=40, i+= 2)
{
printf(“%d”, i);
}
How and why would you write an inifinite loop?
while(1)
An we might use an infinite loop to read input to a program.
Why would we use a do loop over a while loop?
do loops check the controlling statement after executing one time. Therefore they are useful if a code needs to be executed at least once, such as to initialise values. If you use a while loop the code can sometimes never be executed.
Write a for loop that prints every number from 1 to 15.
while(int i = 1, i <=15, i++)
{ printf(“%d”, i); }
What is the difference between break and continue?
break exits a loop.
continue skips the code that follows and goes back to the start of the loop
What are the three datastreams?
STDIN (0) - data into program
STDOUT (1) - data out of program
STDERR (2) - standard error message
How can we add the word ‘dog’ to the end of a text file called ‘pets.txt’?
We would use redirection:
dog»_space; pets.txt
What is piping and why is it useful?
Piping allows us to feed data from one program into another using the ‘|’ symbol. It is useful for getting two programs to interact. We use format:
program1.exe | program2.exe
Where do we define a function?
Define a function after the main() call.
Where do we call a function?
Functions are usually called in the main.
Does passing an argument by value change it’s value? Why/Why not?
Passing an argument by value doesn’t change the variables value. It only creates a local copy and changes that.
Passing an argument by reference includes it’s address so the function can reach the variable and change it’s value.
What is the difference between local and global variables?
Local variables
- Declared in the body of a function.
- Not visible outside the function
- Only exist during function execution.
Global variables
- Declared outside function bodies.
- Visible to all functions in the file.
- Allocated storage for the whole duration of the program.