Variable declaration reset: Difference between revisions
Content added Content deleted
m (→{{header|Red}}: add some comments) |
(Added C) |
||
Line 32: | Line 32: | ||
with [[Rutgers_ALGOL_68|Rutgers Algol 68]]:<br> |
with [[Rutgers_ALGOL_68|Rutgers Algol 68]]:<br> |
||
No output. |
No output. |
||
=={{header|C}}== |
|||
Note firstly that it's possible to create variables in C without initializing them. However, if you do so, the value the variable will contain is unpredictable and so here we give '(g)prev' an initial value of 0 to make absolutely sure it won't clash with the values in the array. |
|||
The following compiles using either C89/90 (-std=c90 -ansi -pedantic) or C99 syntax using gcc 9.4.0. |
|||
<lang c>#include <stdio.h> |
|||
int main() { |
|||
int i, gprev = 0; |
|||
int s[7] = {1, 2, 2, 3, 4, 4, 5}; |
|||
/* There is no output as 'prev' is created anew each time |
|||
around the loop and set implicitly to zero. */ |
|||
for (i = 0; i < 7; ++i) { |
|||
int curr = s[i]; |
|||
int prev = 0; |
|||
if (i > 0 && curr == prev) printf("%d\n", i); |
|||
prev = curr; |
|||
} |
|||
/* Now 'gprev' is used and reassigned |
|||
each time around the loop producing the desired output. */ |
|||
for (i = 0; i < 7; ++i) { |
|||
int curr = s[i]; |
|||
if (i > 0 && curr == gprev) printf("%d\n", i); |
|||
gprev = curr |
|||
} |
|||
return 0; |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
2 |
|||
5 |
|||
</pre> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |