Simple C program to read a file line by line

In C you have character oriented input functions (e.g. getchar, fgetc), you have formatted input functions (e.g. the scanf family) and then you have line oriented input functions. (e.g. fgets and POSIX getline). When you are reading lines of data, line oriented input functions are the proper tool for the job. (taking user input with scanf has many pitfalls that new (and even not so new) C programmers fall into) All line oriented functions read … Read more

wait(null) and wait(&status) C language and Status

If you call wait(NULL) (wait(2)), you only wait for any child to terminate. With wait(&status) you wait for a child to terminate but you want to know some information about it’s termination. You can know if the child terminate normally with WIFEXITED(status) for example. status contains information about processes that you can check with some already defined MACRO.

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

Fastest way to check if a file exist using standard C++/C++11,14,17/C?

Well I threw together a test program that ran each of these methods 100,000 times, half on files that existed and half on files that didn’t. Results for total time to run the 100,000 calls averaged over 5 runs, Method Time exists_test0 (ifstream) 0.485s exists_test1 (FILE fopen) 0.302s exists_test2 (posix access()) 0.202s exists_test3 (posix stat()) 0.134s The stat() function provided the … Read more

How do I create an array of strings in C?

I am trying to create an array of strings in C. If I use this code: gcc gives me “warning: assignment from incompatible pointer type”. What is the correct way to do this? edit: I am curious why this should give a compiler warning since if I do printf(a[1]);, it correctly prints “hmm”.

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

What does “Size in TCHARs” means?

So we all know that char is 8-bit, and wchar_t is 16-bit. (This isn’t always true, but it is on Windows with Microsoft compilers.) Many (nearly all) of the Windows APIs are implemented under the hood in two versions: one supports Unicode (multi-byte characters) and the other supports 8-bit national character sets. These two functions actually have slightly different … Read more

How do I lowercase a string in C?

It’s in the standard library, and that’s the most straight forward way I can see to implement such a function. So yes, just loop through the string and convert each character to lowercase. Something trivial like this: or if you prefer one liners, then you can use this one by J.F. Sebastian:

How to trigger SIGUSR1 and SIGUSR2?

They are user-defined signals, so they aren’t triggered by any particular action. You can explicitly send them programmatically: where pid is the process id of the receiving process. At the receiving end, you can register a signal handler for them: