CrazyEngineers
  • Is C Programming Language Platform Dependent?

    Sindhu Chowdary

    Sindhu Chowdary

    @sindhu-chowdary-tDAv1D
    Updated: Oct 26, 2024
    Views: 5.5K

    The C programming language is not platform-dependent; rather, it is platform-neutral.

    This is one of the features that make C such a popular and enduring programming language.

    You can write C code on one platform and expect it to compile and run on another.

    This portability is one of the key reasons why C has been used to write many operating systems, including Unix and Windows.

    However, while C as a language is platform-independent, the compiled binaries produced from C code are typically platform-dependent.

    This is because C is a compiled language, not an interpreted one. When you write a C program and compile it, the compiler converts your C code into machine code specific to the target platform, such as a specific version of a particular operating system running on a specific type of processor.

    This machine code is not portable and generally cannot be run on another platform without recompiling the source code for that new platform.

    There's another aspect to this, and that's the libraries and APIs your C code might use.

    The C standard library is portable and standardized across platforms, but if you're using other libraries or APIs that are specific to a particular platform or operating system, then your code becomes dependent on those libraries or APIs and, hence, the platform. For instance, using Windows APIs in your C code will make it platform-dependent on Windows.

    In order to write truly platform-independent C code, you need to avoid using platform-specific libraries and APIs and stick to the standard C library as much as possible.

    You also need to be careful about certain aspects of C that can vary between platforms, such as the size of certain data types, endianness, and the alignment of data in memory. By paying attention to these details, you can write C code that is as portable as possible.

    So, to sum up, while C as a language is not platform-dependent, the compiled C code and the libraries/APIs used can make a C program platform-dependent.

    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Kaustubh Katdare

    AdministratorJul 2, 2013

    I object: The last I checked C language works in Windows XP, 7, 8 and also on uBuntu. This proves that C is platform independent.

    PS: I'm changing the title of the thread to make sync with the discussion.
    Are you sure? This action cannot be undone.
    Cancel
  • Sindhu Chowdary

    MemberJul 2, 2013

    Kaustubh Katdare
    I object: The last I checked C language works in Windows XP, 7, 8 and also on uBuntu. This proves that C is platform independent.

    PS: I'm changing the title of the thread to make sync with the discussion.

    Exactly.But then why most of the answers regarding this question in the internet shows that C is a platform dependent language?

    And also what does this line mean?
    "The C language is platform independent , but the generated program or the executable program is not".
    Are you sure? This action cannot be undone.
    Cancel
  • Abhishek Rawal

    MemberJul 2, 2013

    All higher level languages are platform independent. eg : C,Java,C++,Python(it is a scripting language though), etc.
    While, all Machine level language (example Assembly Language) is platform dependent.
    Eg : You cannot run Assembly code of one chip in another.
    Are you sure? This action cannot be undone.
    Cancel
  • rahul69

    MemberJul 2, 2013

    gwendollen
    Exactly.But then why most of the answers regarding this question in the internet shows that C is a platform dependent language?

    And also what does this line mean?
    "The C language is platform independent , but the generated program or the executable program is not".
    This line means that although code in C language itself is platform independent (as u can run same C code on Windows, Linux etc), but when u create a software using C language, it will run only on that platform, as the executable file created in windows will not run in Linux directly.
    Hope it answers your query 😀
    Are you sure? This action cannot be undone.
    Cancel
  • Kaustubh Katdare

    AdministratorJul 2, 2013

    #-Link-Snipped-# : Didn't get it. How do you distinguish between 'C code' and 'C software'? Isn't it the same?
    Are you sure? This action cannot be undone.
    Cancel
  • Sanyam Khurana

    MemberJul 2, 2013

    I think it's not Platform independent whereas Java is.

    The difference is, in C we write our code and then the compiler compiles it to run, but we cannot run the code generated by compiler in different platforms.

    We have to write the whole program again, and compile it once again on other machine then run it.

    While in Java, you write a program, Compiler (javac) converts it into bytecode and then this bytecode once generated can be run anywhere. (on any machine which has JVM [Java Virtual Machine]).

    This works on the principle of " Make once Run everywhere".

    I hope this is clear now.
    Are you sure? This action cannot be undone.
    Cancel
  • Abhishek Rawal

    MemberJul 2, 2013

    Here we are talking about "programming Language" & not the program created "using Programming language".
    Which means we're simply focusing on programming language & whether you can code in C on another platform or not.
    We're not talking about software coded in C, right ?
    Are you sure? This action cannot be undone.
    Cancel
  • Sindhu Chowdary

    MemberJul 2, 2013

    What if we simply copy and paste the code?Will it work or not?
    Just a silly doubt.Because I am getting more and more confused now.
    Are you sure? This action cannot be undone.
    Cancel
  • Sanyam Khurana

    MemberJul 2, 2013

    gwendollen
    What if we simply copy and paste the code?Will it work or not?
    Just a silly doubt.Because I am getting more and more confused now.

    If you mean here Source Code then yes, you have to compile it again on other machine's compiler in C and then it would run.
    Are you sure? This action cannot be undone.
    Cancel
  • Abhishek Rawal

    MemberJul 2, 2013

    Sanyam Khurana
    If you mean here Source Code then yes, you have to compile it again on other machine's compiler in C and then it would run.
    Let's practically test it.
    I have uploaded a source code.Let's test whether it compiles on Windows or not.
    I am using Linux, If the uploaded code compiles on Windows then it means you're statement is valid. 👍
    Are you sure? This action cannot be undone.
    Cancel
  • Sanyam Khurana

    MemberJul 2, 2013

    Abhishek Rawal
    Let's practically test it.
    I have uploaded a source code.Let's test whether it compiles on Windows or not.
    I am using Linux, If the uploaded code compiles on Windows then it means you're statement is valid. 👍
    Yes, let's see if someone Windows user confirms it here.

    PS: Although I've dual booted windows with Ubuntu, but I've C++ Compiler with DOS box , it crashes every now and then. so, can't really rely on it.
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberJul 2, 2013

    Yeah it is platform dependent. The object code created after the compilation of code
    requires different kind of processing depending on the OS
    Are you sure? This action cannot be undone.
    Cancel
  • rahul69

    MemberJul 2, 2013

    Kaustubh Katdare
    #-Link-Snipped-# : Didn't get it. How do you distinguish between 'C code' and 'C software'? Isn't it the same?
    C code is the actual source code, which looks something like this:
    #include<stdio.h>
    int main()
    {
    //...
    //...
    }
    C software is the software developed in C , for example the .exe file we get after compiling the above "C Code".
    Hope it is clear...
    Are you sure? This action cannot be undone.
    Cancel
  • rahul69

    MemberJul 2, 2013

    Abhishek Rawal
    Let's practically test it.
    I have uploaded a source code.Let's test whether it compiles on Windows or not.
    I am using Linux, If the uploaded code compiles on Windows then it means you're statement is valid. 👍
    Ideally it should compile in Windows, the only condition is that your Windows compiler must have all those header-files which you are using in Linux,
    So, as u r using <sys/io.h> in the program u uploaded, so it will not run in most of the cases unless this header-file is added to the compiler in a proper way.
    Are you sure? This action cannot be undone.
    Cancel
  • Abhishek Rawal

    MemberJul 2, 2013

    rahul69
    Ideally it should compile in Windows, the only condition is that your Windows compiler must have all those header-files which you are using in Linux,
    So, as u r using <sys/io.h> in the program u uploaded, so it will not run in most of the cases unless this header-file is added to the compiler in a proper way.
    You can easily add custom header files, right ?
    atleast in Linux we can do that using -I/location/customheader.h
    So, you can do same in Windows too, right ?
    Are you sure? This action cannot be undone.
    Cancel
  • Sanyam Khurana

    MemberJul 2, 2013

    Abhishek Rawal
    You can easily add custom header files, right ?
    atleast in Linux we can do that using -I/location/customheader.h
    So, you can do same in Windows too, right ?
    Yes, Custom Header files can be created. 😀
    Are you sure? This action cannot be undone.
    Cancel
  • [Prototype]

    MemberJul 8, 2013

    In a way yes, you cannot directly take the code written for windows, compile it in linux and it'll work. You still need to make some changes in the code. However, its independent of platform in a sense that you don't have to write everything again. All you gotta do is modify the way interaction with OS is done, with some new system calls depending on the OS.

    JAVA is platform independent because it runs under an Environment(this environment is not platform independent) while C executable directly interacts with kernel without a mediator.
    Are you sure? This action cannot be undone.
    Cancel
  • Kaustubh Katdare

    AdministratorJul 16, 2018

    This is an old discussion, but very important one. Someone asked the same question through email and I thought I'd update this discussion.

    What exactly is a programming language anyway? It's a set of English language words (mostly) that need to be organised for the computer to make sense of it. Now, the Computer itself won't understand words typed in English - and it must convert all that English into something that it understands - zeroes and ones.

    Now there are systems that convert this human readable computer program into 0s and 1s. These systems are either platform dependent or independent; making the language itself platform dependent.

    For example, let's say you've Java code. If you wish to run it - the platform will need JVM installed for the operating system to make sense of the file contents. 

    The best analogy I can offer is that of a video or audio file. While the file will play on any operating system; the operating system will need to have a compatible player installed in order to read and make sense of the MP3 or MP4 file it's reading and playing. 

    I hope this makes sense. 

    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register