C Flashcards

1
Q

How do you start all c programs?

A
#include 
int main(void)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a computer?

A

Any device with the following things:
INPUT/OUTPUT to communicate with the outside world
MEMORY to record data and programs
CPU to carry out instructions.

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

What is an embedded computer?

A

One designed to do only one thing, like in a ATM

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

What is the clock in a computer?

A

Clock – this is a high frequency square wave which is an alternating pulse of zeros and ones. These usually operate at GHz frequencies for desktop and laptops, and at MHz frequencies for embedded computers.

The clock is required by all units inside a computer to ensure that all their operations are synchronised

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

What is a computer bus?

A

Bus – a bus is a collection of wires which connects any two components, such
as the memory and the CPU. If a bus mainly carries data, it is the data bus and
if it carries memory addresses, then it is the address bus.

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

What is a bit?

A

The fundamental unit of information in computing is a bit.
A bit indicates the presence of a high or low voltage on a bus. High-voltage values are given the bit value of ‘1’, low-voltage values have a bit value of ‘0’.

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

How are bytes arranged?

A

Contain 8 bits.

128 on the left (bit 7) and 1 on the right (bit 0)

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

How are negative numbers represented with bytes?

A

Declare and use a sign variable. Bit 7 is worth -128

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

How much in a kilobyte?

A

2^10 bytes. 2^20 in a megabyte, 2^30 in a gigabyte and so on.

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

How are hexadecimal values used in c?

A

int q = 0x102;
begin each number with 0x
printf and scanf both use the format “%x” to enter or display hexadecimal values

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

What is a memory location?

A

A unique location which is the address and o 1 byte of data.

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

What is a word?

A

A group of bytes put together in memory when 1 isn’t big enough. Usually in groups of 2,4 8…

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

What does & do with memory?

A

The ‘&;’ operator is the “address-of” operator. This takes any variable and returns the memory address at which that variable is stored.

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

What does * do with memory?

A

The ‘*’ operator is the “contents-of” operator. This takes a memory address and returns the contents at that memory location.

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

What is the form of a text file?

A

A text file stores all of the data as “ASCII” characters and then stores the binary representation of each ASCII character.
The number 100000 would take up 6 places.

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

What is portability?

A

This means that the data within a file is readable on all kinds of computers and operating systems.

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

Which of text or binary files are human readable?

A

text

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

Which is larger when storing the same amount of data?

A

text

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

What is one of the disadvantages of binary files?

A

Binary files are (in general) non-portable – binary files created using C under one operating system can’t be automatically read on another operating system, unless special procedures are used.
Binary files can only be made portable if we use an internationally-defined standard file format.

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

Give some examples of text and binary files?

A

Examples of text files: .c files, .txt files, .html files, emails.
Examples of binary files - .mp3 files, .gif files, .wav files, .exe files.

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

what are the four data types?

A

int, float, char and double

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

When printing values in printf what are the letters needed to place them?

A

%d for int
%f for float
%c for character
%e for double

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

What must you not do with comments?

A

nest them

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

What does \t mean in a printf?

A

A tab

A small gap in the line

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

What makes a good variable name?

A

Must start with _ or character.

Can have uppercase lowercase or numbers

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

How do you do comments?

A

/* */

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

how would you change the number of decimal places displayed in printf?

A

for a double using %.2e would give it to 2 decimal places

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

What is the casting of variables?

A

Changing their data type.
If you wanted to perform floating point division with integers
float answer
answer=(float)value1/(float)value2;

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

What is a debugger?

A

A program within a compiler that pauses a program and analyses what’s going on.

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

What does ++ do?

A

Increment. Add 1 to the value. Whether it is in front of a variable or after it matters.

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

What is the modulus operator?

A

answer=20%3;

Would return a value of 2 as it gives the remainder when 20 is divided by 3.

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

What does

time*=hours do?

A

time=time*hours

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

What needs to happen for you to use more complex maths functions?

A

include header

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

How do you determine the square root of a number

A

sqrt(x)

math header needed

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

symbol for AND

A

&&

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

Symbol for OR

A

||

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

Symbol for NOT

A

!

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

Symbol for Exclusive-OR

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

How are powers evaluated?

A

pow(x,3) gives xxx

don’t use ^

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

What are the evaluaters used with scanf to enter different variables?

A

%lf or %le for doubles
%f for float
%d for integer

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

How does #define work?

A
Used to make small functions or values
#define square(x) x*x
#define absolute_zero -273
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

What does != mean?

A

Not equal to

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

What does exclusive-or denote ?

A

one can happen, or the other but not both

44
Q

How would you add initial values an array when you create it?

A

Use [ ]

int x, values[]={10,20,30,40,50,60,70,80};

45
Q

How would you initialise a 2 dimensional array?

A

int multi[3][5];

This gives 3 columns and 5 rows.

46
Q

what does strlen do?

A

Measure the length of a string

47
Q

What is the header file needed for string functions?

A

include

48
Q

what does strcpy (string1,string2)?

A

puts the contents of string 2 into string 1

49
Q

what does s=strcmp(str1, str2); do?

A

Purpose: Detects whether two strings are the same.
Returns: (s==0) if the two strings are identical,
(s<0) if str1 is alphabetically lower than str2
(s>0) if str1 is alphabetically greater than str2.

50
Q

t=strstr(str1, str2);

What does this do?

A

Purpose: Detects whether str2 occurs within str1 (e.g. house and us).
Returns: (t==0) if str2 does not occur within str1,
(t!=0) if str2 does occur within str1.
Remember to declare t as: int *t;

51
Q

What modifier to used to output a string with printf?

A

%s

52
Q

How do you input a string with a scanf?

A

char string [20];
scanf(“%s”,string);
Notice that a & is not needed.

53
Q

At the start of a function, what would a void at the start mean?

A

No value is returned to the program.

Other wise the data type that is being returned would be put there.

54
Q

Why when using an array with a function is it different to using an ordinary variable?

A

Because the array used in the main program and the function is the same and not just a copy.

55
Q

What is the difference between local and global variables?

A

Exist only inside the function that created them

56
Q

What are global variables?

A

Created once and can be accessed by any function comprising the program.
They aren’t in any main program, there aren’t present in {}
Difficult to debug and to use when many programmers are working on the same program

57
Q

What is a function prototype?

A

Write the function definition before the main program and then repeat it and the actual function at the end of the main program, so the main program knows that it exists. The wording of the function can be in a different file even.

58
Q

What is an algorithm?

A

Any sequence of instructions to produce any results or to perform any operations.

59
Q

How would you initialise a file pointer?

A

To declare a pointer with name in_file

FILE* in_file;

60
Q

How would you associate a file with a pointer?

A
in_file = fopen("my_file.dat", "r");
in_file is the pointer name
fopen is the function
my_file.dat is the file 
r is what you want to do with the file
61
Q

What are the different permissions you can use with fopen?

A

“r” – Opens an existing text file for reading starting at the beginning of the file.
“w” – If the file does not exist, it creates a new text file for writing, - it deletes
the current file if it already exists.
“a” – Create a new text file for writing, or write at the end of an existing one.
A “b” may also be used in combination with these access modes (eg. “rb”,
“wb”, “ab”) which indicates that a binary file is to be opened.

62
Q

How and why do you close a file?

A

Needs to be closed to put the changes you made onto the disk.
fclose(in_file); closes the file with the pointer in_file

63
Q

How would you extract a character from a file?

A
Use the function getc. This gives 1 character. To get more use a loop.
int ch;
FILE *in_file;
.
.
ch=getc(in_file);
64
Q

How would you read a file character by character?

A
int ch;
while ((ch = getc(in_file)) != EOF)
{
......
}
When it gets to the end of the file getc will return EOF to show the end
65
Q

How is a character put into a file?

A
int ch;
FILE *out_file;
.
.
putc(ch,out_file);
Here putc has put a single character into out_file
66
Q

What does fgets() do?

A

The function fgets() reads a string of characters from a file into a character array until a newline character is read, an end of file is reached, or until n
characters have been read.

67
Q

How do you use fgets()?

A
char buffer [100];
int n;
FILE* in_file
.
.
fgets(buffer,n,in_file);
for long line n is the size of the blocks it reads it in
68
Q

What do fprintf and fscanf do?

A

Work in the same way as printf and scanf but they input and output with files.
Work with text not binary

69
Q

What is the form of the do…while loop?

A

do
{

} while (…..);

70
Q
int a,b;
double c;
	b=5;
	c=a/b;
If a was 20, 23 or 25, what would c be?
A

4, 4 and 5.

It will always round down.

71
Q
int a,b;
	double c;
	a=24;
	b=5;
	c=(float)a/(float)b;
What will c be?
A

4.8000000

72
Q

Consider the following statement:
a = b++;
What does it do?

A

Copies b to a, then adds one to b

73
Q

What does a header file such as math.h mostly contain?

A

Prototypes

74
Q

When might you use a switch statement?

A

When you want to do the work of many if…else clauses.

75
Q

What is the form of the switch statement?

A
scanf("%d",&amp;menu);
switch (menu)
{
case 1: result=number1+number2;
break;
case 2: result=number1-number2;
break;
case 3: result=number1*number2;
break;
default: printf("Invalid operator \n”);
result = 0;
break;
}
Only integers and characters can be used for cases. The default clause is optional
76
Q

Can fwrite() be used to right binary files?

A

Yes

77
Q

What can == compare?

A

Only char and int.

Same for !=

78
Q

How many bytes to an integer?

A

32 bit so 4 bytes, meaning 4 address are needed.

79
Q

What is the size of the float?

A

32 bits, range of ±10^+38

Typically 3 d.p

80
Q

What is the size of the double?

A

64 bits, range of ±10^+308

Typically 9 d.p

81
Q

How to you check if doubles and floats are equal to each other?

A

They might have very small differences e.g. be 8.0000000001 or 7.9999999999. Thus do
if (fabs(a-b) < DIFF) /* If a and b are equal */
In the above program, two floating point values are regarded as equal if their
difference is less than 10^-8

82
Q

What is long int?

A

32 bit integer with a range of 0-2^32 (unsigned long int) or ±2^31 (signed long int)

83
Q

What is short int?

A

16 bit integer (same pattern as long int)

84
Q

What is the nature of the char datatype?

A

The char datatype is used to represent characters, but we can also use it to store any 8-bit value:

85
Q

What are the bitwise operators?

A
&amp; and
|   or
^ excusive or
~  Inverts all the bits
< will shift bits to the left or right depending on the number and direction
86
Q

What do the following do?
r = p | q;
s = ~p;
t = p &laquo_space;2;

A
All the bits in p are OR-ed with all
the bits in q, result stored in r 
 Invert all the bits in p and store the
result in s
 Take all the bits in p and shift them
by two bits to the left
87
Q
double a=1.8, b=2.0;
int c, d;
c = (int) (a * b);
d = (int)a * (int)b;
What will c and d be?
A

3 and 2

int will always round down.

88
Q

What is program control?

A

The order in which statements are executed in C

89
Q

What is selection?

A

Selection chooses which set of program lines to execute depending on the result of some conditional statement. This enables programs to make decisions
about program flow. If and switch are examples

90
Q

What is iteration?

A

Iteration repeatedly executes a number of C program lines until some condition is met – these are referred to as “loops”.

91
Q

What is input validation?

A

. Checking or enforcing that values are entered by the user are in a certain range

92
Q

What are fopen and fread for?

A

reading and writing blocks of binary data

to a binary file

93
Q

Header files contain prototypes, where is the code for the functions kept?

A

In a library

94
Q
int nread, buffer[100];
FILE *in_file;
.
.
nread = fread(buffer, sizeof(int), n, in_file);
 What is happening here?
A

buffer – the array to be used to read the binary data into
sizeof(int) - sizeof is a special operator which works out the memory
used by each data type. We will look at this further in Lecture 9.
n – the number of data items to read in
in_file – the file pointer of the file to be read from

95
Q

How is binary data written into a file?

A

int nread, buffer[100];
FILE *out_file;
.
.
fwrite(buffer, sizeof(int), n, out_file);
In this example, a block of n integers stored in the array buffer[] is written to the file. The function returns the number of data items written to the file that will be n if the write is successfully performed.

96
Q

How would you open a file for reading and have an error message if the file couldn’t be opened?

A

if ((input_file = fopen(filename,”r”))==NULL)

printf(“file open failed “);

97
Q

When fgets reaches the end of a file, what does it do and how is this different to other functions?

A

Unlike the other file reading functions, fgets returns NULL when the end of the
file is reached. Other file read functions usually return EOF upon end-of-file.

98
Q

fgets(buffer, MAX_LENGTH, fp)

What do these things mean?

A

characters from the file pointed by fp are being put in buffer in a group of length=MAX_LENGTH

99
Q

What does fputs do?

A

fputs takes a string array and outputs it to a file.

100
Q

fprintf(output_file, “%s %d \n”,out_string, i);

What is happening here?

A

the string out_string and the integer i are read out from the file output_file

101
Q

What value does fscanf return?

A

fscanf() returns either the number of separate
items read during (in this case there will be 2 items corresponding to the string
and the integer) or an end of file value (EOF).

102
Q

When you have used fopen to open a file, what should you do?

A

Check that the file opened by seeing if fopen(…)==0

103
Q

What is procedural programming?

A

Splitting up a
program into a collection of functions each of which performs a separate part
of the total task

104
Q

How many different data types can be returned with a function?

A

1

105
Q

what is a call by reference?

A

Happens to array arguments, where it is the same in the function and main program

106
Q

When a multi-dimentional array is sent to a function, what has to be specified?

A
the no. of columns but not the no. of rows.
int add2darray(int array[][5], int nrows)