If the line will be declared at the beginning
In C89, definitions must be performed before any statements in a block. If you move it, you do not need to move the entire line (and, of course, you do not want to move the entire line before the code that checks argv[2] checked). Just move the definition of f :
int ch; int key; int (*f)(int,int); ... f = (argv[2][0] == 'd') ? decode : encode;
any better way to do it
This is not necessarily better in this case, but note that this rule is the beginning of a block, not necessarily the beginning of a function.
So you can simply write:
{ int (*f)(int, int) = (argv[2][0] == 'd') ? decode : encode; while (EOF != (ch=getchar())) putchar(f(ch, key)); } return 0;
You can easily delve into the arguments about this coding style. Some people think that every function should define all its variables in front, and introducing a block just to define a variable is cluttered and / or confusing. Some people (and especially those who use C ++, as well as C) believe that you should limit the scope of each variable to as narrow a piece of code as possible, that defining everyone at the beginning of a function is cluttered and / or confusing. But even they may consider excessive blocking excessive.
source share