Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
int *itPins = pins;
for(int i = 0; i < count; i++)

I have the impression that doing two things in one line is bad practice. This is why I am advancing the iterator outside of the only statement, using it. However, I feel blindly following a rule.

  • Is this code less error-prone than if the ++ is inside the function invocation?
  • Why?
  • Does this rule apply to such ultra-simple cases or is it meaningful only for more complex program structures?
share|improve this question
A "side effect" is something non-obvious that a routine does besides yield a return value. But that's not what you're talking about, is it? Your point is whether to pass the counter and advance it in one statement or in two separate statements. That's not a side effect, that's just a question of doing more or less in one line of code. – Kilian Foth Dec 13 '13 at 9:05
I dont think this is an example of "side effects" unless void ConfigureGpifPinAsGpioOutput(T*& pins) { pins++; } is the function. – Charles Beattie Dec 13 '13 at 9:23
@CharlesBeattie: Unless ConfigureGpifPinAsGpioOutput is functionally pure (and therefore pointless), this does show side effects--the question just isn't about that. – Phoshi Dec 13 '13 at 9:25
Having an unused counter variable and incrementing something else instead does look like something's wrong to me. for (int *itPins = pins; itPins < pins + count; itPins++). – aragaer Dec 13 '13 at 9:45
This is a textbook example of why some companies develop coding standards. To some people, a line of code is clear and consise whilst to others it is unduly terse and hard to read. – Robbie Dee Dec 13 '13 at 13:27
up vote 26 down vote accepted

It is not always bad practice to do multiple things on one line, but it does have a higher risk of making the program appear more complicated that it needs to be.

If I were pedantic, I could argue that in the line for(int i = 0; i < count; i++), you are also doing multiple things, but very few people would mark that line as a violation of the rule.

On the other hand, if you had a line like ConfigureGpifPinAsGpioOutput(itPins++);, then the increment in the argument does increase the complexity as I would have to look twice to be sure that the code is correct and I would probably flag it as needlessly complicated.

Another way to write the loop is like this:

for(int i = 0; i < count; i++)

where you rid yourself completely of the issue.

share|improve this answer
To my eye, your revised code makes much more sense. – Robert Harvey Dec 13 '13 at 19:41

It depends. Most times I would to avoid such things, but in a simple case like this


is very idiomatic for most programmers with some experience in the C families of programming languages. With some experience in C code reading, it is IMHO as easy to grasp than the two lines of code in your example.

So I would not be too picky about this special case.

share|improve this answer
Good point about idiomatic code. – Vorac Dec 13 '13 at 11:31
You raise an interesting point here. There is an argument for making the code easier to read for developers coming from another language. The counter-argument is that you end up with a lowest-common-denominator approach that doesn't always make best use of the built-in language features. – Robbie Dee Dec 13 '13 at 13:41
I do not understand why this answer is not getting more upvotes. – Vorac Dec 13 '13 at 15:46
That makes 2 of us. Talk about casting pearls before swine... – Robbie Dee Dec 13 '13 at 15:52
Does putting the increment statement outside of the function call have any deleterious effects in C? One would never combine statements in this way in C#, as the only benefit would be one less line of code in exchange for less readability. – Robert Harvey Dec 13 '13 at 19:38

This is a matter of style, and there is no definitive answer. There are some C programmers who are more dogmatic and will hate code with multiple statements per line. Almost no one will object to code where each statement is on a separate line. If there are people on your team who hate seeing multiple statements on a line, then just don't do it. Argue about something more important. If the team is just you, then suit yourself.

In your specific example, the best answer is to eliminate i and directly iterate through the pin numbers:

for(int* p = pins; p < pins + count; ++p) {

BTW, there are lots of C programmers who will really hate your quasi-Hungarian naming conventions.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.