Is there something that I can do in C and not in C++?
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: -
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 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...
|
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
|
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
|
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
|
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?
| |
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 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...
|
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 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...
|
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...
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |