I know most people call C a low-level language and that made me wonder. Is there any low-level action that C can do that C++ can't? Also, can C++ use the asm("") feature?
I know most people call C a low-level language and that made me wonder. Is there any low-level action that C can do that C++ can't? Also, can C++ use the asm("") feature?
With few exceptions all valid C code is valid C++ code, so no, there is nothing that C can do that C++ can't. When people call C low level it is in comparison to languages like perl or java or haskell more so than in comparison to C++
Right, what he said. C++ is a subset of C. It strives to be more type restricted. Though since even C style casts are allowed in C++, one can even dupe the compiler into not even doing that task.
> C++ is a subset of C.
Other way. I made the same mistake on these boards too. heh.
If I recall correctly, many years ago there was a thread about exactly what you can do in C that you can't do in C++. Maybe I'm crossing my mind and it was actually a thread about what you can do in ASM that you can't do in C, but let's just assume I'm right, okay?
I think they came up with a list of 5 things, and they were all REALLY low-level things that you'd probably only do when writing an OS - stuff like that. But for most intents and purposes, C++ really is a superset of C.
Righto, I meant superset, I swear :P
There are plenty of things one can do in assembler that cannot be done in C without the usage of inline assembler. There are a few differences, actually. But they are more or less issues of syntax than anything. i.e.
Example:
Mean two different things. In C, that functionally means void main(...) whereas in C++ it means void main(void).Code:/* C style prototype */ void main(); // C++ style prototype void main();
Those types of things hardly qualify as what you are talking about, however.
master5001, you choose a very unfortunate example: main() returns an int and should not be prototyped.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Why not?
hmm... looks like I was wrong about the no prototyping thing: C99 states that the implementation shall not declare a prototype for main(), not that main() itself may not be prototyped, and C++03 states that the implementation shall not predefine the main() function. On the other hand, the very fact that main() may not be used within one's program means that there is no reason to declare a prototype for main().Why not?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Speaking of main()...
I believe you can safely return from main() in C to the startup code without worrying about destructors being called for objects with internal or external linkage.
I only bring this up because it is done in tightly constrained embedded programs to save a stack frame. main() contains the startup code and system initialization, but it returns to the startup code to sleep until an interrupt occurs. I don't believe you can safely do that in C++.
I could be wrong though, because I think we step out of the "hosted environment" scope there. Perhaps C++ doesn't technically need to suffer that restriction. I'd be interested to hear if someone has looked at that before.
You can terminate your program abruptly by other means in C++. It's called 'exit' iirc. You don't have to let your program end gracefully.
Or you could avoid globals or statics with destructors, and place the entire body of main in an extra scope guard.
The lack of destructors in C is a burden though, not an advantage.
Last edited by iMalc; 08-28-2008 at 12:59 AM.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Perhaps I failed to make myself understandable. The point is that you want to return from main, or 'exit', without really ending the program. It's just a trick to save a few bytes of memory. I'm just pointing out one of C++'s hidden mechanisms that can throw up obstacles when you want to do some "low-level" stuff. I'm not even sure I'm right in a non-hosted environment. I'm just bringing something up to talk about .
Also, I didn't mean to suggest that destructors were bad, just that they are one issue that you don't have to work around in C. I think they are great, until you don't want them.
C/C++ are actualyl considered mid level languages by most old timers. A low level language is more like assembly, i.e. just above machine code. I woudl say that C++ is slightly higher than C, but its not really a high level language either. IMO it really depends on teh seperation from teh hardware. C/C++ are both very close to the hardware, although not as close as assembly. Perl etc are very abstracted langauges and are considered high level.
But truthfully, these definitions are arbitrary and meaningless. whether you define C/C++ as a low level mid level high level or grapefruit level language has no effect on what it can and cant do.
Last edited by abachler; 08-28-2008 at 11:45 AM.
Returning from main also exits the program so there is no difference.
The obstacle is the fact that in C you have to clean up manually, and not forget to do so. It is better to have to say "No I don't want to clean up this one time", than it is to have to say "And please clean this thing up for me" zillions of times (and try not to forget coding them all).
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"