My system is Ubuntu
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define LEN 16
using namespace std;
int main(){
int a[16] = {2};
for (int i=0; i<16; i++)
{
cout << a[i] << ' ';
}
}
I compiled it by this command in terminal : g++ t1.cpp -o t1 && ./t1
but the result is
2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2
Answers
Any decent book, tutorial or class should have taught you that the definition
is equivalent to
If you want to initialize all elements to a single value you need to explicitly do it.
You can also use
std::fill
after definition to set every element to a value:And some nitpicking: What you’re doing is initialization, not assignment.
You’ve assigned a
2
to index0
of your array.You may wish to use
std::fill
.