473,473 Members | 2,304 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Is there something that I can do in C and not in C++?

1 New Member
Is there something that I can do in C and not in C++?
May 18 '07 #1
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:

Expand|Select|Wrap|Line Numbers
  1. struct A {
  2.   int a;
  3.   char b;
  4.   int c;
  5. };
  6.  
  7. struct A obj = { b = 'd' };
  8.  
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
May 18 '07 #2
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:

Expand|Select|Wrap|Line Numbers
  1. struct A {
  2.   int a;
  3.   char b;
  4.   int c;
  5. };
  6.  
  7. struct A obj = { b = 'd' };
  8.  
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
May 18 '07 #3
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:

Expand|Select|Wrap|Line Numbers
  1.       struct A {
  2.         int a;
  3.         char b;
  4.         int c;
  5.       };
  6.  
  7.       struct A obj = { .b = 'd', .c = 5 };
Note the '.' before b = 'd'. Also seperation between is using a comma, not a semicolon.


Adrian
May 18 '07 #4
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
May 18 '07 #5
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
May 18 '07 #6
weaknessforcats
9,208 Recognized Expert Moderator Expert
Expand|Select|Wrap|Line Numbers
  1. struct A obj = { obj.b = 'd' };
  2.  
works in C++.
May 19 '07 #7
AdrianH
1,251 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1. struct A obj = { obj.b = 'd' };
  2.  
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:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int main() {
  3.       struct A {
  4.         int a;
  5.         char b;
  6.         int c;
  7.       };
  8.  
  9.       struct A obj = { obj.b = 'd', obj.c = 5 };
  10.  
  11.       printf("%d, %c, %d\n", obj.a, obj.b, obj.c);
  12.       return 0;
  13. }
No compile error, but doesn't work either (at least not under g++ with only -Wall switch).


Adrian
May 20 '07 #8
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
May 20 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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...
8
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...
5
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...
7
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,...
44
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...
5
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;
7
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...
4
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
2
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...
5
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...
0
marktang
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,...
0
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...
0
jinu1996
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...
0
tracyyun
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...
1
isladogs
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.