week 3 c programming Flashcards

learn c programming

1
Q

what are void pointers

A

pointers for when you do not know what type to point to

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

what must you make sure you do with void pointers

A

You must cast it to the correct type before using
the underlying values
Pointer arithmetic on void* is illegal in C
You must cast it to the correct pointer type before
doing ptr arithmetic.

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

what is a function pointer

A

a pointer that points to a function

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

explain this : int (*func)(float, int);

A

This example means: func is a pointer to a
function that returns an int and takes a float and
an int as parameters

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

what are examples of standard input/output functions in C

A

scanf()
printf()
fprintf()
fscanf()
getchar()
put char()
getline()

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

what is getchar()

A

gets a character from standard input

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

what is putchar()

A

writes a character to standard output

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

can you write code that uses getchar() and putchar()

A

int main () {
char c;
printf(“Enter a character: “);
c = getchar();
printf(“Character entered: “);
putchar(c);
return(0);
}

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

what is get line()

A

reads an entire line

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

can you write code using getline()

A

int main(){
char *str = NULL;
size_t n;
int res;
printf(“Enter a string: “);
res = getline (&str, &n, stdin);
if (res != -1) {
printf (“%s”, str);
}
free(str);
return 0;
}

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

hwta do we use files for in C

A

Large data volumes

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

what do we to open a file in C

A

fopen(“filename”,”mode”)

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

write code for fopen()

A

FILE fp; /variable fp is pointer to type FILE*/
fp = fopen(“filename”, “mode”);

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

what happens if file does not exists for fopen()

A

returns NULL if it is unable to open the specified file

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

File *fp is a poinet -> what is it pointing to?

A

File pointer fp points to the ‘file’ resource
* contains all information about file
* Communication link between system and program

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

what are the different modes for file opening

A

r - reading mode
w - writing mode
a - appending mode
r+
w+
a+

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

what is reading mode (r) in C

A
  • if the file already exists then it is opened as read-only
  • sets up a pointer which points to the first character in it.
  • else error occurs
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

what is writing mode (w) in C

A

if the file already exists then it is overwritten by a new file
* else a new file with specified name created

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

what is appending mode (a) in C

A

if the file already exists then it is opened
* else new file created
* sets up a pointer that points to the last character in it
* any new content is appended after existing content

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

whatare additional modes for file opening in C

A

r+
w+
a+

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

what is r+

A

r+ opens file for both reading/writing an existing file
* doesn’t delete the content of if the existing file

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

what is w+

A

w+ opens file for reading and writing a new file
* Overwrites if the specified file already exists

23
Q

what is a+

A

a+ open file for reading and writing from the last character in
the file

24
Q

what can you use to read/write frm a file
e>g the standard input/output

A

getc()
putc()
get line()
fprintf()
fscanf()

25
Q

what is getc()

A

read a char from a file

26
Q

what is putc()

A

write a char to a file

27
Q

write some code using getc() and putc()

A

int main(){
char ch;
FILE *fp0, *fp1;
fp0 = fopen(“infile.txt”, “r”);
fp1 = fopen(“outfile.txt”, “w”);
if (fp0==NULL || fp1==NULL){
printf(“Cannot open files\n”);
exit(-1);
}
ch = getc(fp0); // reads one char from first file
while (ch != EOF){ // EOF is ‘end of file’
printf(“%c”, ch); // Displays on screen
putc(ch, fp1); // writes to second file
ch = getc(fp0); // reads another char from first file
}
fclose(fp0);
fclose(fp1);
return 0;
}

28
Q

what is the difference between fprintf() and fscanf() compared to printf() and scanf()

A

in addition the file pointer is provide as an input

29
Q

To read one int from a file with file pointer fp0

A

int i;
fp0=fopen(“some_file”, “r”);
fscanf(fp0, “%d”, &i);

30
Q

To write one int to a file with file pointer fp1

A

int i=4;
fp1=fopen(“some_file”, “w”);
fprintf(f1, “%d”, i);

31
Q

what are some file errors that could occur

A

Typically, errors happen when a program
* tries to read beyond end-of-file (EOF)
* tries to use a file that has not been opened
* performs operation on file not permitted by ‘fopen’ mode
opens file with invalid filename
* writes to write-protected file
Example: files with read-only permission

32
Q

how to handle file errors that could occur in C

A

feof(fp) returns a non-zero value when End-of-File is reached,
else it returns zero
ferror(fp) returns nonzero value if error detected else returns
zero

33
Q

write code for foef(fp)

A

fp = fopen(“somefile”, “somemode”);
. . .
if(feof(fp)){
printf(“End of file\n”);
}

34
Q

write code for ferror(fp)

A

fp = fopen(“somefile”, “somemode”);
. . .
if(ferror(fp) !=0)
printf(“An error has occurred\n”);

35
Q

what is fseek()

A

Data in a file is basically a collection of bytes.
We can directly jump to a target byte-number in a file without
reading previous data using fseek()

36
Q

what is the syntax of fseek()

A

Syntax: fseek(file-pointer, offset, position);
* position: 0 (beginning), 1 (current), 2 (end)
* offset: number of locations to move from specified position

37
Q

what does this do?
fseek(fp, -m, 1);

A

// move back by m bytes from current

38
Q

what does this do?
fseek(fp, m, 0);

A

// move to (m+1)th byte in file

39
Q

what does ftell(fp) do

A

returns current byte position in file

40
Q

what does rewind(fp) do?

A

resets position to start of file

41
Q

what is the syntax for a command line argument

A

int main(int argc, char *argv[]){

}

42
Q

what is argc(argument count)

A

int and automatically stores the number of
command-line arguments passed by the user including name of program

43
Q

what type is arc

A

it is int

44
Q

what is argc (argument vector)

A

is array of char pointers listing all the arguments

45
Q

what is the minimum numer of argv can there be

A

1 as argv[0] is the name of the program
argv[1] is the first command-line argument etc

46
Q

how to deal with larger programs

A

use an automated build system

47
Q

what is a makefile

A

an automated build system

48
Q

descibe object file

A

Object files contain the compiled machine code.
Do not commit them to version control.
They are in the final step linked together for the
full executable

49
Q

what is in an object file

A

c Sources
.h Headers
.o Object files (compiled C)

50
Q

what does this mean in make file:

<target>: <file0> <file1> ...
<command></command>
</file1></file0></target>

A

Means: if target does not exist or any of the files
has changed, execute the command

51
Q

what is a preprocessor

A

Runs before
compilation and does text substitution

52
Q

syntax of preprocessor

A

include “xyz.h”

53
Q

what does this mean?
#include “xyz.h”

A

This will take xyz.h and literally paste it at the
location of the #include statement