Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C and Java questions that I asked my teacher

  1. Dec 26, 2011 #1
    My teacher says : C language programs are portable on all platforms, which includes, Unix, Linux, windows etc.

    My question : What is the difference between C language programs and Java language programs when all the programs of either languages can be run on any platforms?

    My teacher says: C language .exe files cannot be run on all platform. They can only run on the platform where the program was originally developed and executed. But .c files can be run on any platforms. (I know it's not a good idea to spread my .c files to everyone). But in case of Java language programs .class files are given to JVM software which converts the byte code to machine code. And processor knows how to read machine code. So, these files which comes out of JVM can be run on any platform. All computers suppose to have JVM software if you want to run Java language program.

    My question: So, if I want my .exe files to run on all platforms I need a software like JVM which makes .exe files run on any platform?

    My teacher says: Yes.

    My question is:

    1. Suppose if I take a only .class file of Java from one computer with an intention that I want to run it on another computer. Then can I do it? If yes. Then does it matter what version of JVM I am using?

    2. Why C language programs cannot be run on other platforms? Is it because of different hardware architecture of the computer that I want to run my C language program on or is it the Operating system issue?

    3. I know that we need a JVM software for java programs to make the java programs run on all platforms but why do we need a JVM software at all? Is it because the internal hardware architecture of other computer is not compatible with the code or the Operating system of other computer that I want to run my java program on does not handle java program. Operating system issue?

    I asked my teacher above questions but he just kept stressing on JVM software. And how C language don't have any such software.

    Please answer.
    Last edited: Dec 26, 2011
  2. jcsd
  3. Dec 26, 2011 #2


    User Avatar
    Gold Member

    Different computers have different machine languages, which are the ONLY language they each actually run. A C program compiled into an EXE file on one machine contains the machine code for THAT machine, not for any other machine. You have to compile your code on another machine to get the program to run on that machine, and you MAY have problems due to compiler and/or operating system differences.

    Another way to do all that is to have a slightly different KIND of compilation process whereby the "compiler" generates an intermediate code and then you have a final part of the compiler placed on each machine and it turns the intermediate code into the machine code for that machine.

    The latter process is what JAVA / JVM do.
  4. Dec 26, 2011 #3
    Why computers have different machine language? Is it because of internal hardware architecture of the processor? I think this as a reason because, in general, a compiler converts instructions into binary code and only processor can read these binary code.
    We have 32 bit processors and 64 bit processor. So, my thinking says that C program developed on 32 bit processor, cannot be run on 64 bit processor. And I have to find out how these 0s and 1s are processed inside the processor. I know that before these 0s and 1s reaches the processor, these 0s and 1s are loaded in a "loader".

    I know that when it comes to programs developed in assembly language for windows platform, the program will not run on unix platform because the instruction sets are not similar. Because, if a program takes 5 lines of instructions on windows platform the same program will take 10 lines of instructions on other platform. And there are other reasons also regarding statements like add ax, bx. In one platform data from ax, is added to bx and in other platform data from bx, is added to ax. It happens quite oppositely. I don't remember this perfectly. Maybe I am stating it all wrong.

    Please set me right.
    Last edited: Dec 26, 2011
  5. Dec 26, 2011 #4


    User Avatar
    Gold Member

    Yes, the "machine language" is just a way of interacting DIRECTLY with the architecture of the machine and they are all different in the details.

    Not necessarily. I use Window 7 in 64 bit mode but it runs all 32-bit programs just fine. It will NOT work the other way around. A program that specifically makes use of 64-bit capabilties will not run on a 32-bit processor.

    Loaded "IN" a loader is not correct. They are loaded into the machine BY a loader.

    You have this right although if you want to understand computers completely, you do need to study all this a bit more.
  6. Dec 26, 2011 #5


    User Avatar
    Homework Helper

    In the case of Visual Studio, you can create a 64 bit program on a 32-bit processor. The compiler itself is a 32 bit program, but it can optionally produce 64 bit code. The created 64 bit program will only run on 64 bit processors. This is an example of "cross" compiling where the program is compiled and linked on one computer, but will be run on another computer. Another example of this is the ARM processor used in many consumer devices, where the compiler runs on a PC, but produces code for the ARM processor that runs in some consumer device.

    You can have windows and unix or linux running on the same PC (dual boot or virtual PC). The instruction set is the same, but the calls to the operating system (windows versus unix) are not the same.
    Last edited: Dec 26, 2011
  7. Dec 26, 2011 #6


    User Avatar
    Science Advisor

    There are different types of platforms created for various reasons.

    As an example consider two types of processors: standard x86 CPU's and common modern graphics cards.

    The CPU has a lot more flexibility in terms of the instruction set as well as the amount of code it can execute at a time. Nowadays you find around 4-8 cores in a CPU.

    On the other hand a GPU (graphics processor unit) has a lot more cores that operate completely in parallel, but they do not have the amount of instructions that CPU's have and they do not have the memory capacity that normal CPU's have.

    Both of these are designed for different purposes. The CPU is a more general purpose piece of hardware, while the GPU has a more specific role that is based on the standard rendering pipeline that consists largely of vertex and fragment processing, and unsurprisingly, has been designed as such.

    This kind of idea applies to every specific platform: they are all designed for a particular purpose just like computer programs are designed and later optimized to do certain things really well.
  8. Dec 27, 2011 #7
    I know that there are different compilers for different platforms. But am I suppose to have a different compiler if the hardware architecture of computer is same but the operating system is different?
    Suppose I have two computers. One running Windows Vista 32 bit processor. Second PC running Linux/Unix/Solaris but it has the same 32 bit processor. Is a different compiler still required or I can use the same compiler that I have on Windows Vista. I know that Unix comes with built-in C language compiler.
  9. Dec 27, 2011 #8

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    You need to use a compiler that targets your machine, and your operating system, and the standard libraries either provided by your operating system or the compiler. Some of the system functions you call in your program (e.g., printf if you are using C, the stream insertion operator operator<< if you are using C++) are not a part of the generated executable. Those functions/methods are instead part of some standard library that is dynamically loaded at execution time. The name of the library function loaded at execution time, and its behavior, had better be consonant with the assumptions built in to the compiler.
  10. Dec 27, 2011 #9


    User Avatar

    Staff: Mentor

    You can't run a C++ compiler written for Windows, on a Linux system, even on the same processor, for the same reason that you can't take the Windows version of Microsoft Word and run it on a Linux system. The two compilers use different OS-specific functions to interact with the user and the operating system that they run on.

    In addition, the compiler has to generate code that properly uses any OS-specific functions for the OS that the program being compiled is going to run on (the "target system"), and link to appropriate libraries. In principle, a compiler that runs under Windows could generate both programs that run under Windows and programs that run under Linux, but I don't know if any actually do this.
  11. Dec 27, 2011 #10
    Compilers.jpg Compilers installed adjacent to Operating system. Wrong or correct?

    I know that the examples of System softwares are Operating system softwares and Compiler softwares. All the application software needs Operating system so that the application softwares can be able to run. What about the compiler softwares are they installed on Operating system or they are some special software which are installed adjacent to operating system? I get this doubt because as Unix OS has a built in C compiler, the C compiler must be installed adjacent to Operating system, not on the Operating system like the way Application systems are installed. Am I wrong? I don't know if I am using the correct terminology. Sorry about that.
    Last edited: Dec 27, 2011
  12. Dec 27, 2011 #11


    User Avatar
    Gold Member

    A compiler IS an application. It's application is to compile programs. ANY application can be built in as part of an operating system if you so desire.
  13. Dec 27, 2011 #12
    In the above statement the word "machine" is only to refer to the processor or the entire hardware of a PC? I get this doubt because I know that, after the data is processed in the processor, the RAM and the BIOS comes into picture( I don't know which order) and BIOS knows the peripherals very well. So, the word "machine" must only refer to processor?

    Too complex for a newbie. I don't understand the above statement. I am writing what I understand : When a program is executed, and if we write any (pre-defined(?)) library functions(and header files(?)) in that program then the name of the (pre-defined(?)) library functions (and header files(?)) loaded during the execution time and the behavior of these functions (and header files(?)) must be in consonant with ..... (what?).

    What about the header files? No header files, only (pre-defined(?)) library functions?
  14. Dec 27, 2011 #13

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Header files are not a part of our executable. You've been told this, multiple times. One of the very first steps in the compilation process is to deal with preprocessor commands such as #include <stdio.h>. Those header files are just a part of your code as far as the compiler proper is concerned.
  15. Dec 27, 2011 #14


    User Avatar
    Homework Helper

    The dynamic loading can be optional for older C type programs. With Microsoft compilers, it is common for older C type programs to include all library functions as part of the generated executable during the link stage, called static (versus dynamic) linking. For C++ programs, some of the library calls are dynamically loaded from a "redistributable" or ".net" component installed on a system. It is possible to statically link some libraries (include those libraries in your program's executable file), such as MFC (Microsoft Foundation Class), but the preferred (Microsoft's recommendation) method is to use dynamic links for those libraries. Non-standard dynamically linked libraries may require calls in your program to load and make calls to any non-standard dynamic linked library functions.

    The entire PC, or at least most of it (there may be components of the PC unused by a particular program, such as game controllers).

    Normally, other than at boot time, the BIOS isn't used. The operating system will include it's own drivers for all the components on a computer. The main exception to this would be MSDOS.

    The (what?) is your PC and the operating system the PC is currently running on.

    The header files mostly include definitions for variables, and prototypes (or sometimes macros) for functions. The linker and/or the loader and/or your program will load the libraries and/or make calls to the library functions.
    Last edited: Dec 27, 2011
  16. Dec 31, 2011 #15

    1. Preprocessor actually copies the header files into the source code and then the combined file is given to the compiler. Preprocessor takes the header files from the HDD. The compiler generates the equivalent binary code of the file and we call it as .obj file.

    2. Linker is a part of compiler. Linker adds the binary code of .obj files of library funtions into the binary .obj code and a new file is generated called .exe file. This .exe file is stored in HDD.

    3. When we run the .exe file, Loader, which is an Operating system software loads the .exe file into the RAM. RAM contains segments.

    4. During execution all the mathematical operations are performed by ALU present in the processor. Required data is fetched from RAM and data is stored in registers and ALU takes-in the required 'numbers' from these registers and computes them as required and result is put in some register in CPU and then this result is sent back to the segment in RAM. CPU don't store any data.

    I have noted the point that BIOS is used only during boot time, and the operating system will include it's own drivers for all the components on a computer and main exception to this would be MSDOS, but ...

    5. (Figure explanation, continuation from point 4) Data is sent back to the RAM. We can even make changes to the values assigned to the variables during execution. Now, if I want to send data to the devices. (My thinking) Then there is "some component" to which the binary equivalent of this result can be sent. The result is attached with the binary code of device. This predefined binary code is stored in "some component". This signature which is [binary code + binary equivalent of result] is now sent to the devices. Now, let's say I want to display the result of the following program on the monitor.

    #include <stdio.h>
    #include <conio.h>
    void main(){
    int a,b, sum;
    sum= a + b;
    printf("%d", sum);

    If the binary code is matched at the terminal of the monitor, then data/result is allowed by that device into monitor's processing circuitry and there the data/result is processed and it is displayed on the screen. Correct or Wrong?


    If "some component" really exists and if I want to program this "some component" and create the device drivers am I suppose to use Assembly language?

    The exception, which is MSDOS, where BIOS is used, how could BIOS be different from "some component" ?

    Please help.

    EDIT: I don't want to know how monitor or other devices connected to a computer works. I am just asking that if BIOS is not used on the other times then can "some component" (if it exists, please see figure) be used? Should there be "some component" attached to the processor? And we know that processor is sending data back to RAM but is the processor really sending data to other devices as well or, is RAM sending the data to the other devices but not the processor?

    Sorry if my post is confusing, but I tried to give good picture of how I see processor in giving signals to other devices. I think when processors execute drivers provided by windows, the drivers must also have some predefined binary code and I think that this predefined code is stored in "some component" which will help in identifying the correct device attached to the computer.
    My main question is point 5. But if you see any problems in other points, please correct it.
    Last edited: Dec 31, 2011
  17. Dec 31, 2011 #16


    User Avatar
    Gold Member

    I think you would do well to read up more on how monitors work. Your terminology is very confusing and suggests that you do not have a good understand yet of how things end up getting sent to the monitor and in what form, although it is somewhat close.
  18. Dec 31, 2011 #17


    User Avatar
    Homework Helper

    The BIOS includes code that interfaces with most components on the system (monitor, keyboard, disk drives, ...). Most operating systems other than MSDOS, include their own code to interface with those components, and the code for each component is called a device driver.

    The answer to your question is that device drivers or BIOS are "software", machine language code that interfaces with some component, while an actual component is "hardware" made up of elecrical circuits and mechanical components.
  19. Dec 31, 2011 #18


    User Avatar
    Gold Member

    For the OP's sake, I'll clarify a bit on BIOS and "device drivers". Device drivers are just programs that are written to interact directly with hardware. The BIOS (Basic Input Output System) is just device drivers that have been burned into ROM so that they are there as soon as the computer is turned on and do not need to load from a hard drive the way all other software does. All computers have some form of BIOS because it is the BIOS that does the first step in loading the operating system (which then takes over and may or may not use the BIOS code any further --- early versions of Windows used the BIOS but I don't know if current versions do or not.)
  20. Jan 1, 2012 #19
    Thank you.

    My mistake: In post #15, I thought that BIOS is a hardware device and we are
    writing device drivers in the hardware which is BIOS here. Which is wrong.

    Correct: Device drivers or BIOS are "software" which is written inside ROM. ROM
    is an electrical device here.

    And I think in the case where Operating system takes care of newly attached devices by providing the required device drivers, if the device driver is not present then that is the reason why we load the device drivers from the CD that the manufacturer of the device provides us, so, the device drivers are loaded and operating system acquaints itself with the drivers and computer is restarted and the newly attached device starts working.
    CompilerandInterfacing1.jpg In the above figure the device(below processor) attached to the processor is what I now understand is ROM.

    Now, if BIOS is not being used then as Operating system is taking care of the devices, then I thought there must "some component" (below figure) other than ROM which should take care of the peripheral devices. But I think this "some component" is not required because I think when data is sent out from the processor which is suppose to be given to the, say, monitor, then I think all we need is an Multiplexers near the processor end and a De-multiplexer at the monitor end because data cannot be sent in parallel way, I think, parallel way can be done but it's not feasible. So, therefore, we are using Multiplexers and De-multiplexers. And possibly ROM could also send the using Multiplexer and De-multiplexer. Something like the figure below. The below figure and the explanation written above is just my thinking. If you think it's wrong, please correct it.

    Aside from all of the above concept, I have to see why there are different computer architectures that are being implemented. Are all these connections from HDD, RAM, Processor and other components (into and out-of) are architecture dependent. Can you please outline what must be in the computer architecture theory and how I should tackle that subject? I am targeting Artificial Intelligence. I have a background in electronics and communication.
    Last edited: Jan 1, 2012
  21. Jan 1, 2012 #20


    User Avatar
    Gold Member

    It's possible that you'll get some more replies to this, but my own take is that you are asking for WAY too much as a simple forum response. You need to just start reading about computer architecture and then ask questions rather than ask us here to write the book for you.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook