week 3 c programming Flashcards
learn c programming
what are void pointers
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
what must you make sure you do with void pointers
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.
what is a function pointer
a pointer that points to a function
explain this : int (*func)(float, int);
This example means: func is a pointer to a
function that returns an int and takes a float and
an int as parameters
what are examples of standard input/output functions in C
scanf()
printf()
fprintf()
fscanf()
getchar()
put char()
getline()
what is getchar()
gets a character from standard input
what is putchar()
writes a character to standard output
can you write code that uses getchar() and putchar()
int main () {
char c;
printf(“Enter a character: “);
c = getchar();
printf(“Character entered: “);
putchar(c);
return(0);
}
what is get line()
reads an entire line
can you write code using getline()
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;
}
hwta do we use files for in C
Large data volumes
what do we to open a file in C
fopen(“filename”,”mode”)
write code for fopen()
FILE fp; /variable fp is pointer to type FILE*/
fp = fopen(“filename”, “mode”);
what happens if file does not exists for fopen()
returns NULL if it is unable to open the specified file
File *fp is a poinet -> what is it pointing to?
File pointer fp points to the ‘file’ resource
* contains all information about file
* Communication link between system and program
what are the different modes for file opening
r - reading mode
w - writing mode
a - appending mode
r+
w+
a+
what is reading mode (r) in C
- 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
what is writing mode (w) in C
if the file already exists then it is overwritten by a new file
* else a new file with specified name created
what is appending mode (a) in C
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
whatare additional modes for file opening in C
r+
w+
a+
what is r+
r+ opens file for both reading/writing an existing file
* doesn’t delete the content of if the existing file
what is w+
w+ opens file for reading and writing a new file
* Overwrites if the specified file already exists
what is a+
a+ open file for reading and writing from the last character in
the file
what can you use to read/write frm a file
e>g the standard input/output
getc()
putc()
get line()
fprintf()
fscanf()
what is getc()
read a char from a file
what is putc()
write a char to a file
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;
}
what is the difference between fprintf() and fscanf() compared to printf() and scanf()
in addition the file pointer is provide as an input
To read one int from a file with file pointer fp0
int i;
fp0=fopen(“some_file”, “r”);
fscanf(fp0, “%d”, &i);
To write one int to a file with file pointer fp1
int i=4;
fp1=fopen(“some_file”, “w”);
fprintf(f1, “%d”, i);
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
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
write code for foef(fp)
fp = fopen(“somefile”, “somemode”);
. . .
if(feof(fp)){
printf(“End of file\n”);
}
write code for ferror(fp)
fp = fopen(“somefile”, “somemode”);
. . .
if(ferror(fp) !=0)
printf(“An error has occurred\n”);
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()
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
what does this do?
fseek(fp, -m, 1);
// move back by m bytes from current
what does this do?
fseek(fp, m, 0);
// move to (m+1)th byte in file
what does ftell(fp) do
returns current byte position in file
what does rewind(fp) do?
resets position to start of file
what is the syntax for a command line argument
int main(int argc, char *argv[]){
…
}
what is argc(argument count)
int and automatically stores the number of
command-line arguments passed by the user including name of program
what type is arc
it is int
what is argc (argument vector)
is array of char pointers listing all the arguments
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
how to deal with larger programs
use an automated build system
what is a makefile
an automated build system
lists all dependencies for a set of programs and contains
instructions how to build large programs
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
what is in an object file
c Sources
.h Headers
.o Object files (compiled C)
what does this mean in make file:
<target>: <file0> <file1> ...
<command></command>
</file1></file0></target>
Means: if target does not exist or any of the files
has changed, execute the command
what is a preprocessor
Runs before
compilation and does text substitution
it is like an import in java lol
syntax of preprocessor
include “xyz.h”
what does this mean?
#include “xyz.h”
This will take xyz.h and literally paste it at the
location of the #include statement
what syntax do we use to read files in C
File *fpo = fopen(“filename”,”r”);
fscanf(fpo,”%d”,&variable);
what syntax do we use to write files in C
int variable = 7;
File *fpo = fopen(“filename”,”r”);
fprintf(fpo,”%d”,&variable);
what does EOF mean
end - of - file
what is offset in fseek()
number of locations to move from specified positions
what does position 1 mean is fseek
current position
what does position 2 mean in fseek
end position
what does position 0 mean in fseek
beginning position