1

Why isn't C/C++ called platform independent like java when the same source code written in C/C++ can be made to run on different operating systems by different compilers, just like JVM is used in java.

Isn't different compilers and JVM doing same thing and achieving platform independence.

August Karlstrom
  • 10,773
  • 7
  • 38
  • 60
  • 1
    In Java, you write a program, give the executable to your friend and he can run it directly on his computer. In C/C++, you write a program, but cannot hand the executable over unless you know his runtime environment. – Glains Jan 07 '21 at 18:48
  • Where did you find "*platform independent language*" being used? That would be a rather odd thing to say. – dxiv Jan 07 '21 at 18:49
  • you could write a VM for c++ and therefore make it platform independent too – Alan Birtles Jan 07 '21 at 18:58
  • @AlanBirtles ain't different compilers already doing that ? – NoobMaster69 Jan 07 '21 at 18:59
  • Re “the same source code written in C/C++ can be made to run on different operating systems by different compilers”: This is incorrect, if “can be made to run on” means recompiled rather than rewritten. C is not designed to be a language in which all programs are portable to different C implementations. It is designed to be a language that itself can be ported to widely different platforms, allowing a variety of adaptations and flexibility. For example, the width of `int` differs in C implementations. A program written for one size of `int` may not work in another C implementation. – Eric Postpischil Jan 07 '21 at 19:01
  • Annex J of the C 2018 standard lists portability issues. There are many dozens of them. So C is not a platform-independent language. – Eric Postpischil Jan 07 '21 at 19:01
  • @NoobMaster69 See also [How is Java platform-independent when it needs a JVM to run?](https://stackoverflow.com/questions/2748910/how-is-java-platform-independent-when-it-needs-a-jvm-to-run) and [Why is the JVM not platform independent, given that Java (the language) is platform independent?](https://stackoverflow.com/questions/2120654/why-is-the-jvm-not-platform-independent-given-that-java-the-language-is-platf). – dxiv Jan 07 '21 at 19:07
  • I can compile a java program on Windows, and then run this compiled program on a variety of computers and OSs. "In a world without fences, who needs gates." One can read that a couple ways. – NomadMaker Jan 07 '21 at 19:07
  • @dxiv read both, was still confused – NoobMaster69 Jan 07 '21 at 19:08
  • You Java people forget one important thing: The "binaries" Java "builds" need a VM to run on. That's like saying C++ is platform independent - just compile your code to a Linux VM and distribute it together *with that VM*. – nada Jan 07 '21 at 22:28

6 Answers6

5

Isn't different compilers and JVM doing same thing and achieving platform independence.

Not really. Your Java program is running within the JVM, which acts as a translation layer between the Java byte code and the native machine code. It hides the platform-specific details from the Java application code.

This is not the case with C. C code (typically) runs natively, so there is no translation layer isolating it from platform-specific details. Your C code can be directly affected by platform-specific differences (word sizes, type representations, byte order, etc.).

A strictly conforming C program, which uses nothing outside of the standard library and makes no assumptions about type sizes or representation beyond the minimums guaranteed by the language standard, should exhibit the same behavior on any platform for which it is compiled. All you need to do is recompile it for the target platform.

The problem is that most useful real-world C and C++ code isn't strictly conforming; to do almost anything interesting you have to rely on third-party and system-specific libraries and utilities, and as soon as you do you lose that platform independence. I could write a command-line tool manipulates files in the local file system that would run on Windows and MacOS and Linux and VMS and MPE; all I would need to do is recompile it for the different targets. However, if I wanted to write something GUI-driven, or something that communicated over a network, or something that had to navigate the file system, or anything like that, then I'm reliant on system-specific tools and I can't just rebuild the code on different platforms.

John Bode
  • 119,563
  • 19
  • 122
  • 198
2

It's not the language itself that is platform dependent. It's possible to compile both C and C++ for JVM, but it's not very common to do so. Compiling C++ for the JVM

In the same way, it is possible to compile Java for a specific target instead of JVM. Compiling java source code to native exe

But Java and JVM are both designed to work together, so that combination is very natural to use.

C is designed to be very close to the hardware. There's not really a reason to use C if your target is JVM. Then use Java instead.

In theory, you can compile ANY language for ANY target, as long as the target is turing complete.

Sidenote: Don't write "C/C++". They are completely different languages.

klutt
  • 30,332
  • 17
  • 55
  • 95
1

In case of C or C++ (language that are not platform independent), the compiler generates an .exe file which is OS dependent. When we try to run this .exe file on another OS it does not run, since it is OS dependent and hence is not compatible with the other OS.

Christoph
  • 647
  • 5
  • 18
  • How is the ***language*** "*not platform independent*"? – dxiv Jan 07 '21 at 18:51
  • yes but what makes java independent is the use of JVM which is platform dependent just like other C/C++ compilers, so every language should be called platform independent. sorry i am a beginner and this doubt i can't understand properly. – NoobMaster69 Jan 07 '21 at 18:52
  • @dxiv maybe my memory is a bit rusty, but isn't `int` platform-dependent in C? I would say that makes the language *not* platform independent. – Federico klez Culloca Jan 07 '21 at 19:01
  • @FedericoklezCulloca C has minimum guarantees that any compliant implementation must observe. It is certainly possible to write platform independent code in a "hello world" app or a general purpose library. – dxiv Jan 07 '21 at 19:08
1

Isn't different compilers and JVM doing same thing and achieving platform independence.

The JVM per se is the platform. It is abstracting away, whether it is ARM/AMD64/...

C/C++ is getting compiled. It may only run on the processor(family) that it was compiled for. You can't just take a binary for MIPS and execute it on ARM.

Compare it:

[C(++)]
  |
  v
[Processor]

vs

[Java (Classfiles)]
     |
     v
   [JVM] #Abstraction layer
     |
     v
[Processor]
JCWasmx86
  • 3,473
  • 2
  • 11
  • 29
0

The role of JVM in the independent platform is that it acts as a virtual processor. when we used c/c++ to compiler different processor converts the source code into a different binary pattern that's why there are not platform-independent.

Shan
  • 15
  • 1
  • 8
0

C++ language itself doesn't assume any specific platform, so in that sense it is platform independent.

rjhcnf
  • 762
  • 6
  • 12