简体   繁体   中英

How is C cross platform?

C language is ultimately compiled to machine specific assembly language. Then how can C be cross platform if every processor has its own assembly language syntax? If I write an operating system kernel in C then how can I make it run across a variety of processors?

C is portable at the source code level. This means that you can recompile your program in the platform of choice and have your application running there.

You can find the history of C in the wikipedia , for example. At that time, programming was carried out in assembly for each specific machine, so having a high-level (by that time) programming language was a bless: it sped up development and also made possible to port programs from one machine to a new one just by porting the compiler (the C compiler could later be written in C itself, again speeding up the porting process, but that's another story).

Specifically, the idea of porting UNIX between operating systems appeared when they had UNIX running in the PDP-7, and the PDP-11 appeared. Finally, even UNIX's kernel was rewritten in C, so it made the operating system really popular because of the easy porting: there was only the need to write a small amount of assembly for very specific parts of the kernel, so you could have UNIX up and running in a new machine shortly, at least compared to other operating systems of that time.

The language C itself is cross-platform, because you don't directly run C code on machines. The C source code is compiled to assembly, and assembly is the platform specific code. The only non cross-platform part are the compilers and the resulting assembly.

In essence, you can use the same C code, with different compilers, to create the specific assembly needed. Since you use the same C source code, its deemed cross-platform.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM