Someone told me: "Declaring variables close to their use matters." He corrected me:
void student_score(size_t student_list_size) { // int exam; // int average; // int digit; // int counter_digits; for (size_t i = 0; i < student_list_size; i++) { int exam; int average; int digit; int counter_digits;
I think this is bad, because here the variables initialized each loop. Really?
I recommend declaring them as local as possible, and as close as possible to the first use. This makes it easier for the reader to read the declaration and shows what type of variable and that it was initialized. And, of course, the compiler will optimize it.
. .
, , . ?
, , .
.
. C ++ -. C (, , pre-C99, - , !), ++ .
. , :
int i = 0; while (i < 5) { i++; } while (i < 5) { int i = 0; i++; }
, . , ...
int i = 0; while (i < 5) { std::String str = "The number is now " + std::to_string(i); cout << str << endl; i++; }
, , . , , .
, C, -std=c99.
-std=c99
, , , .
, : C , C, ( ). -, , , ( " " ) - , . , ++, , . , .
(int = 0;... , for ( , , ).
:
while (i < 5) { int i = 0; i++; }
, , , , i, , ( , ). , , .
, . , , , .
, ?
, , . .
. , , .
: , .
: , , .
: , , , , .
void student_score(size_t student_list_size) { // int exam; // int average; // int digit; // int counter_digits; /* The above variable declared are local to the function student_score and can't be used in other blocks. In your case its the for loop. */ for (size_t i = 0; i < student_list_size; i++) { int exam; int average; int digit; int counter_digits; ... /* The above declared variable are local to the for loop and not to the function.*/
#include<stdio.h> void fun(); int main() { fun(); return 0; } void fun() { int i,temp=100,avg=200; for (i=0;i<2;i++) { int temp,avg; temp = 10 + 20; avg = temp / 2; printf("inside for loop: %d %d",temp,avg); printf("\n"); } printf("outside for loop: %d %d\n",temp,avg); } Output: inside for loop: 30 15 inside for loop: 30 15 outside for loop: 100 200
, static ( , / ).
static
- . .
, .
Source: https://habr.com/ru/post/1569268/More articles:Streamline data type - performanceHow to perform bulk insertion in Mysql with auto increments of a primary key Guid? - mysqlShow all table rows in a table - phpUsing RAM memory for a Windows application - vb.netPost Android aar to artifactory - androidGstreamer does not play multiple streams at the same time - iosКрасноречивый выбирает первый из каждого отдельного 'id'? - phpCan I write several Assembly instructions on one line? - assemblyUILongPressGestureRecognizer дважды запускается - iosDoes FASM use Intel syntax? - assemblyAll Articles