Simple Windows system information program.

This is a very simple Windows System information program that will print some useful system information about the Windows system you are using. This might be very helpful if you are programming on Windows with Visual Studio. sysinfo.c1 2 3 4 5 6 7 8 9 10 11 12 13…

Read More

How to test if a C program on Linux is running from a TTY or not.

This simple C program will test whether it is running from a TTY or not. This is a simple test, but it could be quite useful. #include <stdio.h> #include <poll.h> #include <unistd.h>   int main(int argc, char* argv[]){   struct pollfd fds[1] = {{.fd = STDIN_FILENO, .events = POLLIN}};  …

Read More

Miscellaneous programming tricks with C.

This is a very simple Hello World program in C. int main() { write(1, "Hello World\n", 14); }int main() { write(1, "Hello World\n", 14); } Counting how long a text string is. #include <stdio.h> #include <string.h> #define MSG "Hello Doctor, let’s get back to the TARDIS!" int main() { int…

Read More

Various tricks with C on Linux.

There are various tricks for programming on Linux. One useful trick is to open a file for reading with C. This is how to do this. The “r” option for fopen() will read the file into memory. #include <stdlib.h> #include <stdio.h>   int main(void) { FILE *f; char buf[256];  …

Read More

Very useful C program. Print a random fortune.

Here is one of my programs. I wrote this ages ago and it uses the standard ASCII character set. This will print a random fortune by running the fortune app to get a fortune and this selects a category to print from. #include <stdio.h> #include <stdlib.h> #include <time.h> #include <unistd.h>…

Read More

Return the length of a string easily in C.

This simple code will return the length of a string easily. #include <stdio.h> #include <stdlib.h>   int main (void) { char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; int len_str;   // calculates length of string len_str = sizeof(base64) / sizeof(base64[0]); printf("%d\n", len_str); return 0; }#include <stdio.h> #include <stdlib.h> int main (void) { char…

Read More

Very useful C code samples. These might be very useful to someone.

Some very useful code samples for any C programmer. These might give you some new ideas. Print the time and date with C. #include <time.h> // For time function (random seed). #include <stdio.h> // For extra functions. printf(). #include <stdlib.h> // For getenv();   #define format "The time and date…

Read More

How to write a Hello World program in C that does not use main().

This simple program is a Hello World example that does not use the main() function. This is certainly possible in a C program. #define syscall(a, D, S, d) __asm__ __volatile__("syscall" : : "a"(a), "D"(D), "S"(S), "d"(d))   void _start(void) { syscall(1, 1, "Hello, World\n", 14); syscall(60, 0, 0, 0); }#define…

Read More