Hello everyone.

I am a new C++ learner and I have a question, is there only a constant C++ syntax in the world or there are many different types of C++?

Because last month when I surf internet to see somebody made his own C++ program, I saw it his C++ syntax is different with mine(not C).

The C++ syntax I learnt is:

#include<iostream.h>
main()
{
   int x;
   cout<<"Simply insert a number(in integer): ";
   cin>>x;
   cout>>"\nYour integer is : "<<x;
   return 0;
}

The syntax above is an example of my C++ syntax that I'm learning.

Got any different types of C++? Anyone please answer me, thanks. :)

Recommended Answers

All 9 Replies

Your syntax is unfortunately wrong, and is not C++. A long time ago, before C++ was standardised, it was just questionable, but now it's wrong.

main() is wrong. int main() is correct.

#include<iostream.h> should be #include<iostream>

cout<<"Simply insert a number(in integer): ";
This should not work, as cout lives in the std namespace. Your compiler should complain that it doesn't know what cout is. You should be using one of the following in the appropriate place:
using namespace std;
using std::cout;
or just std::cout instead of cout

The correct syntax for C++ is defined in the ISO standards; there is C++ 98, C++ 03 and now C++ 11 (which was known as C++ 0X for a long time, but is now just about standardised - I think it's been approved by the C++ board but not yet officially released). In all three cases, your C++ code above is incorrect.

If your compiler accepts your code, it is doing you a disservice and not telling you that you're making mistakes. There are many free compilers available that will point out these errors for you.

Your syntax is unfortunately wrong, and is not C++.

It's C++, just pre-standard C++.

A long time ago, before C++ was standardised, it was just questionable, but now it's wrong.

Not that long ago. :icon_rolleyes: Besides, if you look at it without the pre-standard hatred, the only questionable part is implicit int on main(). While implicit int was inherited from C and supported in pre-standard C++, it was well known for quite some time that taking advantage of it was poor practice. So stylistically it's questionable, but technically correct.

This should not work, as cout lives in the std namespace.

It works fine with <iostream.h> because namespaces weren't included yet. But once you change the header to <iostream>, then it should fail to compile.

If your compiler accepts your code, it is doing you a disservice and not telling you that you're making mistakes.

Or it's just a pre-standard compiler (can we say Turbo C++?). In those cases, standard code is incorrect because it won't compile, and not everyone can get a newer compiler. Perhaps it's you doing the disservice by being so intolerant. ;)

By all means inform people of the differences between pre-standard and standard C++, as well as the fact that pre-standard features are quickly being phased out (eg. modern compilers are removing support for <iostream.h>), but you can do that without treating pre-standard C++ like heresy.

commented: Well said. And very tactfully too. +13

Fair enough. I disagree with your tolerant attitude, but that's life.

>>is there only a constant C++ syntax in the world

Constant, no. Standard, yes. Standards get revised over time, so it's not constant. For C++, the dates to remember are 1998, 2003, and 2011. C++98 is the first ISO standard for C++, anything pre-dating that is "pre-standard" C++ (because the language was drafted and used long before that). Today, you could say that the vast majority of C++ code you see in books or tutorials are in the C++98 syntax (if they are good books or tutorials). The 2003 standard is just a small revision of the C++98 standard, and the differences are not significant (unless you are really advanced or if you are a compiler-vendor). Some extension to the standard libraries have also been introduced, called TR1 (Technical Report 1), which adds some standard features that aren't included in the ISO standard per se (so you might see things like std::tr1::shared_ptr). Then, lately, a new ISO standard is been issued which has be coined as C++0x (because of the hopes of having it release in the late 200x years), but I guess I and many others have to start getting used to saying C++11. That new standard is (almost) entirely compatible with the last one, so that standard C++98 programs are also standard C++11 programs, but you might see additional syntax that might look alien to you if you see a really modern C++ program (like "MyClass&&" or "std::move()" or "auto" and "-> decltype()").


>>there are many different types of C++?

Yes and no. It is like with normal languages, there are different dialects (like American, Canadian, and British English for example), then there are strongly related languages often with a common origin (like German and Dutch). To be able to tell what makes a language just a dialect of another or a new language in its own right is often difficult, but usually, you refer back to some standard definitions. So, per se, there is one type for C++ which can take different forms depending on the standard it complies to (pre-98, C++98/03, and C++11).

However, there are also many dialects of C++, and these are much more subtle. Most compilers are not truly standard compliant. Most compilers permit some things the standard does not. Some compilers provide additional features or keywords. Many compilers have "standard libraries" that include more than what the standard mandates (like std::hash_map that many library implementations have but is not standard). And it goes on like that. All these things are usually called "extensions" provided by compilers. Often, for example, people talk of the "GNU Dialect of C++" which comes from the fact that many people exclusively work on the GNU compiler (GCC) and end up using the extensions on GCC without knowing that their code is not strictly standard. But, to the un-trained eye, the "dialects" are indistinguishable from standard C++, it is just good to remember that if your code compiles on your compiler it doesn't mean it is standard C++ and that it would compile elsewhere, the only real way to know that is to compile your code with as many compilers as possible (which can be a hassle).

Then, there are many many languages that are not C++ but can easily be mistaken for C++, depending on your level of knowledge. The reason for this is that most modern languages are based on or strongly inspired by C++, so, the syntax is deliberately familiar to C++ programmers. Such "sister" languages would include, but not limited to: C++/CLI, C#, Java, and D. None of these are C++, but some pieces of code in those languages can easily be mistaken for C++, but overall they are very different.

Finally, C++ is a multi-paradigm programming language, meaning that there are different ways to design programs in C++. Each of these paradigms constitutes a kind of sub-culture of C++ programming, with its own terminology and commonly found syntax (all standard, it just differs mostly in style and design patterns). But that's not something for you to worry about at this point, you will learn it with time.


>>The syntax above is an example of my C++ syntax that I'm learning.

Well, as said by others, that is a piece of pre-standard C++ code. It is not wrong per se, but just very out-dated, to the point that it might not compile on all contemporary C++ compilers (personally, I have no clue about the status of "pre-standard C++ support" in modern compilers, if it is phasing out or still kept for compatibility, I don't know).

If you are learning from a book or course and this kind of syntax is what is being taught, then I would suggest you either get a more recent book or tell your prof to move to the 21st century, because the C++ language has. I highly recommend that you don't spend to much time learning such pre-standard syntax, it will just make it harder to re-learn modern C++ programming (C++98/03 to today C++11).

EDIT: sorry double post.

Thanks for your answer... So, where can I find a newer version of C++'s tutorials? I want a complete set, not pieces of parts.

And, please don't suggest me the books, my town can't find any programming books, and I'm a teen only, don't have any cards to buy online.

Yesterday I googled about C++0x tutorials, there are few youtube tutorials, but they were pieces into parts and no complete.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.