• Complain

it-ebooks - Learn C The Hard Way

Here you can read online it-ebooks - Learn C The Hard Way full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2016, publisher: iBooker it-ebooks, genre: Home and family. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

it-ebooks Learn C The Hard Way
  • Book:
    Learn C The Hard Way
  • Author:
  • Publisher:
    iBooker it-ebooks
  • Genre:
  • Year:
    2016
  • Rating:
    5 / 5
  • Favourites:
    Add to favourites
  • Your mark:
    • 100
    • 1
    • 2
    • 3
    • 4
    • 5

Learn C The Hard Way: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "Learn C The Hard Way" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

it-ebooks: author's other books


Who wrote Learn C The Hard Way? Find out the surname, the name of the author of the book and a list of all author's works by series.

Learn C The Hard Way — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Learn C The Hard Way" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
Learn C The Hard Way
Learn C The Hard Way

This is the in-progress free version of Learn C the Hard Way. It was just converted to a new format so things might be missing or formatted wrong. Email to report any problems.

Table Of Contents
Frequently Asked Questions

How long does this course take?

You should take as long as it takes to get through it, but focus on doing work every day. Some people take about 3 months, others 6 months, and some only a week.

What kind of computer do I need?

You will need either an OSX or Linux computer to complete this book.

Preface
Preface

This is a rough in-progress dump of the book. The grammar will probably be bad, there will be sections missing, but you get to watch me write the book and see how I do things.

You can also ask for help from me at any time and I'll usually answer within 1 or 2 days.

This list is a discussion list, not an an announce-only list. It's for discussing the book and asking questions.

Finally, don't forget that I have Learn Python The Hard Way which you should read if you can't code yet. LCTHW will not be for beginners, but for people who have at least read LPTHW or know one other programming language.

Exercise 7: More Variables, Some Math
Exercise 7: More Variables, Some Math

Let's get familiar with more things you can do with variables by declaring various ints, floats, chars, and doubles. We'll then use these in various math expressions so you get introduced to C's basic math.

#include int main(int argc, char *argv[]){ int bugs = 100; double bug_rate = 1.2; printf("You have %d bugs at the imaginary rate of %f.\n", bugs, bug_rate); long universe_of_defects = 1L * 1024L * 1024L * 1024L; printf("The entire universe has %ld bugs.\n", universe_of_defects); double expected_bugs = bugs * bug_rate; printf("You are expected to have %f bugs.\n", expected_bugs); double part_of_universe = expected_bugs / universe_of_defects; printf("That is only a %e portion of the universe.\n", part_of_universe); // this makes no sense, just a demo of something weird char nul_byte = '\0'; int care_percentage = bugs * nul_byte; printf("Which means you should care %d%%.\n", care_percentage); return 0;}

Here's what's going on in this little bit of nonsense:

ex7.c:1-4

The usual start of a C program.

ex7.c:5-6

Declare an int and double for some fake bug data.

ex7.c:8-9

Print out those two, so nothing new here.

ex7.c:11

Declare a huge number using a new type long for storing big numbers.

ex7.c:12-13

Print out that number using %ld which adds a modifier to the usual %d. Adding 'l' (the letter ell) means "print this as a long decimal".

ex7.c:15-17

Just more math and printing.

ex7.c:19-21

Craft up a depiction of your bug rate compared to the bugs in the universe, which is a completely inaccurate calculation. It's so small though that we have to use %e to print it in scientific notation.

ex7.c:24

Make a character, with a special syntax '\0' which creates a 'nul byte' character. This is effectively the number 0.

ex7.c:25

Multiply bugs by this character, which produces 0 for how much you should care. This demonstrates an ugly hack you find sometimes.

ex7.c:26-27

Print that out, and notice I've got a %% (two percent chars) so I can print a '%' (percent) character.

ex7.c:28-30

The end of the main function.

This bit of source is entirely just an exercise, and demonstrates how some math works. At the end, it also demonstrates something you see in C, but not in many other languages. To C, a "character" is just an integer. It's a really small integer, but that's all it is. This means you can do math on them, and a lot of software does just that, for good or bad.

This last bit is your first glance at how C gives you direct access to the machine. We'll be exploring that more in later exercises.

What You Should See

As usual, here's what you should see for the output:

$ make ex7cc -Wall -g ex7.c -o ex7$ ./ex7You have 100 bugs at the imaginary rate of 1.200000.The entire universe has 1073741824 bugs.You are expected to have 120.000000 bugs.That is only a 1.117587e-07 portion of the universe.Which means you should care 0%.$
How To Break It

Again, go through this and try breaking the printf by passing in the wrong arguments. See what happens when you try to print out that nul_byte variable too with %s vs. %c. When you break it, run it under Valgrind to see what it says about your breaking attempts.

Extra Credit
  • Make the number you assign to universe_of_defects various sizes until you get a warning from the compiler.
  • What do these really huge numbers actually print out?
  • Change long to unsigned long and try to find the number that makes that one too big.
  • Go search online to find out what unsigned does.
  • Try to explain to yourself (before I do in the next exercise) why you can multiply a char and an int.
Exercise 8: Sizes And Arrays
Exercise 8: Sizes And Arrays

In the last exercise you did math, but with a '\0' (nul) character. This may be odd coming from other languages, since they try to treat "strings" and "byte arrays" as different beasts. C however treats strings as just arrays of bytes, and it's only the different printing functions that know there's a difference.

Before I can really explain the significance of this, I have to introduce a few more concepts: sizeof and arrays. Here's the code we'll be talking about:

#include int main(int argc, char *argv[]){ int areas[] = {10, 12, 13, 14, 20}; char name[] = "Zed"; char full_name[] = { 'Z', 'e', 'd', ' ', 'A', '.', ' ', 'S', 'h', 'a', 'w', '\0' }; // WARNING: On some systems you may have to change the // %ld in this code to a %u since it will use unsigned ints printf("The size of an int: %ld\n", sizeof(int)); printf("The size of areas (int[]): %ld\n", sizeof(areas)); printf("The number of ints in areas: %ld\n", sizeof(areas) / sizeof(int)); printf("The first area is %d, the 2nd %d.\n", areas[0], areas[1]); printf("The size of a char: %ld\n", sizeof(char)); printf("The size of name (char[]): %ld\n", sizeof(name)); printf("The number of chars: %ld\n", sizeof(name) / sizeof(char)); printf("The size of full_name (char[]): %ld\n", sizeof(full_name)); printf("The number of chars: %ld\n", sizeof(full_name) / sizeof(char)); printf("name=\"%s\" and full_name=\"%s\"\n", name, full_name); return 0;}

In this code we create a few arrays with different data types in them. Because arrays of data are so central to how C works, there's a huge number of ways to create them. For now, just use the syntax type name[] = {initializer}; and we'll explore more. What this syntax means is, "I want an array of type that is initialized to {..}." When C sees this it does the following:

  • Look at the type, in this first case it's int.
  • Look at the [] and see that there's no length given.
  • Look at the initializer, {10, 12, 13, 14, 20} and figure out that you want those 5 ints in your array.
  • Create a piece of memory in the computer, that can hold 5 integers one after another.
Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Learn C The Hard Way»

Look at similar books to Learn C The Hard Way. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «Learn C The Hard Way»

Discussion, reviews of the book Learn C The Hard Way and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.