More Like This
Categories (See All)
Recent Resources
-
Breaking Out of Breakout
-
Spring RTS Engineering Internals
-
For a Career in Gaming, are Game Design Degrees Worth It?
-
Building a First-Person Shooter Part 1.2: The Player Class
-
Techniques for Finding Unlisted Game Internships
-
Indie tutorial: Organizing your work as a team
-
Making it in Indie Games: Starter Guide
Satan's Syntax
By Steve Goodwin | Published Nov 17 2000 06:17 AM in General Programming
| If you find this article contains errors or problems rendering it unreadable (missing images or files, mangled code, improper text formatting, etc) please contact the editor so corrections can be made. Thank you for helping us improve this resource |
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:
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); /* Some code that plays with v */ sprintf(str2, "New v=%d",v); strcat(str1, str2); printf(str1);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:<br /><br />sprintf(str, "Old v=%d\]
About the Author(s)
After graduating with honours from Loughborough University, where he wrote his first compiler, Steven Goodwin took a temporary job writing computer games. Many years later, he's still doing it, finding more ways to manipulate the syntax than his colleagues would like! His computing interests include esoteric languages, code optimisation and automatic MIDI compositions. His interests outside of computing also use computers.
Comments
Note: Please offer only positive, constructive comments - we are looking to promote a positive atmosphere where collaboration is valued above all else.






