1- every variable declared outside of functions are static and have the default value of zero(0) .
int dp[100];
bool vis[100];
struct point { int x,y; };
point ps[100];
int main(){
// all elements of dp are zero by default
// all elements of vis are false
// all points have x == 0 and y == 0
}
2- ,
is an operator and returns its rightmost statement value , for example if A
and B
be statements (A,B) == B
.
if,for,while
without a block { }
take one statement so you can simply write multiple statements separated bye ,
int main() {
int a = 1, b = 2;
int c = a,b; // c == 2
int A[100];
int sumA = 0;
for(int i = 0; i < 100; ++i)
cin >> A[i],
sumA += A[i];
}
3- if you declare an array inside a function the value of its elements are garbage , but if you write TYPE var[LEN] = { };
all elements will be zero by default.
int main(){
int A[100] ; // elements of A have garbage values
int B[100] = { } ; // all elements of B are zero
}
4- memset
is defined in <string.h>
and sets the bye values of a block of memory . you can use memset
to initialize all elements of an integer array to 0
or -1
but not the other values .
int main(){
int A[100];
memset(A,0,sizeof(A)); // all elements of A are zero
memset(A,-1,sizeof(A)); // all elements of A are -1
//NOT WORKING
memset(A,other_numbers,sizeof(A)); // WRONG
}
help me update this post with other useful facts . have fun !