In the C programming language, it is my understanding that variables can only be defined at the beginning of a code block, and the variable will have the scope of the block it w
I think you're working with some outdated assumptions. I've been coding straight C using GCC for a few months now, and you don't need to declare variables at the beginning of a block, even though the Second edition of K&R says you have to. You can declare your variable anywhere, like like this not-very-useful example:
char* palstring;
palstring = malloc(LARGEST_STRING);
memset(palstring, 0, sizeof palstring);
fgets(palstring, LARGEST_STRING, fin);
char* cur = palstring;
char letter;
letter = *cur;
So there's no need to do what you're suggesting. The language has moved on.
Another good addition to the C language is Variable Length Arrays, which allow you to pass an array to a function along with its size. In the old days, all you could do was pass in a pointer.
You don't even need an if statement. You can create blocks with {}
That should probably be a separate function however.
Example here:
#include <stdio.h>
int
main(int argc, char **argv) {
int i = 0;
{
int i = 10;
printf("%d\n", i);
}
printf("%d\n", i);
}
I don't think you need the if(true) portion.
Just the { } are required to scope the variables.
My answer is as follows:
This makes seasoned programmers cringe at the very thought of it.
Note that in C99 it is allowed to declare local variables in the middle of blocks.
C99 is the version of the C standard from the year 1999; most modern C compilers support it.