mikeage.net Logo
mikeage.net/tag/c/

mikeage.net @ ז' אייר תשס"ח

Posts Tagged ‘C’

Identifying Big Endian or Little Endian Progmatically

ו' מרחשון תשס"ז - October 27, 2006

Most (99%+) of microprocessors available to the consumer and/or embedded market are either Big Endian or Little Endian; this almost never actually matters. However, sometimes one needs to write endian specific code. Here's how to have code identify if it's running on a big endian or little endian machine:
(more...)

Duff's Device

ה' מרחשון תשס"ז - October 26, 2006

No C programmer can call himself (or herself) a real programmer without having seen (and been amazed by) Duff's Device. It was invented as a more efficient alternative to regular loop unrolling used in copying bytes from memory to a register (each byte went to the same address in memory), by handling the case when there are "leftover" bytes after a full unroll.
(more...)

Dividing Integers (& Casting Precedance)

כ"ו תשרי תשס"ז - October 17, 2006

Quiz:
What does the following code print:

int i,j;
float k;
i = 5;
j = 2;
k = i/j;
printf("%f\n",k);

(more...)

Conditional Assignments

כ"ד תשרי תשס"ז - October 15, 2006

Sometimes, you'll have a scenario with two variables, and you need to use one of them based on a simple test. Rather than a long and awkward if / else statement, C allows the use of the ? : form. It is perfectly acceptable to write:

int i,j,k,l;
...
i = (j > 5? k : l)

which will set i to k if j is greater than 5, and l otherwise.
Another common use is [sf]?printf statements:

printf("The test %s\n",(status == OK) ? "passed" : "failed");

However, one can't use it to handle selective assignments. The following code is illegal:

int i,j,k;
...
((i>5) ? j : k) = 5;

This cannot be used to choose whether j or k is set to 5.
There is, however a way this can be done. Using pointer, we can select which one to deference:

*((i>5) ? &j : &k) = 5;

This will work.

Preprocessor #s, ##s, and other weird stuff

כ"ב תשרי תשס"ז - October 13, 2006

In the post on strings for enums , I used two types of pound symbols in the preprocessor macros: # and ##. Here's some more detail:
(more...)

Quick Map
Content +
Personal +
Archives +
Site Stuff +
RBS Weather +
Search +
New Images
Visitors
Clustermap
Blogroll

Valid XHTML 1.1!
Printer Friendly Page
 

Last Modified: September 04, 2006 @ 09:11 CST