can you assign initial value to global static variable in C? [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a global variable "count". All I want to do is increment it each time the loop runs. Is there a potential problem with initializing static count as 0? How is this works in C?
static unsigned short count = 0;
while(1)
{
count++;
// do something
}

Yes you can, why did you think you can't? But if the value is 0 you can skip the initialization since static variables are automatically initialized to 0. Also, as #M.M commented here it's mandatory that the value is a constant, you can't assign the result of malloc() for instance.
There is no potential problem, and there is no reason to think that there would be a problem. Except integer overflow, in your code that's pretty sure going to happen but it has nothing to do with the storage class of your variable.

Related

Why VS Code, C build works but run code not? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
This is my code. When i run the code it sends me an error:
helloworld.c:8:16: error: storage size of 'bin' isn't constant
static int bin[size];
but when i do a build the exe works. Help pls.
The initializer of a static variable must be known at compilation time. That's why the declaration of bin is rejected. Instead you need to declare bin as a pointer to an integer and allocate memory dynamically, that is at run-time:
int *bin = malloc(size * sizeof *bin);
As pointed out by rioV8, you should also free the allocated memory with free(bin) when you are done with bin.

Is there a difference between "unpredictable" and "garbage" values? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
According to this Guru99 web page, for the storage class auto, since it's local its value is unpredictable if not defined. However, for the storage classregister, its value is garbage if not defined. I'm confused with the words garbage and unpredictable. Can you help with that?
Is there a difference between “unpredictable” and “garbage” values?
Yes, or no depending on what the communicator means by the words. "Unpredictable" is an adjective that can be used to describe "garbage" values.
"Unpredictable" can have a much more general meaning. For example, the result of rand() can be considered unpredicrable (as long as you don't know that the generator has been seeded with), but it is generally not considered "garbage".

I can't understand the placement c=0 and l=0 in the following piece of code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I can't understand the placement c=0 and l=0 in the following piece of code.Here n is number of element in array a and m is number of element in array b.
for(i=2;i<=16;i++){
c=0;
for(j=0;j<2;j++)
{
if(i%a[j]==0)
c++;
}
if(c==2)
{
l=0;
for(k=0;k<3;k++)
{
if(b[k]%i==0)
l++;
}
if(l==3)
count++;}
}
Please help. Thanks.
According to what I read in the comments, what seem to puzzle yourself are so-called local variables.
When you define a variable somewhere, its scope actually spans only to the limits of the block it's written into (from { to }). Its space is dynamically reserved in the stack at the moment you enter the block and is automatically released when you leave it, as the stack frame is destroyed.
It's always a good idea to declare variables only where they're really used instead of gathering all declaration at top of program. First because you ensure this way that your memory will be allocated only when you need it and will be automatically released as soon as possible. Then because it avoids polluting the namespace with identifiers which could be evaluated where they're not relevant, making bugs harder to find.

Is it a good practice to initialize a dynamically allocated structure with all 0 using memset(), if yes what are the benefits? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
In a client-server system where its possible that server may populate only some of the IE and rest has to be treated as default value (0) at client side.
For such a system, is it a good idea to do memset(.., 0, ...) of dynamically allocating memory for the received message before copying the contents of message?
There is no need to initialise memory which immediately gets overwritten after initialisation.
Also unused memory does not need to be initialised. ("unused" here means, that it will never be read.)
All other memory needs to be initialised. Whether this would be done by using memset() and writing 0s to it depends on the specific context and use-case.

Use of static and global variable [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I was going through global and static variables.I have a question.
If I have a .c file and it is having only one user defined function(ABC()). Suppose I have to preserve the value of a variable in that function. Should I make that variable static(locally in that function) or make it global. Which one is best way and why.
Rule of thumb: Define variables/functions in the smallest scope possible while avoiding redundant code and data.
I recommend making the variable a static variable defined in the scope of the function.

Resources