C Programming Flashcards

1
Q

Does C have keywords?

A

Yes but it only has 32 unlike other languages that have hundreds.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does C identify what is a function?

A
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;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What command would you use to compile a C program called hey.c to hello.exe?

A

gcc hello.c -o hello.exe

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why does C need stdio.h?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What command would you use to run a program called hello.exe?

A

You would navigate to it’s directory in Cygwin and use:

./hello.exe

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How would you declare a constant long float variable called pi with the value 3.1415926535?

A

const long float pi = 3.1415926535;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
With the variables:
int age = 27;
char bang = !;
How would you print the statement:
"I am going to be 27 years old this year!"
A

The command you would use is:

printf(“I am going to be %d years old this year%c”, age, bang);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
What are the format specifiers of:
int:
char:
float:
double:
string:
A

int: %d
char: %c
float: %f
double: %lf
string: %s

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
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
A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How would you receive an integer input and store it in a variable called age?

A

int age;

scanf(%d, &age);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Use the binary sequence x = 0100 0110 for the following and calculate the new binary value:
x &laquo_space;2;
x&raquo_space; 2;

A

x = 0100 0110
x &laquo_space;2 = 0001 1000
x&raquo_space; 2 = 0001 0001

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Write an if statement that prints out a users tax percentage based on the 2021 ATO standards and their income.

A

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");
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

C doesn’t have booleans as a data type. How do we get around this?

A
We can define true and false at the start of the code.
E.g
#define TRUE 1
#define FALSE 0
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are 4 things all switch statements need?

A
  1. A controlling expression e.g switch(job)
  2. A case label e.g case ‘Carer’:
  3. Statements e.g do_this;
  4. A break keyword e.g break;

All together:

switch(job){

case ‘Carer’ :
{_do_this;}
break;

case ‘Boxer’:
{_do_that;}
break;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What would we use to find the address of the first byte of int variable t?

A

int t;

&t;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is *y showing us?

A

*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.

17
Q

Are while loops always executed?

A

No. A while loop has the controlling statement evaluated first so there are instances where the statement in the loop may never be triggered.

18
Q

What happens if we put a switch statement within a while loop and a break occurs?

A

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.

19
Q

Write a for loop that prints out the numbers 2,…..,40 in multiples of 2.

A

for(i = 2, i <=40, i+= 2)
{
printf(“%d”, i);
}

20
Q

How and why would you write an inifinite loop?

A

while(1)

An we might use an infinite loop to read input to a program.

21
Q

Why would we use a do loop over a while loop?

A

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.

22
Q

Write a for loop that prints every number from 1 to 15.

A

while(int i = 1, i <=15, i++)

{ printf(“%d”, i); }

23
Q

What is the difference between break and continue?

A

break exits a loop.

continue skips the code that follows and goes back to the start of the loop

24
Q

What are the three datastreams?

A

STDIN (0) - data into program
STDOUT (1) - data out of program
STDERR (2) - standard error message

25
Q

How can we add the word ‘dog’ to the end of a text file called ‘pets.txt’?

A

We would use redirection:

dog&raquo_space; pets.txt

26
Q

What is piping and why is it useful?

A

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

27
Q

Where do we define a function?

A

Define a function after the main() call.

28
Q

Where do we call a function?

A

Functions are usually called in the main.

29
Q

Does passing an argument by value change it’s value? Why/Why not?

A

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.

30
Q

What is the difference between local and global variables?

A

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.