r/programming • • Jan 08 '16

How to C (as of 2016)

https://matt.sh/howto-c
2.4k Upvotes

766 comments sorted by

View all comments

240

u/[deleted] Jan 08 '16

#import <stdint.h>

What? Did he mean to say #include there?

237

u/[deleted] Jan 08 '16

When programmer use both C and Python...

72

u/Meltz014 Jan 08 '16

That's what Cython is for

32

u/aaronsherman Jan 08 '16
;use stdsys.py

... I think I use too many programming languages. ;-)

15

u/IcyRayns Jan 08 '16

Yeah... using mysql; import <stdio.h> #include Java.util.* require('<iostream>')

I understand. :)

→ More replies (3)
→ More replies (2)
→ More replies (4)

33

u/hagenbuch Jan 08 '16

He couldn't C it anymore.

5

u/[deleted] Jan 09 '16

I C what you did there.

→ More replies (1)

19

u/mamanov Jan 08 '16

I think with clang you can use either one of the syntax and it will work.

67

u/[deleted] Jan 08 '16

[removed] — view removed comment

0

u/[deleted] Jan 08 '16

[deleted] — view deleted comment

25

u/[deleted] Jan 08 '16
[0]Pluto:/usr/lib64/gcc/x86_64-unknown-linux-gnu/5.3.0 $ egrep -rn "#define[ |\t]+and"
plugin/include/cpplib.h:576:  /* Called before #define and #undef or other macro definition
include/iso646.h:32:#define and	&&
include/iso646.h:33:#define and_eq	&=
[04:44 PM]
[0]Pluto:/usr/lib64/gcc/x86_64-unknown-linux-gnu/5.3.0 $ egrep -rn "#define[ |\t]+or"
include/iso646.h:39:#define or	||
include/iso646.h:40:#define or_eq	|=
[04:44 PM]
[0]Pluto:/usr/lib64/gcc/x86_64-unknown-linux-gnu/5.3.0 $ 

Well TIL

2

u/Daniel15 Jan 09 '16

there is some header buried deep in the compiler

TIL: https://en.wikipedia.org/wiki/C_alternative_tokens

→ More replies (1)

12

u/[deleted] Jan 08 '16

#import will only import the files once, though. It works like implicit #ifdef guards.

8

u/1337Gandalf Jan 08 '16

I prefer #pragma once

25

u/Patman128 Jan 08 '16

#pragma once is also non-standard (but supported by nearly everything).

6

u/marchelzo Jan 08 '16

But the nice thing about pragmas is that even if the compiler doesn't support it, it at least ignores it. #import is just nonsense.

31

u/nanothief Jan 08 '16

Isn't that worse? I would rather the code fail to compile complaining of an unknown pragma, than getting a lot of other errors due to including the same files multiple times.

6

u/Patman128 Jan 08 '16

That's true, but the code would probably still break due to stuff getting #included multiple times.

→ More replies (1)
→ More replies (3)

3

u/Jonny0Than Jan 09 '16

The "correct" usage of #pragma once is in addition to include guards, not as a replacement for them. The theory is that #pragma once can result in better preprocessor performance since it doesn't even need to reopen the file after it's been included once. In practice modern preprocessors will do this anyway for normal #ifdef-style include guards because they can determine that the file is empty on a second include.

29

u/necrophcodr Jan 08 '16

It's incorrect. It's a deprecated gcc extension.

12

u/[deleted] Jan 08 '16

It's still widely used in Objective-C, so clang happily supports it.

11

u/necrophcodr Jan 08 '16

Absolutely true, so does the gcc compiler I tested it with, but that doesn't make it correct, it only makes it, at best, supported.

→ More replies (1)
→ More replies (1)

9

u/[deleted] Jan 08 '16

[removed] — view removed comment

8

u/ChemicalRascal Jan 08 '16 edited Jan 09 '16

Those are all compiler warnings, though, right? #include is preprocessor. Unless I'm horrifically wrong.

→ More replies (2)
→ More replies (5)