Is C Programming Language Platform Dependent?

Is C Programming Language Platform Dependent?

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.

Replies

  • Kaustubh Katdare
    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.
  • Sindhu Chowdary
    Sindhu Chowdary
    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".
  • Abhishek Rawal
    Abhishek Rawal
    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.
  • rahul69
    rahul69
    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 ๐Ÿ˜€
  • Kaustubh Katdare
    Kaustubh Katdare
    #-Link-Snipped-# : Didn't get it. How do you distinguish between 'C code' and 'C software'? Isn't it the same?
  • Sanyam Khurana
    Sanyam Khurana
    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.
  • Abhishek Rawal
    Abhishek Rawal
    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 ?
  • Sindhu Chowdary
    Sindhu Chowdary
    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.
  • Sanyam Khurana
    Sanyam Khurana
    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.
  • Abhishek Rawal
    Abhishek Rawal
    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. ๐Ÿ‘
  • Sanyam Khurana
    Sanyam Khurana
    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.
  • Vishal Sharma
    Vishal Sharma
    Yeah it is platform dependent. The object code created after the compilation of code
    requires different kind of processing depending on the OS
  • rahul69
    rahul69
    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
    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...
  • rahul69
    rahul69
    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 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.
  • Abhishek Rawal
    Abhishek Rawal
    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 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 ?
  • Sanyam Khurana
    Sanyam Khurana
    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. ๐Ÿ˜€
  • [Prototype]
    [Prototype]
    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.
  • Kaustubh Katdare
    Kaustubh Katdare

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

You are reading an archived discussion.

Related Posts

GUNNARS are high-tech computer glasses designed specially for the group of people like Gamers,Bloggers,Coders,IT Technicians,Video editors,etc. who are glued to the computer for majority time of the day. Usually, these...
Now a days,the usage of mobile phones became excess and almost every person uses a mobile phone. In the environment point of view,the mobile phone radiations are proved to be...
The Director/Producer have to pay the x amount to the y channel for letting their show to be broadcast in their Channel. Moreover they even have to pay to actors...
Finally after a short delay , the LG Optimus G Pro is ready to be introduced in the country.It seems to be the LG's response to the Samsung Galaxy Note...
An initiative by Planetary Resources to build and launch a space telescope for public use has exceeded the goal of $1,000,000 by a huge margin on Kickstarter. The project received...