I have something like,
我有类似的东西,
#include <stdio.h>
#include <stdbool.h>
int main()
{
int t,answer;
bool count;
long long int L;
scanf("%d",&t);
while(t>0)
{
answer = 0;
scanf(" %lld",&L);
bool count[L];
// .....restofthecode. NDA Constraint.
What would be the default value of all the elements of arr[x]
? Is it false
always? Or true
? Or any random value?
arr [x]的所有元素的默认值是什么?这总是假的吗?还是真的?或任何随机值?
2 个解决方案
#1
4
There is no type named boolean
in C but there is _Bool
and in stdbool.h
a macro bool
that expands to _Bool
.
在C中没有名为boolean的类型,但是有_Bool,在stdbool.h中有一个扩展为_Bool的宏bool。
#include <stdbool.h>
#define X 42
bool arr[X];
arr
elements have an initial value of false
(that is 0
) if declared at file scope and indeterminate if declared at block scope.
如果在文件范围内声明,则arr元素的初始值为false(即0),如果在块作用域中声明则不确定。
At block scope, use an initializer to avoid the indeterminate value of the elements:
在块作用域中,使用初始值设定项来避免元素的不确定值:
void foo(void)
{
bool arr[X] = {false}; // initialize all elements to `false`
}
EDIT:
Now the question is slightly different:
现在问题略有不同:
long long int x;
scanf("%lld",&x);
bool arr[x];
This means arr
is a variable length array. VLA can only have block scope, so like any object at block scope it means the array elements have an indeterminate value. You cannot initialize a VLA at declaration time. You can assign a value to the array elements for example with =
operator or using memset
function.
这意味着arr是一个可变长度数组。 VLA只能有块作用域,因此与块作用域中的任何对象一样,这意味着数组元素具有不确定的值。您无法在申报时初始化VLA。您可以为数组元素赋值,例如使用=运算符或使用memset函数。
#2
1
As per your code, in local scope
根据您的代码,在本地范围内
boolean arr[x];
itself is invalid. x
is used uninitialized.
本身无效。 x未使用初始化。
Just FYI, in global [file] scope, all the variables are initialized to 0
. In local scope, they simply contain garbage, unless initialized explicitly.
只是FYI,在全局[file]范围内,所有变量都初始化为0.在本地范围内,它们只包含垃圾,除非明确初始化。
EDIT:
[After the edit] All the variables in the arr
array will have garbage value. It is in local scope [auto].
[编辑后] arr数组中的所有变量都将具有垃圾值。它在本地范围[auto]。
#1
4
There is no type named boolean
in C but there is _Bool
and in stdbool.h
a macro bool
that expands to _Bool
.
在C中没有名为boolean的类型,但是有_Bool,在stdbool.h中有一个扩展为_Bool的宏bool。
#include <stdbool.h>
#define X 42
bool arr[X];
arr
elements have an initial value of false
(that is 0
) if declared at file scope and indeterminate if declared at block scope.
如果在文件范围内声明,则arr元素的初始值为false(即0),如果在块作用域中声明则不确定。
At block scope, use an initializer to avoid the indeterminate value of the elements:
在块作用域中,使用初始值设定项来避免元素的不确定值:
void foo(void)
{
bool arr[X] = {false}; // initialize all elements to `false`
}
EDIT:
Now the question is slightly different:
现在问题略有不同:
long long int x;
scanf("%lld",&x);
bool arr[x];
This means arr
is a variable length array. VLA can only have block scope, so like any object at block scope it means the array elements have an indeterminate value. You cannot initialize a VLA at declaration time. You can assign a value to the array elements for example with =
operator or using memset
function.
这意味着arr是一个可变长度数组。 VLA只能有块作用域,因此与块作用域中的任何对象一样,这意味着数组元素具有不确定的值。您无法在申报时初始化VLA。您可以为数组元素赋值,例如使用=运算符或使用memset函数。
#2
1
As per your code, in local scope
根据您的代码,在本地范围内
boolean arr[x];
itself is invalid. x
is used uninitialized.
本身无效。 x未使用初始化。
Just FYI, in global [file] scope, all the variables are initialized to 0
. In local scope, they simply contain garbage, unless initialized explicitly.
只是FYI,在全局[file]范围内,所有变量都初始化为0.在本地范围内,它们只包含垃圾,除非明确初始化。
EDIT:
[After the edit] All the variables in the arr
array will have garbage value. It is in local scope [auto].
[编辑后] arr数组中的所有变量都将具有垃圾值。它在本地范围[auto]。