week 3 c programming Flashcards
learn c programming
what are void pointers
pointers for when you do not know what type to point to
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