Category Archives: C

Sockets Programming in C Using UDP Datagrams

Below you’ll find the code of a simple server-client program in C using UDP sockets for the transmission. Basically the client sends a message to the server, the server converts the message to uppercase and returns it to the client. If you want to see a simpler program first check this client-server program that only […]

C Program To Investigate Memory Sections

The program below illustrates functions and methods you can use to investigate the allocated (virtual) memory of your process. For instance, you can get and print the limit of your data section, guess the limit of your stack and so on. One curious thing you’ll notice is that small mallocs appear to allocate memory inside […]

GDB Basic Commands

If you never use GDB (The GNU Project Debugger), well, you should! It’s easy to underestimate its usefulness when debugging programs. Below you’ll find some very basic commands that will get you started: First of all you need to compile/assemble your program with the -g flag, which will include in the executable the necessary code […]

How To Concatenate Two Integers in C

This might not be the most efficient way, but it’s certainly one of the easiest. You simply need to convert both integers into strings using sprintf, concatenate those using strcat, and then reverse the combined string to an integer using the atoi function. int concat(int x, int y){     char str1[20];     char str2[20];     sprintf(str1,"%d",x);     sprintf(str2,"%d",y);     strcat(str1,str2); […]

Implementing Huffman Coding in C

Huffman Coding (link to Wikipedia) is a compression algorithm used for loss-less data compression. Here’s the basic idea: each ASCII character is usually represented with 8 bits, but if we had a text filed composed of only the lowercase a-z letters we could represent each character with only 5 bits (i.e., 2^5 = 32, which […]

Poker Hand Evaluator in C

Problem 54 on ProjectEuler.net was an interesting one. Instead of the usual math puzzle it had a more practical topic: Poker. You basically need to evaluate the hands of two players for 1000 rounds, and then determine how many rounds rounds player one wins. The hand evaluator I built was quite naive and used a […]