It is necessary to assign a value to a variable in C once we declare it?
No, that made less sense in older compilers than it does now. The first C compilers required declaring the variable at the beginning of the function before it had any real execution, so if it had to assign a value in the statement there would be an execution (processing cost) that right there in the front would be despised and its value replaced by another in a new assignment, which is inefficient and against the philosophy of C, so assigning value in the statement was in large part disincentive cases.
Over time compilers were able to understand declaration only at the time that the variable is needed, and this is already in the language specification since 1999, so any compiler that doesn’t meet a specification 20 years ago is a rotten compiler and shouldn’t even be considered C anymore.
Unfortunately some people teach programming this way, either because they can not do different than what they learned, only follow cake recipe, or because they believe in the myth that has compiler that does not accept this form (yes, it does, but again it cannot be considered C and most codes will never be compiled by a compiler like this, and the solution to the case if it is necessary to compile this code in a outdated compiler is to use a tool that processes this code before passing to the compiler, so it does not force the code to be worse by an exception (unless it is no exception, have to look at the concrete case), but people do not usually behave like engineers and do not seek appropriate solutions, just follow the flow of someone who had a bad idea one day).
But still in modern compilers it may have some less common case that the statement must be made before its value is assigned and still worth the philosophy of maximum efficiency, therefore C does not require assigning a value in the statement. A typical example is if this variable will receive an external data entry, where there will clearly be an assignment, then why assign a value that will be dropped before its use? This applies to any function that will receive the variable as a parameter by reference only to fill it. And there may be other cases.
The experienced programmer who told you this is right for modern compilers in most cases, but maybe he didn’t say it the right way, or you didn’t quite understand what he said. You should always assign a value to the variable before using it. It does not need to be in the statement, but at some point between the statement and the first access to that value, it may even be in the statement itself, and today in the overwhelming majority of cases it is the ideal place to do this (read one of the most important exceptions in the previous paragraph).
If you fail at this, you’ll access junk that’s in your memory, and the worst that can happen to you is to work, because you’ll think you’re right. Some cases the garbage may be exactly what you expect and look right, but in other cases it will not be so. C is probably the language of "high level" where this maxim is worth more:
One way to prevent this from happening is to assign it right away in the declaration, but do not do so with a single general rule. It’s standard behavior until you have a reason to do it differently.
If you add this as a cake recipe you are learning wrong, if you understand the real motivation and then make a decision depending on the specific case you are making, then you have become a real programmer. And if you improve that skill and always look for creative and sensible solutions to problems you will become an engineer.
My compiler does not generate this random number as it says
It worries me, need to understand what is this "random" in order to make correct decisions (see comment below).
This form would not be for old compilers?
As I said, no, on the contrary, in old compilers it made even more sense that today not assign values during the declaration in most cases.
And if necessary, as I do for character type variables?
Just like other guys, there’s no difference:
char c = 'x';
Helped? I guess not because it depends on the context. There are several reasons to do it one way or another.
Now without declaring and makes sense (here is context):
char c;
scanf("%c", c);
Has a example with context.
You want to do it like string?
char texto[] = "teste";
An example of absurd attribution in unnecessarily declaration:
FILE *fp = NULL;
//mais código aqui
fp = fopen("Emails.txt", "rt");
I put in the Github for future reference.
Only one thing is worse than this, is to have no code between the declaration and the actual initialization. Not by performance, but because it makes no sense to leave in separate lines by no criteria.
See more in:
It is not necessary to assign value to the variable immediately after declaring it.However, we must always assign a value to the variable before it is used. For example, you can declare the variable at the beginning of your code, and only assign value immediately before using the variable.
– IanMoone