Recent Resources
-
GLSL 4.0: Discarding Fragments to Create a Perf...
-
GLSL 4.0: Using Subroutines to Select Shader Fu...
-
Building a Complete Board-based Puzzle Game wit...
-
JIRA: Programming Workflows
-
.NET Generics 4.0: Container Patterns and Best...
-
Raw Meat: Game Design Tips from Team Meat's...
-
Sedge: An Automated Error Reporting Tool
Satan's Syntax
By Steve Goodwin | Published Nov 17 2000 12:17 PM in General Programming
A lot of people code by example. This example becomes a template. Once the template is known, different variables, different values, and different functions are applied to the template and combined with glue code to implement the required solution. By being more aware of the syntax, much of this glue can be removed. This article illustrates a few quirks of the C syntax, and how it can be used (abused?) to implement more efficient code, without qualifying for the IOCCC (International Obfuscated C Code Contest)!
Remember The Return Types
My first example of this "template programming" will involve the formatting output function sprintf. It is not unusual to write code such as:
You should be prudent in using pointers directly, to avoid deferencing a NULL. In this case, strchr(str, '\]
Remember The Return Types
My first example of this "template programming" will involve the formatting output function sprintf. It is not unusual to write code such as:
sprintf(str1, "Old v=%d\t",v);Most instances of sprintf use a temporary string as the first parameter. This is the template that becomes engrained: "sprintf requires a temporary string". Whereas a better template would be a syntactical one, "sprintf requires a pointer to a character [. This is a reminder that we could, instead, use a function that returns a char * as our first parameter, saving a temporary buffer. For example:
/* Some code that plays with v */
sprintf(str2, "New v=%d",v);
strcat(str1, str2);
printf(str1);
You should be prudent in using pointers directly, to avoid deferencing a NULL. In this case, strchr(str, '\]


















