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
What makes a good variable name?
Must start with _ or character. | Can have uppercase lowercase or numbers
26
How do you do comments?
/* */
27
how would you change the number of decimal places displayed in printf?
for a double using %.2e would give it to 2 decimal places
28
What is the casting of variables?
Changing their data type. If you wanted to perform floating point division with integers float answer answer=(float)value1/(float)value2;
29
What is a debugger?
A program within a compiler that pauses a program and analyses what's going on.
30
What does ++ do?
Increment. Add 1 to the value. Whether it is in front of a variable or after it matters.
31
What is the modulus operator?
answer=20%3; | Would return a value of 2 as it gives the remainder when 20 is divided by 3.
32
What does | time*=hours do?
time=time*hours
33
What needs to happen for you to use more complex maths functions?
#include header
34
How do you determine the square root of a number
sqrt(x) | math header needed
35
symbol for AND
&&
36
Symbol for OR
||
37
Symbol for NOT
!
38
Symbol for Exclusive-OR
^
39
How are powers evaluated?
pow(x,3) gives x*x*x | don't use ^
40
What are the evaluaters used with scanf to enter different variables?
%lf or %le for doubles %f for float %d for integer
41
How does #define work?
``` Used to make small functions or values #define square(x) x*x #define absolute_zero -273 ```
42
What does != mean?
Not equal to
43
What does exclusive-or denote ?
one can happen, or the other but not both
44
How would you add initial values an array when you create it?
Use [ ] | int x, values[]={10,20,30,40,50,60,70,80};
45
How would you initialise a 2 dimensional array?
int multi[3][5]; | This gives 3 columns and 5 rows.
46
what does strlen do?
Measure the length of a string
47
What is the header file needed for string functions?
include
48
what does strcpy (string1,string2)?
puts the contents of string 2 into string 1
49
what does s=strcmp(str1, str2); do?
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
t=strstr(str1, str2); | What does this do?
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
What modifier to used to output a string with printf?
%s
52
How do you input a string with a scanf?
char string [20]; scanf("%s",string); Notice that a & is not needed.
53
At the start of a function, what would a void at the start mean?
No value is returned to the program. | Other wise the data type that is being returned would be put there.
54
Why when using an array with a function is it different to using an ordinary variable?
Because the array used in the main program and the function is the same and not just a copy.
55
What is the difference between local and global variables?
Exist only inside the function that created them
56
What are global variables?
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
What is a function prototype?
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
What is an algorithm?
Any sequence of instructions to produce any results or to perform any operations.
59
How would you initialise a file pointer?
To declare a pointer with name in_file | FILE* in_file;
60
How would you associate a file with a pointer?
``` 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
What are the different permissions you can use with fopen?
“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
How and why do you close a file?
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
How would you extract a character from a file?
``` Use the function getc. This gives 1 character. To get more use a loop. int ch; FILE *in_file; . . ch=getc(in_file); ```
64
How would you read a file character by character?
``` 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
How is a character put into a file?
``` int ch; FILE *out_file; . . putc(ch,out_file); Here putc has put a single character into out_file ```
66
What does fgets() do?
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
How do you use fgets()?
``` 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
What do fprintf and fscanf do?
Work in the same way as printf and scanf but they input and output with files. Work with text not binary
69
What is the form of the do...while loop?
do { } while (.....);
70
``` int a,b; double c; b=5; c=a/b; If a was 20, 23 or 25, what would c be? ```
4, 4 and 5. | It will always round down.
71
``` int a,b; double c; a=24; b=5; c=(float)a/(float)b; What will c be? ```
4.8000000
72
Consider the following statement: a = b++; What does it do?
Copies b to a, then adds one to b
73
What does a header file such as math.h mostly contain?
Prototypes
74
When might you use a switch statement?
When you want to do the work of many if...else clauses.
75
What is the form of the switch statement?
``` scanf("%d",&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
Can fwrite() be used to right binary files?
Yes
77
What can == compare?
Only char and int. | Same for !=
78
How many bytes to an integer?
32 bit so 4 bytes, meaning 4 address are needed.
79
What is the size of the float?
32 bits, range of ±10^+38 | Typically 3 d.p
80
What is the size of the double?
64 bits, range of ±10^+308 | Typically 9 d.p
81
How to you check if doubles and floats are equal to each other?
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
What is long int?
32 bit integer with a range of 0-2^32 (unsigned long int) or ±2^31 (signed long int)
83
What is short int?
16 bit integer (same pattern as long int)
84
What is the nature of the char datatype?
The char datatype is used to represent characters, but we can also use it to store any 8-bit value:
85
What are the bitwise operators?
``` & and | or ^ excusive or ~ Inverts all the bits < will shift bits to the left or right depending on the number and direction ```
86
What do the following do? r = p | q; s = ~p; t = p << 2;
``` 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
``` 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? ```
3 and 2 | int will always round down.
88
What is program control?
The order in which statements are executed in C
89
What is selection?
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
What is iteration?
Iteration repeatedly executes a number of C program lines until some condition is met – these are referred to as “loops”.
91
What is input validation?
. Checking or enforcing that values are entered by the user are in a certain range
92
What are fopen and fread for?
reading and writing blocks of binary data | to a binary file
93
Header files contain prototypes, where is the code for the functions kept?
In a library
94
``` int nread, buffer[100]; FILE *in_file; . . nread = fread(buffer, sizeof(int), n, in_file); What is happening here? ```
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
How is binary data written into a file?
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
How would you open a file for reading and have an error message if the file couldn't be opened?
if ((input_file = fopen(filename,"r"))==NULL) | printf("file open failed ");
97
When fgets reaches the end of a file, what does it do and how is this different to other functions?
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
fgets(buffer, MAX_LENGTH, fp) | What do these things mean?
characters from the file pointed by fp are being put in buffer in a group of length=MAX_LENGTH
99
What does fputs do?
fputs takes a string array and outputs it to a file.
100
fprintf(output_file, "%s %d \n",out_string, i); | What is happening here?
the string out_string and the integer i are read out from the file output_file
101
What value does fscanf return?
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
When you have used fopen to open a file, what should you do?
Check that the file opened by seeing if fopen(...)==0
103
What is procedural programming?
Splitting up a program into a collection of functions each of which performs a separate part of the total task
104
How many different data types can be returned with a function?
1
105
what is a call by reference?
Happens to array arguments, where it is the same in the function and main program
106
When a multi-dimentional array is sent to a function, what has to be specified?
``` the no. of columns but not the no. of rows. int add2darray(int array[][5], int nrows) ```