September 1998
Intermediate to advanced
848 pages
20h 13m
English
C requires that all variables other than function parameters be declared at the beginning of a block, that is, just after an opening brace. C++ removes this restriction, and the C++ practice is to declare a variable at the point it first gets a value. C++ even allows you to declare a variable in the initialization slot of afor statement. For example, the following code is valid C++:
#include <stdio.h>
int main()
{
puts("Enter a value for n:");
int n; /* valid C++, invalid C */
scanf("%d", &n);
for (int i = 0; i < n; i++) /* valid C++, invalid C */
{
puts("Thank you!");
puts("Enter a word:");
char word[40]; /* valid C++, invalid C */
scanf("%s", word);
printf("%s is ok.\n", word);
}
return 0;
}
It is not, however, valid C. So if ...
Read now
Unlock full access