473,597 Members | 2,749 Online
Bytes | Software Development & Data Engineering Community
+ 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 2098
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
5990
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 filename part of the original string contains a , then I end up with too many parts. if (!preg_match ('/^(.*),(+)$/', $string, $matches)) {throwError ();} also fails, because the first part of the regular expression becomes greedy if there is...
8
2214
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 "None". So I changed it like
5
3793
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 complex operator, I get stuck. Here's a brief description what I want to do. I want to simulate a matrix (2D array) from a 1D array. so what I have so far is something like this: class Matrix
7
7191
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, sizeof(struct something), it will not give us the correct number (i.e. the size of the structure
44
4176
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 good concept?
5
1929
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
1635
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 something which does the same thing in a two step process. Here's what I don't understand and really not sure how to ask but I want to dim x as something when a program starts and this should be valid for as long as the program is running. Then when...
4
1540
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
2870
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 my case the type must derive from a class, like UserControl, so I at least know I should be able to use that. I can not figure out a way to get this as an instance of the generic class, but with the base class as the type parameter. I am...
5
1550
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 the regular old System.out. It would be looping continuously doing something unimportant, say counting how many times it could count to a billion, until the user enters a command, and then it would simply delay what it was doing, display something...
0
7969
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7886
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8272
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8035
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6688
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3886
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3927
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2404
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1494
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.