# C- Programming books.



## Phishfry (Mar 19, 2017)

I bought some old C books and I am finding that many of the examples would throw compile errors.

This prototype used is what got me messed up:

```
#include<stdio.h>
void main()
{
printf("Goodbye, cruel world\n");
}
```
I found how to fix it (with the help of some excellent compiler messages) but I am surprised so many books got it wrong.
Was this ever an acceptable code prototype?

Messed up that "C-Programming for Dummies" can't get it right.

```
#include<stdio.h>
int main(void)
{
printf("Goodbye, cruel world\n");
return 0;
}
```


----------



## obsigna (Mar 19, 2017)

You can get away with old books for grasping the concepts, however, you might want to accompany these with the latest ISO C11 standard. The finished standard is available only as a pretty expensive purchase, however the final draft from April 2011 is available as a .pdf-file for download:

http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

See also: https://en.wikipedia.org/wiki/C11_(C_standard_revision))

I learned C with the second edition of K&R - The C Programming language.

https://en.wikipedia.org/wiki/The_C_Programming_Language

Anyway, I always use sad ISO-C11 Draft in order to get the syntax straight. All my projects are preset with the compiler flag -std=c11.

You also need a reference to the C-Library. I use the GNU-Documentation, which most of the time applies to FreeBSD as well.

https://www.gnu.org/software/libc/manual/


----------



## bushido95 (Mar 22, 2017)

I own a copy of K&R C 2nd Edition and I have to agree it is a great resource for learning the C Programming language. It is a bit outdated though tbh. Another book I would recommend that is more modern and up to date would be "C programming absolute beginners guide" 3rd edition by Greg Perry. It's a great resource for people who are new to C or new to programming in general, it's much more modern and uses the C11 Standard.


----------



## Phishfry (Mar 23, 2017)

I bought 4 books:
In these two the examples were correct-
C Programming in Easy Steps(2009)
Practical C Programming O'Reilly(1997)

I am slowly working my way through them.
It was a good learning experience to correctly format the other examples to compile on FreeBSD.

The C for Dummies book has understandable real world examples for variables. The others seem to obsessed with math.


----------



## kpedersen (Mar 23, 2017)

Phishfry said:


> I bought some old C books and I am finding that many of the examples would throw compile errors.
> 
> This prototype used is what got me messed up:
> 
> ...



After using some old DOS compilers for a bit of fun, such as Borland 3, Symantec and Watcom (Actually Watcom was very good), it was quite clear that the C standards were not really well understood at the time and I imagine many books were written using code which happened to work on whatever compiler they were using.

That said... The above code is actually very close. With the correct code being this:


```
#include<stdio.h>
int main()
{
printf("Goodbye, cruel world\n");
}
```

It is pretty deep down in the C standard but *main() and only main()* is allowed to declare that it returns an *int* without actually having any return statement in its body. In this case it will implicitly return 0 when it reaches the end of the function. This is a standards thing and should work on any compiler you try it on, even with full warnings enables (-Wall -pedantic)

As for books, the K&R "The C Programming Language" is the cult classic and unlike other programming books I feel it has some history (or nostalgia) about it making it great fun to read. Make sure to get the 2nd edition (ANSI) version and tbh, I advise sticking with pre-C99 if possible since it will make sure your C works on every compiler (I am looking at you Microsoft cl!).
Unlike C++, the latest standard of C doesn't really add additional safety which I suppose would be my only reason to upgrade.

A blog post which has some useful info on the different types of main() (https://vineetgupta22.wordpress.com/2011/11/16/int-main-vs-void-main-vs-int-main-void-vs-main/)


----------



## drhowarddrfine (Mar 23, 2017)

kpedersen said:


> I feel it has some history (or nostalgia) about it making it great fun to read.


Not just that. I find it to be extremely readable. I was a hardware designer who never did more high level than assembly and was dragged kicking and screaming into C by my boss who insisted we learn it and made me buy that "white book".

That book, the first edition, sits in the most honored position on my book shelf, still with slips of paper as bookmarks I just now noticed.


----------



## Phishfry (Mar 23, 2017)

Do you have to be a math wiz to learn programming?

Most programming books dive right in to math. I picked up SICP and the people who complete that course must be genius. First half is all math.


----------



## kpedersen (Mar 23, 2017)

drhowarddrfine said:


> That book, the first edition, sits in the most honored position on my book shelf, still with slips of paper as bookmarks I just now noticed.



heh, nice. I used to read that book so much that my original paperback version fell apart. I am the proud owner of a hard back second edition copy (still pretty rough around the edges now haha)


----------



## kpedersen (Mar 23, 2017)

Phishfry said:


> Do you have to be a math wiz to learn programming?



Math certainly helps with a lot of problem domains within programming such as Graphics, data mining, etc. but it isnt necessary for lots of things. For example GUI development at most requires a vague understanding of coordinates.

In the book mentioned above "The C Programming Language", you will see that there is barely any maths involved for a lot of the examples. The biggest use in that book is to allocate the correct size for arrays via malloc, such as sizeof(float) multiplied by the number of floats you need in an array.


----------



## obsigna (Mar 24, 2017)

kpedersen said:


> ... (ANSI) version and tbh, I advise sticking with pre-C99 if possible since it will make sure your C works on every compiler (I am looking at you Microsoft cl!).



pre-C99 = C90, C89, really?

no inline functions
no variable length arrays
no single line comments //
no long long int, no boolean, no complex numbers
poorly standardized IEEE 754 floating point support
no snprintf()
no designated initializers and no compound literals
Granted, C11 is mostly a refinement of the great improvements which C99 brought to us, anyway, C90, C89 are the past. Young programmers producing new code should do this for the future, and for this reason my suggestion is to do development always against the latest standard, and to only fallback to a respective old standard in cases of maintenance of existing code.


----------



## Phishfry (Mar 24, 2017)

So I guess the publish date of the books would correspond to the different C-standard used.
A variety of books has not been bad. I like doing the examples. If you type something over and over it becomes natural.
Nice to reflect back on the work. The many directories of my examples take up almost no space. Even compiled.

I also have been doing parallel work with online learning.
https://www.eskimo.com/~scs/cclass/notes/top.html

Thanks for the link obsigna 
I always wondered what the double ampersand meant.
prog1 && prog2
Good explanation about the various uses of main().

I have some learning to do about the bit in brackets.
int main(int argc, char *agrv[]),
I understand the data type bit, not argc or *agrv[].
Will get to that chapter soon. One day at a time. Still hovering around chapters 3.


----------



## drhowarddrfine (Mar 24, 2017)

Spoiler alert!

argc is just the "argument count": the number of arguments being passed into the function, main, contained in argv[] which contains those values, the first value being the name of the program itself.


----------



## kpedersen (Mar 24, 2017)

obsigna said:


> pre-C99 = C90, C89, really?
> 
> no inline functions
> no variable length arrays
> ...


Pretty much, yeah. Unless you also count Plan9 C as a standard 

Inline functions don't work on Microsoft's compiler as recently as 2013 and many projects are still using 2010. Instead I use macros in a similar way as demonstrated here: http://fileadmin.cs.lth.se/cs/Personal/Tomas_Akenine-Moller/code/tritri_isectline.txt
Variable length arrays are again lacking in Microsoft's compiler up to 2012. And alloca() can be used in place (its not nice... but it compiles .
long long is a problem for me mainly because the OpenCL* compiler is C99 compliant but still cannot support long long correctly due to hardware limitations. So even though the standard supports it, sometimes it still isnt feasible. Similar with the floating point support.

I don't use snprintf or initializers. memset/calloc is good enough for me since 99.9% of the time I just want things to be zero and not garbage.
My solutions aren't safe... For that I would use C++ over C11 anyway. That said I also only recommend C++98 (and some sort of smart pointer implementation), but that is another story 

That said, I often use libraries that are C99+, I dont actively avoid them. I just try my hardest to make sure that the code I write works on the most jankiest of compilers haha.

* I know, not technically a proper C compiler but it is nice to have code compiling the same on both. Perhaps my use case is unique but I am sure many embedded C compilers have similar limitations.


----------



## obsigna (Mar 25, 2017)

kpedersen said:


> obsigna said:
> 
> 
> > pre-C99 = C90, C89, really? ...
> ...



For me the only valid C-Standard in the moment is C11 (ISO/IEC 9899:2011), nothing more and nothing less. Declaring a 5 years old Microsoft C compiler as a de facto standard is IMHO an arbitrary personal choice. Why would people who want to develop for FreeBSD in C with the most modern tool chain LLVM/clang (v.4.0 is ante portas in FreeBSD 12) restrict themselves because of something that Microsoft didn't get straight five years ago?

Microsoft produced so much bullshit over the decades, and I need to take care about this? For what reason? Please give me a break!


----------



## kpedersen (Mar 25, 2017)

Heh, honestly I would agree but I don't always get to make the decision on the tools we use. The 3D software "industry" is still obsessed with Microsoft and until these guys stop sponsoring my doctorate, I am a bit stuck.

I have enough fights on my hands when it comes to wrestling OpenGL over DirectX and keeping the C# developers safely in the scripting layer where they belong


----------



## drhowarddrfine (Mar 26, 2017)

kpedersen said:


> The 3D software "industry" is still obsessed with Microsoft


Really? Or do you mean the gaming industry?


----------



## kpedersen (Mar 26, 2017)

drhowarddrfine said:


> Really? Or do you mean the gaming industry?


Heh good catch. CAD and architecture is pretty platform agnostic but games and tools for games is still pretty much Windows or nothing.


----------



## azathoth (Jan 23, 2018)

Phishfry said:


> Do you have to be a math wiz to learn programming?
> 
> Most programming books dive right in to math. I picked up SICP and the people who complete that course must be genius. First half is all math.



SICP is one of the best programming books out there.
Did you read it?
It's very well written.
You don't need calculus to finish it.


----------



## herrbischoff (Jan 29, 2018)

sourabh dattalkar said:


> I have many of best books of C programming language with direct download without any hassle.



You posted a link to a pirate eBook site, which violates forum rules and is illegal practically everywhere in the world.


----------



## Snurg (Jan 29, 2018)

bushido95 said:


> ... K&R C 2nd Edition ...


Imho the only book that one needs about C.
Still using my heavily worn-out copy of it, since 1989.

And, clang is such a fine compiler that it tells in easy-to-understand manner when, what and how I have to change some detail stuff that changed since old ANSI standard, as I didn't do C for more than a decade until recently.



kpedersen said:


> ... Actually Watcom was very good ...


It was best compiler of the MSDOS era I know of.
And the package included Bjarne Stroustrup's book about C++, which imho probably is the only book on C++ one needs.


----------



## azathoth (Jan 30, 2018)

so many cool languages aside from c
werc.cat-v.org www.cat-v.org
common lisp cliki.net
swi prolog
www.pharo.org and www.aidaweb.si or www.seaside.st
happstack.com
learnadanow.com
gnoga.com
starting forth and gforth
racket and how to design programs book
chicken scheme
woo wee the possibilities


----------

