Register to reply

C and Java questions that I asked my teacher

by pairofstrings
Tags: asked, java, teacher
Share this thread:
pairofstrings
#1
Dec26-11, 10:00 AM
P: 193
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.
Phys.Org News Partner Science news on Phys.org
Pilot sites in energy from coffee waste show good results
Startups offer banking for smartphone users
Factor in naked mole rat's cells enhances protein integrity
phinds
#2
Dec26-11, 10:20 AM
PF Gold
phinds's Avatar
P: 6,336
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.
pairofstrings
#3
Dec26-11, 11:59 AM
P: 193
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.

phinds
#4
Dec26-11, 12:24 PM
PF Gold
phinds's Avatar
P: 6,336
C and Java questions that I asked my teacher

Quote Quote by pairofstrings View Post
Why computers have different machine language? Is it because of internal hardware architecture of the processor?
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.

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.
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.

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".
Loaded "IN" a loader is not correct. They are loaded into the machine BY 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.
You have this right although if you want to understand computers completely, you do need to study all this a bit more.
rcgldr
#5
Dec26-11, 07:19 PM
HW Helper
P: 7,131
Quote Quote by pairofstrings View Post
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.
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.

Quote Quote by pairofstrings View Post
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.
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.
chiro
#6
Dec26-11, 07:57 PM
P: 4,573
Quote Quote by pairofstrings View Post
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.
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.
pairofstrings
#7
Dec27-11, 10:20 AM
P: 193
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.
D H
#8
Dec27-11, 10:58 AM
Mentor
P: 15,167
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.
jtbell
#9
Dec27-11, 11:29 AM
Mentor
jtbell's Avatar
P: 11,780
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.
pairofstrings
#10
Dec27-11, 12:17 PM
P: 193
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.
phinds
#11
Dec27-11, 12:42 PM
PF Gold
phinds's Avatar
P: 6,336
Quote Quote by pairofstrings View Post
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.
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.
pairofstrings
#12
Dec27-11, 01:15 PM
P: 193
Quote Quote by D H View Post
You need to use a compiler that targets your machine.
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?

Quote Quote by D H View Post
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.
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?
D H
#13
Dec27-11, 01:39 PM
Mentor
P: 15,167
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.
rcgldr
#14
Dec27-11, 05:19 PM
HW Helper
P: 7,131
Quote Quote by D H View Post
Some of the system functions you call in your program ... C , 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 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.

Quote Quote by pairofstrings View Post
In the above statement the word "machine" is only to refer to the processor or the entire hardware of a PC?
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).

Quote Quote by pairofstrings View Post
After the data is processed in the processor, the RAM and the BIOS comes into picture, and BIOS knows the peripherals very well.
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.

Quote Quote by pairofstrings View Post
When a program is executed ... library functions ... header files must be in consonant with (what?).
The (what?) is your PC and the operating system the PC is currently running on.

Quote Quote by pairofstrings View Post
What about the header files? No header files, only (pre-defined(?)) library functions?
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.
pairofstrings
#15
Dec31-11, 12:14 PM
P: 193


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;
a=10;
b=20;
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?

And

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.
phinds
#16
Dec31-11, 12:40 PM
PF Gold
phinds's Avatar
P: 6,336
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?
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.
rcgldr
#17
Dec31-11, 01:22 PM
HW Helper
P: 7,131
Quote Quote by pairofstrings View Post
The exception, which is MSDOS, where BIOS is used, how could BIOS be different from "some component"?
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.
phinds
#18
Dec31-11, 01:39 PM
PF Gold
phinds's Avatar
P: 6,336
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.)


Register to reply

Related Discussions
Questions asked on an Interview for a Phd position Academic Guidance 6
Where did the frequently asked physics questions go? Forum Feedback & Announcements 5
Questions asked on the MCAT's Medical Sciences 12
Can I get the archive of all the past questions that have asked so far? Introductory Physics Homework 4