A simple netcat program in C to receive data and then acknowledge this with a message.
A very nice C program to open a listening port on Linux and respond to a message.
Read MoreA very nice C program to open a listening port on Linux and respond to a message.
Read MoreThere is an easy way to print multiple items from an array in C. The example below shows a simple program that uses the sleep command and will print 5 random items from an array. photos.c1 2 3 4 5 6 7 8 9 10 11 12 13 14 15…
Read MoreThere is yet another way to print useful code to the terminal in C. This program below shows a very simple way to print text to the terminal and this is not using printf(). simple.c1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16…
Read MoreThis 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 MoreThis 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 MoreThis is a very useful C++ code snippet for reading a line of text into STDIN. Hopefully, this will be useful to someone. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <iostream> #include <string.h> #include…
Read MoreTrailing whitespace in a C source file is very annoying, especially if it is a very large file. There is an easy way to fix this with the Linux command line. This sed(1) one-liner will remove all spaces from the file, erasing all of the unwanted spaces after characters that…
Read MoreThis 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 MoreThis is an interesting version of my random monster program. I added a simple function to get a random number in milliseconds. randommonster.c1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28…
Read MoreThis is an improved version of my random monster program. This shows how to get the size of a const char array and then select a random entry from the array. monsters.c1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19…
Read MoreThis is a very simple program that prints information about the kernel version and nodename. This is very useful when building a larger system information program. sysinfo.c1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24…
Read MoreThis program allows the user to search an array and then return any matches. Give the program two letters and it will search the array and try to find the matches. compare.cpp1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19…
Read MoreThis is a simple program that prints a few strings one after another in a loop, but it is randomized. This is very interesting. This goes to show it is very easy to do something like this. strings.c1 2 3 4 5 6 7 8 9 10 11 12 13…
Read MoreThis is a nice example in C. This will append text to a file whenever it is run. This is the proper way to do this in this programming language. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <stdlib.h> #include <stdio.h> int…
Read MoreThere 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 MoreThis code generates a random string when run, this could be quite useful. Print some very useful Linux system information using C. These two C programming samples should be quite useful to any programmer on Linux. Print a random fortune from an array. This program prints a random fortune from…
Read MoreHere 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 MoreThis 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 MoreSome 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 MoreThis 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