Can anyone suggest me how can I write a platform independent C code. The code should work properly irrespective of whether it is a 32 bit or 64 bit system/compiler. Is there any compiler which provides me this feature ? OR what are the things which I should take care while writing these kind of code?
-
3Your question is too broad - there's no real way to answer it accurately if at all in this format. Entire books could be written on how to do what you're asking. – Andrew Henle Jun 21 '16 at 09:10
-
"Is there any compiler which provides me this feature?" - a compiler is platform-dependent **by definition** (i.e., when a compiler is implemented, it is designated for a specific platform). – barak manos Jun 21 '16 at 09:15
-
If you are asking about platform independent *code*, then C is already platform independent if you stick to the standard. – ElderBug Jun 21 '16 at 09:20
-
[How to write portable code in c++?](http://stackoverflow.com/q/3103568/995714), [Writing a portable C program - which things to consider?](http://stackoverflow.com/q/2303516/995714), [Why is it difficult to write portable C programs?](http://stackoverflow.com/q/1257923/995714) – phuclv Jun 21 '16 at 09:21
-
This is no small topic. There are hundreds of dirty details in the C language itself that you need to be aware of in order to write such code properly. In other words, you need to be a veteran C programmer. – Lundin Jun 21 '16 at 11:47
-
@LưuVĩnhPhúc Please don't encourage these kind of questions by digging up old trash. All those posts are not suitable for SO any longer: too broad and too opinion-based. – Lundin Jun 21 '16 at 11:49
2 Answers
If the code should be able to be compiled on x86 and amd64 there shouldn't be too much of a problem (given a hosted environment) when you write proper code that adheres to the standard and doesn't use hardware-specific features.
Even more if you are lucky you can assume to have a POSIX-environment. If that's the case you should have (almost) no problems programming portable code.
However if you want your binary to run on multiple architectures you need to restrict yourself to compiling to a binary that uses the smallest common set of instructions shared by the ISAs you compile for. This won't work for ARM & x86 but you should be able to run x86 programs just fine on amd64 as amd64 are extensions.
You could however compile your code for multiple architectures and write a wrapper script that detects the architecture and launches the corresponding binary if that's feasible.

- 751
- 1
- 5
- 21
C in itself if platform dependent because it compiles the code to machine's CPU's instruction set. You cannot create a compiled C executable that would run on different platforms but you can definitely write platform independent C code. You'll need to use functionalities of frameworks which provided this feature.
One such framework which I've used is the Qt Framework. You write your logic utilizing Qt's features instead of language's default features such as file read/write, network access, creating UI components etc.

- 2,016
- 1
- 29
- 54
-
"_C in itself if platform dependent_" No, C is platform independent. Just like C++: http://stackoverflow.com/a/11810542/2703418 It's just that OS's like Linux and Windows have different libraries they offer for the OS. – bzeaman Jun 21 '16 at 11:30