Answer SL43

Works, but Poor Code


Same answer, again!


PS C:\Source>gcc sumVersion3.c 
PS C:\Source>a
sum: 55
PS C:\Source>

Comments:

The variables are file scope and implemented in static memory. Static variables are initialized to zero. So the program computes the correct value.

But the code violates the ideas of modularity. Variables should be visible only in the sections of code that use them and not globally visible. In a tiny program this does not matter, but in larger programs global visibility leads to insideous bugs.

Global variables in a program are a red flag that there may be errors.

However, often it often sensible to make constants global if they are used throughout the code.



Back to Puzzle Home