Basically, I didn't know where to put this so I choose the Super of the 2 Languages.
Our lecturer gave us an assignment to lay out diff. b/w C & C++, which started with a rather dirty spurge of concepts.

He stated:
In C program , the MAIN() should NOT RETURN a value.
In C++,MAIN() should RETURN a value.
But going by the ISO Standards, main is expected to return an int. He stated I'm wrong. Can someone lend me a hand here & verify my correctness?

dynamic_cast<template> () is ALLOWED in C
Meh! Give me a break.

Apart from this I googled and came to know about some strange differences:
C does NOT have REFERENCE variables.
C++ ,REFERENCE variables are USED in functions.
Maybe?

C constants are defined as 'MACROS'.
We can make a use of 'CONST' TO declare a constant in C++
Huh? I doubt.

C does NOT SUPPORT DEFAULT arguments.
C++ PROVIDES DEFAULT argument concept.
Really?

I only need to verify if the following are correct or not.

Regards,
Nisheeth Barthwal

Recommended Answers

All 32 Replies

The relevant part of the C standard is in 5.1.2.2.1:

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int

C does not have inheritance, so obviously there is no dynamic_cast either.

There are references in C++, but not in C.

Yes, constants are indeed often realized through macros in C, although const exists just like in C++.

Yes, C does not support default arguments or any other function overloading for that matter. C++ does.

Some of those questions may be true or false depending on the version of c and c++ standards. But current c/c++ standards are like this:

>>In C program , the MAIN() should NOT RETURN a value
That's a lie, and has always been false. Either you misunderstood him, or it has no clue what he is talking about.

>>He stated I'm wrong.
Stop taking that course because all he will do is tell you more lies and mis-information.

>>C does NOT have REFERENCE variables.
True -- C has pointers

>>C++ ,REFERENCE variables are USED in functions
Obviously true. Reference variables could be declared globally but not used there.

>>C does NOT SUPPORT DEFAULT arguments.
True

Thank you for your reply. However can you please elaborate on this part:

Yes, constants are indeed often realized through macros in C, although const exists just like in C++.

I mean if we can just use const in C, then why use #define, off the top of my head, is it due to this? :

#define SZARRAY 5
const int szArray = 5;
.
.
char myArray[szArray];   //Invalid in C but not in C++?
char yourArray[SZARRAY]; //Valid in both.

Also this is from the ISO Standards regarding main:

There are two valid declarations:

	int main(void)
	int main(int argc, char **argv)
although they can be written in a variety of ways. The second parameter may be declared char *argv[] (see question 6.4), you can use any names for the two parameters, and you can use old-style syntax:
	int main()

	int main(argc, argv)
	int argc; char **argv;
See also questions 11.12b to 11.15.

[B]References: ISO Sec. 5.1.2.2.1, Sec. G.5.1 [/B]
H&S Sec. 20.1 p. 416 
CT&P Sec. 3.10 pp. 50-51

So I guess, it's nowhere written that C expects VOID MAIN!

>>it's nowhere written that C expects VOID MAIN!
compilers are (and do) allowed to support that.

>> mean if we can just use const in C, then why use #define, off the top of my head, is it due to this? :

Both C and C++ supprot macros and const. macros are used for other things which const can not.

>>>>In C program , the MAIN() should NOT RETURN a value
>>That's a lie. Either you misunderstood him, or it has no clue what he is talking about.
Well he dictated that for that matter, I refused to note down that point and earned myself a detention -_-"

>>>>He stated I'm wrong.
>>Stop taking that course because all he will do is tell you more lies and mis-information.
Wish I could. Earlier he was stating cout/cin were functions to send Output/Input. *face desk*

Another thing regarding wchar_t

wchar_t someWCHAR = L'ab'; //2 characters

According to me this should be invalid. Since 'ab' should be a string.
He says: wchar_t or Wide Character stores 2 bytes instead of 1 so we can feed 2 chars. Please tell me this is wrong.
From what I've learnt Wide Char is to support Unicode where ONE Character's Unicode information is stored in 2bytes unlike ASCII.

Thank you.

>>Well he dictated that for that matter,
That is a different story altogether. You mis-quoted him in your original statement. There is a difference between telling you he doesn't want your programs to do that then it is telling you the c standards say you can't do that. Its called "coding standards", which you will encounter throughout your programming career.

>>He says: wchar_t or Wide Character stores 2 bytes instead of 1 so we can feed 2 chars

Again you probably misunderstood him. Yes, wchar_t is more than 1 byte (sometimes 2, sometimes 4 or more depending on the operating system). That doesn't mean it stores two or more characters in just one wchar_t. wchar_t someWCHAR = L"ab"; Like all strings it has to be surrounded with double quotes, not single.

The reason wchar_t is more than 1 byte per character is because an alphabetical character in some languages can not be represented in just one byte.

Well since I had a discussion regarding this, it's little that I misunderstood him.
He clearly stated this & ordered me to SEARCH & VERIFY my claims.
1. Regarding void main()
He spoke & dictated: In C the main is *EXPECTED* to return void while in C++ int.

2. Regarding wchar_t
He wrote the following exact code:

wchar_t a = L'ab';

This is wrong as I've already clarified in my post & by your humble reply. But also

wchar_t a = L"ab";

should be wrong as wchar_t can store a character & it should've been either wchar_t* or some data type WideString.

I mean if we can just use const in C, then why use #define, off the top of my head, is it due to this?

Not at all, both variants are perfectly legal in C. In fact, you can even use non-constants as the array size since C99.
That #define is still used is probably because bad habits die slowly - const was added to C in 1990, but the language had been existing for quite a while until then.

commented: Informative. +1

Not at all, both variants are perfectly legal in C. In fact, you can even use non-constants as the array size since C99.
That #define is still used is probably because bad habits die slowly - const was added to C in 1990, but the language had been existing for quite a while until then.

I'd love to believe you but AD is stating otherwise. Maybe it is my fault I didn't specify that the discussion is regarding the current ISO Standards (C99).

I'd love to believe you but AD is stating otherwise.

No. I think your main problem is that you tend to misinterpret things people say - it's probably the same with your instructor.

Strange, I remember seeing in AD's post that const doesn't exist in C. @AD did you edit by any chance?
Well it's hard to misinterpret when the person is right in front of you & you ask numerous counter questions & he first asks you the answers then gives you a wrong one.

There should have been a * in that statement wchar_t *a = L"ab"; or like this wchar_t a[] = L"ab"; In any event you can't put two characters in single quotes and stuff them in a single byte of wchar_t.

>>@AD did you edit by any chance?

Yes. My statement was incorrect. C99 allows const, previous versions did not. You have to be careful about what versions of c standards people are talking about. The new Cx00 (or whatever it will be called) allows a lot of other stuff that is new to the C language. such as this

int x = 100;
int array[x];
commented: You could've said No & make me look stupid; but you didn't :) +1

Thank you for clarifying & supporting that I had not misunderstood. Also apologies for my inability to provide the correct details. Guess this thread should be marked "solved".

Thank you gentlemen.

On the main() function, see this SO-thread. Your prof is wrong on this matter. Even the original C standards have always defined main as returning an int, although the old standards allowed to omit the return type, in which case the int type was implied. This was changed in C99 standard and is no longer allowed. Also, it is possible that compilers accept a non-standard void main(). Finally, some C compilers may not complain if you don't give a value to return. Thus, in some weird old-standard inspired, but non-standard C compiler, this would be possible:

main() { //here return type is omitted but implied as int, not void.
  ...
  return; //or no return statement at all. All compilers I know would not allow this.
};

You are right about the wide-chars, they occupy two bytes of memory but represent one character. I don't know exactly how you write a literal wchar_t that has a non-ASCII value (> 0xFF), but still, it's not with 'ab' for sure.


Instead of arguing with your prof. Put him in front of a computer with C / C++ compilers (which are strictly compliant with the latest standard C99 and C++03 or C++0x-draft) and ask him to compile his own code snippets.

commented: Thank you. +1

Yes. My statement was incorrect. C99 allows const, previous versions did not.

const was introduced in C90, actually.

Instead of arguing with your prof. Put him in front of a computer with C / C++ compilers (which are strictly compliant with the latest standard C99 and C++03 or C++0x-draft) and ask him to compile his own code snippets.

If it weren't for those darn TC compilers, I'd be a happier man now.

>>I don't know exactly how you write a literal wchar_t that has a non-ASCII value

You need a keyboard that supports multi-byte languages, such as Chinese and Japanese.

Some more differences between C and C++
- C++ is more strongly typed than C
Using the most cliched in C++ we have to typecast the value returned by malloc

- I think the maximum length the variable can have in C is limited to 32 chars. What I mean is if the names of 2 variables are identical for the first 32 chars then they are treated same by the compiler

- Enums in C++ are a separate type

- Size of char constant is equal to the size of an int in C. But in C++ size of a char constant is equal to the size of a variable char

C is procedure oriented language..

C++ is a object oriented language

we have many differences between c and c++
1. C does'nt support name mangalling which is one of the technique of reducing the code length or one can optimize the code
2. c support variable size of arrays on the stack but C++ doesn't so execution of teh code is faster in C as compared to C++
3. C++ support oop concepts but c doesn't

C is procedure oriented language..

C++ is a object oriented language

I beg to differ, C++ is not an OO language.

I beg to differ, C++ is not an OO language.

What do you call the set of languages that support classes, inheritance, and runtime polymorphism? Unless you're planning on sperging out some "C++ is multiparadigm" crap, or unless you're a dynamic language weenie, there's no way you can say C++ is not an OO language with a straight face. You haven't bothered explaining yourself or being helpful, and your post is designed entirely to flaunt your sense of self-importance, so back your statement up or GTFO.

we have many differences between c and c++
1. C does'nt support name mangalling which is one of the technique of reducing the code length or one can optimize the code

It's not a technique for reducing the code length and it's not a technique for optimizing code. Name lengths don't affect function speed. Name mangling is an implementation detail used to exist in the C ecosystem.

2. c support variable size of arrays on the stack but C++ doesn't so execution of teh code is faster in C as compared to C++

Variable-sized arrays in C99 are very rarely used, and it's generally a bad idea to use them, and even if it weren't, you can use alloca in C++ anyway, and they're not faster than techniques that avoid using them (and avoid using alloca) except in marginal small-length one-time-use cases that don't affect program performance anyway (and if you did really care about performance you could resort to alloca anyway). So it's not a big deal or anything that people spend time worrying about.

What do you call the set of languages that support classes, inheritance, and runtime polymorphism? Unless you're planning on sperging out some "C++ is multiparadigm" crap, or unless you're a dynamic language weenie, there's no way you can say C++ is not an OO language with a straight face. You haven't bothered explaining yourself or being helpful, and your post is designed entirely to flaunt your sense of self-importance, so back your statement up or GTFO.

I am not sure why you are always angry and aggravated? But you need to stop taking it out on the users here. Go take a vacation or something.

Anyways, C++ has OOP but is not necessarily and object oriented language. Its not "sperging
out some "C++ is multiparadigm" crap. It is what it is. C++ supports both OOP and a C-style procedural programming. Even the creater of C++ says that C++ is a multiparadigm language

For C++ to be a complete OOP language, it would have such things as garbage collection.
Its not pure OOP language because not everything is an object. One could write a C++
program without even using one object. Even main is not a function of an object.

I would like to hear your argument of why C++ is a Object Oriented Language if you don't mind.

> For C++ to be a complete OOP language, it would have such things as garbage collection.

Why do you need garbage collection to be a "complete" OOP language?

> Its not pure OOP language because not everything is an object.

Actually in C++ everything is an object.

> One could write a C++ program without even using one object. Even main is not a function of an object.

main is an object.

> I would like to hear your argument of why C++ is a Object Oriented Language if you don't mind.

C++ lets people use inheritance to implement run-time polymorphism. And that's what an object oriented language is. (Why are you capitalizing "Object Oriented", is this some kind of religious thing like you see in the Bible?)

Why do you need garbage collection to be a "complete" OOP language?

Because Objects should be able to be created and destroyed by them selves.

Actually in C++ everything is an object.

No not really. A int, float, double, short and any variation of them are not object, they are primitive data types. Functions are not object as well.

main is an object.

I get the feeling that the way you are using the word "object" is in a different sense
than the way object refers to in Object Oriented Programming.

C++ lets people use inheritance to implement run-time polymorphism. And that's
what an object oriented language is.

Is that your definition or is it a standard definition? C++ also makes people go crazy
and insane, but that doesn't mean its a drug?

Why are you capitalizing "Object Oriented", is this some kind of religious thing like you see in the Bible?)

English is not my first language, so whats the correct way of writing Object Oriented?

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.