What’s the difference between a file descriptor and file pointer?

A file descriptor is a low-level integer “handle” used to identify an opened file (or socket, or whatever) at the kernel level, in Linux and other Unix-like systems. You pass “naked” file descriptors to actual Unix calls, such as read(), write() and so on. A FILE pointer is a C standard library-level construct, used to represent a file. The FILE wraps the … Read more

C read file line by line

If your task is not to invent the line-by-line reading function, but just to read the file line-by-line, you may use a typical code snippet involving the getline() function (see the manual page here):

conflicting types error when compiling c program using gcc

If you don’t declare a function and it only appears after being called, it is automatically assumed to be int, so in your case, you didn’t declare before you call it in main, so the compiler assume there are functions which their prototypes are int my_print2 (char *); and int my_print2 (char *); and you can’t have two functions with … Read more

Categories C Tags ,

warning: expression result unused

You get the warning because the expression gets calculated, and then the result is dropped. This is related to the “reaching the end of the function without returning a value” error: adding return in front of the expression will fix both:

Categories C Tags

When is it a good idea to use strdup (vs malloc / strcpy)

Which one is better? strdup(s); itself does not create a problem when allocation failures (calling code still needs to handle a NULL return), unlike the below which is undefined behavior or UB. A typical implementation of strdup(s) does not walk the length of s twice like the alternate might. A good strdup(s) will make one pass and use optimal copy code when the length warrants it. Perhaps by … Read more

Categories C Tags

The Definitive C Book Guide and List[

This question attempts to collect a community-maintained list of quality books on the c programming language, targeted at various skill levels. C is a complex programming language that is difficult to pick up on-the-go by reading online tutorials. A comprehensive book is often the best way to learn the language, and finding a good book is the first step. … Read more

Categories C Tags