-2

I am a novice programmer in C.

Here is a program:

#include <stdio.h>

void main()
{
    int a,b, c ;
    printf("%d %d %d",a,b,c);
}

output:

7811664 31 2130567168 (1st time)
4665936 31 2130567168 (2nd time)

I didn't get this output?

  • 3
    Because they are not initialized, what did you expect? – Yu Hao Oct 19 '13 at 12:33
  • @UndefinedPothik Oh my God. 1. Why should they? 2. `void main()` is an error. `main()` returns `int`. 3. **Can't you use whitespace?** Just have a look at your code. It's only five lines but it's already a mess. 4. It's a "program". A "programme" is something completely different. –  Oct 19 '13 at 12:36
  • 1
    @UndefinedPothik Why only `a` is changing isn't really a C question. The C standard guarantees nothing about them; they are undefined. – potrzebie Oct 19 '13 at 12:37
  • @AdamLiss Maybe he's learning from some very bad resources then. (Also, how is not using proper indentation related to the fact that OP is a beginner?) –  Oct 19 '13 at 12:39
  • H2co3 can you suggest me good resources? – Undefined Pothik Oct 19 '13 at 12:41
  • @UndefinedPothik Sure. First, **the** book on C is K&R (Kerninghan and Ritchie, the creators of C): "The C programming language". Furthermore, there's a long list on SO with good book recommendations, I'll find the link for you, a moment. –  Oct 19 '13 at 12:42
  • @UndefinedPothik [Here's the list](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list), choose wisely. –  Oct 19 '13 at 12:43
  • how "teach yourslf c"-herbart shield? – Undefined Pothik Oct 19 '13 at 12:54

3 Answers3

3

These are garbage values as you have not initialized the variables. As pointed in one of the below links Unassigned variables has so-called indeterminate state that can be implemented in whatever way ie, its an undefined behavior.

On a side note:-

The garbage values are not assigned they are allocated to the variable when they are declared ie, the value is already there. When you initialize the variable the garbage value is overwritten

For refernce you may like to check these Threads:-

Also worth mentioning that the main should have a int return type rather than void

You may check this:-

In C and C++, the function prototype of the main function looks like one of the following:

int main(void);
int main();

int main(int argc, char **argv);
int main(int argc, char *argv[]);
Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
2

You've declared the variables, which means the compiler has allocated memory to them, but you haven't initialized them, which means their memory contains whatever data happened to be present when the the program began.

Since the program may not always be loaded into the same area of memory, and since the contents of memory can change between runs, the uninitialized values appear to be random.

This is a perfect demonstration of the reason you should always initialize variables before using them.

Adam Liss
  • 47,594
  • 12
  • 108
  • 150
  • 1
    Before *reading* them, that is. Third: the program has double-UB because of `void main` and because of reading the uninitialized variables. –  Oct 19 '13 at 12:38
  • I thought this detail was missing i.e. mentioning a random address in memory could contain random data unless assigned and started writing it. Then saw this answer covering that so stopped and instead upvoted. – fkl Oct 19 '13 at 12:38
1

Why not give them values? You got undefined behaviour

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • Yay, only correct answer so far. –  Oct 19 '13 at 12:40
  • Answer by @Adam Liss is perfectly correct. Rather it explains the reason too. The original focus of question is not on the return value of main, though it is worth mentioning. – fkl Oct 19 '13 at 12:42