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
Void pointers are used when the type of the pointer is unspecified.
They are a poor replacement for the missing generic types in C

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

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

what is a+

A

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

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
what is getc()
read a char from a file
26
what is putc()
write a char to a file
27
write some code using getc() and putc() that inclueds all the file pointers and syntax such as fopen(make sure to beware of errors that could occur)
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
what is the difference between fprintf() and fscanf() compared to printf() and scanf()
in addition the file pointer is provide as an input
29
To read one int from a file with file pointer fp0
int i; fp0=fopen("some_file", "r"); fscanf(fp0, "%d", &i);
30
To write one int to a file with file pointer fp1
int i=4; fp1=fopen("some_file", "w"); fprintf(f1, "%d", i);
31
what are some file errors that could occur
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
how to handle file errors that could occur in C
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
write code for foef(fp)
fp = fopen("somefile", "somemode"); . . . if(feof(fp)){ printf("End of file\n"); }
34
write code for ferror(fp)
fp = fopen("somefile", "somemode"); . . . if(ferror(fp) !=0) printf("An error has occurred\n");
35
what is fseek()
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
what is the syntax of fseek()
Syntax: fseek(file-pointer, offset, position); * position: 0 (beginning), 1 (current), 2 (end) * offset: number of locations to move from specified position
37
what does this do? fseek(fp, -m, 1);
// move back by m bytes from current
38
what does this do? fseek(fp, m, 0);
// move to (m+1)th byte in file
39
what does ftell(fp) do
returns current byte position in file
40
what does rewind(fp) do?
resets position to start of file
41
what is the syntax for a command line argument
int main(int argc, char *argv[]){ ... }
42
what is argc(argument count)
int and automatically stores the number of command-line arguments passed by the user including name of program
43
what type is arc
it is int
44
what is argc (argument vector)
is array of char pointers listing all the arguments
45
what is the minimum numer of argv can there be
1 as argv[0] is the name of the program argv[1] is the first command-line argument etc
46
how to deal with larger programs
use an automated build system
47
what is a makefile
an automated build system lists all dependencies for a set of programs and contains instructions how to build large programs
48
descibe object file
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
what is in an object file
c Sources .h Headers .o Object files (compiled C)
50
what does this mean in make file: : ...
Means: if target does not exist or any of the files has changed, execute the command
51
what is a preprocessor
Runs before compilation and does text substitution it is like an import in java lol
52
syntax of preprocessor
#include “xyz.h”
53
what does this mean? #include “xyz.h”
This will take xyz.h and literally paste it at the location of the #include statement
54
what syntax do we use to read files in C
File *fpo = fopen("filename","r"); fscanf(fpo,"%d",&variable);
55
what syntax do we use to write files in C
int variable = 7; File *fpo = fopen("filename","r"); fprintf(fpo,"%d",&variable);
56
what does EOF mean
end - of - file
57
what is offset in fseek()
number of locations to move from specified positions
58
what does position 1 mean is fseek
current position
59
what does position 2 mean in fseek
end position
60
what does position 0 mean in fseek
beginning position