0

I wrote a very basic program but was not able to understand the behaviour of it.

     # include<stdio.h>
     # include<iostream.h>
     # include<conio.h>

     using namespace std;
     int main()
     {
       cout << "50" << oct <<"50" << hex <<"50" << abc << "50";// error abc not defined
       cout << "50" << oct <<"50" << hex <<"50"; // No error output 505050
       getch();   
    } 

Are oct and hex defined as some macro in any of the files that I have included which is the reason why I don't get an error for second cout statement?

Anders Lindahl
  • 41,582
  • 9
  • 89
  • 93
Invictus
  • 4,028
  • 10
  • 50
  • 80

3 Answers3

6

oct and hex are stream manipulators defined in <ios>, whereas abc is not a symbol defined in any of the Standard header. Hence you see the error about only abc as you've not declared it yourself in your program.

Apart from that it seems that you're using very old compiler which comes with <iostream.h>. I would suggest you to update your compiler (or switch to better compiler), and use <iostream> instead of <iostream.h> which is not Standard header.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
3

You are making a big mistake here. You should not try to learn C++ by just reading source code and experimenting. This is a path that will lead you nowhere.

The only way to try to learn C++ is to get a good book and read it cover to cover.

C++ is illogical in many places and no matter how smart you are there is no way you will guess correctly all of C++. Actually being too smart can be a problem for guessing because there are cases in which the C++ language design contains true mistakes that cannot be removed for backward compatibility reasons. If you are smart your guesses will be logical... and dead wrong.

Also note that while often you can write a few clear and reasonably looking lines of C++ and they will make the right thing efficiently, it's also well possible to write another few lines that are even more logical and good looking but that instead will have horrible performance and/or undefined behavior.

Undefined behavior also means that when you make a mistake in many cases the compiler and the runtime will not complain and things will just start behave crazily after a while. Undefined behavior is the very reason for which experimenting is not a good path for learning C++.

Community
  • 1
  • 1
6502
  • 112,025
  • 15
  • 165
  • 265
  • While it is a good post in general, it doesn't answer the question. Initial two lines addressing the problem with an answer, would have been better. – Nawaz Apr 29 '12 at 07:13
  • I think this is addressing the *real* issue. Explaining why `abc` is not recognized and what `hex` really means IMO doesn't help because the explanation cannot be understood unless you also understand what `using namespace std` does and you also dig into the IMO broken design of C++ output formatting. OP will probably just memorize some nonsense wrong rationalization and will suffer even more later. – 6502 Apr 29 '12 at 08:18
1

hex and oct are manipulators that are included in the std namespace. Their full names are actually std::hex and std::oct. There is no manipulator named abc, so that is why you get the error.

More info at www.cplusplus.com : std::hex and std::oct

Ove
  • 6,227
  • 2
  • 39
  • 68