Is there something that I can do in C and not in C++?
8 2087 AdrianH 1,251
Recognized Expert Top Contributor
Is there something that I can do in C and not in C++?
Spell its name with a single character. :D
C++ is a superset of C (more or less, less now that C99 is out). C99 has a bunch of things that C++ cannot do, such as specifying initialisation elements: -
struct A {
-
int a;
-
char b;
-
int c;
-
};
-
-
struct A obj = { b = 'd' };
-
That is valid using the new C standard (called C99). It is not valid in C++.
Variable arguments for macros is another one.
Those are two of the most prominent difference I've found. There are undoubtedly others. Check out the difference by looking up C99 on the web. If I'm not mistaken, C++ is a true superset of C89. I could be wrong though.
Adrian
Motoma 3,237
Recognized Expert Specialist
Haha, when I read this I could not believe it. I had to fire up g++ and test out for myself that this would not work in C++. Thanks for the post AdrianH!
Spell its name with a single character. :D
C++ is a superset of C (more or less, less now that C99 is out). C99 has a bunch of things that C++ cannot do, such as specifying initialisation elements: -
struct A {
-
int a;
-
char b;
-
int c;
-
};
-
-
struct A obj = { b = 'd' };
-
That is valid using the new C standard (called C99). It is not valid in C++.
Variable arguments for macros is another one.
Those are two of the most prominent difference I've found. There are undoubtedly others. Check out the difference by looking up C99 on the web. If I'm not mistaken, C++ is a true superset of C89. I could be wrong though.
Adrian
AdrianH 1,251
Recognized Expert Top Contributor
Haha, when I read this I could not believe it. I had to fire up g++ and test out for myself that this would not work in C++. Thanks for the post AdrianH!
What? You didn't know that C could spell its own name with a single letter? :D
Actually I think it should have been: - struct A {
-
int a;
-
char b;
-
int c;
-
};
-
-
struct A obj = { .b = 'd', .c = 5 };
Note the '.' before b = 'd'. Also seperation between is using a comma, not a semicolon.
Adrian
JosAH 11,448
Recognized Expert MVP
C++ is a true superset of C89. I could be wrong though.
No it isn't. The widening promotions are different in C (both C89 and C99). e.g.
a char will be an int when passed as a parameter to a function that takes a
variable number of arguments in C; it will be a char in C++ though ...
but I agree: the differences are minor (but essential).
kind regards,
Jos
AdrianH 1,251
Recognized Expert Top Contributor
No it isn't. The widening promotions are different in C (both C89 and C99). e.g.
a char will be an int when passed as a parameter to a function that takes a
variable number of arguments in C; it will be a char in C++ though ...
but I agree: the differences are minor (but essential).
kind regards,
Jos
True. Forgot about that one.
Adrian
-
struct A obj = { obj.b = 'd' };
-
works in C++.
AdrianH 1,251
Recognized Expert Top Contributor -
struct A obj = { obj.b = 'd' };
-
works in C++.
WOW, it is surprising that it compiles (without warnings I might add with full warnings enabled). What is not as surprising is that it doesn't work. ;)
Try this code: -
#include <stdio.h>
-
int main() {
-
struct A {
-
int a;
-
char b;
-
int c;
-
};
-
-
struct A obj = { obj.b = 'd', obj.c = 5 };
-
-
printf("%d, %c, %d\n", obj.a, obj.b, obj.c);
-
return 0;
-
}
No compile error, but doesn't work either (at least not under g++ with only -Wall switch).
Adrian
Yeah. No warnings but it doesn't work in Visual Studio.NET.2005 either. Previously, I just reported on th basis of no compiler messages.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Martin Lucas-Smith |
last post by:
I am trying to take a string and split it into a filename and a number
where the number is what follows the *last* instance of a comma in that
string.
Split and explode won't work because if the...
|
by: Sam Sungshik Kong |
last post by:
Hello!
I use Python for ASP programming.
I found something weird.
Response.Write(Request("something"))
It draws "None" when there's no value for something.
Actually I expect "" instead of...
|
by: Jason |
last post by:
Hello. I am trying to learn how operator overloading works so I wrote
a simple class to help me practice. I understand the basic opertoar
overload like + - / *, but when I try to overload more...
|
by: Zero |
last post by:
If we have a structure like:
struct something{
int *a;
int b;
};
We allocate mempry for a using malloc or calloc. The question is when
we want to know the size of the structure,...
|
by: Tolga |
last post by:
As far as I know, Perl is known as "there are many ways to do
something" and Python is known as "there is only one way". Could you
please explain this? How is this possible and is it *really* a...
| |
by: Daniel Vukadinovic |
last post by:
Can anyone explain me these things in C++?
1.What is :: used for like in the next case:
if(....
{
...
::one;
|
by: cj |
last post by:
I'm sure it's simple but it's new to me
I can dim x as new something which defines x as a new instance of
something in one statement. I can also dim x as something then later
make x = new...
|
by: D.Hering |
last post by:
When I see something like this prototype:
SOMETHING int func( int x );
what is SOMETHING doing there?
thanks very much,
-Dieter
|
by: Andrew Backer |
last post by:
I hope someone out there can help me with this, because I am stuck.
My problem is that I have an instance of a generic class, but I don't know
ahead of time what the type parameter used was. In...
|
by: Ayebaleet |
last post by:
Hello everybody,
I understand the basics of Java, but not such features as are helpful in an applet namely Listeners.
I wanted to create a feature like this:
A program would be displayed in...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |