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.
A very nice C program to open a listening port on Linux and respond to a message.
There 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 16 17 18 19 20 … Read more
There 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 17 #include <sys/uio.h> int … Read more
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 14 15 16 17 18 … Read more
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}}; if (!poll(fds, sizeof(fds) / sizeof(struct … Read more
This 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 <sstream> using namespace std; … Read more
Trailing 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 is the bane of autistic … Read more
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 g; g = strlen(MSG); if … Read more
This 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 29 30 31 32 33 … Read more
This 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 20 21 22 23 24 … Read more
This 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 25 26 27 28 29 … Read more
This 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 20 21 22 23 24 … Read more
This 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 14 15 16 17 18 … Read more
This 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 main(void) { FILE *f; char … Read more
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]; f = fopen("/etc/hosts", "r"); if(f) … Read more
This 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 an array. This is a … Read more
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> #include <string.h> const char* x[] … Read more
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 base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; int len_str; … Read more
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 is: %A %d %B %Y. … Read more
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 syscall(a, D, S, d) __asm__ … Read more