TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: How do I learn C properly?

432 点作者 buchanae大约 5 年前
I have 15+ years of experience in many languages: javascript, python, go, etc.<p>I can write C, but I&#x27;m not confident I&#x27;m doing it the right way. There are now decades of forums and blogs I can search through, but I&#x27;d love to have a single, clean, clear source of truth for recommendations on how to write C code.<p>I&#x27;ve started reading &quot;Modern C&quot; by Jens Gustedt. I&#x27;m not sure if this is the closest to a modern source of truth.<p>Note, I&#x27;d like my C code to work on Windows, which possibly restricts the &quot;right way&quot; to features provided by C89. But again, I&#x27;m not sure, the internet is super clear on this.<p>Thanks for the tips!

59 条评论

sramsay大约 5 年前
It sounds like you&#x27;re not asking, &quot;How do I learn the language?&quot; but &quot;How do I know I&#x27;m doing it right?&quot;<p>I think Gustedt&#x27;s book is superb if you&#x27;re just trying to learn the language, and it does a good job of presenting the most up-to-date standard. I admire the K&amp;R as much as anyone else, but it&#x27;s not exactly an up-to-date language resource at this point, and it doesn&#x27;t really provide any guidance on the design and structure of systems in C (that&#x27;s not it&#x27;s purpose).<p>You might have a look at Hanson&#x27;s <i>C Interfaces and Implementations: Techniques for Creating Reusable Software</i>. That&#x27;s focused on large projects and APIs, but it will give you a good sense of the &quot;cognitive style&quot; of C.<p><i>21st Century C</i> is very opinionated, and it spends a great deal of time talking about tooling, but overall, it&#x27;s a pretty good orientation to the C ecosystem and more modern idioms and libraries.<p>I might also put in a plug for Reese&#x27;s <i>Understanding and Using C Pointers</i>. That sounds a bit narrow, but it&#x27;s really a book about how you handle -- and think about -- memory in C, and it can be very eye-opening (even for people with a lot of experience with the language).<p>C forces you to think about things that you seldom have to consider with Javascript, Python, or Go. And yes: it&#x27;s an &quot;unsafe&quot; language that can set your hair on fire. But there are advantages to it as well. It&#x27;s screaming-fast, the library ecosystem is absolutely gigantic, there are decades of others&#x27; wisdom and experience upon which to draw, it shows no signs of going away any time soon, and you&#x27;ll have very little trouble keeping up with changes in the language.<p>It&#x27;s also a language that you can actually hold in your head at one time, because there&#x27;s very little sugar going on. It&#x27;s certainly possible to write extremely obfuscated code, but in practice, I find that I&#x27;m only rarely baffled reading someone else&#x27;s C code. If I am, it&#x27;s usually because I don&#x27;t understand the problem domain, not the language.
评论 #22521704 未加载
评论 #22522186 未加载
评论 #22521433 未加载
评论 #22521122 未加载
评论 #22521506 未加载
评论 #22537133 未加载
krychu大约 5 年前
The proper way to learn C is to get a good book, read it, and do exercises along the way.<p>Here’s an excellent book: C Programming: A Modern Approach, by King.<p>Avoid online tutorials, and know that there are many poor books on C, including the recommended here “Learn C the Hard Way”. It has factual problems; see under “Stuff that should be avoided” here: <a href="http:&#x2F;&#x2F;iso-9899.info&#x2F;wiki&#x2F;Main_Page" rel="nofollow">http:&#x2F;&#x2F;iso-9899.info&#x2F;wiki&#x2F;Main_Page</a><p>Note also, that unlike some other languages, C is not a language that you should learn by “trying things out”, due to the nature of “undefined behavior”.<p>Another recommendation would be to ask for CR on IRC.<p>Good luck!
评论 #22520757 未加载
评论 #22520557 未加载
评论 #22520354 未加载
ndesaulniers大约 5 年前
I work on Clang and the Linux kernel. JavaScript was the first language I feel I&#x27;ve mastered, before C. Of the many C books I have and have read, I only recommend 2:<p>Head First C by Griffiths and Griffiths<p>Expert C Programming: Deep C Secrets by van der Linden<p>The first is an excellent introduction, especially if you treat it as a workbook.<p>The second is a great intermediate book.<p>Really advanced stuff comes from seeing things in the wild.
评论 #22523614 未加载
grafelic大约 5 年前
An interesting way IME to learn C, is to code for an old platform like the Amiga.<p>All the same best practices apply, but the underlying OS is more simple and the constraints, like missing floating point, forces you investigate many aspects of C.<p>You can use Bebbo&#x27;s m68k cross compiler to compile for the platform: <a href="https:&#x2F;&#x2F;github.com&#x2F;bebbo&#x2F;amiga-gcc" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;bebbo&#x2F;amiga-gcc</a><p>No single source of truth for C best practices exist, but I can recommend using static analyzers like:<p><a href="https:&#x2F;&#x2F;splint.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;splint.org&#x2F;</a><p>and<p><a href="http:&#x2F;&#x2F;cppcheck.sourceforge.net&#x2F;" rel="nofollow">http:&#x2F;&#x2F;cppcheck.sourceforge.net&#x2F;</a><p>to steer you in the right direction.<p>Also, compile your code with the following gcc options: -pedantic -std=&lt;c99 or c89&gt; -ggdb3 -O0 -Wall -Wextra -Wformat=2 -Wmissing-include-dirs -Winit-self -Wswitch-default -Wswitch-enum -Wunused-parameter -Wfloat-equal -Wundef -Wshadow -Wlarger-than-1000 -Wunsafe-loop-optimizations -Wbad-function-cast -Wcast-qual -Wcast-align -Wconversion -Wlogical-op -Waggregate-return -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes -Wmissing-declarations -Wpacked -Wpadded -Wredundant-decls -Wnested-externs -Wunreachable-code -Winline -Winvalid-pch -Wvolatile-register-var -Wstrict-aliasing=2 -Wstrict-overflow=2 -Wtraditional-conversion -Wwrite-strings<p>Not that they all necessarily always makes sense, but they will reveal weak points in your code.
lmilcin大约 5 年前
Given the fact you already know some higher level languages I think the best way to learn C is to go low level.<p>C is fantastic language because it is within your reach to go and understand all small details of how various features are actually implemented. I find it very helpful to be able to understand how a piece of code actually functions on a low level.<p>Another tip: you are used to having high level features on your disposal. Don&#x27;t try to duplicate those features. Figure out how actual C projects deal with program structure and flow issues that you are used to solving with high level constructs.
评论 #22522084 未加载
评论 #22521970 未加载
pritovido大约 5 年前
C is a very simple language. I have used just the official old book to create a compiler for it:<p><a href="https:&#x2F;&#x2F;www.amazon.com&#x2F;Programming-Language-2nd-Brian-Kernighan&#x2F;dp&#x2F;0131103628" rel="nofollow">https:&#x2F;&#x2F;www.amazon.com&#x2F;Programming-Language-2nd-Brian-Kernig...</a><p>The best thing you can do is finding some open source code that interest you, read it and write it.<p>For example, I write my own controllers for CNCs, and there are lots of code for controlling stepper motors out there. I also learned OpenGL and sockets this way long time ago.<p>On the other hand, C is a terrible language for any big project unless you automate it some way.<p>I use mostly lisp in order to write C code automatically. C is too low level for writing any substantial code. Lisp has things like Macros(nothing to do with c macros) that can make you write code 10x, to 50x faster easily.
评论 #22520842 未加载
评论 #22520159 未加载
评论 #22520440 未加载
quelsolaar大约 5 年前
Once you know the basics watch this: <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=443UNeGrFoM" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=443UNeGrFoM</a><p>;-)
jakobdabo大约 5 年前
In addition to the other recommendations, make sure you fully understand the memory model of your target architecture, the stack, the heap, the linkage.<p>Also, understand the undefined behavior, the compiler optimizations and how it can affect your code if you forget about the UB or if you ignore the compiler warnings, and read about the `volatile` keyword.<p>And a personal tip, please tell the compiler not to use strict aliasing (I think it&#x27;s stupid) - you will get rid of some UB without much sacrifice.
评论 #22521469 未加载
评论 #22520269 未加载
tmotwu大约 5 年前
Like anything else, set yourself a goal for a project. What kinds? C today is typically used for developing embedded systems, kernel internals, or device drivers.<p>Depending on how deep you want to go. The most in depth way to understand C is to learn assembly on an ISA first.
pyuser583大约 5 年前
If you’re an experienced programmer, with some understanding of C, I recommend: “Expert C Programming: Deep Secrets” by Peter van der Linden.<p>It’s one of the best books in the world. About anything. Ever.
评论 #22524203 未加载
jussij大约 5 年前
&gt; Note, I&#x27;d like my C code to work on Windows, which possibly restricts the &quot;right way&quot; to features provided by C89<p>That might be the case if you plan to use the Microsoft C compiler as they have publicly said the don&#x27;t have plans to update their C compiler.<p>However that is not your only option, since clang and several GCC ports will also work on Windows.
评论 #22521765 未加载
评论 #22521329 未加载
juped大约 5 年前
This is actually pretty hard. I recommend reading C codebases, but it can be hard to know which ones are good. Code to C99 for now; keep an eye on C11 etc. Have a personal library of C implementations of things. Don&#x27;t worry about being on Windows, although if you&#x27;re doing Windows development C#&#x2F;.NET is the lowest friction path.<p>I&#x27;m writing a book on C. I don&#x27;t think it will ever be finished or see publication. I recommend writing a book yourself - it&#x27;s a great way to learn, and maybe you&#x27;re a better writer than me and will publish it.
begriffs大约 5 年前
It&#x27;s hard to beat this list of quality books:<p><a href="http:&#x2F;&#x2F;iso-9899.info&#x2F;wiki&#x2F;Books#Learning_C" rel="nofollow">http:&#x2F;&#x2F;iso-9899.info&#x2F;wiki&#x2F;Books#Learning_C</a><p>Read some and do the exercises. You&#x27;ll learn to write portable code to a standard rather than stuff that &quot;accidentally works.&quot; There&#x27;s a lot of crap C code in the world, full of GNUisms, overzealous macros, header-only nonsense abusing translation units, and copypasta. Don&#x27;t pick up bad habits from that stuff.
carapace大约 5 年前
Learn (a little bit of) assembly first. It will make more sense IMO if you came at it from below rather than above, so to speak.
Areading314大约 5 年前
I would recommend &quot;Learn C the Hard Way&quot; by Zed Shaw to get you started for something practical, because it goes over how C is written in the real world. Of course, situations will vary. The original book about C by K&amp;R is an excellent book too.
评论 #22532084 未加载
评论 #22521049 未加载
keithnz大约 5 年前
There is no clear source of truth for C. Depending on what you want to do there is quite different approaches. C for embedded systems tends to look quite different from C for systems programming. For instance MISRA is a C coding standard for embedded devices in Motor Vehicles (but many of its ideas are ideas from writing robust embedded systems), it says don&#x27;t use things like dynamic memory. You&#x27;d never use that if you are writing software for linux&#x2F;windows. So learning C &quot;properly&quot; would be about learning the different approaches people use in C to write robust code and what tradeoffs they are making. Understanding the C standard and the variations. Understanding bare metal programming. Understanding the effect of different underlying processors and hardware architectures. Understanding systems programming and how to consume APIs and how to create reusable binary modules. Understanding the compiler&#x2F;build system the intermediate files &#x2F; code generation &#x2F; memory layout. Read a variety of books ( including K&amp;R ) and study code ( Luas implementation is some very nicely written C )
chucksmash大约 5 年前
I liked &quot;C Programming: A Modern Approach&quot;[1]. It has been several years but as I recall it was well suited for self study and was pretty explicit in calling out places it was talking about C99 as opposed to C89.<p>[1]: <a href="https:&#x2F;&#x2F;www.amazon.com&#x2F;C-Programming-Modern-Approach-2nd&#x2F;dp&#x2F;0393979504" rel="nofollow">https:&#x2F;&#x2F;www.amazon.com&#x2F;C-Programming-Modern-Approach-2nd&#x2F;dp&#x2F;...</a>
评论 #22520344 未加载
fhars大约 5 年前
Essential reading:<p><a href="https:&#x2F;&#x2F;blog.regehr.org&#x2F;archives&#x2F;213" rel="nofollow">https:&#x2F;&#x2F;blog.regehr.org&#x2F;archives&#x2F;213</a><p><a href="http:&#x2F;&#x2F;blog.llvm.org&#x2F;2011&#x2F;05&#x2F;what-every-c-programmer-should-know.html?m=1" rel="nofollow">http:&#x2F;&#x2F;blog.llvm.org&#x2F;2011&#x2F;05&#x2F;what-every-c-programmer-should-...</a>
评论 #22521136 未加载
flohofwoe大约 5 年前
The Visual Studio C compiler (not the C++ compiler) supports &quot;almost all&quot; of C99, I would definitely recommend skipping C89, and use at least the new initialization features from C99 because they can reduce bugs caused by uninitialized data. Also, C++ compilers support a subset C99 that&#x27;s somewhere around &quot;C95&quot; and is already a much friendlier version compared to C89.<p>TBH, I think the best way to learn C is to tinker a bit with assembly first (it doesn&#x27;t have to be a modern assembly dialect, just write a few 6502 or Z80 assembly programs in a home computer emulator). Coming from &quot;below&quot; instead of &quot;above&quot; C&#x27;s abstraction level helps understanding the design rationale behind C, and the trade-offs between flexibility and unsafe memory handling.
dandigangi大约 5 年前
I learned a few basic levels of C using my subscription to pluralsight.com.<p>But... I quickly switched to Rust as I see it being the C of the future as it continues to develop. That&#x27;s just me though. (I have similar opinions towards things like Kotlin or Go.)<p>The books I am reading in the comments below are great resources.
cryptonector大约 5 年前
With C++, going modern-first is a great approach. But C hasn&#x27;t really been modernized that much, and because Microsoft has been so slow to fully support even just C99, let alone C11 or any GCC&#x2F;clang extensions, often one has to to stick to a subset of C99 anyways -- exactly as you surmised.<p>The problem with C is that it&#x27;s extremely dangerous. Besides learning how to program in C, you&#x27;ll really want to know how to use memory debuggers such as valgrind and Dr. Memory (<a href="https:&#x2F;&#x2F;www.drmemory.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.drmemory.org&#x2F;</a>), as well as AFL for fuzzing, and various GCC and clang sanitizers. You&#x27;ll really want to let functional programming language experience inform your approach to C API design and implementation.
评论 #22523586 未加载
bane大约 5 年前
The most important difference between a higher level language and C is that you really <i>have</i> to understand a bit about the underlying hardware architecture to do it &quot;right&quot;. C is a fairly thin veneer over assembly and it helps to have that mental model in mind when working in it.<p>It&#x27;s a very small language with almost no batteries included, built very much around the manipulation of pointers and memory more than just about anything else.<p>To do C the &quot;right&quot; way means approaching problems from that perspective and building mountains out of toothpicks while thinking about how the billions of toothpicks need to interact with one another so they don&#x27;t crash&#x2F;catch fire&#x2F;etc.
WalterBright大约 5 年前
I&#x27;d just type in the examples from K+R, try them out, and modify them to do new things.
评论 #22520088 未加载
评论 #22520104 未加载
vocram大约 5 年前
Becoming proficient in a language usually involves: - writing code with it long enough, - collaborate in a project based on it, - reading code other people wrote, - keep yourself up to date by following the language evolution, - go more in depth in language internals, compilers, etc...<p>I wrote C for 10+ years (mostly bare metal FW), yet I am still amazed of how little I know about it. Recently for example I learnt of all the things the dynamic linker does in Linux, how symbols memory addresses are resolved at runtime using PLT, ....<p>The good point about C is that it can be used for very different kind of projects, so the choices are a lot.
评论 #22520228 未加载
6c696e7578大约 5 年前
The C FAQ used to be a good place to start. Note, <i>used</i> to be:<p><a href="http:&#x2F;&#x2F;c-faq.com&#x2F;" rel="nofollow">http:&#x2F;&#x2F;c-faq.com&#x2F;</a><p>What did you want to do on Windows? I think this depends as much on the compiler as the code.
评论 #22521461 未加载
thefourthchime大约 5 年前
I would read K&amp;R, it’s a right of passage. Look at some of the larger open source projects for how to organize and manage a large c project.<p>And practice pointers. Get really comfortable with how memory works.
_benj大约 5 年前
I’m currently learning C myself and something that I realized is that often the hard part to learn is not C the language but the domain in which is being used.<p>For example, I’m trying to learn Linux drivers or embedded programming and I thought that I’d find in a C book information about registers and such, but registers don’t really belong to the C domain but to the hardware domain.<p>Thus, maybe find a way to clarify what you want to use C for and then learn those domain problems and how C solves them.
springogeek大约 5 年前
Programming in C requires you to take proper responsibility for what you write, but also allows for great power in return.<p>I&#x27;ve been working on a reasonably large (and cross platform) project in C (<a href="https:&#x2F;&#x2F;domeengine.com" rel="nofollow">https:&#x2F;&#x2F;domeengine.com</a>) for a couple years now, and I find that C generally requires a certain kind of discipline. When you interact with an API, you need to read the docs, figure out their specific preconditions, and requirements on handling frees, establishing what you own and what you don&#x27;t.<p>It also helps to make sure that if you allocate something, you clean up after in a clear and well defined way.<p>You should also get very familiar with debuggers (LLDB&#x2F;GDB) and other dev tools, to help when you have made a mistake.<p>In the modern era, you have a couple of options for getting it to run on Windows. C89 using Visual Studio, The MSYS2&#x2F;MinGW2 gcc toolchain, or a gcc toolchain using the WSL. I use the MSYS2 tools for DOME because it requires the fewest platform-specific modifications to my code, but this varies by usecase.
fergie大约 5 年前
Pretty obvious answer, but for those who are new to C, the C Programming Language by Brian Kernighan and Dennis Ritchie is a good place to start
评论 #22521498 未加载
bryanwb大约 5 年前
An important question to ask yourself is why do you want to learn C? If you want to be able to read large C codebases makes sense. But if you want to write embedded software or systems software, rust or c++11 (ugh) are arguably a better place to start. I say this as someone who has learned C but moved on to c++ without regrets. Though I have serious rust envy :)
评论 #22523734 未加载
jerrysievert大约 5 年前
while not a definitive guide, <a href="https:&#x2F;&#x2F;matt.sh&#x2F;howto-c" rel="nofollow">https:&#x2F;&#x2F;matt.sh&#x2F;howto-c</a> is a good example of how to approach &quot;modern&quot; c.<p>c99 is fully implemented in windows with msvc (2015), gcc, clang, and intel&#x27;s compiler, so &quot;right way&quot; should not need to involve c89. most are c11 compliant as well.
m0rphling大约 5 年前
If you&#x27;re into visual&#x2F;interactive learning, please give Harvard&#x27;s CS50 a try.<p><a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;channel&#x2F;UCcabW7890RKJzL968QWEykA" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;channel&#x2F;UCcabW7890RKJzL968QWEykA</a> <a href="https:&#x2F;&#x2F;cs50.harvard.edu" rel="nofollow">https:&#x2F;&#x2F;cs50.harvard.edu</a>
vbtemp大约 5 年前
First step is making sure you&#x27;re using the flags<p>`-Wall -Werror -pedantic` and then one of `-std=c89` or `-std=c99`<p>(Or equivalent in whatever Windows C compiler)
评论 #22520717 未加载
LocalMan大约 5 年前
I&#x27;ve written many tens of thousands of lines of C, but retired 15 years ago. The two best books I know are:<p>1. C: A Reference Manual, by Harbison and Steele. <a href="https:&#x2F;&#x2F;www.amazon.com&#x2F;Reference-Manual-Samuel-P-Harbison&#x2F;dp&#x2F;013089592X&#x2F;ref=dp_ob_title_bk" rel="nofollow">https:&#x2F;&#x2F;www.amazon.com&#x2F;Reference-Manual-Samuel-P-Harbison&#x2F;dp...</a><p>2. The C Puzzle Book, by Alan Fueur. <a href="https:&#x2F;&#x2F;www.amazon.com&#x2F;Puzzle-Book-Alan-R-Feuer&#x2F;dp&#x2F;0201604612" rel="nofollow">https:&#x2F;&#x2F;www.amazon.com&#x2F;Puzzle-Book-Alan-R-Feuer&#x2F;dp&#x2F;020160461...</a><p>Harbison and Steele has much better explanations than K&amp;R. The Fueur book taught me a lot about C declarations. Declarations are that part of C language that is them most unnecessarily difficult.<p>You asked about a slightly different question, best practices. But in the real world you&#x27;ll run into a lot of code that practices below that level.
haolez大约 5 年前
I would focus on sources that use C11 or C18, since there are some niceties in the newer standards.<p>As a reference, I like QEMU&#x27;s source code[1]. It&#x27;s huge, but the style and practices found in any file will help you get a grip on good C.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;qemu&#x2F;qemu" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;qemu&#x2F;qemu</a>
master_yoda_1大约 5 年前
This is a fun book to start with &quot;Expert C Programming Pete van Linden&quot; <a href="https:&#x2F;&#x2F;www.amazon.com&#x2F;Expert-Programming-Peter-van-Linden&#x2F;dp&#x2F;0131774298" rel="nofollow">https:&#x2F;&#x2F;www.amazon.com&#x2F;Expert-Programming-Peter-van-Linden&#x2F;d...</a>
johnisgood大约 5 年前
In Modern C (book mentioned by OP), there is takeaway 2.11.1.14 which says &quot;Don&#x27;t use NULL&quot; because:<p>The definition in the C standard of a possible expansion of the macro NULL is quite loose; it just has to be a null pointer constant. Therefore, a C compiler could choose any of the following for it: 0U, 0, &#x27;\0&#x27;, 0UL, 0L, 0ULL, 0LL, or (void * )0. It is important that the type behind NULL is not prescribed by the C standard. Often, people use it to emphasize that they are talking about a pointer constant, which it simply isn’t on many platforms. Using NULL in a context that we have not mastered completely is even dangerous. This will in particular appear in the context of functions with a variable number of arguments.<p>NULL hides more than it clarifies. Either use 0 or, if you really want to emphasize that the value is a pointer, use the magic token sequence (void * )0 directly.<p><a href="https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;libc&#x2F;manual&#x2F;html_node&#x2F;Null-Pointer-Constant.html" rel="nofollow">https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;libc&#x2F;manual&#x2F;html_node&#x2F;Null-Poin...</a> says:<p>The preferred way to write a null pointer constant is with NULL. You can also use 0 or (void * )0 as a null pointer constant, but using NULL is cleaner because it makes the purpose of the constant more evident.<p>If you use the null pointer constant as a function argument, then for complete portability you should make sure that the function has a prototype declaration. Otherwise, if the target machine has two different pointer representations, the compiler won’t know which representation to use for that argument. You can avoid the problem by explicitly casting the constant to the proper pointer type, but we recommend instead adding a prototype for the function you are calling.<p><a href="https:&#x2F;&#x2F;man.openbsd.org&#x2F;style" rel="nofollow">https:&#x2F;&#x2F;man.openbsd.org&#x2F;style</a> says:<p>NULL is the preferred null pointer constant. Use NULL instead of (type * )0 or (type * )NULL in all cases except for arguments to variadic functions where the compiler does not know the type.<p>---<p>Readers: what is your take on it and why?
评论 #22523673 未加载
评论 #22522183 未加载
评论 #22521723 未加载
Foobar8568大约 5 年前
I am currently relearning C, I tried different books over the years (to not say decades), never did more than a toy project. The book I picked up this time was extreme c (from Amazon reviews, failry new book), by Kamran Amini, and so far (few chapters in) it&#x27;s fairly enjoyable as it takes C, compilation process etc from the &quot;basics&quot;, fairly hands-on and not too much theorical and relatively fast paced. I feel that the book is more about how to make a program C and not teaching literally the language itself. So really what I needed.
msaqib8181大约 5 年前
I think you can use any version of Visual studio to write C programs. I recently did a video tutorial using Visual Studio: <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=JaDzgmZvY00" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=JaDzgmZvY00</a> on the &quot;C++ Modulus Operator&quot; <a href="https:&#x2F;&#x2F;www.mycplus.com&#x2F;tutorials&#x2F;cplusplus-programming-tutorials&#x2F;the-c-modulus-operator&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.mycplus.com&#x2F;tutorials&#x2F;cplusplus-programming-tuto...</a>
enesunal大约 5 年前
Essential parts of C language are actually really a small set.<p>What I&#x27;d <i>suggest</i> is, learn syntax and try to understand linux kernel (which is one of the very well crafted software piece we have written in C). If you don&#x27;t want to go that much of a deep dive, you can check sqlite source code as well. Writing code starts with reading it. Do yourself a favour and spend more time on reading code than reading books.
Paperweight大约 5 年前
Perhaps better than a book, can anyone authoritatively point to a few small-and-readable but best-in-class open-source C projects to use as a reference?
评论 #22520160 未加载
评论 #22520193 未加载
评论 #22520234 未加载
评论 #22520075 未加载
评论 #22520262 未加载
评论 #22520068 未加载
justforfunhere大约 5 年前
All the advice already here is very good and implementable.<p>Here are my two cents.<p>I have been programming in C professionally for the past 12 years, and I think the way to go would be to go implement a well known tool in C and then compare your code with the open source code of that tool. e.g. Git.<p>You will learn a lot from how the feature that you chose to implement in a certain way was implemented in a completely different way for various reasons.
guerrilla大约 5 年前
It&#x27;s not easy. The obvious answer is read and write lots of code. Experiment. Make sure you actually know how a computer works. Learning some assembly even if you don&#x27;t ever use it can help with that (free ebook &quot;Programming from the Ground up&quot; can be done in a weekend) Read existing code bases, different styles, like linux or some GNOME stuff.
makach大约 5 年前
Finding &quot;the right way&quot; is closer to religion than it is to problem solving and science.<p>You will always be vulnerable to criticism and other people&#x27;s opinions, and you need to open up and acknowledge this fact and work with that. There are best practices and principles you can adhere to. Stand up for your design decisions, and refactor when you have to.
makz大约 5 年前
I&#x27;d say there&#x27;s no proper way. Every project has its way of writing C. With the use of macros, sometimes it looks a bit like a DSL for every specific project.<p>My advice: think about what kind of software you want to write and look for similar projects, libraries, or contribute with a new module, functionality, adapt something to your needs...
评论 #22520230 未加载
jennyyang大约 5 年前
Get code reviews from someone that actually knows C. Mentorship and harsh code reviews are the best way to learn.
drran大约 5 年前
I use C since 1992, but I never confident in my C code, unless it heavy with macros to check error codes and return early, register memory allocations and free memory at return, and so on. Rust improved my confidence in C code a lot, but it looks more like Rust code now.
stephc_int13大约 5 年前
In my opinion, one of the main reasons C looks old and not practical to beginners is the standard libs API.<p>But it is perfectly possible to use C as a language with a more modern and easier to read API, but you&#x27;ll probably have to build your own.
Nursie大约 5 年前
There are other compilers for windows than MSVC++, so you can move beyond C89 if you wish.<p>stdint.h is useful and should be required reading. I still come across too many C projects that reimplement it poorly.<p>Learn to use valgrind, and maybe ddd too.
jfkebwjsbx大约 5 年前
You can compile with clang on Windows, so you can use later standards.
aWidebrant大约 5 年前
The main thing that C provides over other languages is direct hardware access. So study the hardware, find out what you want to do with it, then use C to implement it.
iRobbery大约 5 年前
Besides all that is mentioned already to read or do. I would suggest to try writing something you coded in python or go previously in C.
ajb大约 5 年前
Read John Regehr&#x27;s blog, eg <a href="https:&#x2F;&#x2F;blog.regehr.org&#x2F;archives&#x2F;213" rel="nofollow">https:&#x2F;&#x2F;blog.regehr.org&#x2F;archives&#x2F;213</a><p>Read &quot;Who Says C is simple?&quot;: <a href="http:&#x2F;&#x2F;cil-project.github.io&#x2F;cil&#x2F;doc&#x2F;html&#x2F;cil&#x2F;" rel="nofollow">http:&#x2F;&#x2F;cil-project.github.io&#x2F;cil&#x2F;doc&#x2F;html&#x2F;cil&#x2F;</a><p>Take up a different language :-)
gjkood大约 5 年前
To start with, &quot;The C Programming Langauge&quot;, Kernighan &amp; Richie (K &amp; R) [1]. Find the latest edition that you can buy.<p>I think the primary topic to master in C is pointers. This is where most falter. It takes a few years to &quot;master&quot; (if we ever do). Here I would recommend &quot;Understanding and Using C Pointers&quot;, Richard Reese. [2]<p>If you are interested in networking, any of the classic &quot;TCP&#x2F;IP Illustrated Vols I&#x2F;II&#x2F;III&quot;, W. Richard Stevens, [3] contain a ton of C code to implement components of TCP&#x2F;IP.<p>If you are interested in Graphics, then &quot;Graphics Gems&quot;, Andrew Glassner [4] is a good source.<p>&quot;An Introduction to GCC&quot;, Brian Gough, [5] to understand the tooling and its various bells and whistles.<p>My learning swimming by jumping into the deep end of the pool experience was realized by learning Windows Programming using the Charles Petzold book and navigating through Microsoft Foundation Classes in the late 80s&#x2F;early 90s. The state of the art in tooling wasn&#x27;t that great in those days and I spent months with the book to get things going. This was done after I had built a foundation with K&amp;R and a decent amount of Unix network programming.<p>I see a lot of the other posts recommend more modern books. But you still need to build your foundation on C and Pointers in particular.<p>Good luck on your journey.<p>[1] <a href="https:&#x2F;&#x2F;www.amazon.com&#x2F;Programming-Language-2nd-Brian-Kernighan&#x2F;dp&#x2F;0131103628&#x2F;ref=sr_1_1?crid=2FEGL0ITD7BO&amp;keywords=the+c+programming+language&amp;qid=1583705302&amp;sprefix=The+C+Progra%2Caps%2C212&amp;sr=8-1" rel="nofollow">https:&#x2F;&#x2F;www.amazon.com&#x2F;Programming-Language-2nd-Brian-Kernig...</a><p>[2] <a href="https:&#x2F;&#x2F;www.amazon.com&#x2F;Understanding-Using-Pointers-Techniques-Management&#x2F;dp&#x2F;1449344186&#x2F;ref=sr_1_2?crid=32I1T4I421WPZ&amp;keywords=understanding+and+using+c+pointers&amp;qid=1583705390&amp;sprefix=Understanding+and+Using+C+P%2Caps%2C221&amp;sr=8-2" rel="nofollow">https:&#x2F;&#x2F;www.amazon.com&#x2F;Understanding-Using-Pointers-Techniqu...</a><p>[3] <a href="https:&#x2F;&#x2F;www.amazon.com&#x2F;TCP-Illustrated-Protocols-Addison-Wesley-Professional&#x2F;dp&#x2F;0321336313&#x2F;ref=sr_1_1?crid=V0LZ9TYHEOFS&amp;keywords=tcp%2Fip+illustrated&amp;qid=1583705448&amp;sprefix=TCP%2Fip+%2Caps%2C222&amp;sr=8-1" rel="nofollow">https:&#x2F;&#x2F;www.amazon.com&#x2F;TCP-Illustrated-Protocols-Addison-Wes...</a><p>[4] <a href="https:&#x2F;&#x2F;www.amazon.com&#x2F;Graphics-Gems-Andrew-S-Glassner&#x2F;dp&#x2F;0122861663&#x2F;ref=sr_1_1?crid=1WEA9UYLALBRE&amp;keywords=graphics+gems&amp;qid=1583705599&amp;sprefix=Graphics+Ge%2Caps%2C212&amp;sr=8-1" rel="nofollow">https:&#x2F;&#x2F;www.amazon.com&#x2F;Graphics-Gems-Andrew-S-Glassner&#x2F;dp&#x2F;01...</a><p>[5] <a href="https:&#x2F;&#x2F;www.amazon.com&#x2F;Introduction-GCC-GNU-Compilers&#x2F;dp&#x2F;0954161793&#x2F;ref=sr_1_1?crid=1L8K7ERCL07RZ&amp;keywords=an+introduction+to+gcc&amp;qid=1583705516&amp;sprefix=introduction+to+GCC%2Caps%2C218&amp;sr=8-1" rel="nofollow">https:&#x2F;&#x2F;www.amazon.com&#x2F;Introduction-GCC-GNU-Compilers&#x2F;dp&#x2F;095...</a>
vineel567大约 5 年前
I am in the same boat as you are...bare with me by no means I am an expert.<p>In fact, I read couple of chapters in Modern C yesterday :). Here are some of the things I am doing to improve my C skills to match with some of the admired&#x2F;professional developers.<p>Decide which platform to use<p>~~~~~~~~~~~~~~~~~~<p>Unfortunately, to become proficient in it we need to write code and focus on a platform. I have been fighting between whether to develop on Windows vs Linux. I am very experienced in Windows environment(using debuggers&#x2F;cl&#x2F;linkers&#x2F;Windbg etc) but when it comes to writing good quality C code(not C++) and for learning how to write good maintainable moderately large source code, my research showed that Windows compilers&#x2F;C standard APIs are not great, in fact they hinder your productivity. I have wasted countless number of hours to just figure out how to properly create a simple C project with a decent build system. Unfortunately, I could not find one. The closest I could find is CMake as MSBuild is a nightmare to work with. I even tried NMAKE but failed. When it comes to documentation of finding basic C Api usage, MSDN(<a href="https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;cpp&#x2F;c-runtime-library&#x2F;run-time-routines-by-category" rel="nofollow">https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;cpp&#x2F;c-runtime-library&#x2F;run-t...</a>) does a decent job. But in the name of security you will find zillion variations(<i>_s, </i>_l) of an API and by default msvc compiler will not let you use some of the API in their simple forms. Instead, you have to define _CRT_SECURE_NO_WARNINGS etc. I think for someone just getting started to develop&#x2F;learn to write a decent code base in C these restrictions really hinder the productivity. So finally, I have decided to instead focus my learning on Linux platform(currently through WSL - Windows subsystem for Linux) with its POSIX apis. You know what, `man 3 printf` or `man 3 strlen` is soooooo much better than googling msdn<p>Mastering C<p>~~~~~~~<p>I think, the simple and straight answer here is reading good code and writing &quot;good&quot; code and also reading good C content(be it books or articles). I think these are the three ingredients necessary to get started. Of all the open source projects that I have investigated, I found Linux Kernel and related projects seems to have very good taste in terms of code quality. Currently, I am just focused how they use the language rather than what they actually do in the project. Things like, how they structure the project, how they name things, how they use types, how they create structures, how they pass structures to function, how they use light weight object based constructs, how they handle errors in function(for example forward only goto exits), how they use signed&#x2F;unsigned variables etc(more of my learnings to the end), how they use their own data structures. I think its good to initially focus&#x2F;target on ANSI C API with C99 instead of heavily relying on the OS specific API on which ever platform you choose. For example, such projects could be writing binary file parsers for example projects like .ISO file format etc.<p>Good C projects&#x2F;articles<p>~~~~~~~~~~~~~~~<p>1. Winlib.net - <a href="https:&#x2F;&#x2F;github.com&#x2F;jcpowermac&#x2F;wimlib" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jcpowermac&#x2F;wimlib</a> is a great source of information<p>2. e2fsprogs - <a href="https:&#x2F;&#x2F;git.kernel.org&#x2F;pub&#x2F;scm&#x2F;fs&#x2F;ext2&#x2F;e2fsprogs.git&#x2F;" rel="nofollow">https:&#x2F;&#x2F;git.kernel.org&#x2F;pub&#x2F;scm&#x2F;fs&#x2F;ext2&#x2F;e2fsprogs.git&#x2F;</a><p>3. MUSL - <a href="https:&#x2F;&#x2F;git.musl-libc.org&#x2F;cgit&#x2F;musl&#x2F;tree&#x2F;" rel="nofollow">https:&#x2F;&#x2F;git.musl-libc.org&#x2F;cgit&#x2F;musl&#x2F;tree&#x2F;</a><p>4. General C Coding Style - <a href="https:&#x2F;&#x2F;git.kernel.org&#x2F;pub&#x2F;scm&#x2F;linux&#x2F;kernel&#x2F;git&#x2F;torvalds&#x2F;linux.git&#x2F;tree&#x2F;Documentation&#x2F;process&#x2F;coding-style.rst" rel="nofollow">https:&#x2F;&#x2F;git.kernel.org&#x2F;pub&#x2F;scm&#x2F;linux&#x2F;kernel&#x2F;git&#x2F;torvalds&#x2F;lin...</a><p>4. <a href="https:&#x2F;&#x2F;nullprogram.com&#x2F;tags&#x2F;c&#x2F;" rel="nofollow">https:&#x2F;&#x2F;nullprogram.com&#x2F;tags&#x2F;c&#x2F;</a> - great source of C knowledge<p>5. CCAN - <a href="https:&#x2F;&#x2F;github.com&#x2F;rustyrussell&#x2F;ccan&#x2F;tree&#x2F;master&#x2F;ccan" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rustyrussell&#x2F;ccan&#x2F;tree&#x2F;master&#x2F;ccan</a> - great source of C tidbits from none other than Rusty Russell - I haven&#x27;t read all of them<p>6. POSIX 2018 standard - <a href="https:&#x2F;&#x2F;pubs.opengroup.org&#x2F;onlinepubs&#x2F;9699919799.2018edition&#x2F;" rel="nofollow">https:&#x2F;&#x2F;pubs.opengroup.org&#x2F;onlinepubs&#x2F;9699919799.2018edition...</a><p>continued in the comment....
评论 #22520978 未加载
tedunangst大约 5 年前
Radical suggestion: read the C standard.
评论 #22520347 未加载
LessDmesg大约 5 年前
Screw C, really. Let this crappy old language die off quietly. Learn Zig (the best replacement for C) or at least Rust.
ryan-allen大约 5 年前
I&#x27;d check out Zed Shaw&#x27;s book [0]. It&#x27;s opinionated, but I think that&#x27;s a good thing for something like C.<p>It&#x27;s broken up into exercises that you start working through straight away, and you start early with valgrind.<p>[0] <a href="https:&#x2F;&#x2F;learncodethehardway.org&#x2F;c&#x2F;" rel="nofollow">https:&#x2F;&#x2F;learncodethehardway.org&#x2F;c&#x2F;</a>
评论 #22521522 未加载
PureParadigm大约 5 年前
C is great for learning about how computers work at a low level. In my college, we started by writing a simple compiler. This should force you to understand pointers and memory which, as others have mentioned, are fundamental. You&#x27;ll have to both know how the assembly works and to write correct C code. So writing a simple compiler will force you to understand it two different ways at once. My opinion is you&#x27;ll learn more by diving in and doing things, especially with your existing programming background.<p>But, for your own sanity and everyone else&#x27;s, do not start new projects in C! (Aside from purely academic ones for you to learn.) The point of a programming language is to help humans write safe and correct code. In this sense, C has completely failed. The syntax itself is just begging you to make a subtle mistake. And due to lack of memory safety, even the most well tested and scrutinized projects in C&#x2F;C++ (such as Chromium) suffer from buffer overflows or memory leaks, potential security vulnerabilities that are automatically prevented by design in other languages. If you need to do something low level with any sort of confidence, use a memory-safe language like Rust, which can even do interop with existing C libraries and code.<p>(Edit: typo)
评论 #22521494 未加载
评论 #22520377 未加载