Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with C file. #87

Open
nezumisama opened this issue Mar 21, 2018 · 0 comments
Open

Problems with C file. #87

nezumisama opened this issue Mar 21, 2018 · 0 comments

Comments

@nezumisama
Copy link

Empty string is the smallest valid c code - it has no executable code and no data, but it remains valid. You can compile it to an object file (.o) and a shared library (.so on UNIX). So the c file in this repo should be an empty file, as this is exactly the smallest valid file of this type.
What you can't do with it, is to link it as a program, as that requires exactly one main function. That is what you're aiming for in the current c file. But there are a few problems with this file (int main;):

  • As mentioned above, this is not the smallest possible c file.
  • The file produces undefined behavior. Normally, main is a function. You've declared main as an int variable. In such case, the C standard AFAIK doesn't specify what happens, although it's permitted (i.e. it's undefined behavior). What a c compiler will typically do in this case, is to put the contents of main in an executable section and call it. This actually allows to run any machine code by assigning an array to main. Anyway, in this case, calling main causes the computer to run machine code consisting of one or more zero bytes (depending on the size of int), which e.g. on my linux x86-64 system causes a segmentation fault. To fix this, you can make main an empty function, by adding {} (returning 0 from main is implicit).
  • The type (int) is not needed, int is an implicit type used in c when none is given. Also, the ending semicolon is not needed as well.
    Given the above, I propose the following:
  • Make c.c an empty file.
  • Add another file, maybe called c_program.c, with the following contents: main(){}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant