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".
Related
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 2 years ago.
Improve this question
I rarely log on and am very new to the C language, therefore I apologize if this is a duplicate question or if this is a silly query.
I'm currently learning C and am hitting a wall with strings. I understand that char arrays are used in place of strings in the language. My question is, is there a better way than to assign an arbitrary value when declaring a char[] for user input(i.e setting the size of the array to one value when the user might enter more or less than that amount of characters)?
If you're on a POSIX system (basically anything that's not Windows), you can use getline(3) to do this. It will automatically allocate a buffer of the right size for you. Otherwise, you'll have to guess a length, allocate that, then read the input up to that length, and if you guessed wrong, use realloc to increase your guess and try again.
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 saw this special macro when I read a source code. If I remember correctly, it is defined in the standard library.
The name of this macro is related to the buffer size, and in my machine its implementation is 1024.
Now I want to use it to initialize the buffer but I forgot what it is called.
So is there any one who can help me make my code look more professional?
If I don't know what I am looking for specifically, how can I clearly say what I need?
Are you talking about BUFSIZ? It's a macro provided by <stdio.h> and it expands to the size of the buffer used by setbuf().
I'm not sure what use it has in your own code.
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.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I find that I refer to the value at the memory location pointed to by a pointer far more often then I want to refer to the actual value of the pointer. As a result I wonder why C does not use the asterisk in the to refer to the actual value of the pointer since it is more typing.
I have already read a post on another website about how B did it this way. This does not answer my question; it just changes it to why did B do it this way.
I doubt anyone can tell you why it was designed the way it was. But a pointer is a variable that holds an address. Therefore, it's value is the address. I would find it strange and unexpected if it required special syntax to get the value of any variable.
Note that C++ lets you use references to access the value pointed to without special syntax. But you are dereferencing the pointer, so for me it makes perfect sense to require a different syntax.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
This is a slightly subjective answer, but how large do you think a structure should be before you start using pointers to it in other structures or function calls rather than the structure by-value?
Depends on the compiler and architecture.
C and C++ define the size of the types they use, and how functions are written, but they don't define how they are implemented.
This is means the standard itself doesn't define how the structurs are passed, just that they are in essence copied.
The compiler might decide to do something else entirely, like not copying the struct at all if there's a default copy constructor , and the variable isn't used.
But after saying all that, most common-sense compiler implementations would store the struct in a register if it fits. So depends on the architecture, make sure the structure fits in a register.