Category Archives: C

Learning by Doing: Programming Razz Simulator

Posted in kerneltrap.org on February 5, 2010 – 9:54am As a final assignment in my Languages for Scientific Computing class, I develop a Razz Simulator. Basically Razz is the inverse of Poker in which you try to come up with … Continue reading

Posted in C, Learning Experience, Programming Languages, Software Engineering | Tagged | 1 Comment

How to Get the Length of a UDP Datagram/Packet

Posted in kerneltrap.org on September 3, 2009 – 12:33pm While developing the logger for GNU/Linux Anywhere USB Controller, I needed to know the length of the UDP packet to be read so that I could do `malloc()’ with the appropriate … Continue reading

Posted in C, Programming Languages | Tagged | Leave a comment

Getting the File Name from a File Descriptor (fd) or a File Pointer (FILE *)

Posted in kerneltrap.org on September 3, 2009 – 11:51am While developing the logger for GNU/Linux Anywhere USB Controller, I created the following function that will store a `FILE *’ into a global table for a later retrieval: static int save_open_file … Continue reading

Posted in C, Programming Languages | Tagged | Leave a comment

Crafting Function: With or Without Buffer Length Parameter?

Posted in kerneltrap.org on August 4, 2009 – 2:07pm Developing a C API that deals with writing to external provided buffers, I was wondering whether or not I should include parameter buffer length when the length of the data to … Continue reading

Posted in C, Programming Languages | Tagged | 2 Comments

Should a header file includes another header file?

Posted in kerneltrap.org on July 10, 2009 – 5:12pm Now that I’d got another chance to develop a C application, whenever I crafted a header file, I always wondered whether the header file needed to include another header file such … Continue reading

Posted in C, Programming Languages | Tagged , , | 4 Comments

How to differentiate between EOF and a character whose ASCII code is 255

Posted in kerneltrap.org on June 1, 2008 – 10:05pm A call to fgetc() or getchar() will return EOF if the end-of-file has been reached. As far as I know, GNU C Library defines EOF to be (-1). Those who are … Continue reading

Posted in C, GNU C Library, Programming Languages | Tagged , , | 2 Comments

Printing size_t or ssize_t variable

Posted in kerneltrap.org on May 18, 2008 – 10:28pm Looking at the man page of printf (man 3 printf), in the length modifier section, it is written: z       A following integer conversion corresponds to a size_t or ssize_t argument. (Linux libc5 … Continue reading

Posted in C, GNU C Library, Programming Languages | Tagged , | Leave a comment

Zero-length array to provide an elegant link

Posted in kerneltrap.org on May 18, 2008 – 10:22pm Consider the following struct and program: #include <stdio.h> #include <stdlib.h> struct fix_data { char data [5]; unsigned char optional_part [0]; } __attribute__ ((packed)); int main (int argc, char **argv, char **envp) … Continue reading

Posted in C, GCC Extensions, Hacking, Linux Kernel, Networking, Programming Languages, Software | Tagged | Leave a comment

Why do the function prototypes use extern

Posted in kerneltrap.org on May 18, 2008 – 10:14pm It is a universal truth that with or without the extern keyword a function prototype will just do the same. So, why does almost all function prototypes in Linux kernel source … Continue reading

Posted in C, Hacking, Linux Kernel, Programming Languages, Software | Tagged | 8 Comments

Getting the size of a member of a struct or union

Posted in kerneltrap.org on May 18, 2008 – 10:07pm Looking at my previous post, I realized that you can also use the same technique to get the size of a member of a struct or a union without declaring a … Continue reading

Posted in C, Hacking, Linux Kernel, Programming Languages, Software | Tagged | Leave a comment