7 ms·
Learn C in Y Minutes
- 174SIGSEGV 5y agoThe author recommends -std=c99 (-pedantic) default flags, yet suggests K&R (2nd edition, presumably) as a reference...
- pantalaimon 5y agoLittle reason to not use at least -std=c11 these days
- bierjunge 5y agoLinux is C99, most microcontrollers use C99 or even C89. It would be nice to get the new features, but it's not always possible.
- pantalaimon 5y ago> most microcontrollers use C99 or even C89 Most microcontrollers are ARM Cortex-M and are programmed with a recent GCC toolchain
- flohofwoe 5y agoMSVC has C11 support only since very, very recently: https://devblogs.microsoft.com/cppblog/c11-and-c17-standard-support-arriving-in-msvc/ https://devblogs.microsoft.com/cppblog/c11-and-c17-standard-... (it has a solid subset of C99 since around VS2015 though, just _Generic was missing - and ignoring VLAs, which have been "dropped" in later C standards anyway).
- junon 5y agoVerifiers haven't caught up to C11 yet.
- flohofwoe 5y agoAlso, why still use the old confusing long, short, long long etc... types when the much better fixed-width types had been standardized in stdint.h since C99 (even the Visual Studio C compiler has those since at least VS2015).
- yitchelle 5y agoOh man, multiple declaration on the single of code is nasty. Passing this kind of advice causes the beginners to develop bad habits. At least these are not uninitialised variables. // Shorthands for multiple declarations: int i1 = 1, i2 = 2; float f1 = 1.0, f2 = 2.0; int b, c; b = c = 0; Edited:.. After 1hr of posting my original comment, I noticed that there many opinions here. I should have added why I think this is bad. Readability and maintenance. I would rather it have one line per declaration like the following.. int i1=2; int i2=2; Each variable easier to be changed, and reading it is clearer as there are no ambiguity to its type. A common error I have see from "the multiple declarations in the single line" construct is that changing the type impacts all the variables whereas the intention was only for the first variable.
- flohofwoe 5y agoThis is also bad advice for C: void function_1(); Most people coming from other languages (like C++) will probably think that this is identical with "void function_1(void)", but without the explicit void argument list, the compiler will not warn if the function is accidentally called with parameters.
- okl 5y ago-Wstrict-prototypes "Warn if a function is declared or defined without specifying the argument types."
- junon 5y agoThis isn't the case nowadays.
- flohofwoe 5y agoHuh? Of course it is: https://godbolt.org/z/8qYeK89rh https://godbolt.org/z/8qYeK89rh It's only an error in C++, but not in C, and the warning for this isn't included in the usual "-Wall -Wextra" set.
- shoto_io 5y ago
- teddyh 5y agoI also strongly suggest the C Programming FAQ: http://c-faq.com/ http://c-faq.com/
- junon 5y agoWas expecting some snarky "Should I use C? No." single page but this was pleasantly well done.
- avinassh 5y agoAs a web developer, who mostly spends time with Go and Python, is there any benefits of learning C?
- bitexploder 5y agoC is a low level language that compiles down directly to assembly. It is similar to Go in this regard, but is a bit lower level. You have to manage memory yourself and pointers in C are much more dangerous. Reasons to learn C would include: absolute performance needed maybe for say a Python C extension, kernel hacking, some other project you want to contribute to uses C. I think it’s good to at least play with C and understand how memory management and pointers in C work, but I don’t think I would recommend you go out of your way to learn C or spend any significant amount of time writing it. At the end of the day it’s just another tool in the toolbox. I would recommend the book Code, by Petzold, if you want to really want to explore computers at a low level in a friendly format.
- inb4_cancelled 5y ago> language that compiles down directly to assembly Directly to machine code, not assembly.
- fractalb 5y agonitpick. I think C compiles directly to machine code. There's no intermediate step of compiling to assembly first.
- heinrichhartman 5y agoWriting correct C is REALLY hard and slow (strings, manual memory management, concurrency pitfalls). So the investment is not paying off unless you are writing highly performance critical backend services. What can pay-off is learning to read C. Node.js, Firefox, Postgres, Linux are all written in C/C++ so if you ever want to really understand what's going on or have to debug a nasty bug, then you will need to be able to find your way around these code-bases.
- 5y ago
- pjmlp 5y ago> Ah, C. Still the language of modern high-performance computing. Yet its major compilers are written in C++ nowadays, and C++ is the main language for GPGPU computing, including the memory model used by NVidia on their cards. I suggest some benchmarks updates to the author.
- qsatx 5y agoCUDA kernels look very much like C, you cannot use the STL and a bunch of other things. For practical purposes, it is C.
- pjmlp 5y agoCUDA has stopped being C in version 3.0, update yourself.
- johnnycerberus 5y agoTo be fair, C++ just brings over templated wrappers around battle-tested C CUDA libraries like cuDNN, cuBLAS, etc.
- pjmlp 5y agoCppCon 2019: Olivier Giroux “The One-Decade Task: Putting std::atomic in CUDA.” https://www.youtube.com/watch?v=VogqOscJYvk https://www.youtube.com/watch?v=VogqOscJYvk Future of ISO and CUDA C++ https://developer.nvidia.com/gtc/2020/video/cwe21284-vid https://developer.nvidia.com/gtc/2020/video/cwe21284-vid Thrust, the CUDA C++ template library https://docs.nvidia.com/cuda/thrust/index.html https://docs.nvidia.com/cuda/thrust/index.html Optix, single-ray shader programming model using C++, and ray Tracing acceleration using RT Cores. https://developer.nvidia.com/optix https://developer.nvidia.com/optix Phew, so many wrappers yet to list.
- jcelerier 5y agohttps://github.com/NVIDIA/libcudacxx https://github.com/NVIDIA/libcudacxx
- bvrmn 5y ago> (-11) % 3; // => -2, as one would expect Nope, it's very unexpected. Because parity check can now return 3 outcomes. (-3) % 2; // => -1
- 10x-dev 5y agoIf you want to write applications in C, do yourself a favor and read 21st Century C by Klemens.
- reedwolf 5y agoMy takeaway from this thread is that C programmers are completely insufferable.
- 10x-dev 5y agoThe one thing that is hard to tolerate in this thread is the negative comment that makes sweeping generalizations about other people based on their choice of programming language.
- yjftsjthsd-h 5y agoWhy? I see one subthread with a holy war on code style, but nothing else.
- 0xdeaddeaf 5y agoYou clearly haven’t met Haskell or Rust programmers.
- flohofwoe 5y agoYou haven't been in many programming language discussion threads have you? ;) IME C coders are a fairly relaxed bunch on average, definitely more relaxed than some of the younger and more "radical" language communities in C's neighborhood.
- fsociety 5y agoYep we are completely insufferable, now let us be :)
- baybal2 5y agoC is easy to learn, but very hard to master. Having self learned C devs do a big, complex program is an invitation for security vulnerabilities. Most of the time people find themselves fighting against the language, and struggling to have the program simply not to crash. If you see that, you start to think how much those guys would have time left to ensure anything like input sanitation, or code hardening in general.
- j-james 5y agoFor someone planning on learning C in the next couple of months (through K&R), do you have any advice on where to learn best modern practices afterwards?
- baybal2 5y agoUnfortunately not, and I see it as a big downside of C where you can only learn about important corner cases with your own bitter experience, direct instruction. Advanced C usage remaining a black magic is not strength, it's weakness. This assures that attackers will always have a significantly stronger upper hand against industry's salaryman developers.
- junon 5y ago"Advanced C usage" can you explain this, please? What in C is so "advanced" that it's more black magic than C++ or Rust or any other high level language? With C, what you see is what you get. I don't see how that can possibly be black magic.
- baybal2 5y agoThe problem is that you don't see that much. Compiler these days optimise the code beyond human comprehension. Memory management libs, and what hardware does with your physical memory layout is inscrutable. Register level vulnerabilities are very much black magic for anybody, but silicon maker itself. Memory management libs used by any big C program add to complexity. Fancy runtime pointer, and ROP sanitation libs fix some types of vulnerabilities, and add another. And so, and so, and so. I am a big proponent of C, but will not hide that C needs substantial education beyond just programming to use properly, and safely.
- nathias 5y ago"C, a language that combines all the elegance and power of assembly language with all the readability and maintainability of assembly language "
- andi999 5y agoY=36000?
- mauvehaus 5y agoThe core language is something a person might plausibly learn in Y minutes. Getting any useful work done almost invariably requires learning a lot about the environment and system your code is running in. The idioms of the Windows filesystem stack and most Linux userland code are radically different. Being able to write code for one will not confer immediate productivity in the other.
- qwerty456127 5y ago> Constants are written in all-caps out of convention, not requirement I wonder if there even is a language where something like this IS required. Most of the languages I know are case-sensitive yet the case doesn't mean anything specific in any of them, the case seemingly always is a matter of style.
- flohofwoe 5y agoAFAIK Go uses this for symbol visibility (e.g. symbols starting with an upper-case letter are exported, lower-case are private symbols).
- keb_ 5y agoThis guide recommends Learn C The Hard Way by Zed Shaw as further reading, but the ##C Freenode Channel specifically recommends avoiding it[1]. Can anyone tell me why this book is so contentious? [1] http://www.iso-9899.info/wiki/Books http://www.iso-9899.info/wiki/Books
- raspyberr 5y agohttp://www.iso-9899.info/wiki/Main_Page#Stuff_that_should_be_avoided http://www.iso-9899.info/wiki/Main_Page#Stuff_that_should_be...
- qwerty456127 5y agoBy the way, IMHO the value of a language tutorial which omits the subject of safe Unicode strings is questionable. The tutorial by the link suggests using zero-terminated byte arrays - nobody in their sane mind should be doing so.
- vncajules 5y agoI'd say start learning in C++ and slowly transition to C. That's what I did; it was worth it. There are a lot of good resources in learning C & C++.
- Goosee 5y agoAny book or website recs for c++?
- emilfihlman 5y ago>// Must declare a 'function prototype' before main() when >functions occur after >// your main() function. This should have been formulated as: You must declare a function as a prototype if you use the function before its definition appears in the code. It's not just main but all functions.