# Idioms

Here are some C idioms that may be usefull.

Place `\0` at the location pointed to by `ptr` then increment `ptr`

       *ptr++ = '\0';

Increment `ptr` then place `\0` at the location pointed to by `ptr`

       *++ptr = '\0';

This program will print its self! I guess its not of any real use, but I think its clever.

      main(a) {a="main(a) {a=%c%s%c;printf(a,34,a,34);}";printf(a,34,a,34);}

This is something I saw out on the web. It swaps the value of two variables without using a third variable as a temporary store.

       one ^= two;
       two ^= one;
       one ^= two;

Have you ever had a SEGV from printf because you passed a NULL pointer to a %s flag???. This idiom will put a stop to all that nonsence.

       printf("%s\n", Str ? Str : "Null");

Program swapping the contents of two variables.

## See Also:

Common Coding Errors.

Top | Master Index | Keywords | Functions