Today's Question:  What does your personal desk look like?        GIVE A SHOUT

 ALL


  do {...} while (0) in macros

If you are a C programmer, you must be familiar with macros. They are powerful and can help you ease your work if used correctly. However, if you don't define macros carefully, they may bite you and drive you crazy. In many C programs, you may see a special macro definition which may seem not so straightforward. Here is one example:#define __set_task_state(tsk, state_value) \ do { (tsk)->state = (state_value); } while (0)There are many this kind of macros which uses do{...}while(0) in Linux kernels and other popular C libraries. What's the use of this macro? Robert Love from Google(...

139,613 14       C C++ MACRO


  Do you really understand C? 21st International Obfuscated C Code Contest winning entries

The 21st International Obfuscated C Code Contest(IOCCC) officially launched the winning source code. IOCCC requires contestants to write the most creative and the most obfuscated C codes with the size limited to 4kb and less Work of each participant is impressive. The winners, including one French, one Korean, five Americans, one Belgian, one Israeli, one British, four Japanese and one Chinese.Here we list some codes:Best short programSeonghoon Kang  from Korea- Decodes spelled out numberslong long n,u,m,b;main(e,r)char **r;{f\or(;n++||(e=getchar()|32)>=0;b="ynwtsflrabg"[n%=11]-e?...

11,618 0       C OBSFUCATION CONTEST


  Books for entry level C programmers

In computing, C is a general-purpose programming language initially developed by Dennis Ritchie between 1969 and 1973 at Bell Labs Its design provides constructs that map efficiently to typical machine instructions, and therefore it found lasting use in applications that had formerly been coded in assembly language, most notably system software like the Unix computer operating system.To learn C, we need to read many C books and have many practices. Here we summarize a list of C books which may help you learn C.1. How to Think Like a Computer Scientist :C versionAlthough it contains only basic ...

8,837 0       C BOOK BEGINNING


  How regular expression works

Rob Pike wrote 30 lines of codes to realize a simple regular expression matcher in his book The practice of Programming. This piece of code is really cool. Let's take a look at the code.Meaning of different characters.CharacterMeaningcGeneral character.Match any single character^Match start of a string$Match end of a string*Match zero or many occurrences of a character/*match :Test the regexp in text*/int match(char* regexp,char* text){    if(regexp[0] == '^')        return matchhere(regexp+1,text);     do{ /*Check the strin...

3,209 0       C REGULAR EXPRESSION IMPLEMENTATION ROB PIKE


  C Macro Tips and Tricks

Preprocessor vs CompilerTo properly understand C macros, you must understand how a C program is compiled. In particular, you must understand the different things that happen in the preprocessor and in the compiler.The preprocessor runs first, as the name implies. It performs some simple textual manipulations, such as:Stripping comments. Resolving #include directives and replacing them with the contents of the included file. Evaluating #if and #ifdef directives. Evaluating #defines. Expading the macros found in the rest of the code according to those #defines.It is, of course, these...

18,352 0       C MACRO PREPROCESSOR TRICK


  Which programming language should I learn first?

Recently I saw somebody asked a question in a forum, the question is "Which programming language should I learn first?". Then someone answered this question. His answer:Depends.To program in an expressive and powerful language: PythonTo get a website up quickly: PHPTo mingle with programmers who call themselves “rockstars”: Ruby.To really learn to program: C.To achieve enlightenment: Scheme.To feel depressed: SQLTo drop a chromosome: Microsoft Visual BasicTo get a guaranteed, mediocre, but well paying job writing financial applications in a cubicle under fluorescent lights: Ja...

112,250 18       JAVASCRIPT C PROGRAMMING LANGUAGE LEARN


  Error handling style in C

Following are three error handling styles in C.Which one you like the most? Or you don't like any one?1. /* Problem : Not enough. Easy to be wrong */int foo(int bar){        int return_value = 0;        int doing_okay = 1;        doing_okay = do_something( bar );        if (doing_okay)        {                doing_oka...

19,756 13       C ERROR HANDLING STYLE GOTO NESTED IF


  The most stupid C bug ever

I have been programming for a number of years already. I have seen others introduce bugs, and I have also introduced (and solved!) many bugs while coding. Off-by-one, buffer-overflow, treating pointers as pointees, different behaviors or the same function (this is specially true for cross-platform applications), race conditions, deadlocks, threading issues. I think I have seen quite a few of the typical issues.Yet recently I lost a lot of time to what I would call the most stupid C bug in my career so far, and probably ever.I am porting a Unix-only application which uses tmpfile() to create te...

2,521 0       C BUG COMMENT BACK SLASH