473,656 Members | 2,762 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Any C code are valid C++ code?

Since C is a subset of C++, so any C code or C libraries (printf(),
scanf(), etc...)
are valid C++ code. Is that correct? so even though the whole program
is written in
C, but with .cpp extension, we still consider as C++ program?
Please advise. Thanks

Jul 22 '05 #1
64 3491
jr********@hotm ail.com wrote:
Since C is a subset of C++ [...]


Wrong premise. Wrong conclusion. The answer to your subj is "no".
Jul 22 '05 #2
The answer is 'no' in general.For example one of the basic difference
(even if you create a simple hello world program and compile it under
with a C and a C++ compiler) between C and C++ relies almost in the
name mangling mechanism. C++ uses an extended decoration method to give
the linker indications about the name resolutions.
That tecnique is not compatible with C declaration naming.
This is the reason why you have to specify extern "C" {... } around C
code to ensure
compatibility.
Obviously there are other things that makes the two language very far
even so similar! ;)

Gianguglielmo

Jul 22 '05 #3
jr********@hotm ail.com wrote:
Since C is a subset of C++


C is not a subset of C++. C++ has some incompatible changes from C.
However, they are compatible enough that a lot of code runs in both.

Some incompatibiliti es includes:

* different linking mechanisms (this is workaroundable w/ extern C)
* different interpretation of multidimensiona l arrays
* many C programs include typedefs and define which override C++
keywords, and therefore aren't allowed in C++ (typedef int bool; for
example)

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Jul 22 '05 #4

"Jonathan Bartlett" <jo*****@eskimo .com> wrote in message news:41******** @news.tulsaconn ect.com...
jr********@hotm ail.com wrote:
Since C is a subset of C++
C is not a subset of C++. C++ has some incompatible changes from C.
However, they are compatible enough that a lot of code runs in both.

Some incompatibiliti es includes:

[snip]
* different interpretation of multidimensiona l arrays

What is the difference?

[snip]

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 22 '05 #5
jr********@hotm ail.com wrote:
Since C is a subset of C++,
Wrong. A common notion that is completely wrong.
so any C code or C libraries (printf(), scanf(), etc...)
are valid C++ code.
Not true. For example, the following valid C program is not valid C++:

#include <stdlib.h>

int main(void)
{
/* new is a reserved word in C++ */
char new, *buf;
/* Implicit conversion from void* to char* not valid in C++ */
buf = malloc(1024);
free(buf);
return 0;
}

It is the case that you can use the C standard library functions in C++
code. However, it is rarely the best way to accomplish the task.
Is that correct? so even though the whole program
is written in
C, but with .cpp extension, we still consider as C++ program?


You can consider it a C++ program when it conforms to the relevant
standards. Writing in C is not a good way to conform to those standards.
Jul 22 '05 #6
In article <11************ **********@c13g 2000cwb.googleg roups.com>
<jr********@hot mail.com> wrote:
Since C is a subset of C++, so any C code or C libraries (printf(),
scanf(), etc...) are valid C++ code. Is that correct?


No.

Compile the following program as a C program and run it. Then,
compile it as a C++ program and run that. Observe the different
output.

#include <stdio.h>

struct A { char c[1000]; };

int main(void) {
struct B { struct A { char c[1]; } a; char c[1]; };

printf("sizeof( struct A): %lu\n", (unsigned long)sizeof(str uct A));
return 0;
}

This is, of course, a carefully-constructed example -- but real C
programs really do fail to work when compiled as C++ programs,
sometimes, because of small but significant semantic changes.

(Exercise: *why* is the output different?)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jul 22 '05 #7
On Fri, 10 Dec 2004 20:42:03 +0200, in comp.lang.c , "Alex Vinokur"
<al****@big-foot.com> wrote:
"Jonathan Bartlett" <jo*****@eskimo .com> wrote
* different interpretation of multidimensiona l arrays
What is the difference?


C lets you blur the distinction between ** and *[ ] and [ ][ ] rather
more, especially in function calls.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >
Jul 22 '05 #8
* different interpretation of multidimensiona l arrays


What is the difference?


Apparently I was incorrect. I had thought that C allocated them in a
static block while C++ allocated them as arrays of arrays, but a little
experimentation showed my ideas to be faulty.

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Jul 22 '05 #9
C is not a subset of C++.

The C++ standard does require support for the C library, but with a few
changes in how that support must be provided (e.g. more restrictions on
which parts can be implemented as macros).

There are other differences, some of which have been pointed out
already, but I'll add yet another bit of code to demonstrate a
difference I haven't seen pointed out yet:

char x[sizeof('a')-1];

All properly-functioning C++ compilers are guaranteed to reject this.
In theory a C compiler could reject it as well, but I've never seen one
that did.
From the opposite viewpoint, most reasonably-written C can be converted

to C++ with only minimal changes, perhaps the most obvious of which is
that in well-written C, there should not be an explicit cast on the
value returned by malloc, while C++ requires one -- though well-written
C++ will rarely use malloc at all.

--
Later,
Jerry.

The universe is a figment of its own imagination.
--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
8172
by: lawrence | last post by:
I have a string which I want to send to eval(). How can I test it ahead of time to make sure it is valid code? I don't want to send it to eval and get parse errors. I want to do something like this: $valid = checkPHP($string); if ($valid) { eval($string); } else { $resultsObject->addToErrorResults("We wanted to send our template to eval(), but the PHP it contained was invalid.");
1
1920
by: carl bloc | last post by:
Have this much done but need help with this bit of code: allow user to modify existing draw data. I think I need a counter to give week numbers so the user can select a week number rather than a date. I need to view data for a selected week given the week number or date. need some controls on not allowing the ball number to go >49 or <0 and no duplicates.
23
1911
by: James Aguilar | last post by:
Someone showed me something today that I didn't understand. This doesn't seem like it should be valid C++. Specifically, I don't understand how the commas are accepted after the function 'fprintf'. What effect do they have in those parenthesis? I understand how the or is useful and why never to do it, I'm really just asking about that construction "(fprintf(stderr, "Can't open file.\n"), exit(0), 1))". ---- CODE ----
109
5850
by: Andrew Thompson | last post by:
It seems most people get there JS off web sites, which is entirely logical. But it is also a great pity since most of that code is of such poor quality. I was looking through the JS FAQ for any question that identifies the warning signs to look out for, the things that most easily and clearly identify the author of code as something less than a master of the art. I did not find an FAQ that answered it, but I think the FAQ
2
2363
by: rked | last post by:
I get nameSPAN1 is undefined when I place cursor in comments box.. <%@ LANGUAGE="VBScript" %> <% DIM ipAddress ipAddress=Request.Servervariables("REMOTE_HOST") %> <html> <head> <meta http-equiv="Content-Type" content="text/html;
40
3030
by: Neo The One | last post by:
I think C# is forcing us to write more code by enforcing a rule that can be summarized as 'A local variable must be assgined *explicitly* before reading its value.' If you are interested in what I mean, please look at this feedback my me: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=3074c204-04e4-4383-9dd2-d266472a84ac If you think I am right, please vote for this feedback.
3
2179
by: Jamie Risk | last post by:
I'm attempting to improve some serially executing code (that uses the SerialPort class) bogging Windows down when it runs. To do the 'antibogging' I'm following the example from MSDN Windows.IO.Ports.SerialPort page and use threading. I'm not sure if I'm creating problems with this implementation and would appreciate your input. The original serial code: {
1
1569
by: jjmontreal | last post by:
I am trying to run this code in a form when a user double clicks in a particular field. I am unsure how I should do it since it uses 2 text files, one to generate and the other to store to eleminate redundancy. If you can help this newbie, thanks. '========================================================================== ' ' NAME: RandomPasswordGenerator.vbs ' ' AUTHOR: Mark D. MacLachlan , The Spider's Parlor ' URL:...
4
3788
by: Miha V | last post by:
Hi! We are using ASMX web service with WSE (we're using WS-Addressing) and IIS returns HTTP status code 200 even if XML is malformed (it can contain illegal characters in it). The request does not get through the stack to the application, but if I debug the application, I can see that exception is thrown somewhere "down there".
0
8297
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
8816
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
8498
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
8600
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5629
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1930
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1600
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.