Click here to Skip to main content
15,882,113 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Firstly i want to ask C/C++ is Platform Independent or not?and why??

And if it is not then why we say c++ is not platform independent, as a game developed in c++ can be played on any system?

(This question was asked by an interviewer to me)
Posted
Updated 4-Dec-13 23:18pm
v3

Your question title copied into google gives "About 3,310,000 results (0.38 seconds)" - even with the spelling oversight.

Simply put, c & c++ compile to machine instructions that are specific to a particular architecture.

Consider the following source-code:

C++
#include <cstdio>
int main()
{
  printf("Hello World");
}


If I compile it for Arm Cortex-M3, Intel x86, Atmel ATMega, Motorolla 68000 it will produce different machine code. The output from the compiler will be very different in each case - yet clearly, the output of the program will be identical.

Java or HTML for instance, use the exact same code regardless of what you run it on. It's just the Java Virtual Machine or the Browser that are platform dependant. Yet, if written in c/c++, they can be compiled for each target platform with little or no modification.
 
Share this answer
 
Comments
thatraja 5-Dec-13 7:19am    
5!
enhzflep 5-Dec-13 22:25pm    
Thanks thatraja. :)
The C++ programing language is platform independent.
An application written in C++ produces machine-specific code but you can compile it for different platforms. Most of C++ applications however use 'platform-peculiar' libraries (that is libraries you find in one platform but not in another one) and hence are NOT platform independent.
 
Share this answer
 
Comments
thatraja 5-Dec-13 7:19am    
5!
CPallini 5-Dec-13 7:51am    
Thank you.
Atul Khanduri 5-Dec-13 14:18pm    
Oopsss, i am confused that C++ is what, Platform dependent or independent..???
Some says it is dependent and other says independent.....
BillW33 5-Dec-13 16:17pm    
The C++ language and the code written in that language, is independent of the platform.
The compiled program created from the C++ code is platform dependent.
enhzflep 5-Dec-13 22:27pm    
Well made distinction on the use of libraries. Totally overlooked them myself.
The language is independent, as long as you can find a compiler for your target platform since the compiler product (binary) is not.

What CPallini expanded on was that if you use a library, that may not be independent, and since your binary is combination of the whole (your base plus libraries) there's a possibility where the library would limit the cross-compatibility.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900