├── .gitignore ├── Makefile ├── README ├── conf.py ├── dia ├── Infix0.dia ├── PA.dia ├── PageMap.dia ├── PersonObject.dia ├── Postfix0.dia ├── RecursiveReverse.dia ├── SlidingWindow.dia ├── StudentObject2.dia ├── TCPConnectClose.dia ├── VA.dia ├── arch.box.dia ├── arch.cpumode.dia ├── arch.memhie.dia ├── arch.pabox.dia ├── arch.readmem.dia ├── arch.vabox.dia ├── arch.von.dia ├── array.count.dia ├── array.multichar.dia ├── array.multidim.dia ├── array.string.dia ├── asm.elfoverview.dia ├── asm.load.dia ├── asmc.array.dia ├── asmc.bitfield.dia ├── asmc.eax.dia ├── asmc.gcc.dia ├── asmc.link.dia ├── asmc.nocache.dia ├── asmc.stackframe.dia ├── asmc.struct.dia ├── cond.switch1.dia ├── cond.switch2.dia ├── expr.parse.dia ├── expr.variable.dia ├── fs.datablock.dia ├── fs.datablock_raw.png ├── fs.datablockaddr.dia ├── fs.dup.dia ├── fs.ext2layout.dia ├── fs.gd.dia ├── fs.gd_raw.png ├── fs.repr.dia ├── fs.rootinode.dia ├── fs.rootinode_raw.png ├── fs.sb.dia ├── fs.sb_raw.png ├── fs.vfs.dia ├── func.funccall.dia ├── func.scope.dia ├── func2.factorial.dia ├── func2.stratify.dia ├── interface.malloc.dia ├── interface.vaarg.dia ├── intro.bytecode.dia ├── intro.compile.dia ├── intro.interpret.dia ├── io.fd.dia ├── io.fdredirect.dia ├── io.mmap.dia ├── io.syscall.dia ├── jobs.pg.dia ├── jobs.pseudotty.dia ├── jobs.terminalmodule.dia ├── jobs.terminalqueue.dia ├── link.addrspace.dia ├── link.discontpa.dia ├── link.includeh.dia ├── link.indirect.dia ├── link.multiobj.dia ├── link.sepva.dia ├── link.swap.dia ├── linkedlist.binarytraverse.dia ├── linkedlist.binarytree.dia ├── linkedlist.circular.dia ├── linkedlist.constructbinary.dia ├── linkedlist.delete.dia ├── linkedlist.doubly.dia ├── linkedlist.hashtab.dia ├── linkedlist.insert.dia ├── linkedlist.pdelete.dia ├── linkedlist.reverse.dia ├── linkedlist.sentinel.dia ├── make.graph.dia ├── mmu.box.dia ├── mmu.directmap.dia ├── mmu.fullassoc.dia ├── mmu.memacc.dia ├── mmu.memacc_raw.png ├── mmu.mmuenable.dia ├── mmu.nocache.dia ├── mmu.pabox.dia ├── mmu.pagetable.dia ├── mmu.pagetable_raw.png ├── mmu.patag.dia ├── mmu.sepva.dia ├── mmu.setassoc.dia ├── mmu.ttw.dia ├── mmu.vabox.dia ├── number.addfloat.dia ├── number.biasfloat025.dia ├── number.carry.dia ├── number.digitallogic.dia ├── number.digitallogic_raw.png ├── number.float.dia ├── number.float17.dia ├── number.normalfloat17.dia ├── number.overflow.dia ├── number.overflowp.dia ├── op.bitwise.dia ├── op.shiftleft.dia ├── op.shiftright.dia ├── pointer.argv.dia ├── pointer.array.dia ├── pointer.linkedlist.dia ├── pointer.pointer0.dia ├── pointer.pointer1.dia ├── process.environ.dia ├── process.exec.dia ├── process.fork.dia ├── process.forkexec.dia ├── process.ipc.dia ├── process.pipe.dia ├── shellscript.shellexec.dia ├── signal.catch.dia ├── signal.internal.dia ├── signal.reentrancy.dia ├── socket.sockaddr.dia ├── socket.tcpflowchart.dia ├── socket.udpflowchart.dia ├── sortsearch.fn0.dia ├── sortsearch.mergesort.dia ├── stackqueue.bfs.dia ├── stackqueue.bfsqueue.dia ├── stackqueue.circular.dia ├── stackqueue.dfs.dia ├── stackqueue.stack.dia ├── stdlib.buffer.dia ├── stdlib.fgets.dia ├── struct.abstraction.dia ├── struct.parameter.dia ├── syllabus.dia ├── thread.corrupt.dia └── thread.philosopher.dia ├── en ├── cppbasics.rst ├── cppgp.rst ├── cppoo.rst ├── index.rst └── preface.rst ├── images ├── app-encoding.extascii.png ├── arch.box.png ├── arch.cpumode.png ├── arch.memhie.png ├── arch.pabox.png ├── arch.pobox.png ├── arch.readmem.png ├── arch.vabox.png ├── arch.von.png ├── array.count.png ├── array.multichar.png ├── array.multidim.png ├── array.string.png ├── asm.elfoverview.png ├── asm.load.png ├── asmc.array.png ├── asmc.bitfield.png ├── asmc.eax.png ├── asmc.gcc.png ├── asmc.link.png ├── asmc.nocache.png ├── asmc.stackframe.png ├── asmc.struct.png ├── cond.switch1.png ├── cond.switch2.png ├── cppoo.personobject.png ├── cppoo.studentobject.png ├── cppoo.studentobject2.png ├── expr.parse.png ├── expr.variable.png ├── fs.datablock.png ├── fs.datablockaddr.png ├── fs.dup.png ├── fs.ext2layout.png ├── fs.gd.png ├── fs.repr.png ├── fs.rootinode.png ├── fs.sb.png ├── fs.vfs.png ├── func.funccall.png ├── func.scope.png ├── func2.factorial.png ├── func2.stratify.png ├── interface.malloc.png ├── interface.vaarg.png ├── intro.bytecode.png ├── intro.compile.png ├── intro.interpret.png ├── io.fd.png ├── io.fdredirect.png ├── io.mmap.png ├── io.syscall.png ├── jobs.pg.png ├── jobs.pseudotty.png ├── jobs.terminalmodule.png ├── jobs.terminalqueue.png ├── link.addrspace.png ├── link.discontpa.png ├── link.includeh.png ├── link.indirect.png ├── link.multiobj.png ├── link.sepva.png ├── link.swap.png ├── linkedlist.binarytraverse.png ├── linkedlist.binarytree.png ├── linkedlist.circular.png ├── linkedlist.constructbinary.png ├── linkedlist.delete.png ├── linkedlist.doubly.png ├── linkedlist.hashtab.png ├── linkedlist.insert.png ├── linkedlist.pdelete.png ├── linkedlist.reverse.png ├── linkedlist.sentinel.png ├── make.graph.png ├── mmu.ap.png ├── mmu.box.png ├── mmu.cp15instr.png ├── mmu.directmap.png ├── mmu.discontpa.png ├── mmu.domainacc.png ├── mmu.fullassoc.png ├── mmu.level1desc.png ├── mmu.level2desc.png ├── mmu.memacc.png ├── mmu.mmuenable.png ├── mmu.nocache.png ├── mmu.pa.png ├── mmu.pabox.png ├── mmu.pagetable.png ├── mmu.patag.png ├── mmu.sectiondescmap.png ├── mmu.sepva.png ├── mmu.setassoc.png ├── mmu.swap.png ├── mmu.ttw.png ├── mmu.ttwdetail.png ├── mmu.vabox.png ├── normalize.py ├── note.png ├── number.addfloat.png ├── number.biasfloat025.png ├── number.carry.png ├── number.digitallogic.png ├── number.float.png ├── number.float17.png ├── number.fulladder.png ├── number.normalfloat17.png ├── number.overflow.png ├── number.overflowp.png ├── op.bitwise.png ├── op.shiftleft.png ├── op.shiftright.png ├── pointer.argv.png ├── pointer.array.png ├── pointer.linkedlist.png ├── pointer.pointer0.png ├── pointer.pointer1.png ├── pointer.type.gif ├── process.addressspace.png ├── process.environ.png ├── process.exec.png ├── process.fork.png ├── process.forkexec.png ├── process.ipc.png ├── process.pipe.png ├── road-works-sign.gif ├── shellscript.shellexec.png ├── signal.catch.png ├── signal.internal.png ├── signal.reentrancy.png ├── socket.sockaddr.png ├── socket.tcpflowchart.png ├── socket.udpflowchart.png ├── sortsearch.calcrecurrence.png ├── sortsearch.fn0.png ├── sortsearch.mergesort.png ├── sortsearch.recurrence1.png ├── sortsearch.recurrence2.png ├── sortsearch.sortcards.png ├── sortsearch.theta.png ├── stackqueue.bfs.png ├── stackqueue.bfsqueue.png ├── stackqueue.circular.png ├── stackqueue.dfs.png ├── stackqueue.stack.png ├── stdlib.buffer.png ├── stdlib.fgets.png ├── struct.abstraction.png ├── struct.complex.png ├── struct.parameter.png ├── tcpip.addressclass.png ├── tcpip.arpformat.png ├── tcpip.datagram.png ├── tcpip.ethernetformat.png ├── tcpip.ipformat.png ├── tcpip.loopback.png ├── tcpip.multiplex.png ├── tcpip.slidingwindow.png ├── tcpip.stack.png ├── tcpip.tcpconnection.png ├── tcpip.tcpformat.png ├── tcpip.transferlan.png ├── tcpip.transferovernet.png ├── tcpip.udpformat.png ├── thread.corrupt.png └── thread.philosopher.png ├── index.rst ├── mytheme ├── layout.html ├── sidebarextra.html ├── static │ ├── default.css_t │ ├── me.alipay.png │ └── sidebar.js └── theme.conf └── zh ├── app-encoding.rst ├── app-gfdl.rst ├── arch.rst ├── array.rst ├── asm.rst ├── asmc.rst ├── bibli.rst ├── codingstyle.rst ├── cond.rst ├── expr.rst ├── func.rst ├── func2.rst ├── gdb.rst ├── history.rst ├── index.rst ├── interface.rst ├── intro.rst ├── iter.rst ├── link.rst ├── linkedlist.rst ├── number.rst ├── op.rst ├── pointer.rst ├── preface.rst ├── prep.rst ├── sortsearch.rst ├── stackqueue.rst ├── stdlib.rst ├── struct.rst ├── summary1.rst └── type.rst /.gitignore: -------------------------------------------------------------------------------- 1 | _build 2 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | PAPER = 8 | BUILDDIR = _build 9 | 10 | # Internal variables. 11 | PAPEROPT_a4 = -D latex_paper_size=a4 12 | PAPEROPT_letter = -D latex_paper_size=letter 13 | ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 14 | # the i18n builder cannot share the environment and doctrees with the others 15 | I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 16 | 17 | .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext 18 | 19 | help: 20 | @echo "Please use \`make ' where is one of" 21 | @echo " html to make standalone HTML files" 22 | @echo " dirhtml to make HTML files named index.html in directories" 23 | @echo " singlehtml to make a single large HTML file" 24 | @echo " pickle to make pickle files" 25 | @echo " json to make JSON files" 26 | @echo " htmlhelp to make HTML files and a HTML help project" 27 | @echo " qthelp to make HTML files and a qthelp project" 28 | @echo " devhelp to make HTML files and a Devhelp project" 29 | @echo " epub to make an epub" 30 | @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" 31 | @echo " latexpdf to make LaTeX files and run them through pdflatex" 32 | @echo " text to make text files" 33 | @echo " man to make manual pages" 34 | @echo " texinfo to make Texinfo files" 35 | @echo " info to make Texinfo files and run them through makeinfo" 36 | @echo " gettext to make PO message catalogs" 37 | @echo " changes to make an overview of all changed/added/deprecated items" 38 | @echo " linkcheck to check all external links for integrity" 39 | @echo " doctest to run all doctests embedded in the documentation (if enabled)" 40 | 41 | clean: 42 | -rm -rf $(BUILDDIR)/* 43 | 44 | html: 45 | $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html 46 | @echo 47 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." 48 | 49 | dirhtml: 50 | $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml 51 | @echo 52 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." 53 | 54 | singlehtml: 55 | $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml 56 | @echo 57 | @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." 58 | 59 | pickle: 60 | $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle 61 | @echo 62 | @echo "Build finished; now you can process the pickle files." 63 | 64 | json: 65 | $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json 66 | @echo 67 | @echo "Build finished; now you can process the JSON files." 68 | 69 | htmlhelp: 70 | $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp 71 | @echo 72 | @echo "Build finished; now you can run HTML Help Workshop with the" \ 73 | ".hhp project file in $(BUILDDIR)/htmlhelp." 74 | 75 | qthelp: 76 | $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp 77 | @echo 78 | @echo "Build finished; now you can run "qcollectiongenerator" with the" \ 79 | ".qhcp project file in $(BUILDDIR)/qthelp, like this:" 80 | @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/akabook.qhcp" 81 | @echo "To view the help file:" 82 | @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/akabook.qhc" 83 | 84 | devhelp: 85 | $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp 86 | @echo 87 | @echo "Build finished." 88 | @echo "To view the help file:" 89 | @echo "# mkdir -p $$HOME/.local/share/devhelp/akabook" 90 | @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/akabook" 91 | @echo "# devhelp" 92 | 93 | epub: 94 | $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub 95 | @echo 96 | @echo "Build finished. The epub file is in $(BUILDDIR)/epub." 97 | 98 | latex: 99 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 100 | @echo 101 | @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." 102 | @echo "Run \`make' in that directory to run these through (pdf)latex" \ 103 | "(use \`make latexpdf' here to do that automatically)." 104 | 105 | latexpdf: 106 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 107 | @echo "Running LaTeX files through pdflatex..." 108 | $(MAKE) -C $(BUILDDIR)/latex all-pdf 109 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 110 | 111 | text: 112 | $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text 113 | @echo 114 | @echo "Build finished. The text files are in $(BUILDDIR)/text." 115 | 116 | man: 117 | $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man 118 | @echo 119 | @echo "Build finished. The manual pages are in $(BUILDDIR)/man." 120 | 121 | texinfo: 122 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 123 | @echo 124 | @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." 125 | @echo "Run \`make' in that directory to run these through makeinfo" \ 126 | "(use \`make info' here to do that automatically)." 127 | 128 | info: 129 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 130 | @echo "Running Texinfo files through makeinfo..." 131 | make -C $(BUILDDIR)/texinfo info 132 | @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." 133 | 134 | gettext: 135 | $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale 136 | @echo 137 | @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." 138 | 139 | changes: 140 | $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes 141 | @echo 142 | @echo "The overview file is in $(BUILDDIR)/changes." 143 | 144 | linkcheck: 145 | $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck 146 | @echo 147 | @echo "Link check complete; look for any errors in the above output " \ 148 | "or in $(BUILDDIR)/linkcheck/output.txt." 149 | 150 | doctest: 151 | $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest 152 | @echo "Testing of doctests in the sources finished, look at the " \ 153 | "results in $(BUILDDIR)/doctest/output.txt." 154 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Learning Linux C/C++ Programming from Scratch 2 | 3 | Prerequisite 4 | ============ 5 | 6 | $ sudo apt-get install python-setuptools 7 | $ sudo easy_install sphinx 8 | 9 | Build 10 | ===== 11 | 12 | $ make html 13 | $ make epub 14 | 15 | The build result will be generated under _build/html and _build/epub respectively. 16 | 17 | TODO 18 | ==== 19 | 20 | Generate images under images directory from dia files under dia directory automatically through Makefile, rather than manually. 21 | 22 | -------------------------------------------------------------------------------- /conf.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # akabook documentation build configuration file, created by 4 | # sphinx-quickstart on Thu Aug 2 17:00:42 2012. 5 | # 6 | # This file is execfile()d with the current directory set to its containing dir. 7 | # 8 | # Note that not all possible configuration values are present in this 9 | # autogenerated file. 10 | # 11 | # All configuration values have a default; values that are commented out 12 | # serve to show the default. 13 | 14 | import sys, os 15 | 16 | # If extensions (or modules to document with autodoc) are in another directory, 17 | # add these directories to sys.path here. If the directory is relative to the 18 | # documentation root, use os.path.abspath to make it absolute, like shown here. 19 | #sys.path.insert(0, os.path.abspath('.')) 20 | 21 | # -- General configuration ----------------------------------------------------- 22 | 23 | # If your documentation needs a minimal Sphinx version, state it here. 24 | #needs_sphinx = '1.0' 25 | 26 | # Add any Sphinx extension module names here, as strings. They can be extensions 27 | # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. 28 | extensions = ['sphinx.ext.todo'] 29 | 30 | # Add any paths that contain templates here, relative to this directory. 31 | templates_path = ['_templates'] 32 | 33 | # The suffix of source filenames. 34 | source_suffix = '.rst' 35 | 36 | # The encoding of source files. 37 | #source_encoding = 'utf-8-sig' 38 | 39 | # The master toctree document. 40 | master_doc = 'index' 41 | 42 | # General information about the project. 43 | project = u'akabook' 44 | copyright = u'2012, Sean Soong' 45 | 46 | # The version info for the project you're documenting, acts as replacement for 47 | # |version| and |release|, also used in various other places throughout the 48 | # built documents. 49 | # 50 | # The short X.Y version. 51 | version = '3.0' 52 | # The full version, including alpha/beta/rc tags. 53 | release = '3.0' 54 | 55 | # The language for content autogenerated by Sphinx. Refer to documentation 56 | # for a list of supported languages. 57 | #language = None 58 | 59 | # There are two options for replacing |today|: either, you set today to some 60 | # non-false value, then it is used: 61 | #today = '' 62 | # Else, today_fmt is used as the format for a strftime call. 63 | #today_fmt = '%B %d, %Y' 64 | 65 | # List of patterns, relative to source directory, that match files and 66 | # directories to ignore when looking for source files. 67 | exclude_patterns = ['_build'] 68 | 69 | # The reST default role (used for this markup: `text`) to use for all documents. 70 | #default_role = None 71 | 72 | # If true, '()' will be appended to :func: etc. cross-reference text. 73 | #add_function_parentheses = True 74 | 75 | # If true, the current module name will be prepended to all description 76 | # unit titles (such as .. function::). 77 | #add_module_names = True 78 | 79 | # If true, sectionauthor and moduleauthor directives will be shown in the 80 | # output. They are ignored by default. 81 | #show_authors = False 82 | 83 | # The name of the Pygments (syntax highlighting) style to use. 84 | pygments_style = 'sphinx' 85 | 86 | # A list of ignored prefixes for module index sorting. 87 | #modindex_common_prefix = [] 88 | 89 | 90 | # -- Options for HTML output --------------------------------------------------- 91 | 92 | # The theme to use for HTML and HTML Help pages. See the documentation for 93 | # a list of builtin themes. 94 | html_theme = 'mytheme' 95 | 96 | # Theme options are theme-specific and customize the look and feel of a theme 97 | # further. For a list of options available for each theme, see the 98 | # documentation. 99 | #html_theme_options = {} 100 | 101 | # Add any paths that contain custom themes here, relative to this directory. 102 | html_theme_path = ['.'] 103 | 104 | # The name for this set of Sphinx documents. If None, it defaults to 105 | # " v documentation". 106 | #html_title = None 107 | 108 | # A shorter title for the navigation bar. Default is the same as html_title. 109 | #html_short_title = None 110 | 111 | # The name of an image file (relative to this directory) to place at the top 112 | # of the sidebar. 113 | #html_logo = None 114 | 115 | # The name of an image file (within the static path) to use as favicon of the 116 | # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 117 | # pixels large. 118 | #html_favicon = None 119 | 120 | # Add any paths that contain custom static files (such as style sheets) here, 121 | # relative to this directory. They are copied after the builtin static files, 122 | # so a file named "default.css" will overwrite the builtin "default.css". 123 | html_static_path = ['_static'] 124 | 125 | # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, 126 | # using the given strftime format. 127 | #html_last_updated_fmt = '%b %d, %Y' 128 | 129 | # If true, SmartyPants will be used to convert quotes and dashes to 130 | # typographically correct entities. 131 | #html_use_smartypants = True 132 | 133 | # Custom sidebar templates, maps document names to template names. 134 | html_sidebars = { '**': ['localtoc.html', 'relations.html', 'sourcelink.html', 'searchbox.html', 'sidebarextra.html'] } 135 | 136 | # Additional templates that should be rendered to pages, maps page names to 137 | # template names. 138 | #html_additional_pages = {} 139 | 140 | # If false, no module index is generated. 141 | #html_domain_indices = True 142 | 143 | # If false, no index is generated. 144 | #html_use_index = True 145 | 146 | # If true, the index is split into individual pages for each letter. 147 | #html_split_index = False 148 | 149 | # If true, links to the reST sources are added to the pages. 150 | #html_show_sourcelink = True 151 | 152 | # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. 153 | #html_show_sphinx = True 154 | 155 | # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. 156 | #html_show_copyright = True 157 | 158 | # If true, an OpenSearch description file will be output, and all pages will 159 | # contain a tag referring to it. The value of this option must be the 160 | # base URL from which the finished HTML is served. 161 | #html_use_opensearch = '' 162 | 163 | # This is the file name suffix for HTML files (e.g. ".xhtml"). 164 | #html_file_suffix = None 165 | 166 | # Output file base name for HTML help builder. 167 | htmlhelp_basename = 'akabookdoc' 168 | 169 | 170 | # -- Options for LaTeX output -------------------------------------------------- 171 | 172 | latex_elements = { 173 | # The paper size ('letterpaper' or 'a4paper'). 174 | #'papersize': 'letterpaper', 175 | 176 | # The font size ('10pt', '11pt' or '12pt'). 177 | #'pointsize': '10pt', 178 | 179 | # Additional stuff for the LaTeX preamble. 180 | #'preamble': '', 181 | } 182 | 183 | # Grouping the document tree into LaTeX files. List of tuples 184 | # (source start file, target name, title, author, documentclass [howto/manual]). 185 | latex_documents = [ 186 | ('index', 'akabook.tex', u'akabook Documentation', 187 | u'Sean Soong', 'manual'), 188 | ] 189 | 190 | # The name of an image file (relative to this directory) to place at the top of 191 | # the title page. 192 | #latex_logo = None 193 | 194 | # For "manual" documents, if this is true, then toplevel headings are parts, 195 | # not chapters. 196 | #latex_use_parts = False 197 | 198 | # If true, show page references after internal links. 199 | #latex_show_pagerefs = False 200 | 201 | # If true, show URL addresses after external links. 202 | #latex_show_urls = False 203 | 204 | # Documents to append as an appendix to all manuals. 205 | #latex_appendices = [] 206 | 207 | # If false, no module index is generated. 208 | #latex_domain_indices = True 209 | 210 | 211 | # -- Options for manual page output -------------------------------------------- 212 | 213 | # One entry per manual page. List of tuples 214 | # (source start file, name, description, authors, manual section). 215 | man_pages = [ 216 | ('index', 'akabook', u'akabook Documentation', 217 | [u'Sean Soong'], 1) 218 | ] 219 | 220 | # If true, show URL addresses after external links. 221 | #man_show_urls = False 222 | 223 | 224 | # -- Options for Texinfo output ------------------------------------------------ 225 | 226 | # Grouping the document tree into Texinfo files. List of tuples 227 | # (source start file, target name, title, author, 228 | # dir menu entry, description, category) 229 | texinfo_documents = [ 230 | ('index', 'akabook', u'akabook Documentation', 231 | u'Sean Soong', 'akabook', 'One line description of project.', 232 | 'Miscellaneous'), 233 | ] 234 | 235 | # Documents to append as an appendix to all manuals. 236 | #texinfo_appendices = [] 237 | 238 | # If false, no module index is generated. 239 | #texinfo_domain_indices = True 240 | 241 | # How to display URL addresses: 'footnote', 'no', or 'inline'. 242 | #texinfo_show_urls = 'footnote' 243 | 244 | 245 | # -- Options for Epub output --------------------------------------------------- 246 | 247 | # Bibliographic Dublin Core info. 248 | epub_title = u'akabook' 249 | epub_author = u'Sean Soong' 250 | epub_publisher = u'Sean Soong' 251 | epub_copyright = u'2012, Sean Soong' 252 | 253 | # The language of the text. It defaults to the language option 254 | # or en if the language is not set. 255 | #epub_language = '' 256 | 257 | # The scheme of the identifier. Typical schemes are ISBN or URL. 258 | #epub_scheme = '' 259 | 260 | # The unique identifier of the text. This can be a ISBN number 261 | # or the project homepage. 262 | #epub_identifier = '' 263 | 264 | # A unique identification for the text. 265 | #epub_uid = '' 266 | 267 | # A tuple containing the cover image and cover page html template filenames. 268 | #epub_cover = () 269 | 270 | # HTML files that should be inserted before the pages created by sphinx. 271 | # The format is a list of tuples containing the path and title. 272 | #epub_pre_files = [] 273 | 274 | # HTML files shat should be inserted after the pages created by sphinx. 275 | # The format is a list of tuples containing the path and title. 276 | #epub_post_files = [] 277 | 278 | # A list of files that should not be packed into the epub file. 279 | #epub_exclude_files = [] 280 | 281 | # The depth of the table of contents in toc.ncx. 282 | #epub_tocdepth = 3 283 | 284 | # Allow duplicate toc entries. 285 | #epub_tocdup = True 286 | -------------------------------------------------------------------------------- /dia/Infix0.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/Infix0.dia -------------------------------------------------------------------------------- /dia/PA.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/PA.dia -------------------------------------------------------------------------------- /dia/PageMap.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/PageMap.dia -------------------------------------------------------------------------------- /dia/PersonObject.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/PersonObject.dia -------------------------------------------------------------------------------- /dia/Postfix0.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/Postfix0.dia -------------------------------------------------------------------------------- /dia/RecursiveReverse.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/RecursiveReverse.dia -------------------------------------------------------------------------------- /dia/SlidingWindow.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/SlidingWindow.dia -------------------------------------------------------------------------------- /dia/StudentObject2.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/StudentObject2.dia -------------------------------------------------------------------------------- /dia/TCPConnectClose.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/TCPConnectClose.dia -------------------------------------------------------------------------------- /dia/VA.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/VA.dia -------------------------------------------------------------------------------- /dia/arch.box.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/arch.box.dia -------------------------------------------------------------------------------- /dia/arch.cpumode.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/arch.cpumode.dia -------------------------------------------------------------------------------- /dia/arch.memhie.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/arch.memhie.dia -------------------------------------------------------------------------------- /dia/arch.pabox.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/arch.pabox.dia -------------------------------------------------------------------------------- /dia/arch.readmem.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/arch.readmem.dia -------------------------------------------------------------------------------- /dia/arch.vabox.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/arch.vabox.dia -------------------------------------------------------------------------------- /dia/arch.von.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/arch.von.dia -------------------------------------------------------------------------------- /dia/array.count.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/array.count.dia -------------------------------------------------------------------------------- /dia/array.multichar.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/array.multichar.dia -------------------------------------------------------------------------------- /dia/array.multidim.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/array.multidim.dia -------------------------------------------------------------------------------- /dia/array.string.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/array.string.dia -------------------------------------------------------------------------------- /dia/asm.elfoverview.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/asm.elfoverview.dia -------------------------------------------------------------------------------- /dia/asm.load.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/asm.load.dia -------------------------------------------------------------------------------- /dia/asmc.array.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/asmc.array.dia -------------------------------------------------------------------------------- /dia/asmc.bitfield.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/asmc.bitfield.dia -------------------------------------------------------------------------------- /dia/asmc.eax.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/asmc.eax.dia -------------------------------------------------------------------------------- /dia/asmc.gcc.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/asmc.gcc.dia -------------------------------------------------------------------------------- /dia/asmc.link.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/asmc.link.dia -------------------------------------------------------------------------------- /dia/asmc.nocache.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/asmc.nocache.dia -------------------------------------------------------------------------------- /dia/asmc.stackframe.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/asmc.stackframe.dia -------------------------------------------------------------------------------- /dia/asmc.struct.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/asmc.struct.dia -------------------------------------------------------------------------------- /dia/cond.switch1.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/cond.switch1.dia -------------------------------------------------------------------------------- /dia/cond.switch2.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/cond.switch2.dia -------------------------------------------------------------------------------- /dia/expr.parse.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/expr.parse.dia -------------------------------------------------------------------------------- /dia/expr.variable.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/expr.variable.dia -------------------------------------------------------------------------------- /dia/fs.datablock.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/fs.datablock.dia -------------------------------------------------------------------------------- /dia/fs.datablock_raw.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/fs.datablock_raw.png -------------------------------------------------------------------------------- /dia/fs.datablockaddr.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/fs.datablockaddr.dia -------------------------------------------------------------------------------- /dia/fs.dup.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/fs.dup.dia -------------------------------------------------------------------------------- /dia/fs.ext2layout.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/fs.ext2layout.dia -------------------------------------------------------------------------------- /dia/fs.gd.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/fs.gd.dia -------------------------------------------------------------------------------- /dia/fs.gd_raw.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/fs.gd_raw.png -------------------------------------------------------------------------------- /dia/fs.repr.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/fs.repr.dia -------------------------------------------------------------------------------- /dia/fs.rootinode.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/fs.rootinode.dia -------------------------------------------------------------------------------- /dia/fs.rootinode_raw.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/fs.rootinode_raw.png -------------------------------------------------------------------------------- /dia/fs.sb.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/fs.sb.dia -------------------------------------------------------------------------------- /dia/fs.sb_raw.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/fs.sb_raw.png -------------------------------------------------------------------------------- /dia/fs.vfs.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/fs.vfs.dia -------------------------------------------------------------------------------- /dia/func.funccall.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/func.funccall.dia -------------------------------------------------------------------------------- /dia/func.scope.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/func.scope.dia -------------------------------------------------------------------------------- /dia/func2.factorial.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/func2.factorial.dia -------------------------------------------------------------------------------- /dia/func2.stratify.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/func2.stratify.dia -------------------------------------------------------------------------------- /dia/interface.malloc.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/interface.malloc.dia -------------------------------------------------------------------------------- /dia/interface.vaarg.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/interface.vaarg.dia -------------------------------------------------------------------------------- /dia/intro.bytecode.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/intro.bytecode.dia -------------------------------------------------------------------------------- /dia/intro.compile.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/intro.compile.dia -------------------------------------------------------------------------------- /dia/intro.interpret.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/intro.interpret.dia -------------------------------------------------------------------------------- /dia/io.fd.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/io.fd.dia -------------------------------------------------------------------------------- /dia/io.fdredirect.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/io.fdredirect.dia -------------------------------------------------------------------------------- /dia/io.mmap.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/io.mmap.dia -------------------------------------------------------------------------------- /dia/io.syscall.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/io.syscall.dia -------------------------------------------------------------------------------- /dia/jobs.pg.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/jobs.pg.dia -------------------------------------------------------------------------------- /dia/jobs.pseudotty.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/jobs.pseudotty.dia -------------------------------------------------------------------------------- /dia/jobs.terminalmodule.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/jobs.terminalmodule.dia -------------------------------------------------------------------------------- /dia/jobs.terminalqueue.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/jobs.terminalqueue.dia -------------------------------------------------------------------------------- /dia/link.addrspace.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/link.addrspace.dia -------------------------------------------------------------------------------- /dia/link.discontpa.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/link.discontpa.dia -------------------------------------------------------------------------------- /dia/link.includeh.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/link.includeh.dia -------------------------------------------------------------------------------- /dia/link.indirect.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/link.indirect.dia -------------------------------------------------------------------------------- /dia/link.multiobj.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/link.multiobj.dia -------------------------------------------------------------------------------- /dia/link.sepva.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/link.sepva.dia -------------------------------------------------------------------------------- /dia/link.swap.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/link.swap.dia -------------------------------------------------------------------------------- /dia/linkedlist.binarytraverse.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/linkedlist.binarytraverse.dia -------------------------------------------------------------------------------- /dia/linkedlist.binarytree.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/linkedlist.binarytree.dia -------------------------------------------------------------------------------- /dia/linkedlist.circular.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/linkedlist.circular.dia -------------------------------------------------------------------------------- /dia/linkedlist.constructbinary.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/linkedlist.constructbinary.dia -------------------------------------------------------------------------------- /dia/linkedlist.delete.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/linkedlist.delete.dia -------------------------------------------------------------------------------- /dia/linkedlist.doubly.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/linkedlist.doubly.dia -------------------------------------------------------------------------------- /dia/linkedlist.hashtab.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/linkedlist.hashtab.dia -------------------------------------------------------------------------------- /dia/linkedlist.insert.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/linkedlist.insert.dia -------------------------------------------------------------------------------- /dia/linkedlist.pdelete.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/linkedlist.pdelete.dia -------------------------------------------------------------------------------- /dia/linkedlist.reverse.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/linkedlist.reverse.dia -------------------------------------------------------------------------------- /dia/linkedlist.sentinel.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/linkedlist.sentinel.dia -------------------------------------------------------------------------------- /dia/make.graph.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/make.graph.dia -------------------------------------------------------------------------------- /dia/mmu.box.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/mmu.box.dia -------------------------------------------------------------------------------- /dia/mmu.directmap.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/mmu.directmap.dia -------------------------------------------------------------------------------- /dia/mmu.fullassoc.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/mmu.fullassoc.dia -------------------------------------------------------------------------------- /dia/mmu.memacc.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/mmu.memacc.dia -------------------------------------------------------------------------------- /dia/mmu.memacc_raw.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/mmu.memacc_raw.png -------------------------------------------------------------------------------- /dia/mmu.mmuenable.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/mmu.mmuenable.dia -------------------------------------------------------------------------------- /dia/mmu.nocache.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/mmu.nocache.dia -------------------------------------------------------------------------------- /dia/mmu.pabox.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/mmu.pabox.dia -------------------------------------------------------------------------------- /dia/mmu.pagetable.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/mmu.pagetable.dia -------------------------------------------------------------------------------- /dia/mmu.pagetable_raw.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/mmu.pagetable_raw.png -------------------------------------------------------------------------------- /dia/mmu.patag.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/mmu.patag.dia -------------------------------------------------------------------------------- /dia/mmu.sepva.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/mmu.sepva.dia -------------------------------------------------------------------------------- /dia/mmu.setassoc.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/mmu.setassoc.dia -------------------------------------------------------------------------------- /dia/mmu.ttw.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/mmu.ttw.dia -------------------------------------------------------------------------------- /dia/mmu.vabox.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/mmu.vabox.dia -------------------------------------------------------------------------------- /dia/number.addfloat.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/number.addfloat.dia -------------------------------------------------------------------------------- /dia/number.biasfloat025.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/number.biasfloat025.dia -------------------------------------------------------------------------------- /dia/number.carry.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/number.carry.dia -------------------------------------------------------------------------------- /dia/number.digitallogic.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/number.digitallogic.dia -------------------------------------------------------------------------------- /dia/number.digitallogic_raw.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/number.digitallogic_raw.png -------------------------------------------------------------------------------- /dia/number.float.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/number.float.dia -------------------------------------------------------------------------------- /dia/number.float17.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/number.float17.dia -------------------------------------------------------------------------------- /dia/number.normalfloat17.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/number.normalfloat17.dia -------------------------------------------------------------------------------- /dia/number.overflow.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/number.overflow.dia -------------------------------------------------------------------------------- /dia/number.overflowp.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/number.overflowp.dia -------------------------------------------------------------------------------- /dia/op.bitwise.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/op.bitwise.dia -------------------------------------------------------------------------------- /dia/op.shiftleft.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/op.shiftleft.dia -------------------------------------------------------------------------------- /dia/op.shiftright.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/op.shiftright.dia -------------------------------------------------------------------------------- /dia/pointer.argv.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/pointer.argv.dia -------------------------------------------------------------------------------- /dia/pointer.array.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/pointer.array.dia -------------------------------------------------------------------------------- /dia/pointer.linkedlist.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/pointer.linkedlist.dia -------------------------------------------------------------------------------- /dia/pointer.pointer0.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/pointer.pointer0.dia -------------------------------------------------------------------------------- /dia/pointer.pointer1.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/pointer.pointer1.dia -------------------------------------------------------------------------------- /dia/process.environ.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/process.environ.dia -------------------------------------------------------------------------------- /dia/process.exec.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/process.exec.dia -------------------------------------------------------------------------------- /dia/process.fork.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/process.fork.dia -------------------------------------------------------------------------------- /dia/process.forkexec.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/process.forkexec.dia -------------------------------------------------------------------------------- /dia/process.ipc.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/process.ipc.dia -------------------------------------------------------------------------------- /dia/process.pipe.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/process.pipe.dia -------------------------------------------------------------------------------- /dia/shellscript.shellexec.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/shellscript.shellexec.dia -------------------------------------------------------------------------------- /dia/signal.catch.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/signal.catch.dia -------------------------------------------------------------------------------- /dia/signal.internal.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/signal.internal.dia -------------------------------------------------------------------------------- /dia/signal.reentrancy.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/signal.reentrancy.dia -------------------------------------------------------------------------------- /dia/socket.sockaddr.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/socket.sockaddr.dia -------------------------------------------------------------------------------- /dia/socket.tcpflowchart.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/socket.tcpflowchart.dia -------------------------------------------------------------------------------- /dia/socket.udpflowchart.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/socket.udpflowchart.dia -------------------------------------------------------------------------------- /dia/sortsearch.fn0.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/sortsearch.fn0.dia -------------------------------------------------------------------------------- /dia/sortsearch.mergesort.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/sortsearch.mergesort.dia -------------------------------------------------------------------------------- /dia/stackqueue.bfs.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/stackqueue.bfs.dia -------------------------------------------------------------------------------- /dia/stackqueue.bfsqueue.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/stackqueue.bfsqueue.dia -------------------------------------------------------------------------------- /dia/stackqueue.circular.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/stackqueue.circular.dia -------------------------------------------------------------------------------- /dia/stackqueue.dfs.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/stackqueue.dfs.dia -------------------------------------------------------------------------------- /dia/stackqueue.stack.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/stackqueue.stack.dia -------------------------------------------------------------------------------- /dia/stdlib.buffer.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/stdlib.buffer.dia -------------------------------------------------------------------------------- /dia/stdlib.fgets.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/stdlib.fgets.dia -------------------------------------------------------------------------------- /dia/struct.abstraction.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/struct.abstraction.dia -------------------------------------------------------------------------------- /dia/struct.parameter.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/struct.parameter.dia -------------------------------------------------------------------------------- /dia/syllabus.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/syllabus.dia -------------------------------------------------------------------------------- /dia/thread.corrupt.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/thread.corrupt.dia -------------------------------------------------------------------------------- /dia/thread.philosopher.dia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/dia/thread.philosopher.dia -------------------------------------------------------------------------------- /en/cppgp.rst: -------------------------------------------------------------------------------- 1 | Template and Generic Programming 2 | ======================================= 3 | 4 | **TODO: THIS WHOLE CHAPTER NEEDS OVERHAUL AND BROADENING** 5 | 6 | .. figure:: ../images/road-works-sign.gif 7 | 8 | Template 9 | ----------- 10 | 11 | C++ has many different data types: 12 | 13 | * primitive type: ``int``, ``char``, ``float``, ``double`` and so on 14 | * pointer 15 | * class and struct 16 | * array 17 | 18 | These data types are quite different but have some similar operations. However, so far we cannot write a general function to apply to all these types. For example, we can't write a single function ``power(x, exp)`` applying to all these types: ``int``, ``float``, ``class Complex`` and so on. C++ provides templates to implement general functions like this. We can define template classes or template functions with the ``template`` keyword. This is called **generic programming**. For example, 19 | 20 | .. code-block:: c++ 21 | :linenos: 22 | 23 | template 24 | struct Buffer 25 | { 26 | T v[max]; 27 | }; 28 | 29 | Buffer intBuf10; 30 | 31 | template 32 | T power(T a, int exp) 33 | { 34 | T ans = a; 35 | while (--exp > 0) { 36 | ans *= a; 37 | } 38 | return (ans); 39 | } 40 | 41 | The parameters in ``<>`` are template parameters. Through template parameters we can pass not only values but also types, in which case we should declare the type parameter with keyword ``class`` or ``typename``. Even declared with ``class``, the type parameter can be built-in type as well as class type. You can consider template as a kind of macro. For example, if we use ``Buffer `` as a type, the following type definition will be generated: 42 | 43 | .. code-block:: c++ 44 | :linenos: 45 | 46 | struct Buffer 47 | { 48 | int v[10]; 49 | }; 50 | 51 | If we invoke ``power(c, 5)`` and ``c`` is a ``Complex`` object, the following function definition will be generated; 52 | 53 | .. code-block:: c++ 54 | :linenos: 55 | 56 | Complex power(Complex a, int exp) 57 | { 58 | Complex ans = a; 59 | while (--exp > 0) { 60 | ans *= a; 61 | } 62 | return (ans); 63 | } 64 | 65 | If we invoke ``power(2.13, 4)``, another function definition will be generated. This processing is similar to macro expansion. It's called **template instantiation**. Template definitions are usually placed in header files. 66 | 67 | Inheritance is invented for code reuse, so is template. Then what makes reusable code? Suppose you wrote a function ``func1`` yesterday, and today you write code that calls ``func1``. That's the basic form of code reuse. Now Suppose you wrote a template function ``power`` yesterday to handle power calculation of built-in types, and today you implement a ``Complex`` class. It's interesting to note that the function you wrote yesterday can also apply to the type you implement today. The template function ``power`` works with any new type as long as that type implements the overloaded operator ``*=``. This case is similar to what we saw in the last section. Suppose you wrote the ``Animal`` class and the ``whosay`` function yesterday, and add a new class ``Dog`` today which inherits from the ``Animal`` class. Then the function ``whosay`` you wrote yesterday can also apply to the new derived class ``Dog``, because it implements the ``saywhat`` interface required by ``Animal`` class. 68 | 69 | Now try it yourself: write a template function ``myswap`` to swap two objects of any type. Test your code with different kinds of objects. Note the template cannot be named ``swap`` because there is one in C++ standard library. 70 | 71 | Template Class 72 | ------------------- 73 | 74 | Now we implement a template class. It behaves as a stack (supports push and pop) and can hold objects of any type. 75 | 76 | stack.h 77 | 78 | .. code-block:: c++ 79 | :linenos: 80 | 81 | #include 82 | using namespace std; 83 | 84 | template class Node { 85 | public: 86 | Node(T invalue): m_Value(invalue), m_Next(0) {} 87 | ~Node() ; 88 | 89 | T getValue() const {return m_Value;} 90 | void setValue(T value) {m_Value = value;} 91 | Node* getNext() const {return m_Next;} 92 | void setNext(Node* next) {m_Next = next;} 93 | private: 94 | T m_Value; 95 | Node* m_Next; 96 | }; 97 | 98 | template Node::~Node() { 99 | cout << m_Value << " deleted " << endl; 100 | if(m_Next) { 101 | delete m_Next; 102 | } 103 | } 104 | 105 | template class Stack { 106 | public: 107 | Stack(): m_Head(0), m_Count(0) {} 108 | ~Stack() {delete m_Head;} 109 | void push(const T& t); 110 | T pop(); 111 | T top() const; 112 | int count() const; 113 | private: 114 | Node *m_Head; 115 | int m_Count; 116 | }; 117 | 118 | template void Stack::push(const T& value) { 119 | Node *newNode = new Node(value); 120 | newNode->setNext(m_Head); 121 | m_Head = newNode; 122 | ++m_Count; 123 | } 124 | 125 | template T Stack::pop() { 126 | Node *popped = m_Head; 127 | if (m_Head != 0) { 128 | 129 | m_Head = m_Head->getNext(); 130 | T retval = popped->getValue(); 131 | popped->setNext(0); 132 | delete popped; 133 | --m_Count; 134 | return retval; 135 | } 136 | return 0; 137 | } 138 | 139 | template inline T Stack::top() const { 140 | return m_Head->getValue(); 141 | } 142 | 143 | template inline int Stack::count() const { 144 | return m_Count; 145 | } 146 | 147 | Since template class definition can be placed in a header file, there's no ``stack.cpp``. 148 | 149 | main.cpp 150 | 151 | .. code-block:: c++ 152 | :linenos: 153 | 154 | #include "stack.h" 155 | 156 | int main(void) { 157 | Stack intstack1, intstack2; 158 | int val; 159 | for(val = 0; val < 4; ++val) { 160 | intstack1.push(val); 161 | intstack2.push(2 * val); 162 | } 163 | while (intstack1.count()) { 164 | val = intstack1.pop(); 165 | cout << val << endl; 166 | } 167 | Stack stringstack; 168 | stringstack.push('A'); 169 | stringstack.push('B'); 170 | stringstack.push('C'); 171 | char val2; 172 | while (stringstack.count()) { 173 | val2 = stringstack.pop(); 174 | cout << val2 << endl; 175 | } 176 | cout << "Now intstack2 will be destructed." << endl; 177 | return 0; 178 | } 179 | 180 | Here is the result:: 181 | 182 | 3 deleted 183 | 3 184 | 2 deleted 185 | 2 186 | 1 deleted 187 | 1 188 | 0 deleted 189 | 0 190 | C deleted 191 | C 192 | B deleted 193 | B 194 | A deleted 195 | A 196 | Now intstack2 will be destructed. 197 | 6 deleted 198 | 4 deleted 199 | 2 deleted 200 | 0 deleted 201 | 202 | At the end of ``main`` function, ``intstack2`` is automatically destructed because it's allocated on the stack and runs out of duration. But the elements contained in ``intstack2`` are allocated by the ``new`` operator, how can they be destructed one after another? Please find the answer from the code. 203 | 204 | Template classes like this are called **containers**. They can hold elements of any type and can automatically manage the allocation and deallocation of these elements. C++ standard library has many different containers, each providing different interfaces for storing and accessing elements. 205 | -------------------------------------------------------------------------------- /en/index.rst: -------------------------------------------------------------------------------- 1 | English Version: Learning Linux C/C++ Programming from Scratch 2 | ********************************************************************* 3 | 4 | This new version is under construction and still incomplete. For the complete old version please refer to http://akaedu.github.com/book/. 5 | 6 | .. toctree:: 7 | :maxdepth: 1 8 | 9 | preface 10 | 11 | PART I: C Programming for Beginners 12 | 13 | PART II: Essential C Programming 14 | 15 | PART III: Linux System Programming 16 | 17 | PART IV: from C to C++ 18 | 19 | .. toctree:: 20 | :maxdepth: 2 21 | :numbered: 22 | 23 | cppbasics 24 | cppoo 25 | cppgp 26 | 27 | PART V: from C/C++ to Dynamic Programming Language 28 | 29 | APPENDIX 30 | 31 | :ref:`genindex` 32 | -------------------------------------------------------------------------------- /en/preface.rst: -------------------------------------------------------------------------------- 1 | Preface 2 | ************* 3 | 4 | Origin 5 | ========= 6 | 7 | This book is originated from a vocational training course when I once worked for a training company called AKA Education (http://www.akaedu.org). One of their courses is called Linux system developer. That is a four-month fulltime training course. At the beginning students may know nothing at all about programming or Linux system, but at the end of the course they should gain very solid C programming skills and also be competent as a Linux system administrator, furthermore, they should have a deep insight about computer architecture and the functioning of operating system and device drivers. 8 | 9 | As I see it, vocational training in computer science is more challenging than university education. Students' backgrounds vary strikingly: 10 | 11 | * graduates, undergraduates and even high school students 12 | * majored in computer science, other engineering disciplines and even accounting or human resource 13 | * some are merely at the age of 20, while some are more than 35 14 | 15 | It is all these students with different backgrounds and perception levels that constitute our class. Yet they do have common ground: to learn programming skills and have their occupation diverted to IT industry. This unique teaching experience gives rise to the birth of this book - because I couldn't find a textbook out of the shelf to utilize, I decided to write one myself. 16 | 17 | To be a system developer is no easy job. It takes four years to graduate from university, and students majored in computer science should learn loads of fundamental courses such as calculus, linear algebra, probability, discrete mathematics, combinatorics, automata theory, compiler, computer architecture, operating system, plus loads of applied courses such as C, C++, Java, database, computer networks, information security, software engineering, computer graphics, so on and so forth. By contrast, we only have four months to train a student from scratch, and when the course is over we must assure the student can find a job as a system developer. 18 | 19 | Why four months? That specific timespan is determined by the market. Most students won't be willing to pay for a longer and more expensive training course. We could have cut the course even shorter if we were satisfied with just teaching some Linux commands and service configurations to yield a system administrator, or teaching some JavaScript/CSS/HTML to yield a web designer. But we couldn't stop there. We aim to make students not only know WHAT and HOW, but also know WHY. 20 | 21 | So this is four years of university education vs. four months of vocational training to achieve about the same goal, i.e. to make students capable of landing a job. What we have done is optimizing the knowledge hierarchy, so that one doesn't have to learn all those mathematics or computer science fundamentals before learning how to program. We teach Linux C programming the whole time, but we do ensure that relevant fundamental knowledge will be mentioned at every right moment. To put it another way, Linux C programming is like the string, whilst mathematics, compiler, computer architecture, operating system, computer networks, software engineering, etc, are all beads, therefore we carry forward the training course by stringing the beads. That also accounts for why we teach C programming instead of some popular programming languages like Java, Python, PHP or whatever. C is more fundamental than Java, Python or PHP because the interpreters of those languages are all written in C. Java, Python, PHP or something like those are designed for the purpose of establishing a more abstract easy-to-use programming model and hiding some aspects of computer architecture and operating system, which means they are not suitable to be the string for beads. 22 | 23 | .. crossreference for interpreter above 24 | 25 | Our training course is comprised of five sections: 26 | 27 | #. Some basic Linux commands and operations. To be prepared for the next section, students will learn to do basic file and disk operations, work with an text editor and run a compiler. 28 | #. C programming. In this section students will gain a thorough understanding of C language syntax as well as the functioning of C compiler and linker. 29 | #. Linux system programming with C, which involves file and I/O, process, thread, network programming, information security, etc. In this section, students will learn more about what functions or services the Linux operating system provides and how to invoke them. Students will also learn some more advanced Linux system administration commands and service configurations, and will be capable of understanding or inferring how those commands and services work. 30 | #. C++ fundamentals and object-oriented programming. With solid C programming skills, it's natural to make the transition to C++. Besides, having written lots of codes for all the topics in section 3, now students can appreciate why object-oriented programming is necessary, and how to improve their code in object-oriented paradigm. 31 | #. Domain-specific developing, such as GUI, database, web backend, etc. Grounded by all 4 sections above, plus skills in any one of these domains, a really competent system developer is cranked out. 32 | 33 | This book is written for the 2nd, 3rd, 4th and 5th sections as a textbook. Now you know where it comes from. 34 | 35 | What I Care Most in Writing This Book 36 | ============================================ 37 | 38 | First of all, I strive to make it concise and precise. I won't employ jokes or other nonsense, and I will try hard to avoid ambiguities. As a programmer, I've got tons of technical books to read which costs me a lot of time. I have to go through each book quickly and spot the answer to my programming problem. So I can't read such books for fun and appreciate those unnourishing jokes, which to me is just beating around the bush. So I know what it's like when you read my book. Maybe I have little sense of humor, but I am trying to save your time. 39 | 40 | Secondly, I try to resolve all interdependencies among those knowledge and concepts. We often find in technical books that concept A depends on concept B, which means the defintion of A can only be described in terms of the definition of B, then concept B depends on concept C, then concept C, unfortunately, depends on concept A. So which one should be introduced first? Sometimes this happens because the arrangement of topics is less than ideal. In other cases such interdependency may be inherent and unavoidable. In this book I try hard to arrange topics and employ cross-references judiciously and try not to frustrate readers when interdependency is encountered. Thererfore all you need to do is reading this booking linearly from cover to cover. If you encouter some topics that you have been quite familiar with, please at lease skim for a while to avoid omitting some key concepts depended by later chapters. 41 | 42 | Lastly I hope this book can serve as an introductory guide to software technology. When being asked how to learn programming, many senior programmers would like to recommend a book list full of classical books. IMHO this creates more problems than it solves for newbies: 43 | 44 | * Okay, these are all classical books, but which one should I pick up first? 45 | * Every single book assumes I already have some sort of knowledge which I don't have. Is there a proper order to go through these classics? 46 | 47 | No there isn't a proper order. Because these classical books are written independently, the dependencies among them are meshy rather than linear. In this book I assume no prior knowledge first and bring you along the way. Each time I will introduce to you a classical book only when I think you already have the prior knowledge to follow it. After you finish this book, you know for yourself where to go further. 48 | 49 | What Should Be Prepared before Reading This Book 50 | ======================================================== 51 | 52 | This book aims to teach programming from scratch, so you don't need to have prior knowledge about programming. But before reading this book you should meet the following prerequisites: 53 | 54 | #. Be familiar with basic Linux system operations, such as file and directory operation commands, text editor and software package management. 55 | #. At least have high school mathematical knowledge to be able to follow this book. It goes without saying that the more mathematical knowledge the better, because computer technology is all about mathematics. 56 | #. Be eager to find out how programs and computers work under the hood. Knowing WHAT and HOW is sufficient for landing a job in IT industry, but if you are equivalently eager to know WHY, then this book is for you. 57 | 58 | Why This Book Instead of K&R 59 | =================================== 60 | 61 | [K&R]_ is the C programming bible. All C programmers know it, and many of them recommend it to newbies. We had once used [K&R]_ as the textbook in our teaching practice, then we found it too difficult for beginners. It's nothing short of a nightmare and frustrates beginners badly. [K&R]_ is only suitable for a skillful C programmer who wants to go over his knowledge of C and tries to correct some error notions he has held about C. And I swear it's not for beginners, so don't try this at home. 62 | 63 | Here's another thing you need to know. Before any ANSI C standard was formulated, the first edition of [K&R]_ was the de facto C standard. Then after C89 standard was formulated, [K&R]_ was revised to its second edition to comply with C89. Unfortunately, since then [K&R]_ hasn't been revised any more to reflect new amendments to C89 and the latest C99 standard, while this book complies with C99 standard and is more current than [K&R]_. 64 | 65 | Why Learning C Programming under Linux Instead of Windows 66 | ================================================================== 67 | 68 | C is a low level language, which means programs written in C interacts closely with operating system. And Linux is a completely open source operating system. When learning C programming under Linux, we can drill any function call down to the source code of the C runtime library, the kernel or even the hardware instructions. We can always find out implementation details to any extent that we like to. Or if we are not skillful enough, we can always find answers in all kinds of online maillists, newsgroups and IRC channels. 69 | 70 | This is not the case under Windows, where we can't see the source code of operating system and can only rely on those inevitably ambiguous documents. Even though there are also thousands of Microsoft specialists to consult, they are only more specialized in guessing the implementation details. Under Windows you can know WHAT and HOW, but you can never know exactly WHY. 71 | 72 | Software development under Windows often depends heavily on IDE (Integrated Development Environment) such as Visual Studio and Eclipse, which might not be good for beginners. We should start learning C programming from the most basic concepts such as compiler, linker, Makefile and command line environment. IDE conceals all these concepts with buttons and menus. Without knowledge of the underlying mechanisms you can't fully understand C programming language. Therefore IDE doesn't facilitate learning, in fact it impedes learning. Some day when you thoroughly understand how IDE integrates compiler, linker, Makefile etc you may choose to use IDE. But I guess by then you would prefer vi or Emacs instead, which are more versatile than IDE. 73 | 74 | Conventions 75 | ================== 76 | 77 | The font like ``The quick brown fox jumps over the lazy dog`` is used in this book for source code and terminal output. For monospace font I prefer Dejavu Sans Mono to Courier New, because digit ``1`` and lowercase letter ``l`` can be distinguished clearly, which is also the case with digit ``0`` and uppercase letter ``O``. At class I frequently find beginners copying the wrong characters printed in fonts other than Dejavu Sans Mono. 78 | 79 | Unfortunately I can't guarantee the source codes of online version are displayed in Dejavu San Mono. That depends on readers' browser. 80 | 81 | A source code block is like this: 82 | 83 | .. code-block:: bash 84 | :linenos: 85 | 86 | #! /bin/sh 87 | VAR=1 88 | VAR=$(($VAR+1)) 89 | echo $VAR 90 | 91 | The terminal output includes Shell prompt, commands input, and results output:: 92 | 93 | $ /bin/sh script.sh 94 | 2 95 | 96 | In this book we assume $ as Shell prompt. 97 | 98 | **Words in boldface** stand for emphasis. 99 | 100 | Acknowledgements 101 | ======================= 102 | 103 | This book could never have come into existence without my working experience in AKA Education and without approval from the company. So special thanks go to my former leaders, Teacher Li Ming and Teacher He Jiasheng. 104 | 105 | Besides, thanks to all the teachers that have helped me and inspired me: Di haixia, Lang Tieshan, Zhu Zhongtao, Liao Wenjiang, Han Chao, Qin Wei, Wu Wei, Zhang Di, Xing Wenpeng, He Xiaolong, Lin Xiaozhu, Wei Jianfan, Guo Tongbin, Wang Bo, Wang Lei, Hong Feng. 106 | 107 | Thanks Broadview of PHEI for publishing the Chinese edition of this book. 108 | 109 | Thanks to all the students and cyber friends who give valuable comments on this book. 110 | 111 | To readers of this book, thank you for trusting me and choosing this book as your very first one to learn programming. Hope this book can live up to your expectation. 112 | -------------------------------------------------------------------------------- /images/app-encoding.extascii.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/app-encoding.extascii.png -------------------------------------------------------------------------------- /images/arch.box.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/arch.box.png -------------------------------------------------------------------------------- /images/arch.cpumode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/arch.cpumode.png -------------------------------------------------------------------------------- /images/arch.memhie.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/arch.memhie.png -------------------------------------------------------------------------------- /images/arch.pabox.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/arch.pabox.png -------------------------------------------------------------------------------- /images/arch.pobox.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/arch.pobox.png -------------------------------------------------------------------------------- /images/arch.readmem.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/arch.readmem.png -------------------------------------------------------------------------------- /images/arch.vabox.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/arch.vabox.png -------------------------------------------------------------------------------- /images/arch.von.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/arch.von.png -------------------------------------------------------------------------------- /images/array.count.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/array.count.png -------------------------------------------------------------------------------- /images/array.multichar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/array.multichar.png -------------------------------------------------------------------------------- /images/array.multidim.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/array.multidim.png -------------------------------------------------------------------------------- /images/array.string.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/array.string.png -------------------------------------------------------------------------------- /images/asm.elfoverview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/asm.elfoverview.png -------------------------------------------------------------------------------- /images/asm.load.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/asm.load.png -------------------------------------------------------------------------------- /images/asmc.array.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/asmc.array.png -------------------------------------------------------------------------------- /images/asmc.bitfield.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/asmc.bitfield.png -------------------------------------------------------------------------------- /images/asmc.eax.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/asmc.eax.png -------------------------------------------------------------------------------- /images/asmc.gcc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/asmc.gcc.png -------------------------------------------------------------------------------- /images/asmc.link.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/asmc.link.png -------------------------------------------------------------------------------- /images/asmc.nocache.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/asmc.nocache.png -------------------------------------------------------------------------------- /images/asmc.stackframe.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/asmc.stackframe.png -------------------------------------------------------------------------------- /images/asmc.struct.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/asmc.struct.png -------------------------------------------------------------------------------- /images/cond.switch1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/cond.switch1.png -------------------------------------------------------------------------------- /images/cond.switch2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/cond.switch2.png -------------------------------------------------------------------------------- /images/cppoo.personobject.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/cppoo.personobject.png -------------------------------------------------------------------------------- /images/cppoo.studentobject.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/cppoo.studentobject.png -------------------------------------------------------------------------------- /images/cppoo.studentobject2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/cppoo.studentobject2.png -------------------------------------------------------------------------------- /images/expr.parse.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/expr.parse.png -------------------------------------------------------------------------------- /images/expr.variable.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/expr.variable.png -------------------------------------------------------------------------------- /images/fs.datablock.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/fs.datablock.png -------------------------------------------------------------------------------- /images/fs.datablockaddr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/fs.datablockaddr.png -------------------------------------------------------------------------------- /images/fs.dup.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/fs.dup.png -------------------------------------------------------------------------------- /images/fs.ext2layout.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/fs.ext2layout.png -------------------------------------------------------------------------------- /images/fs.gd.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/fs.gd.png -------------------------------------------------------------------------------- /images/fs.repr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/fs.repr.png -------------------------------------------------------------------------------- /images/fs.rootinode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/fs.rootinode.png -------------------------------------------------------------------------------- /images/fs.sb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/fs.sb.png -------------------------------------------------------------------------------- /images/fs.vfs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/fs.vfs.png -------------------------------------------------------------------------------- /images/func.funccall.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/func.funccall.png -------------------------------------------------------------------------------- /images/func.scope.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/func.scope.png -------------------------------------------------------------------------------- /images/func2.factorial.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/func2.factorial.png -------------------------------------------------------------------------------- /images/func2.stratify.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/func2.stratify.png -------------------------------------------------------------------------------- /images/interface.malloc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/interface.malloc.png -------------------------------------------------------------------------------- /images/interface.vaarg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/interface.vaarg.png -------------------------------------------------------------------------------- /images/intro.bytecode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/intro.bytecode.png -------------------------------------------------------------------------------- /images/intro.compile.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/intro.compile.png -------------------------------------------------------------------------------- /images/intro.interpret.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/intro.interpret.png -------------------------------------------------------------------------------- /images/io.fd.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/io.fd.png -------------------------------------------------------------------------------- /images/io.fdredirect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/io.fdredirect.png -------------------------------------------------------------------------------- /images/io.mmap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/io.mmap.png -------------------------------------------------------------------------------- /images/io.syscall.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/io.syscall.png -------------------------------------------------------------------------------- /images/jobs.pg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/jobs.pg.png -------------------------------------------------------------------------------- /images/jobs.pseudotty.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/jobs.pseudotty.png -------------------------------------------------------------------------------- /images/jobs.terminalmodule.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/jobs.terminalmodule.png -------------------------------------------------------------------------------- /images/jobs.terminalqueue.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/jobs.terminalqueue.png -------------------------------------------------------------------------------- /images/link.addrspace.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/link.addrspace.png -------------------------------------------------------------------------------- /images/link.discontpa.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/link.discontpa.png -------------------------------------------------------------------------------- /images/link.includeh.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/link.includeh.png -------------------------------------------------------------------------------- /images/link.indirect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/link.indirect.png -------------------------------------------------------------------------------- /images/link.multiobj.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/link.multiobj.png -------------------------------------------------------------------------------- /images/link.sepva.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/link.sepva.png -------------------------------------------------------------------------------- /images/link.swap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/link.swap.png -------------------------------------------------------------------------------- /images/linkedlist.binarytraverse.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/linkedlist.binarytraverse.png -------------------------------------------------------------------------------- /images/linkedlist.binarytree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/linkedlist.binarytree.png -------------------------------------------------------------------------------- /images/linkedlist.circular.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/linkedlist.circular.png -------------------------------------------------------------------------------- /images/linkedlist.constructbinary.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/linkedlist.constructbinary.png -------------------------------------------------------------------------------- /images/linkedlist.delete.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/linkedlist.delete.png -------------------------------------------------------------------------------- /images/linkedlist.doubly.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/linkedlist.doubly.png -------------------------------------------------------------------------------- /images/linkedlist.hashtab.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/linkedlist.hashtab.png -------------------------------------------------------------------------------- /images/linkedlist.insert.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/linkedlist.insert.png -------------------------------------------------------------------------------- /images/linkedlist.pdelete.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/linkedlist.pdelete.png -------------------------------------------------------------------------------- /images/linkedlist.reverse.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/linkedlist.reverse.png -------------------------------------------------------------------------------- /images/linkedlist.sentinel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/linkedlist.sentinel.png -------------------------------------------------------------------------------- /images/make.graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/make.graph.png -------------------------------------------------------------------------------- /images/mmu.ap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/mmu.ap.png -------------------------------------------------------------------------------- /images/mmu.box.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/mmu.box.png -------------------------------------------------------------------------------- /images/mmu.cp15instr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/mmu.cp15instr.png -------------------------------------------------------------------------------- /images/mmu.directmap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/mmu.directmap.png -------------------------------------------------------------------------------- /images/mmu.discontpa.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/mmu.discontpa.png -------------------------------------------------------------------------------- /images/mmu.domainacc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/mmu.domainacc.png -------------------------------------------------------------------------------- /images/mmu.fullassoc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/mmu.fullassoc.png -------------------------------------------------------------------------------- /images/mmu.level1desc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/mmu.level1desc.png -------------------------------------------------------------------------------- /images/mmu.level2desc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/mmu.level2desc.png -------------------------------------------------------------------------------- /images/mmu.memacc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/mmu.memacc.png -------------------------------------------------------------------------------- /images/mmu.mmuenable.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/mmu.mmuenable.png -------------------------------------------------------------------------------- /images/mmu.nocache.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/mmu.nocache.png -------------------------------------------------------------------------------- /images/mmu.pa.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/mmu.pa.png -------------------------------------------------------------------------------- /images/mmu.pabox.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/mmu.pabox.png -------------------------------------------------------------------------------- /images/mmu.pagetable.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/mmu.pagetable.png -------------------------------------------------------------------------------- /images/mmu.patag.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/mmu.patag.png -------------------------------------------------------------------------------- /images/mmu.sectiondescmap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/mmu.sectiondescmap.png -------------------------------------------------------------------------------- /images/mmu.sepva.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/mmu.sepva.png -------------------------------------------------------------------------------- /images/mmu.setassoc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/mmu.setassoc.png -------------------------------------------------------------------------------- /images/mmu.swap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/mmu.swap.png -------------------------------------------------------------------------------- /images/mmu.ttw.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/mmu.ttw.png -------------------------------------------------------------------------------- /images/mmu.ttwdetail.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/mmu.ttwdetail.png -------------------------------------------------------------------------------- /images/mmu.vabox.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/mmu.vabox.png -------------------------------------------------------------------------------- /images/normalize.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import Image, glob 3 | 4 | filelist = [] 5 | filelist += glob.glob("*.png") 6 | filelist += glob.glob("*.PNG") 7 | filelist += glob.glob("*.jpg") 8 | filelist += glob.glob("*.JPG") 9 | filelist += glob.glob("*.gif") 10 | filelist += glob.glob("*.GIF") 11 | 12 | for f in filelist: 13 | im = Image.open(f) 14 | width, height = im.size 15 | if width > 600: 16 | im.resize((600, height*600/width), Image.BICUBIC).save(f) 17 | 18 | -------------------------------------------------------------------------------- /images/note.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/note.png -------------------------------------------------------------------------------- /images/number.addfloat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/number.addfloat.png -------------------------------------------------------------------------------- /images/number.biasfloat025.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/number.biasfloat025.png -------------------------------------------------------------------------------- /images/number.carry.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/number.carry.png -------------------------------------------------------------------------------- /images/number.digitallogic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/number.digitallogic.png -------------------------------------------------------------------------------- /images/number.float.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/number.float.png -------------------------------------------------------------------------------- /images/number.float17.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/number.float17.png -------------------------------------------------------------------------------- /images/number.fulladder.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/number.fulladder.png -------------------------------------------------------------------------------- /images/number.normalfloat17.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/number.normalfloat17.png -------------------------------------------------------------------------------- /images/number.overflow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/number.overflow.png -------------------------------------------------------------------------------- /images/number.overflowp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/number.overflowp.png -------------------------------------------------------------------------------- /images/op.bitwise.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/op.bitwise.png -------------------------------------------------------------------------------- /images/op.shiftleft.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/op.shiftleft.png -------------------------------------------------------------------------------- /images/op.shiftright.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/op.shiftright.png -------------------------------------------------------------------------------- /images/pointer.argv.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/pointer.argv.png -------------------------------------------------------------------------------- /images/pointer.array.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/pointer.array.png -------------------------------------------------------------------------------- /images/pointer.linkedlist.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/pointer.linkedlist.png -------------------------------------------------------------------------------- /images/pointer.pointer0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/pointer.pointer0.png -------------------------------------------------------------------------------- /images/pointer.pointer1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/pointer.pointer1.png -------------------------------------------------------------------------------- /images/pointer.type.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/pointer.type.gif -------------------------------------------------------------------------------- /images/process.addressspace.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/process.addressspace.png -------------------------------------------------------------------------------- /images/process.environ.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/process.environ.png -------------------------------------------------------------------------------- /images/process.exec.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/process.exec.png -------------------------------------------------------------------------------- /images/process.fork.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/process.fork.png -------------------------------------------------------------------------------- /images/process.forkexec.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/process.forkexec.png -------------------------------------------------------------------------------- /images/process.ipc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/process.ipc.png -------------------------------------------------------------------------------- /images/process.pipe.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/process.pipe.png -------------------------------------------------------------------------------- /images/road-works-sign.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/road-works-sign.gif -------------------------------------------------------------------------------- /images/shellscript.shellexec.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/shellscript.shellexec.png -------------------------------------------------------------------------------- /images/signal.catch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/signal.catch.png -------------------------------------------------------------------------------- /images/signal.internal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/signal.internal.png -------------------------------------------------------------------------------- /images/signal.reentrancy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/signal.reentrancy.png -------------------------------------------------------------------------------- /images/socket.sockaddr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/socket.sockaddr.png -------------------------------------------------------------------------------- /images/socket.tcpflowchart.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/socket.tcpflowchart.png -------------------------------------------------------------------------------- /images/socket.udpflowchart.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/socket.udpflowchart.png -------------------------------------------------------------------------------- /images/sortsearch.calcrecurrence.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/sortsearch.calcrecurrence.png -------------------------------------------------------------------------------- /images/sortsearch.fn0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/sortsearch.fn0.png -------------------------------------------------------------------------------- /images/sortsearch.mergesort.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/sortsearch.mergesort.png -------------------------------------------------------------------------------- /images/sortsearch.recurrence1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/sortsearch.recurrence1.png -------------------------------------------------------------------------------- /images/sortsearch.recurrence2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/sortsearch.recurrence2.png -------------------------------------------------------------------------------- /images/sortsearch.sortcards.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/sortsearch.sortcards.png -------------------------------------------------------------------------------- /images/sortsearch.theta.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/sortsearch.theta.png -------------------------------------------------------------------------------- /images/stackqueue.bfs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/stackqueue.bfs.png -------------------------------------------------------------------------------- /images/stackqueue.bfsqueue.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/stackqueue.bfsqueue.png -------------------------------------------------------------------------------- /images/stackqueue.circular.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/stackqueue.circular.png -------------------------------------------------------------------------------- /images/stackqueue.dfs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/stackqueue.dfs.png -------------------------------------------------------------------------------- /images/stackqueue.stack.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/stackqueue.stack.png -------------------------------------------------------------------------------- /images/stdlib.buffer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/stdlib.buffer.png -------------------------------------------------------------------------------- /images/stdlib.fgets.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/stdlib.fgets.png -------------------------------------------------------------------------------- /images/struct.abstraction.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/struct.abstraction.png -------------------------------------------------------------------------------- /images/struct.complex.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/struct.complex.png -------------------------------------------------------------------------------- /images/struct.parameter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/struct.parameter.png -------------------------------------------------------------------------------- /images/tcpip.addressclass.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/tcpip.addressclass.png -------------------------------------------------------------------------------- /images/tcpip.arpformat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/tcpip.arpformat.png -------------------------------------------------------------------------------- /images/tcpip.datagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/tcpip.datagram.png -------------------------------------------------------------------------------- /images/tcpip.ethernetformat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/tcpip.ethernetformat.png -------------------------------------------------------------------------------- /images/tcpip.ipformat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/tcpip.ipformat.png -------------------------------------------------------------------------------- /images/tcpip.loopback.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/tcpip.loopback.png -------------------------------------------------------------------------------- /images/tcpip.multiplex.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/tcpip.multiplex.png -------------------------------------------------------------------------------- /images/tcpip.slidingwindow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/tcpip.slidingwindow.png -------------------------------------------------------------------------------- /images/tcpip.stack.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/tcpip.stack.png -------------------------------------------------------------------------------- /images/tcpip.tcpconnection.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/tcpip.tcpconnection.png -------------------------------------------------------------------------------- /images/tcpip.tcpformat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/tcpip.tcpformat.png -------------------------------------------------------------------------------- /images/tcpip.transferlan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/tcpip.transferlan.png -------------------------------------------------------------------------------- /images/tcpip.transferovernet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/tcpip.transferovernet.png -------------------------------------------------------------------------------- /images/tcpip.udpformat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/tcpip.udpformat.png -------------------------------------------------------------------------------- /images/thread.corrupt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/thread.corrupt.png -------------------------------------------------------------------------------- /images/thread.philosopher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/images/thread.philosopher.png -------------------------------------------------------------------------------- /index.rst: -------------------------------------------------------------------------------- 1 | Learning Linux C/C++ Programming from Scratch 2 | ################################################ 3 | 4 | .. toctree:: 5 | :maxdepth: 1 6 | 7 | zh/index.rst 8 | en/index.rst 9 | -------------------------------------------------------------------------------- /mytheme/layout.html: -------------------------------------------------------------------------------- 1 | {# 2 | default/layout.html 3 | ~~~~~~~~~~~~~~~~~~~ 4 | 5 | Sphinx layout template for the default theme. 6 | 7 | :copyright: Copyright 2007-2011 by the Sphinx team, see AUTHORS. 8 | :license: BSD, see LICENSE for details. 9 | #} 10 | {% extends "basic/layout.html" %} 11 | 12 | {% if theme_collapsiblesidebar|tobool %} 13 | {% set script_files = script_files + ['_static/sidebar.js'] %} 14 | {% endif %} 15 | -------------------------------------------------------------------------------- /mytheme/sidebarextra.html: -------------------------------------------------------------------------------- 1 | 2 | 15 | 20 | 21 | 22 | 23 | 24 | 25 | 30 |
16 | 请输入您的email订阅本书的邮件列表
17 | (Please enter your email to subscribe to this book's mailing list)
18 |
19 |

关注我的微博

Checkout the source from github: https://github.com/learning-linux-c-cpp/akabook
26 |
感谢您的慷慨捐赠!
27 | Thanks for your generous donation!
28 | 捐赠 29 |
31 | -------------------------------------------------------------------------------- /mytheme/static/default.css_t: -------------------------------------------------------------------------------- 1 | /* 2 | * default.css_t 3 | * ~~~~~~~~~~~~~ 4 | * 5 | * Sphinx stylesheet -- default theme. 6 | * 7 | * :copyright: Copyright 2007-2011 by the Sphinx team, see AUTHORS. 8 | * :license: BSD, see LICENSE for details. 9 | * 10 | */ 11 | 12 | @import url("basic.css"); 13 | 14 | /* -- page layout ----------------------------------------------------------- */ 15 | 16 | body { 17 | font-family: {{ theme_bodyfont }}; 18 | font-size: 100%; 19 | background-color: {{ theme_footerbgcolor }}; 20 | color: #000; 21 | margin: 0; 22 | padding: 0; 23 | } 24 | 25 | div.document { 26 | background-color: {{ theme_sidebarbgcolor }}; 27 | } 28 | 29 | div.documentwrapper { 30 | float: left; 31 | width: 100%; 32 | } 33 | 34 | div.bodywrapper { 35 | margin: 0 0 0 {{ theme_sidebarwidth|toint }}px; 36 | } 37 | 38 | div.body { 39 | background-color: {{ theme_bgcolor }}; 40 | color: {{ theme_textcolor }}; 41 | padding: 0 20px 30px 20px; 42 | } 43 | 44 | {%- if theme_rightsidebar|tobool %} 45 | div.bodywrapper { 46 | margin: 0 {{ theme_sidebarwidth|toint }}px 0 0; 47 | } 48 | {%- endif %} 49 | 50 | div.footer { 51 | color: {{ theme_footertextcolor }}; 52 | width: 100%; 53 | padding: 9px 0 9px 0; 54 | text-align: center; 55 | font-size: 75%; 56 | } 57 | 58 | div.footer a { 59 | color: {{ theme_footertextcolor }}; 60 | text-decoration: underline; 61 | } 62 | 63 | div.related { 64 | background-color: {{ theme_relbarbgcolor }}; 65 | line-height: 30px; 66 | color: {{ theme_relbartextcolor }}; 67 | } 68 | 69 | div.related a { 70 | color: {{ theme_relbarlinkcolor }}; 71 | } 72 | 73 | div.sphinxsidebar { 74 | {%- if theme_stickysidebar|tobool %} 75 | top: 30px; 76 | bottom: 0; 77 | margin: 0; 78 | position: fixed; 79 | overflow: auto; 80 | height: auto; 81 | {%- endif %} 82 | {%- if theme_rightsidebar|tobool %} 83 | float: right; 84 | {%- if theme_stickysidebar|tobool %} 85 | right: 0; 86 | {%- endif %} 87 | {%- endif %} 88 | } 89 | 90 | {%- if theme_stickysidebar|tobool %} 91 | /* this is nice, but it it leads to hidden headings when jumping 92 | to an anchor */ 93 | /* 94 | div.related { 95 | position: fixed; 96 | } 97 | 98 | div.documentwrapper { 99 | margin-top: 30px; 100 | } 101 | */ 102 | {%- endif %} 103 | 104 | div.sphinxsidebar h3 { 105 | font-family: {{ theme_headfont }}; 106 | color: {{ theme_sidebartextcolor }}; 107 | font-size: 1.4em; 108 | font-weight: normal; 109 | margin: 0; 110 | padding: 0; 111 | } 112 | 113 | div.sphinxsidebar h3 a { 114 | color: {{ theme_sidebartextcolor }}; 115 | } 116 | 117 | div.sphinxsidebar h4 { 118 | font-family: {{ theme_headfont }}; 119 | color: {{ theme_sidebartextcolor }}; 120 | font-size: 1.3em; 121 | font-weight: normal; 122 | margin: 5px 0 0 0; 123 | padding: 0; 124 | } 125 | 126 | div.sphinxsidebar p { 127 | color: {{ theme_sidebartextcolor }}; 128 | } 129 | 130 | div.sphinxsidebar p.topless { 131 | margin: 5px 10px 10px 10px; 132 | } 133 | 134 | div.sphinxsidebar ul { 135 | margin: 10px; 136 | padding: 0; 137 | color: {{ theme_sidebartextcolor }}; 138 | } 139 | 140 | div.sphinxsidebar a { 141 | color: {{ theme_sidebarlinkcolor }}; 142 | } 143 | 144 | div.sphinxsidebar input { 145 | border: 1px solid {{ theme_sidebarlinkcolor }}; 146 | font-family: sans-serif; 147 | font-size: 1em; 148 | } 149 | 150 | {% if theme_collapsiblesidebar|tobool %} 151 | /* for collapsible sidebar */ 152 | div#sidebarbutton { 153 | background-color: {{ theme_sidebarbtncolor }}; 154 | } 155 | {% endif %} 156 | 157 | /* -- hyperlink styles ------------------------------------------------------ */ 158 | 159 | a { 160 | color: {{ theme_linkcolor }}; 161 | text-decoration: none; 162 | } 163 | 164 | a:visited { 165 | color: {{ theme_visitedlinkcolor }}; 166 | text-decoration: none; 167 | } 168 | 169 | a:hover { 170 | text-decoration: underline; 171 | } 172 | 173 | {% if theme_externalrefs|tobool %} 174 | a.external { 175 | text-decoration: none; 176 | border-bottom: 1px dashed {{ theme_linkcolor }}; 177 | } 178 | 179 | a.external:hover { 180 | text-decoration: none; 181 | border-bottom: none; 182 | } 183 | 184 | a.external:visited { 185 | text-decoration: none; 186 | border-bottom: 1px dashed {{ theme_visitedlinkcolor }}; 187 | } 188 | {% endif %} 189 | 190 | /* -- body styles ----------------------------------------------------------- */ 191 | 192 | div.body h1, 193 | div.body h2, 194 | div.body h3, 195 | div.body h4, 196 | div.body h5, 197 | div.body h6 { 198 | font-family: {{ theme_headfont }}; 199 | background-color: {{ theme_headbgcolor }}; 200 | font-weight: normal; 201 | color: {{ theme_headtextcolor }}; 202 | border-bottom: 1px solid #ccc; 203 | margin: 20px -20px 10px -20px; 204 | padding: 3px 0 3px 10px; 205 | } 206 | 207 | div.body h1 { margin-top: 0; font-size: 200%; } 208 | div.body h2 { font-size: 160%; } 209 | div.body h3 { font-size: 140%; } 210 | div.body h4 { font-size: 120%; } 211 | div.body h5 { font-size: 110%; } 212 | div.body h6 { font-size: 100%; } 213 | 214 | a.headerlink { 215 | color: {{ theme_headlinkcolor }}; 216 | font-size: 0.8em; 217 | padding: 0 4px 0 4px; 218 | text-decoration: none; 219 | } 220 | 221 | a.headerlink:hover { 222 | background-color: {{ theme_headlinkcolor }}; 223 | color: white; 224 | } 225 | 226 | div.body p, div.body dd, div.body li { 227 | text-align: justify; 228 | line-height: 130%; 229 | } 230 | 231 | div.admonition p.admonition-title + p { 232 | display: inline; 233 | } 234 | 235 | div.admonition p { 236 | margin-bottom: 5px; 237 | } 238 | 239 | div.admonition pre { 240 | margin-bottom: 5px; 241 | } 242 | 243 | div.admonition ul, div.admonition ol { 244 | margin-bottom: 5px; 245 | } 246 | 247 | div.note { 248 | background-color: #eee; 249 | border: 1px solid #ccc; 250 | } 251 | 252 | div.seealso { 253 | background-color: #ffc; 254 | border: 1px solid #ff6; 255 | } 256 | 257 | div.topic { 258 | background-color: #eee; 259 | } 260 | 261 | div.warning { 262 | background-color: #ffe4e4; 263 | border: 1px solid #f66; 264 | } 265 | 266 | p.admonition-title { 267 | display: inline; 268 | } 269 | 270 | p.admonition-title:after { 271 | content: ":"; 272 | } 273 | 274 | pre { 275 | padding: 5px; 276 | background-color: {{ theme_codebgcolor }}; 277 | color: {{ theme_codetextcolor }}; 278 | line-height: 120%; 279 | border: 1px solid #ac9; 280 | border-left: none; 281 | border-right: none; 282 | } 283 | 284 | tt { 285 | background-color: #ecf0f3; 286 | padding: 0 1px 0 1px; 287 | font-size: 0.95em; 288 | } 289 | 290 | th { 291 | background-color: #ede; 292 | } 293 | 294 | .warning tt { 295 | background: #efc2c2; 296 | } 297 | 298 | .note tt { 299 | background: #d6d6d6; 300 | } 301 | 302 | .viewcode-back { 303 | font-family: {{ theme_bodyfont }}; 304 | } 305 | 306 | div.viewcode-block:target { 307 | background-color: #f4debf; 308 | border-top: 1px solid #ac9; 309 | border-bottom: 1px solid #ac9; 310 | } 311 | -------------------------------------------------------------------------------- /mytheme/static/me.alipay.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-linux-c-cpp/akabook/f9a062b67289eb28cb333b9574dc3abac0824f58/mytheme/static/me.alipay.png -------------------------------------------------------------------------------- /mytheme/static/sidebar.js: -------------------------------------------------------------------------------- 1 | /* 2 | * sidebar.js 3 | * ~~~~~~~~~~ 4 | * 5 | * This script makes the Sphinx sidebar collapsible. 6 | * 7 | * .sphinxsidebar contains .sphinxsidebarwrapper. This script adds 8 | * in .sphixsidebar, after .sphinxsidebarwrapper, the #sidebarbutton 9 | * used to collapse and expand the sidebar. 10 | * 11 | * When the sidebar is collapsed the .sphinxsidebarwrapper is hidden 12 | * and the width of the sidebar and the margin-left of the document 13 | * are decreased. When the sidebar is expanded the opposite happens. 14 | * This script saves a per-browser/per-session cookie used to 15 | * remember the position of the sidebar among the pages. 16 | * Once the browser is closed the cookie is deleted and the position 17 | * reset to the default (expanded). 18 | * 19 | * :copyright: Copyright 2007-2011 by the Sphinx team, see AUTHORS. 20 | * :license: BSD, see LICENSE for details. 21 | * 22 | */ 23 | 24 | $(function() { 25 | // global elements used by the functions. 26 | // the 'sidebarbutton' element is defined as global after its 27 | // creation, in the add_sidebar_button function 28 | var bodywrapper = $('.bodywrapper'); 29 | var sidebar = $('.sphinxsidebar'); 30 | var sidebarwrapper = $('.sphinxsidebarwrapper'); 31 | 32 | // for some reason, the document has no sidebar; do not run into errors 33 | if (!sidebar.length) return; 34 | 35 | // original margin-left of the bodywrapper and width of the sidebar 36 | // with the sidebar expanded 37 | var bw_margin_expanded = bodywrapper.css('margin-left'); 38 | var ssb_width_expanded = sidebar.width(); 39 | 40 | // margin-left of the bodywrapper and width of the sidebar 41 | // with the sidebar collapsed 42 | var bw_margin_collapsed = '.8em'; 43 | var ssb_width_collapsed = '.8em'; 44 | 45 | // colors used by the current theme 46 | var dark_color = $('.related').css('background-color'); 47 | var light_color = $('.document').css('background-color'); 48 | 49 | function sidebar_is_collapsed() { 50 | return sidebarwrapper.is(':not(:visible)'); 51 | } 52 | 53 | function toggle_sidebar() { 54 | if (sidebar_is_collapsed()) 55 | expand_sidebar(); 56 | else 57 | collapse_sidebar(); 58 | } 59 | 60 | function collapse_sidebar() { 61 | sidebarwrapper.hide(); 62 | sidebar.css('width', ssb_width_collapsed); 63 | bodywrapper.css('margin-left', bw_margin_collapsed); 64 | sidebarbutton.css({ 65 | 'margin-left': '0', 66 | 'height': bodywrapper.height() 67 | }); 68 | sidebarbutton.find('span').text('»'); 69 | sidebarbutton.attr('title', _('Expand sidebar')); 70 | document.cookie = 'sidebar=collapsed'; 71 | } 72 | 73 | function expand_sidebar() { 74 | bodywrapper.css('margin-left', bw_margin_expanded); 75 | sidebar.css('width', ssb_width_expanded); 76 | sidebarwrapper.show(); 77 | sidebarbutton.css({ 78 | 'margin-left': ssb_width_expanded-12, 79 | 'height': bodywrapper.height() 80 | }); 81 | sidebarbutton.find('span').text('«'); 82 | sidebarbutton.attr('title', _('Collapse sidebar')); 83 | document.cookie = 'sidebar=expanded'; 84 | } 85 | 86 | function add_sidebar_button() { 87 | sidebarwrapper.css({ 88 | 'float': 'left', 89 | 'margin-right': '0', 90 | 'width': ssb_width_expanded - 28 91 | }); 92 | // create the button 93 | sidebar.append( 94 | '
«
' 95 | ); 96 | var sidebarbutton = $('#sidebarbutton'); 97 | light_color = sidebarbutton.css('background-color'); 98 | // find the height of the viewport to center the '<<' in the page 99 | var viewport_height; 100 | if (window.innerHeight) 101 | viewport_height = window.innerHeight; 102 | else 103 | viewport_height = $(window).height(); 104 | sidebarbutton.find('span').css({ 105 | 'display': 'block', 106 | 'margin-top': (viewport_height - sidebar.position().top - 20) / 2 107 | }); 108 | 109 | sidebarbutton.click(toggle_sidebar); 110 | sidebarbutton.attr('title', _('Collapse sidebar')); 111 | sidebarbutton.css({ 112 | 'color': '#FFFFFF', 113 | 'border-left': '1px solid ' + dark_color, 114 | 'font-size': '1.2em', 115 | 'cursor': 'pointer', 116 | 'height': bodywrapper.height(), 117 | 'padding-top': '1px', 118 | 'margin-left': ssb_width_expanded - 12 119 | }); 120 | 121 | sidebarbutton.hover( 122 | function () { 123 | $(this).css('background-color', dark_color); 124 | }, 125 | function () { 126 | $(this).css('background-color', light_color); 127 | } 128 | ); 129 | } 130 | 131 | function set_position_from_cookie() { 132 | if (!document.cookie) 133 | return; 134 | var items = document.cookie.split(';'); 135 | for(var k=0; k 94 5e 136 ^ 126 7e 176 ~ 47 | 31 1f 37 US (unit separator) 63 3f 77 ? 95 5f 137 _ 127 7f 177 DEL 48 | === == === ============================ === == === ======= === == === ==== === == === ==== 49 | 50 | .. index:: k扩展ASCII码, Extended ASCII 51 | 52 | 绝大多数计算机的一个字节是8位,取值范围是0~255,而ASCII码并没有规定编号为128~255的字符,为了能用一个字节表示更多的字符,各厂商制定了很多种ASCII码的扩展规范。注意,虽然通常把这些规范称为扩展ASCII码(Extended ASCII),但其实它们并不属于ASCII码标准。例如下面这种扩展ASCII码由IBM制定,在字符终端下被广泛采用,其中包含了很多表格边线字符用来画界面。 53 | 54 | .. figure:: ../images/app-encoding.extascii.png 55 | 56 | IBM的扩展ASCII码表 57 | 58 | .. index:: ISO-8859-1, Latin-1 59 | 60 | 在图形界面下最广泛使用的扩展ASCII码是ISO-8859-1,也称为Latin-1,其中包含欧洲各国语言中最常用的非英文字母,但毕竟只扩展了128个字符,一些不常用的字母就没有包含进来。如下表所示。 61 | 62 | .. table:: ISO-8859-1 63 | 64 | === = === = === = === = === = === = 65 | 160 176 ° 192 À 208 Ð 224 à 240 ð 66 | 161 ¡ 177 ± 193 Á 209 Ñ 225 á 241 ñ 67 | 162 ¢ 178 ² 194  210 Ò 226 â 242 ò 68 | 163 £ 179 ³ 195 à 211 Ó 227 ã 243 ó 69 | 164 ¤ 180 ´ 196 Ä 212 Ô 228 ä 244 ô 70 | 165 ¥ 181 µ 197 Å 213 Õ 229 å 245 õ 71 | 166 ¦ 182 ¶ 198 Æ 214 Ö 230 æ 246 ö 72 | 167 § 183 · 199 Ç 215 × 231 ç 247 ÷ 73 | 168 ¨ 184 ¸ 200 È 216 Ø 232 è 248 ø 74 | 169 © 185 ¹ 201 É 217 Ù 233 é 249 ù 75 | 170 ª 186 º 202 Ê 218 Ú 234 ê 250 ú 76 | 171 « 187 » 203 Ë 219 Û 235 ë 251 û 77 | 172 ¬ 188 ¼ 204 Ì 220 Ü 236 ì 252 ü 78 | 173 ­ 189 ½ 205 Í 221 Ý 237 í 253 ý 79 | 174 ® 190 ¾ 206 Î 222 Þ 238 î 254 þ 80 | 175 ¯ 191 ¿ 207 Ï 223 ß 239 ï 255 ÿ 81 | === = === = === = === = === = === = 82 | 83 | 编号128~159的是一些控制字符,在上表中没有列出。 84 | 85 | Unicode和UTF-8 86 | ------------------------ 87 | 88 | .. index:: ISO 10646, UCS, Universal Character Set, p平面, Plane, BMP, Basic Multilingual Plane, Plane 0, Unicode 89 | 90 | 为了在一套编码中涵盖全世界各国语言文字和专业领域符号(例如数学符号、乐谱符号),ISO制定了ISO 10646标准,也称为UCS(Universal Character Set)。UCS编码的长度是31位,可以表示2\ :sup:`31` 个字符。如果两个字符编码的高位相同,只有低16位不同,则它们属于同一个平面(Plane),所以一个平面由2\ :sup:`16` 个字符组成。目前绝大多数常用字符都位于第一个平面(编码范围是0x0000~0xFFFF),称为BMP(Basic Multilingual Plane)或Plane 0,为了向后兼容,其中编号为0~256的字符和ASCII码以及Latin-1相同。UCS编码通常用U-xxxxxxxx这种形式表示,而BMP的编码通常用U+xxxx这种形式表示,其中x是十六进制数字。在ISO制定UCS的同时,另一个厂商联合组织也在着手制定这样的编码,称为Unicode,后来两家联手制定统一的编码,但各自发布各自的标准文档,所以UCS编码和Unicode码是相同的。 91 | 92 | .. index:: UCS-4, UTF-32, UTF, Unicode Transformation Format, UCS-2, UTF-16, Surrogate Pair 93 | 94 | 有了字符编码,另一个问题就是这样的编码在计算机中怎么表示。现在已经不可能用一个字节表示一个字符了,最直接的想法就是用四个字节表示一个字符,这种表示方法称为UCS-4或UTF-32,UTF是Unicode Transformation Format的缩写。这样表示显然比较浪费存储空间,如果表示BMP字符,4个字节中的两个高位字节都是0,如果表示ASCII或Latin-1字符,4个字节中的3个高位字节都是0,而我们常用的绝大多数字符都在BMP、ASCII或Latin-1字符集中。另一种比较节省存储空间的办法是用两个字节表示一个字符,称为UCS-2或UTF-16,这样只能表示BMP中的字符,但BMP中有一些控制字符用于扩展,可以用两个这样的控制字符表示其他平面的字符,称为Surrogate Pair。 95 | 96 | 无论是UTF-32还是UTF-16都有一个更严重的问题就是和C语言不兼容,在C语言中字节0表示字符串结尾,库函数 ``strlen`` 、 ``strcpy`` 等等都依赖于这一点,如果字符串用UTF-32或UTF-16存储,其中有很多字节0并不表示字符串结尾,那就乱套了。 97 | 98 | .. index:: UTF-8 99 | 100 | UNIX之父Ken Thompson提出的UTF-8编码很好地解决了这个问题,因此得到广泛应用。和UTF-16、UTF-32不同的是,UTF-8编码的长度不固定,每个字符用1~6个字节表示。UTF-8编码具有以下性质: 101 | 102 | * 编码为U+0000~U+007F的字符只占一个字节,就是0x00~0x7F,和ASCII码兼容。 103 | * 编码大于U+007F的字符用2~6个字节表示,每个字节的最高位都是1,而所有ASCII码的最高位都是0,因此非ASCII码字符的UTF-8编码中不会出现ASCII码的字节(也不会出现字节0)。 104 | * 在非ASCII码字符的多字节编码中,第一个字节的取值范围是0xC0~0xFD,根据第一个字节可以判断后面还有几个字节也属于当前字符的编码,后面每个字节的取值范围都是0x80~0xBF,详见下面的编码格式。 105 | * 所有Unicode字符(共231个)都可以用UTF-8编码表示出来。 106 | * UTF-8编码最长6个字节,BMP字符的UTF-8编码最长三个字节。 107 | * 0xFE和0xFF这两个字节在UTF-8编码中不会出现。 108 | 109 | 具体来说,UTF-8编码有以下几种格式:: 110 | 111 | U-00000000 – U-0000007F: 0xxxxxxx 112 | U-00000080 – U-000007FF: 110xxxxx 10xxxxxx 113 | U-00000800 – U-0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx 114 | U-00010000 – U-001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 115 | U-00200000 – U-03FFFFFF: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 116 | U-04000000 – U-7FFFFFFF: 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 117 | 118 | * ASCII码字符的UTF-8编码只有一个字节(就是ASCII码本身),最高位是0。 119 | * 非ASCII码字符的第一个字节最高位是1,并且后面至少还要跟一个1,最高位后面跟几个1就表示后面还有几个字节也属于当前字符的编码,例如111110xx,最高位后面跟4个1,表示后面还有4个字节也属于当前字符的编码。 120 | * 后面每个字节的最高两位都是10,而第一个字节的最高两位要么是0x,要么是11,因此可以和后面的字节区分开。这样的设计有利于误码同步,例如在网络传输过程中丢失了几个字节,很容易判断当前字符是不完整的,也很容易找到下一个字符应该从哪开始,顶多丢掉一两个字符就可以同步了,而不会导致后面的解码过程全部错乱。 121 | * 上面的格式中标为x的位就是字符的Unicode码,最后一种6字节的格式中x位有31个,可以表示31位的Unicode码。UTF-8编码就像一列火车,第一个字节是车头,后面每个字节是车厢,其中承载的货物是Unicode码。UTF-8规定承载的Unicode码以大端表示,就是说第一个字节中的x位是Unicode码的高位,后面字节中的x位是Unicode码的低位。 122 | * UTF-8规定每个字符必须用尽可能少的字节来编码,换句话说,在符合编码格式的前提下,Unicode码从最高位开始的0位要尽可能少。 123 | 124 | 举例来说,U+00A9(©字符)的二进制是10101001,编码成UTF-8是11000010 10101001(0xC2 0xA9),但不能编码成11100000 10000010 10101001。 125 | 126 | 在Linux C编程中使用Unicode和UTF-8 127 | -------------------------------------- 128 | 129 | 目前各种Linux发行版都支持UTF-8编码,在磁盘上保存一个含有非ASCII字符的文本文件,默认是以UTF-8编码的。当前系统的字符编码设置可以用 :command:`locale` 命令查看:: 130 | 131 | $ locale 132 | LANG=en_US.UTF-8 133 | LANGUAGE= 134 | LC_CTYPE="en_US.UTF-8" 135 | LC_NUMERIC="en_US.UTF-8" 136 | LC_TIME="en_US.UTF-8" 137 | LC_COLLATE="en_US.UTF-8" 138 | LC_MONETARY="en_US.UTF-8" 139 | LC_MESSAGES="en_US.UTF-8" 140 | LC_PAPER="en_US.UTF-8" 141 | LC_NAME="en_US.UTF-8" 142 | LC_ADDRESS="en_US.UTF-8" 143 | LC_TELEPHONE="en_US.UTF-8" 144 | LC_MEASUREMENT="en_US.UTF-8" 145 | LC_IDENTIFICATION="en_US.UTF-8" 146 | LC_ALL= 147 | 148 | .. index:: Locale 149 | 150 | Locale定义了语言、字符编码、日期时间格式、数字格式、货币格式等参数,详见 :manpage:`locale(1)` 、 :manpage:`locale(5)` 、 :manpage:`locale(7)` 。 :command:`locale` 命令列出的这些参数每一个都可以用环境变量单独设置,但通常这些参数的取值是一致的。在我的系统中只设置了环境变量 ``LANG`` ,其他参数没有设置,用 :command:`locale` 命令可以看到其他Locale参数也继承了环境变量 ``LANG`` 的值。 151 | 152 | 常用汉字都位于BMP中,所以一个汉字的UTF-8编码通常是3个字节。例如编辑一个C程序: 153 | 154 | .. code-block:: c 155 | :linenos: 156 | 157 | #include 158 | 159 | int main(void) 160 | { 161 | printf("你好\n"); 162 | return 0; 163 | } 164 | 165 | 源文件是以UTF-8编码存储的:: 166 | 167 | $ hexdump -C nihao.c 168 | 00000000 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e |#include ..int main(voi| 170 | 00000020 64 29 0a 7b 0a 09 70 72 69 6e 74 66 28 22 e4 bd |d).{..printf("..| 171 | 00000030 a0 e5 a5 bd 5c 6e 22 29 3b 0a 09 72 65 74 75 72 |....\n");..retur| 172 | 00000040 6e 20 30 3b 0a 7d 0a |n 0;.}.| 173 | 00000047 174 | 175 | .. index:: Multibyte Character 176 | 177 | 其中 ``e4 bd a0`` 这三个字节就是“你”的UTF-8编码, ``e5 a5 bd`` 这三个字节就是“好”的UTF-8编码。把它编译成目标文件,"你好\n"这个字符串就成了这样一串字节: ``e4 bd a0 e5 a5 bd 0a 00`` ,转义序列由两个字节变成一个字节,字符串末尾添了一个字节0,而汉字仍然占3个字节,在C标准中多字节编码的字符称为Multibyte Character。运行这个程序会把这一串字节输出到当前终端设备,如果当前终端能够识别UTF-8编码(比如图形界面的终端窗口)就能打印出汉字,如果不能识别UTF-8编码(比如一般的字符终端)就打印不出汉字。也就是说,在这个程序中识别汉字的工作既不是由C编译器做的也不是由 ``printf`` 函数做的,C编译器原封不动地把源文件中的UTF-8编码复制到目标文件中, ``printf`` 函数再把这一串字节当作以Null结尾的字符串原封不动地输出到终端设备,识别汉字的工作是由终端设备做的。 178 | 179 | .. index:: Wide Character 180 | 181 | 仅有这种程度的汉字支持是不够的,有时候我们需要在C程序中操作字符串里的字符,比如求字符串"你好\n"中有几个汉字或字符,用 ``strlen`` 就不灵了,因为 ``strlen`` 求的是字节数而不是字符数。为了在程序中操作Unicode字符,C标准定义了宽字符(Wide Character)类型 ``wchar_t`` (使用这个类型名需包含头文件 :file:`wchar.h` )。在字符常量或字符串字面值前面加一个 ``L`` 就表示宽字符常量或宽字符串,例如定义 ``wchar_t c = L'你';`` ,变量 ``c`` 的值就是汉字“你”的31位Unicode码,而 ``L"你好\n"`` 就相当于数组 ``wchar_t str[] = { L'你', L'好', L'\n', 0 };`` 。C标准还定义了一些操作宽字符串的库函数,例如 ``wcslen`` 函数可以取宽字符串中的字符个数。 182 | 183 | 注意Wide Character和Multibyte Character这两个概念的区别: 184 | 185 | #. C标准没有规定Wide Character和Multibyte Character应该采用什么编码,但目前各种Linux发行版的Wide Character都采用Unicode码,Multibyte Character都采用UTF-8编码。 186 | #. 每个Wide Character有固定的长度,用 ``wchar_t`` 类型来表示。而每个Multibyte Character的长度不固定,没有规定一种类型来表示Multibyte Character。 187 | #. Wide Character中可能包含字节0,所以不能保存在普通的以Null结尾的字符串中,而必须保存在宽字符串中。而Multibyte Character中除了Null字符外不允许出现字节0,因此可以保存在以Null结尾的字符串中。 188 | #. Wide Character适合做字符运算,比如统计字符数,而Multibyte Character适合做存储和传输,存储时比较节省空间,传输时有较好的容错性。 189 | 190 | 看下面的例子: 191 | 192 | .. code-block:: c 193 | :linenos: 194 | 195 | #include 196 | #include 197 | 198 | int main(void) 199 | { 200 | if (!setlocale(LC_CTYPE, "")) { 201 | fprintf(stderr, "Can't set the specified locale! " 202 | "Check LC_ALL, LC_CTYPE, LANG.\n"); 203 | return 1; 204 | } 205 | printf("%ls", L"你好\n"); 206 | return 0; 207 | } 208 | 209 | 宽字符串 ``L"你好\n"`` 在源代码中当然还是UTF-8编码,但编译器会把它转换成4个Unicode码 ``0x00004f60 0x0000597d 0x0000000a 0x00000000`` 保存到目标文件中,按小端存储就是 ``60 4f 00 00 7d 59 00 00 0a 00 00 00 00 00 00 00`` ,用 ``hexdump`` 命令查看目标文件可以找到这些字节。 210 | 211 | ``printf`` 的转换说明 ``%ls`` 表示把后面的参数按宽字符串解释,不是见到字节0就结束,而是见到Null字符的Unicode码(4个字节0)才结束,但输出到终端仍然要以Multibyte Character编码输出,这样终端设备才能识别,所以 ``printf`` 函数先把宽字符串转换成UTF-8编码的Multibyte Character字符串再输出到终端。我们把输出重定向到文件会看得更清楚:: 212 | 213 | $ gcc main.c 214 | $ ./a.out > foo 215 | $ hexdump -C foo 216 | 00000000 e4 bd a0 e5 a5 bd 0a |.......| 217 | 00000007 218 | 219 | 最后解释一下 :manpage:`setlocale(3)` 函数: 220 | 221 | #. 虽然当前系统的Locale设置是 ``"en_US.UTF-8"`` ,但C程序在启动时各种Locale参数都设置成默认值 ``"C"`` ,并不继承当前系统的Locale设置。这样规定是为了代码的可移植性,如果一个C程序不调用 ``setlocale`` 函数,那么它不管在什么系统上运行,其各种Locale参数都是 ``"C"`` ,其中 ``LC_CTYPE`` 参数是 ``"C"`` 表示采用ASCII字符集。 222 | #. 我们调用 ``setlocale`` 传的第一个参数是 ``LC_CTYPE`` ,它在C语言中被定义成一个整数常量,第二个参数是设置给它的值。如果第二个参数是空字符串 ``""`` ,则表示采用当前系统的Locale设置, ``setlocale`` 函数依次查找环境变量 ``LC_ALL`` 、 ``LC_CTYPE`` 和 ``LANG`` ,找到第一个有定义的环境变量就用它的值来设置 ``LC_CTYPE`` 。因此,调用 ``setlocale`` 函数后 ``LC_CTYPE`` 参数的值变成了 ``"en_US.UTF-8"`` 。 223 | #. ``LC_CTYPE`` 参数影响C语言对宽字符和宽字符串的处理,正因为我们通过这个参数设置了UTF-8编码, ``printf`` 函数才会把宽字符串转换成UTF-8编码的Multibyte Character字符串再输出到终端。上面的程序如果去掉 ``setlocale`` 调用, ``LC_CTYPE`` 参数的值默认是 ``"C"`` (采用ASCII字符集),则无法打印出“你好”,因为这两个字符不属于ASCII字符集。 224 | 225 | 关于Unicode和UTF-8本节只介绍了最基本的概念,部分内容出自 [UnicodeFAQ]_ ,读者可进一步参考这篇文章。 226 | -------------------------------------------------------------------------------- /zh/arch.rst: -------------------------------------------------------------------------------- 1 | 计算机体系结构基础 2 | ==================== 3 | 4 | .. _arch.cpu: 5 | 6 | CPU 7 | ----- 8 | 9 | .. _arch.memaddr: 10 | 11 | 内存与地址 12 | --------------- 13 | -------------------------------------------------------------------------------- /zh/asm.rst: -------------------------------------------------------------------------------- 1 | x86汇编程序基础 2 | ================== 3 | -------------------------------------------------------------------------------- /zh/asmc.rst: -------------------------------------------------------------------------------- 1 | 汇编与C之间的关系 2 | ================== 3 | 4 | .. _asmc.funccall: 5 | 6 | 函数调用 7 | ------------- 8 | 9 | .. _asmc.main: 10 | 11 | main函数、启动例程和退出状态 12 | ------------------------------ 13 | 14 | .. _asmc.storage: 15 | 16 | 变量的存储布局 17 | --------------------- 18 | 19 | .. _asmc.structunion: 20 | 21 | 结构体和联合体 22 | --------------------- 23 | 24 | -------------------------------------------------------------------------------- /zh/bibli.rst: -------------------------------------------------------------------------------- 1 | 参考书目 2 | ========== 3 | 4 | .. [ThinkCpp] How To Think Like A Computer Scientist: C++ Version. Allen B. Downey. 5 | 6 | .. [GroundUp] Programming from the Ground Up: An Introduction to Programming using Linux Assembly Language. Jonathan Bartlett. 7 | 8 | .. [K&R] The C Programming Language. 2. Brian W. Kernighan和Dennis M. Ritchie. 9 | 10 | .. [StandardC] Standard C: A Reference. P. J. Plauger和Jim Brodie. 11 | 12 | .. [StandardCLibrary] The Standard C Library. P. J. Plauger. 13 | 14 | .. [C99Rationale] Rationale for International Standard - Programming Languages - C. 5.10. 15 | 16 | .. [UNIX编程艺术] The Art of UNIX Programming. Eric Raymond. 17 | 18 | .. [C99] ISO/IEC 9899: Programming Languages - C. 2. 19 | 20 | .. [数字逻辑基础] Fundamentals of Digital Logic with VHDL Design. 2. Stephen Brown和Zvonko Vranesic. 21 | 22 | .. [IATLC] Introduction to Automata Theory, Languages, and Computation. 2. John E. Hopcroft、Rajeev Motwani和Jeffrey D. Ullman. 23 | 24 | .. [DragonBook] Compilers: Principles, Techniques, & Tools. 2. Alfred V. Aho、Monica S. Lam、Ravi Sethi和Jeffrey D. Ullman. 25 | 26 | .. [SICP] Structure and Interpretation of Computer Programs. 2. Harold Abelson、Gerald Jay Sussman和Julie Sussman. 27 | 28 | .. [人月神话] The Mythical Man-Month: Essays on Software Engineering. Anniversary Edition. Frederick P. Brooks, Jr.. 29 | 30 | .. [CodingStyle] Linux内核源代码目录下的Documentation/CodingStyle文件. 31 | 32 | .. [GDB] Debugging with GDB: The GNU Source-Level Debugger. 9. Richard Stallman、Roland Pesch和Stan Shebs. 33 | 34 | .. [算法导论] Introduction to Algorithms. 2. Thomas H. Cormen、Charles E. Leiserson、Ronald L. Rivest和Clifford Stein. 35 | 36 | .. [TAOCP] The Art of Computer Programming. Donald E. Knuth. 37 | 38 | .. [编程珠玑] Programming Pearls. 2. Jon Bentley. 39 | 40 | .. [OOSC] Object-Oriented Software Construction. Bertrand Meyer. 41 | 42 | .. [ADSP] Algorithms + Data Structures = Programs. Niklaus Wirth. 43 | 44 | .. [ExpertC] Expert C Programming. Peter van der Linden. 45 | 46 | .. [AssemblyHOWTO] Linux Assembly HOWTO(http://tldp.org/HOWTO/Assembly-HOWTO/). Konstantin Boldyshev和Francois-Rene Rideau. 47 | 48 | .. [x86Assembly] Introduction to 80x86 Assembly Language and Computer Architecture. Richard C. Detmer. 49 | 50 | .. [GCCdoc] GCC online documentation(http://gcc.gnu.org/onlinedocs/). 51 | 52 | .. [GCCref] GCC: The Complete Reference. Arthur Griffith. 53 | 54 | .. [Binutils] GNU Binutils Documentation(http://www.gnu.org/software/binutils/). 55 | 56 | .. [GNUmake] GNU Make Manual(http://www.gnu.org/software/make/manual/). 57 | 58 | .. [SmashStack] Smashing The Stack For Fun And Profit,网上到处都可以搜到这篇文章. Aleph One. 59 | 60 | .. [BeganFORTRAN] The New C: It All Began with FORTRAN(http://www.ddj.com/cpp/184401313). Randy Meyers. 61 | 62 | .. [具体数学] Concrete Mathematics. 2. Ronald L. Graham、Donald E. Knuth和Oren Patashnik. 63 | 64 | .. [APUE2e] Advanced Programming in the UNIX Environment. 2. W. Richard Stevens和Stephen A. Rago. 65 | 66 | .. [ULK] Understanding the Linux Kernel. 3. Daniel P. Bovet和Marco Cesati. 67 | 68 | .. [TCPIP] TCP/IP Illustrated, Volume 1: The Protocols. W. Richard Stevens. 69 | 70 | .. [UNPv13e] UNIX Network Programming, Volume 1: The Sockets Networking API. 3. W. Richard Stevens、Bill Fenner和Andrew M. Rudoff. 71 | 72 | .. [UnicodeFAQ] UTF-8 and Unicode FAQ, http://www.cl.cam.ac.uk/~mgk25/unicode.html. Markus Kuhn. 73 | -------------------------------------------------------------------------------- /zh/cond.rst: -------------------------------------------------------------------------------- 1 | 分支语句 2 | ================== 3 | 4 | .. _cond.if: 5 | 6 | if语句 7 | ----------- 8 | 9 | 目前我们写的简单函数中可以有多条语句,但这些语句总是从前到后顺序执行的。除了顺序执行之外,有时候我们需要检查一个条件,然后根据检查的结果执行不同的后续代码,在C语言中可以用分支语句实现,比如: 10 | 11 | .. code-block:: c 12 | :linenos: 13 | 14 | if (x != 0) { 15 | printf("x is nonzero.\n"); 16 | } 17 | 18 | .. index:: k控制表达式, Controlling Expressoin, k控制流程, f分支, Branch 19 | 20 | 其中 ``x != 0`` 表示“x不等于0”的条件,这个表达式称为控制表达式(Controlling Expression),如果条件成立,则{}中的语句被执行,否则{}中的语句不执行,直接跳到}后面。 ``if`` 和控制表达式改变了程序的控制流程(Control Flow),不再是从前到后顺序执行,而是根据不同的条件执行不同的语句,这种控制流程称为分支(Branch)。上例中的 ``!=`` 运算符表示“不等于”,像这样的运算符有: 21 | 22 | .. table:: 关系运算符和相等性运算符 23 | 24 | ========= =========== 25 | 运算符 含义 26 | ========= =========== 27 | == 等于 28 | != 不等于 29 | > 大于 30 | < 小于 31 | >= 大于或等于 32 | <= 小于或等于 33 | ========= =========== 34 | 35 | 注意以下几点: 36 | 37 | .. index:: z真, True, j假, False, x相等性运算符, Equality Operator, g关系运算符, Relational Operator 38 | 39 | #. 这里的 ``==`` 表示数学中的相等关系,相当于数学中的=号,初学者常犯的错误是在控制表达式中把 ``==`` 写成 ``=`` ,在C语言中=号是赋值运算符,两者的含义完全不同。 40 | #. 如果表达式所表示的比较关系成立则值为真(True),否则为假(False),在C语言中分别用 ``int`` 型的1和0表示。如果变量 ``x`` 的值是-1,那么 ``x > 0`` 这个表达式的值为0, ``x > -2`` 这个表达式的值为1。 41 | #. 在数学中a 0); 94 | printf("x is positive\n"); 95 | 96 | .. _cond.ifelse: 97 | 98 | if/else语句 99 | ------------------- 100 | 101 | .. index:: z子句, Clause 102 | 103 | ``if`` 语句还可以带一个 ``else`` 子句(Clause),例如: 104 | 105 | .. code-block:: c 106 | :linenos: 107 | 108 | if (x % 2 == 0) 109 | printf("x is even\n"); 110 | else 111 | printf("x is odd\n"); 112 | 113 | .. index:: q取模, Modulo, y余数, Remainder 114 | 115 | 这里的%是取模(Modulo)运算符, ``x%2`` 表示 ``x`` 除以2所得的余数(Remainder),C语言规定%运算符的两个操作数必须是整型的。两个正数相除取余数很好理解,如果操作数中有负数,结果应该是正是负呢?C99规定:如果 ``a`` 和 ``b`` 是整型, ``b`` 不等于0,则表达式 ``(a/b)*b+a%b`` 的值总是等于 ``a`` 。再结合 :ref:`expr.expression` 讲过的整数除法运算要Truncate towards Zero,可以得到一个结论: **%运算符的结果总是与被除数同号** (读者可以自己推导一下)。其他编程语言对取模运算的规定各不相同,也有规定结果和除数同号的,也有不做明确规定的。 116 | 117 | 取模运算在程序非常有用,例如上面的例子判断 ``x`` 的奇偶性,看 ``x`` 除以2的余数是不是0,如果是0则打印 ``x is even`` ,如果不是0则打印 ``x is odd`` 。读者应该能看出 ``else`` 在这里的作用了:假如从上面的例子中去掉 ``else`` ,则不管 ``x`` 是奇是偶, ``printf("x is odd\n");`` 这条语句总是被执行。 118 | 119 | 为了让这个判断和打印操作更有用,可以把它封装成一个函数: 120 | 121 | .. code-block:: c 122 | :linenos: 123 | 124 | void print_even_or_odd(int x) 125 | { 126 | if (x % 2 == 0) 127 | printf("x is even\n"); 128 | else 129 | printf("x is odd\n"); 130 | } 131 | 132 | **把语句封装成函数的基本步骤是:把语句放到函数体中,把变量改成函数的参数。** 这样,以后要检查一个数的奇偶性只需调一下函数即可,不必重复写这个 ``if/else`` 语句了,例如: 133 | 134 | .. code-block:: c 135 | :linenos: 136 | 137 | print_even_or_odd(17); 138 | print_even_or_odd(18); 139 | 140 | ``if/else`` 语句的语法规则如下:: 141 | 142 | 语句 → if (控制表达式) 语句 else 语句 143 | 144 | 右边的“语句”既可以是一条语句,也可以是由{}括起来的语句块。一条 ``if`` 语句中包含一条子语句,一条 ``if/else`` 语句中包含两条子语句,子语句可以是任何语句或语句块,当然也可以是另外一条 ``if`` 或 ``if/else`` 语句。根据组合规则, ``if`` 或 ``if/else`` 可以嵌套使用。例如可以这样: 145 | 146 | .. code-block:: c 147 | :linenos: 148 | 149 | if (x > 0) { 150 | printf("x is positive\n"); 151 | } else { 152 | if (x < 0) 153 | printf("x is negative\n"); 154 | else 155 | printf("x is zero\n"); 156 | } 157 | 158 | 等价于下面这种不用语句块的写法: 159 | 160 | .. code-block:: c 161 | :linenos: 162 | 163 | if (x > 0) 164 | printf("x is positive\n"); 165 | else if (x < 0) 166 | printf("x is negative\n"); 167 | else 168 | printf("x is zero\n"); 169 | 170 | 现在问题来了:如果不用语句块,那么类似 ``if (A) if (B) C; else D;`` 形式的语句该怎么理解呢?可以理解成 171 | 172 | .. code-block:: c 173 | :linenos: 174 | 175 | if (A) 176 | if (B) 177 | C; 178 | else 179 | D; 180 | 181 | 也可以理解成 182 | 183 | .. code-block:: c 184 | :linenos: 185 | 186 | if (A) 187 | if (B) 188 | C; 189 | else 190 | D; 191 | 192 | .. index:: Dangling-else 193 | 194 | 在 :ref:`expr.helloworld` 讲过,C代码的缩进只是为了程序员看起来方便,实际上对编译器不起任何作用,你的代码不管写成上面哪一种缩进格式,在编译器看起来都是一样的。那么编译器到底按哪种方式理解呢?换句话说,编译器认为 ``else`` 到底是和 ``if (A)`` 配对还是和 ``if (B)`` 配对?很多编程语言的语法都有这个问题,称为Dangling-else问题。C语言规定, **else总是和它前面最近的一个if配对** ,因此应该理解成 ``else`` 和 ``if (B)`` 配对,也就是按第二种方式理解。如果你写成上面第一种缩进的格式就很危险了:你看到的是这样,而编译器理解的却是那样。如果你希望编译器按第一种方式理解,应该明确加上{}: 195 | 196 | .. code-block:: c 197 | :linenos: 198 | 199 | if (A) { 200 | if (B) 201 | C; 202 | } else 203 | D; 204 | 205 | .. rubric:: 习题 206 | 207 | #. 写两个表达式,分别取整型变量 ``x`` 的个位和十位。 208 | #. 写一个函数,参数是整型变量 ``x`` ,功能是打印 ``x`` 的个位和十位。 209 | 210 | .. _cond.bool: 211 | 212 | 布尔代数 213 | ------------------ 214 | 215 | 在 :ref:`cond.if` 讲过,在数学上a 1)`` ,如果表达式 ``x > 1`` 的值非零,则 ``!(x > 1)`` 的值为0。逻辑非运算符只有一个操作数,称为单目运算符(Unary Operator),以前讲过的加减乘除、赋值、相等性、关系、逻辑与、逻辑或运算符都有两个操作数,称为双目运算符(Binary Operator)。 277 | 278 | .. index:: b布尔代数, Boolean Algebra 279 | 280 | 关于逻辑运算的数学体系称为布尔代数(Boolean Algebra),以它的创始人布尔命名。在编程语言中表示真和假的数据类型叫做布尔类型,在C语言中通常用 ``int`` 型来表示,非0表示真,0表示假 [#]_ 。布尔逻辑是写程序的基本功之一,程序中的很多错误都可以归因于逻辑错误。以下是一些布尔代数的基本定理,为了简洁易读,真和假用1和0表示,AND用*号表示,OR用+号表示(从真值表可以看出AND和OR运算确实有点像乘法和加法运算),NOT用¬表示,变量x、y、z的值可能是0也可能是1,无论x、y、z怎么取值,以下等式都成立。 281 | 282 | .. [#] C99也定义了专门的布尔类型 ``_Bool`` ,但目前没有被广泛使用。 283 | 284 | :: 285 | 286 | ¬¬x=x 287 | 288 | x*0=0 289 | x+1=1 290 | 291 | x*1=x 292 | x+0=x 293 | 294 | x*x=x 295 | x+x=x 296 | 297 | x*¬x=0 298 | x+¬x=1 299 | 300 | x*y=y*x 301 | x+y=y+x 302 | 303 | x*(y*z)=(x*y)*z 304 | x+(y+z)=(x+y)+z 305 | 306 | x*(y+z)=x*y+x*z 307 | x+y*z=(x+y)*(x+z) 308 | 309 | x+x*y=x 310 | x*(x+y)=x 311 | 312 | x*y+x*¬y=x 313 | (x+y)*(x+¬y)=x 314 | 315 | ¬(x*y)=¬x+¬y 316 | ¬(x+y)=¬x*¬y 317 | 318 | x+¬x*y=x+y 319 | x*(¬x+y)=x*y 320 | 321 | x*y+¬x*z+y*z=x*y+¬x*z 322 | (x+y)*(¬x+z)*(y+z)=(x+y)*(¬x+z) 323 | 324 | 除了第一个公式之外,这些公式都是每两个一组的,每组的两个公式就像对联一样:把其中一个公式中的*换成+、+换成*、0换成1、1换成0,就变成了与它对称的另一个公式。这些定理都可以用真值表来证明,更多细节可参考有关数字逻辑的教材,例如 [数字逻辑基础]_ 。我们将在本节的练习题中强化训练对这些定理的理解。 325 | 326 | 目前为止介绍的这些运算符按优先级从高到低的顺序依次是: 327 | 328 | * ! 329 | * \* / % 330 | * \+ \- 331 | * > < >= <= 332 | * == != 333 | * && 334 | * || 335 | * = 336 | 337 | 写一个表达式往往会用到好几种运算符,如果记不清楚运算符的优先级一定要多套括号。我们将在 :ref:`op.op` 总结C语言所有运算符的优先级和结合性。 338 | 339 | 最后要注意一点,浮点型的精度有限,不适合用 ``==`` 运算符做精确比较。以下代码可以说明问题: 340 | 341 | .. code-block:: c 342 | :linenos: 343 | 344 | double a = 0.3 - 0.2; 345 | double b = 0.2 - 0.1; 346 | if (a == b) 347 | printf("Equal\n"); 348 | else 349 | printf("Unequal\n"); 350 | 351 | 程序的执行结果非常意外,居然是Unequal。虽然理论上 ``a`` 和 ``b`` 的值都应该是0.1, 但浮点型无法精确表示0.1这个值,实际上可能会存在一点偏差,由于 ``a`` 和 ``b`` 的值的计算过程不同,存在的偏差也不一样,最后两者做精确比较的结果是不相等。等学习了 :ref:`number.float` 你就知道为什么浮点型的精度有限了。如果你需要判断两个符点数是否近似相等,可以这样比较: ``if (a > b - 0.001 && a < b + 0.001) ...`` 。 352 | 353 | .. rubric:: 习题 354 | 355 | #. 把代码段 356 | 357 | .. code-block:: c 358 | :linenos: 359 | 360 | if (x > 0 && x < 10); 361 | else 362 | printf("x is out of range.\n"); 363 | 364 | 改写成下面这种形式: 365 | 366 | .. code-block:: c 367 | :linenos: 368 | 369 | if (____ || ____) 370 | printf("x is out of range.\n"); 371 | 372 | ____应该怎么填? 373 | 374 | #. 把代码段: 375 | 376 | .. code-block:: c 377 | :linenos: 378 | 379 | if (x > 0) 380 | printf("Test OK!\n"); 381 | else if (x <= 0 && y > 0) 382 | printf("Test OK!\n"); 383 | else 384 | printf("Test failed!\n"); 385 | 386 | 改写成下面这种形式: 387 | 388 | .. code-block:: c 389 | :linenos: 390 | 391 | if (____ && ____) 392 | printf("Test failed!\n"); 393 | else 394 | printf("Test OK!\n"); 395 | 396 | ____应该怎么填? 397 | 398 | #. 有这样一段代码: 399 | 400 | .. code-block:: c 401 | :linenos: 402 | 403 | if (x > 1 && y != 1) { 404 | ... 405 | } else if (x < 1 && y != 1) { 406 | ... 407 | } else { 408 | ... 409 | } 410 | 411 | 要进入最后一个else,x和y需要满足条件____ || ____。这里应该怎么填? 412 | 413 | #. 以下哪一个 ``if`` 判断条件是多余的可以去掉?这里所谓的“多余”是指:在 ``x`` 和 ``y`` 取值的任何一种可能的情况下,如果本来应该打印 ``Test OK`` ,去掉这个多余条件后仍然打印 ``Test OK`` ,如果本来应该打印 ``Test failed`` ,去掉这个多余条件后仍然打印 ``Test failed`` 。 414 | 415 | .. code-block:: c 416 | :linenos: 417 | 418 | if (x<3 && y>3) 419 | printf("Test OK\n"); 420 | else if (x>=3 && y>=3) 421 | printf("Test OK\n"); 422 | else if (z>3 && x>=3) 423 | printf("Test OK\n"); 424 | else if (z<=3 && y>=3) 425 | printf("Test OK\n"); 426 | else 427 | printf("Test failed\n"); 428 | 429 | #. 以下两段代码是否等价? 430 | 431 | .. code-block:: c 432 | :linenos: 433 | 434 | if (A && B) 435 | statement1; 436 | else 437 | statement2; 438 | 439 | .. code-block:: c 440 | :linenos: 441 | 442 | if (A) { 443 | if (B) 444 | statement1; 445 | } else 446 | statement2; 447 | 448 | .. _cond.switch: 449 | 450 | switch语句 451 | ------------------ 452 | 453 | ``switch`` 语句可以产生具有多个分支的控制流程。它的格式是:: 454 | 455 | switch (控制表达式) { 456 | case 整型常量表达式: 零或多条语句 457 | case 整型常量表达式: 零或多条语句 458 | ... 459 | default: 零或多条语句 460 | } 461 | 462 | 例如以下程序根据传入的参数1~7分别打印Monday~Sunday: 463 | 464 | .. _cond.switch1: 465 | 466 | .. figure:: ../images/cond.switch1.png 467 | 468 | switch语句 469 | 470 | 如果传入的参数是2,则从 ``case 2`` 分支开始执行,先是打印相应的信息,然后遇到 ``break`` 语句,它的作用是跳出整个 ``switch`` 语句块。C语言规定各 ``case`` 分支的常量表达式必须互不相同,如果控制表达式不等于任何一个常量表达式,则从 ``default`` 分支开始执行,通常的习惯是把 ``default`` 分支写在最后。 471 | 472 | 使用 ``switch`` 语句要注意几点: 473 | 474 | .. index:: Fall Through 475 | 476 | #. ``case`` 后面跟的表达式必须是常量表达式,它的值和全局变量的初始值一样必须能在编译时计算出来。 477 | #. 在上一节讲过浮点型不适合做精确比较,所以C语言规定case后面跟的必须是整型常量表达式。 478 | #. 进入 ``case`` 后如果没有遇到 ``break`` 语句就会一直往下执行(这称为Fall Through),后面其他 ``case`` 或 ``default`` 分支的语句也会被执行到,直到遇到 ``break`` ,或者执行到整个 ``switch`` 语句块的末尾。通常每个 ``case`` 后面都要加上 ``break`` 语句,但有时会故意不加 ``break`` ,例如: 479 | 480 | .. _cond.switch2: 481 | 482 | .. figure:: ../images/cond.switch2.png 483 | 484 | 缺break的switch语句 485 | 486 | ``switch`` 语句不是必不可缺的,显然可以用一组 ``if ... else if ... else if ... else ...`` 代替,但还是推荐写程序时多用 ``switch`` :一方面用 ``switch`` 语句会使代码更清晰,另一方面,有时候编译器会对 ``switch`` 语句做整体优化,使它比等价的 ``if/else`` 语句所生成的指令执行效率更高。 487 | -------------------------------------------------------------------------------- /zh/history.rst: -------------------------------------------------------------------------------- 1 | 历史 2 | ====== 3 | 4 | 本书的版权许可证 5 | ------------------ 6 | 7 | 本书以 :doc:`GNU Free Documentation License ` 发布,改编和包含了以下两本书的部分章节。 8 | 9 | How To Think Like A Computer Scientist: C++ Version 10 | 11 | 作者Allen B. Downey。原书由Green Tea Press发行,可以从 http://www.greenteapress.com/ 下载到。 12 | 13 | Programming from the Ground Up: An Introduction to Programming using Linux Assembly Language 14 | 15 | 作者Jonathan Bartlett。原书由Bartlett Publishing发行,可以从 http://savannah.nongnu.org/projects/pgubook/ 下载到。 16 | 17 | 本书的历史 18 | -------------- 19 | 20 | 2009年2月 21 | 本书首次发布在网络上,当时我在亚嵌教育工作,用公司的服务器,发布地址是 http://learn.akae.cn/ 。 22 | 23 | 2009年12月 24 | 在本书的网络版发行大半年之后,电子工业出版社出版了本书,书名是《Linux C编程一站式学习》( http://book.douban.com/subject/4141733/ )。 25 | 26 | 本书的网络版从一开始就声明以 :doc:`GNU Free Documentation License ` 发布,但在国内的出版环境下没人关心这个,出版社有自己的法律条款。显然,违反了 :doc:`GNU Free Documentation License ` 不会有人追究,而违反了出版社的法律条款则是会有人追究的。所以,自从本书与出版社签了协议之后,网络版就停止了更新。最终出版的版本在网络版的基础上做了很多细节上的修订,主要是消除歧义和表达得更顺畅一些,其中大部分修订意见来自网络版的网友留言。 27 | 28 | 尽管如此,我还是向出版社争取到了在网络上发布本书最新修订的权利,但要在本书出版一年之后(也就是2010年12月以后)才可以在网络上更新。 29 | 30 | 2010年12月以后 31 | 我始终挂念着“2010年12月以后允许更新到网络上的部分”,但我已经换了工作,没有权限访问亚嵌教育的服务器akae.cn了,而且忙于新的工作,所以一直没有更新网络版。 32 | 33 | 2011年3月 34 | 本书加入了一些新的内容,由电子工业出版社重新出版,书名是《一站式学习C编程》( http://book.douban.com/subject/6025290/ 对豆瓣上9分+的评分我感到十分欣慰,谢谢各位读者了^^)。新增内容主要有以下几点: 35 | 36 | * 增加了关于Windows下的C编译器的介绍,把书名中的“Linux”字眼去掉了,希望更多的读者能读到本书。 37 | * 明确指出书中大部分例子的运行环境是Ubuntu 10.04 LTS 32位x86版,如果读者也使用同样的运行环境,一些平台相关的现象应该会和书中的描述一致。 38 | * “Makefile基础”一章中“自动处理头文件依赖关系”一节,我结合工作经验给出一个更好的解法,和上一版(《Linux C编程一站式学习》)的该小节完全不同。 39 | 40 | 很遗憾,出版社不同意在网络上更新这些新增内容,即使在出版一年之后也不行。 41 | 42 | 2012年7月 43 | 我辞去了工作准备出国,终于有时间处理“2010年12月以后允许更新到网络上的部分”了。根据之前和出版社的约定,《一站式学习C编程》的新增内容不能更新到网络上,所以我决定放弃这些内容,继续在原来的《Linux C编程一站式学习》的基础上更新。 44 | 45 | 我在自己的服务器 http://songjinshan.com/ 上重新发布本书,最新版本不涉及Windows下的C编译器的内容,将运行环境设定为最新的Ubuntu 12.04 LTS 32位x86版,至于“Makefile基础”一章中“自动处理头文件依赖关系”一节,我给出了完全不同于前两种解法的第三种解法。总之,最新版本不包含《一站式学习C编程》里的任何修订内容和新增内容。此外,最新版本包含英文版,我的英文写作水平一般般,为国外的生活做准备练练笔而已,献丑了。英文版和中文版并非一一对译的,但英文版肯定会覆盖中文版的所有技术点。 46 | -------------------------------------------------------------------------------- /zh/index.rst: -------------------------------------------------------------------------------- 1 | 中文版:Linux C/C++编程一站式学习 2 | ************************************** 3 | 4 | 这个新版本正在建设中,还不完整。在 http://akaedu.github.com/book/ 可以找到完整的旧版本。 5 | 6 | .. toctree:: 7 | :maxdepth: 1 8 | 9 | history 10 | preface 11 | 12 | 第一部分:C语言入门 13 | 14 | .. toctree:: 15 | :maxdepth: 2 16 | :numbered: 17 | 18 | intro 19 | expr 20 | func 21 | cond 22 | func2 23 | iter 24 | struct 25 | array 26 | codingstyle 27 | gdb 28 | sortsearch 29 | stackqueue 30 | summary1 31 | 32 | 第二部分:C语言本质 33 | 34 | .. toctree:: 35 | :maxdepth: 2 36 | :numbered: 37 | 38 | number 39 | type 40 | op 41 | arch 42 | asm 43 | asmc 44 | link 45 | prep 46 | pointer 47 | interface 48 | stdlib 49 | linkedlist 50 | 51 | 第三部分:Linux系统编程 52 | 53 | 第四部分:从C到C++ 54 | 55 | 第五部分:从C/C++到动态语言 56 | 57 | 附录 58 | 59 | .. toctree:: 60 | :maxdepth: 1 61 | 62 | app-encoding 63 | app-gfdl 64 | bibli 65 | 66 | :ref:`genindex` 67 | -------------------------------------------------------------------------------- /zh/interface.rst: -------------------------------------------------------------------------------- 1 | 函数接口 2 | ============ 3 | 4 | .. _interface.va: 5 | 6 | 可变参数 7 | ---------- 8 | -------------------------------------------------------------------------------- /zh/intro.rst: -------------------------------------------------------------------------------- 1 | 程序的基本概念 2 | ==================== 3 | 4 | .. _intro.program: 5 | 6 | 程序和编程语言 7 | -------------------- 8 | 9 | .. index:: c程序, Program 10 | 11 | 程序(Program)告诉计算机应如何完成一个计算任务,这里的计算可以是数学运算,比如解方程,也可以是符号运算,比如查找和替换文档中的某个单词。从根本上说,计算机是由数字电路组成的运算机器,只能对数字做运算,程序之所以能做符号运算,是因为符号在计算机内部也是用数字表示的。此外,程序还可以处理声音和图像,声音和图像在计算机内部必然也是用数字表示的,这些数字经过专门的硬件设备转换成人可以听到、看到的声音和图像。 12 | 13 | 程序由一系列基本操作组成,基本操作有以下几类: 14 | 15 | .. index:: s输入, Input 16 | 17 | 输入(Input) 18 | 从键盘、文件或者其他设备获取数据。 19 | 20 | .. index:: s输出, Output 21 | 22 | 输出(Output) 23 | 把数据显示到屏幕,或者存入一个文件,或者发送到其他设备。 24 | 25 | 基本运算 26 | 最基本的数据访问和数学运算(加减乘除)。 27 | 28 | 测试和分支 29 | 测试某个条件,然后根据不同的测试结果执行不同的后续操作。 30 | 31 | 循环 32 | 重复执行一系列操作。 33 | 34 | 你曾用过的任何一个程序,不管它有多么复杂,都是按这几类基本操作一步一步执行的。程序是那么的复杂,而编写程序可以用的基本操作却只有这么简单的几种,这中间巨大的落差就要由程序员去填补了,所以编写程序理应是一件相当复杂的工作。 **编写程序可以说是这样一个过程:把复杂的任务分解成子任务,把子任务再分解成更简单的任务,层层分解,直到最后简单得可以用以上几种基本操作来完成。** 35 | 36 | .. index:: b编程语言, Programming Language, d低级语言, Low-level Language, g高级语言, High-level Language, j机器语言, Machine Language, h汇编语言, Assembly Language, z指令, Instruction, y语句, Statement 37 | 38 | 编程语言(Programming Language)分为低级语言(Low-level Language)和高级语言(High-level Language)。机器语言(Machine Language)和汇编语言(Assembly Language)属于低级语言,直接用计算机指令(Instruction)编写程序。而C、C++、Java、Python等属于高级语言,用语句(Statement)编写程序,语句是计算机指令的抽象表示。 39 | 40 | 举个例子,同样一个语句用机器语言、汇编语言和C语言分别表示如下: 41 | 42 | .. table:: 一个语句的三种表示(32位x86平台) 43 | 44 | ============= ========================= 45 | 编程语言 表示形式 46 | ============= ========================= 47 | 机器语言 :: 48 | 49 | a1 18 a0 04 08 50 | 83 c0 01 51 | a3 1c a0 04 08 52 | 汇编语言 .. code-block:: gas 53 | 54 | mov 0x804a018,%eax 55 | add $0x1,%eax 56 | mov %eax,0x804a01c 57 | C语言 a = b + 1; 58 | ============= ========================= 59 | 60 | .. index:: z助记符, Mnemonic, h汇编器, Assembler 61 | 62 | 计算机只能对数字做运算,符号、声音、图像在计算机内部都要用数字表示,指令也不例外,上表中的机器语言完全由十六进制数字组成。最早的程序员都是直接用机器语言编程,但是很麻烦,需要查大量的表格来确定每个数字表示什么意思,编写出来的程序很不直观,而且容易出错,于是有了汇编语言,把机器语言中一组一组的数字用助记符(Mnemonic)表示,直接用这些助记符写出汇编程序,然后让汇编器(Assembler)去查表把助记符替换成数字,也就把汇编语言翻译成了机器语言。 63 | 64 | 从上面的例子可以看出,汇编语言和机器语言的指令是一一对应的,汇编语言有三条指令,机器语言也有三条指令,汇编器就是做一个简单的替换工作。例如在第一条指令中,把 ``mov ?,%eax`` 这种格式的指令替换成机器码 ``a1 ?`` ,?表示一个地址,在汇编指令中是 ``0x804a018`` ,转换成机器码之后是 ``18 a0 04 08`` (这是指令中十六进制数的小端表示,小端表示将在 :ref:`arch.cpu` 介绍)。 65 | 66 | .. index:: b编译, Compile, b编译器, Compiler 67 | 68 | 从上面的例子还可以看出,C语言的语句和低级语言的指令之间不是简单的一一对应关系,一条 ``a = b + 1;`` 语句要翻译成三条汇编或机器指令,这个过程称为编译(Compile),由编译器(Compiler)来完成,显然编译器的功能比汇编器要复杂得多。 69 | 70 | 编写、编译和执行一个C程序的步骤如下: 71 | 72 | .. index:: y源代码, Source Code, k可执行文件, Executable, c操作系统, Operating System 73 | 74 | #. 用文本编辑器写一个C程序,然后保存成一个文件,例如 :file:`program.c` (通常C程序的文件名后缀是 ``.c`` ),这称为源代码(Source Code)或源文件。 75 | #. 运行编译器对它进行编译,编译的过程并不执行程序,而是把源代码全部翻译成机器指令,再加上一些描述信息,生成一个新的文件,例如 :file:`a.out` ,这个文件称为可执行文件(Executable)。 76 | #. 可执行文件可以被操作系统(Operating System)加载执行,计算机执行该文件中由编译器生成的指令。 77 | 78 | .. figure:: ../images/intro.compile.png 79 | 80 | 编译执行的过程 81 | 82 | 现在你就可以跳到本章后面的 :ref:`intro.firstprogram` ,按照书上的步骤自己动手试试。 83 | 84 | .. index:: j解释执行, Interpret, j脚本, Script 85 | 86 | 有些高级语言写的程序不需要经过编译这个步骤,而是以解释的方式执行,解释执行(Interpret)的程序通常又叫做脚本(Script),解释执行的过程和C语言的编译执行过程很不一样。例如编写一个Shell脚本 :file:`script.sh` ,内容如下: 87 | 88 | .. code-block:: bash 89 | :linenos: 90 | 91 | #! /bin/sh 92 | VAR=1 93 | VAR=$(($VAR+1)) 94 | echo $VAR 95 | 96 | 这个脚本的第一行表明它是个Shell脚本,后面三行的意思分别是:定义变量 ``VAR`` 的初始值是1,然后自增1,最后打印 ``VAR`` 的值。用Shell程序 :file:`/bin/sh` 解释执行这个脚本,结果如下:: 97 | 98 | $ /bin/sh script.sh 99 | 2 100 | 101 | 这里的 :file:`/bin/sh` 称为解释器(Interpreter),解释器本身是一个可执行文件,而我们写的脚本 :file:`script.sh` 却不是一个真正的可执行文件。解释器 :file:`/bin/sh` 也是由C程序经过编译得到的包含机器指令的可执行文件,它被操作系统加载执行时,它所包含的机器指令指示它做这样的事情:把我们写的脚本 :file:`script.sh` 当成数据文件读取,理解我们所写的每一行程序的意思,并一行一行地执行相应的操作。 102 | 103 | .. figure:: ../images/intro.interpret.png 104 | 105 | 解释执行的过程 106 | 107 | .. index:: k可移植, Portable, p平台无关, Platform Independent, k跨平台, Cross-platform 108 | 109 | 理解了这些概念之后,我们在编译型高级语言、解释型高级语言和低级语言之间做个比较。用高级语言写的程序不能直接被计算机执行,需要经过编译之后变成可执行文件才能执行,或者需要通过一个解释器来解释执行。但用高级语言写程序有很多优点:首先,用高级语言写程序更简便,写出来的代码更紧凑,可读性更强,出了错也更容易改正;其次,高级语言是可移植的(Portable),或者称为平台无关的(Platform Independent)、跨平台的(Cross-platform)。 110 | 111 | .. index:: p平台, Platform, j计算机体系结构, Architecture 112 | 113 | 平台(Platform)这个词可以指计算机体系结构(Architecture),也可以指操作系统(Operating System)。有些程序只能在某个特定平台上执行,而有些程序拿到各种不同的平台上都可以执行,后者就称为平台无关的程序。下面我们来具体分析: 114 | 115 | .. index:: z指令集, Instruction Set 116 | 117 | #. 操作系统相同,计算机体系结构不同会怎么样? 118 | 119 | 不同的计算机体系结构有不同的指令集(Instruction Set),可以识别的机器指令格式是不同的,直接用某种体系结构的汇编或机器指令写出来的程序只能在这种体系结构的计算机上执行。 120 | 121 | 不同体系结构的计算机有各自的C编译器,可以把C程序编译成相应的机器指令,这意味着用C语言写的程序要想在各种不同体系结构的计算机上执行,只需用相应的编译器编译过即可。比如上面 ``a = b + 1;`` 的例子是在32位x86平台下编译的得到的结果,如果在ARMv4平台上编译则得到完全不同的结果(编译生成的可执行文件也完全不同): 122 | 123 | .. table:: 一个语句的三种表示(ARMv4平台) 124 | 125 | ============= ========================= 126 | 编程语言 表示形式 127 | ============= ========================= 128 | 机器语言 :: 129 | 130 | e59f2018 131 | e59f3018 132 | e5933000 133 | e2833001 134 | e5823000 135 | 汇编语言 :: 136 | 137 | ldr r2, [pc, #24] 138 | ldr r3, [pc, #24] 139 | ldr r3, [r3] 140 | add r3, r3, #1 141 | str r3, [r2] 142 | C语言 a = b + 1; 143 | ============= ========================= 144 | 145 | 同样道理,不同体系结构的计算机有各自的Shell解释器,一个Shell脚本要想在不同体系结构的计算机上执行,只需运行相应的Shell解释器来解释执行即可。 146 | 147 | #. 体系结构相同,操作系统不同会怎么样? 148 | 149 | 同样是32位x86平台,把一个Windows下的可执行文件(通常扩展名是 ``.exe`` )拷到Linux下能执行吗?答案是不能。虽然这个Windows下的可执行文件包含的是32位x86指令,但其文件格式和Linux下的可执行文件有很大差别,换句话说,能被Windows操作系统加载执行的程序不能被Linux操作系统加载执行,因为这两种操作系统加载执行程序的机制很不一样。 150 | 151 | 那么,把一个Windows下的C程序的源代码拷到Linux下,还能用C编译器编译执行吗?答案是不一定。如果这个C程序只用到了C标准库,是可以跨平台的;如果这个C程序用到了Windows操作系统提供而Linux操作系统没有提供的库和接口,那么在Linux下是编译不了的。 152 | 153 | 把一个在Windows下的Cygwin环境中能正常执行的Shell脚本拷到Linux下,还能正常执行吗?答案是能执行,但结果不一定正确。如果这个脚本访问了Windows下特有的资源(比如 :file:`C:\\a.txt` 这样的文件路径是Windows特有的,Linux的路径格式完全不同),则不能在Linux下正确执行。 154 | 155 | 用解释型语言写的程序执行起来一定比编译型语言慢,因为用解释型语言写的程序每次执行时解释器都要把源代码分析一遍,理解程序员写这些代码是想要做什么,再去执行相应的操作,而对于编译型语言来说,这个步骤只需要做一次,就是编译器把源代码分析一遍生成可执行文件,而之后可执行文件在每次执行时就不需要再分析源代码了。用解释型语言写的程序也有它的优点:换个平台就可以直接执行,而不需要先编译一遍,此外,解释型语言写的程序调试起来比编译型语言方便得多。 156 | 157 | .. index:: z字节码, Byte Code, x虚拟机, Virtual Machine 158 | 159 | 既然解释型语言和编译型语言各有各的优点,有一些高级语言就把两者的优点结合起来,采用编译和解释相结合的方式执行。Java、Python、Perl等编程语言都采用这种方式。以Python为例,程序员写的源代码文件(扩展名为 ``.py`` )在首次执行时被编译成字节码(Byte Code)文件(扩展名为 ``.pyc`` ),以后每次执行该程序时Python解释器直接解释执行字节码文件,而不再编译源代码。字节码文件中也包含指令,但并非机器指令,而是Python语言定义的一种虚拟机(Virtual Machine)的指令。Python语言在各种平台上都实现这种虚拟机,因此字节码文件从一种平台拷到另一种平台上仍然能被该平台的Python解释器解释执行。 160 | 161 | .. figure:: ../images/intro.bytecode.png 162 | 163 | 虚拟机执行的过程 164 | 165 | .. index:: 1GL, 1st Generation Programming Language, 2GL, 3GL, 4GL, 5GL, j结构化查询语言, SQL, Structured Query Language 166 | 167 | 编程语言仍在发展演化。以上介绍的机器语言称为第一代编程语言(1GL,1st Generation Programming Language),汇编语言称为第二代编程语言(2GL),C、C++、Java、Python等可以称为第三代编程语言(3GL)。目前已经有了4GL和5GL的概念。3GL的编程语言虽然是用语句编程而不直接用指令编程,但语句也分为输入、输出、基本运算、测试分支和循环等几种,和指令有直接的对应关系。而4GL以后的编程语言更多是描述要做什么(Declarative)而不描述具体一步一步怎么做(Imperative),具体一步一步怎么做完全由编译器或解释器决定,例如SQL语言(SQL,Structured Query Language,结构化查询语言)就是这样的例子。 168 | 169 | .. _intro.naturalformallang: 170 | 171 | 自然语言和形式语言 172 | ----------------------- 173 | 174 | .. index:: z自然语言, Natural Language, x形式语言, Formal Language 175 | 176 | 自然语言(Natural Language)就是人类讲的语言,比如汉语、英语和法语。这类语言不是人为设计(虽然有人试图强加一些规则)而是自然进化的。形式语言(Formal Language)是为了特定应用而人为设计的语言。例如数学家用的数字和运算符号、化学家用的分子式等。编程语言也是一种形式语言,是专门设计用来表达计算过程的形式语言。 177 | 178 | .. index:: y语法, Syntax, Token, c词法, Lexical, Grammar 179 | 180 | 形式语言有严格的语法(Syntax)规则,例如,3+3=6是一个语法正确的数学等式,而3=+6$则不是,H\ :sub:`2`\ O是一个正确的分子式,而 :sub:`2`\ Zz则不是。语法规则是由符号(Token)和结构的规则所组成的。Token的概念相当于自然语言中的单词和标点、数学式中的数和运算符、化学分子式中的元素名和数字,例如3=+6$的问题之一在于$不是一个合法的数也不是一个事先定义好的运算符,而 :sub:`2`\ Zz的问题之一在于没有一种元素的缩写是Zz。结构是指Token的排列方式,3=+6$还有一个结构上的错误,虽然加号和等号都是合法的运算符,但是不能在等号之后紧跟加号,而 :sub:`2`\ Zz的另一个问题在于分子式中必须把下标写在化学元素名称之后而不是前面。关于Token的规则称为词法(Lexical)规则,而关于结构的规则称为语法(Grammar)规则 [#]_ 。 181 | 182 | .. [#] 很不幸,Syntax和Grammar通常都翻译成“语法”,这让初学者非常混乱,Syntax的含义其实包含了Lexical和Grammar的规则,还包含一部分语义的规则(例如在C程序中变量应先声明后使用)。即使在英文的文献中Syntax和Grammar也经常混用,在有些文献中Syntax的含义不包括Lexical规则,只要注意上下文就不会误解。另外,本书在翻译容易引起混淆的时候通常直接用英文名称,例如Token没有十分好的翻译,直接用英文名称。 183 | 184 | .. index:: j解析, Parse, s上下文, Context, y语义, Semantic 185 | 186 | 当阅读一个自然语言的句子或者一种形式语言的语句时,你不仅要搞清楚每个词(Token)是什么意思,而且必须搞清楚整个句子的结构是什么样的(在自然语言中你只是没有意识到,但确实这样做了,尤其是在读外语时你肯定也意识到了)。这个分析句子结构的过程称为解析(Parse)。例如,当你听到“The other shoe fell.”这个句子时,你理解the other shoe是主语而fell是谓语动词,一旦解析完成,你就搞懂了句子的意思,如果知道shoe是什么东西,fall意味着什么,这句话是在什么上下文(Context)中说的,你还能理解这个句子主要暗示的内容--这属于语义(Semantic)的范畴。 187 | 188 | 虽然形式语言和自然语言有很多共同之处,包括Token、结构和语义,但是也有很多不一样的地方。 189 | 190 | .. index:: q歧义性, Ambiguity, r冗余性, Redundancy, y隐喻, Metaphor, z字面, Literal 191 | 192 | 歧义性(Ambiguity) 193 | 自然语言充满歧义,人们通过上下文的线索和自己的常识来解决这个问题。形式语言的设计要求是清晰的、毫无歧义的,这意味着每个语句都必须有确切的含义而不管上下文如何。 194 | 195 | 冗余性(Redundancy) 196 | 为了消除歧义减少误解,自然语言引入了相当多的冗余。结果是自然语言经常说得啰里啰唆,而形式语言则更加紧凑,极少有冗余。 197 | 198 | 与字面意思的一致性 199 | 自然语言充斥着成语和隐喻(Metaphor),我在某种场合下说“The other shoe fell”,可能并不是说谁的鞋掉了。而形式语言中字面(Literal)意思基本上就是真实意思,也会有一些例外,例如下一章要讲的C语言转义序列,但即使有例外也会明确规定哪些字面意思不是真实意思,它们所表示的真实意思又是什么。 200 | 201 | 说自然语言长大的人(实际上没有人例外),往往有一个适应形式语言的困难过程。某种意义上,形式语言和自然语言之间的不同正像诗歌和说明文的区别: 202 | 203 | 诗歌 204 | 词语的发音和意思一样重要,全诗作为一个整体创造出一种效果或者表达一种感情。歧义和非字面意思不仅是常见的而且是刻意使用的。 205 | 206 | 说明文 207 | 词语的字面意思显得更重要,并且结构能传达更多的信息。诗歌只能看一个整体,而说明文更适合逐字逐句分析,但仍然充满歧义。 208 | 209 | 程序 210 | 计算机程序是毫无歧义的,字面和本意高度一致,能够完全通过对Token和结构的分析加以理解。 211 | 212 | 这里给出一些关于阅读程序(包括其他形式语言)的建议:首先请记住形式语言远比自然语言紧凑,所以要多花点时间来读;其次,结构很重要,从上到下从左到右读往往不是一个好办法,而应该学会在大脑里解析--识别Token,分解结构;最后,请记住细节的影响,诸如拼写错误和标点错误这些在自然语言中可以忽略的小毛病会把形式语言搞得面目全非。 213 | 214 | .. _intro.debug: 215 | 216 | 程序的调试 217 | -------------- 218 | 219 | .. index:: t调试, Debug 220 | 221 | 只要是人做的事情就难免会出错,何况编程还是一件这么复杂的工作。据说有这样一个典故:早期的计算机体积都很大,有一次一台计算机不能正常工作,工程师们找了半天原因最后发现是一只虫子(Bug)钻进计算机中造成的。从此以后,程序中的错误被叫做Bug,而找到这些Bug并加以纠正的过程就叫做调试(Debug)。有时候调试是一件非常复杂的工作,要求程序员概念明确、逻辑清晰、性格沉稳,还需要一点运气。调试的技能我们在后续的学习中慢慢培养,但首先我们要区分清楚程序中的Bug分为哪几类。 222 | 223 | 编译时错误 224 | 编译器只能翻译语法正确的程序,否则将导致编译失败,无法生成可执行文件。对于自然语言来说,一点语法错误不是很严重的问题,因为我们仍然可以读懂句子。而编译器就没那么宽容了,只要有哪怕一个很小的语法错误,编译器就会输出一条错误提示信息然后罢工,你就得不到你想要的结果。虽然大部分情况下编译器给出的错误提示信息能够指出错误代码的位置,但也有个别时候编译器给出的错误提示信息帮助不大,甚至会误导你。在开始学习编程的前几个星期,你可能会花大量的时间来纠正语法错误。等到有了一些经验之后,还是会犯这样的错误,不过会少得多,而且你能更快地发现错误原因。等到经验更丰富之后你就会觉得,语法错误是最简单最低级的错误,编译器的错误提示也就那么几种,即使错误提示是有误导的也能够立刻找出真正的错误原因是什么。相比下面两种错误,语法错误解决起来要容易得多。 225 | 226 | .. index:: r运行时, Run-time 227 | 228 | 运行时错误 229 | 编译器检查不出这类错误,仍然可以生成可执行文件,但在运行时会出错而导致程序崩溃。对于我们接下来的几章将编写的简单程序来说,运行时错误很少见,到了后面的章节你会遇到越来越多的运行时错误。读者在以后的学习中要时刻 **注意区分编译时和运行时(Run-time)这两个概念** ,不仅在调试时需要区分这两个概念,在学习C语言的很多语法和规则时都需要区分这两个概念,有些事情在编译时做,有些事情则在运行时做。 230 | 231 | 逻辑错误和语义错误 232 | 第三类错误是逻辑错误和语义错误。如果程序里有逻辑错误,编译和运行都会很顺利,看上去也不产生任何错误信息,但是程序没有干它该干的事情,而是干了别的事情。当然不管怎么样,计算机只会按你写的程序去做,问题在于你写的程序不是你真正想要的,这意味着程序的意思(即语义)是错的。找到逻辑错误在哪需要十分清醒的头脑,要通过观察程序的输出回过头来判断它到底在做什么。 233 | 234 | 通过本书你将掌握的最重要的技巧之一就是调试。调试的过程可能会让你感到一些沮丧,但调试也是编程中最需要动脑的、最有挑战和乐趣的部分。从某种角度看调试就像侦探工作,根据掌握的线索来推断是什么原因和过程导致了你所看到的结果。调试也像是一门实验科学,每次想到哪里可能有错,就修改程序然后再试一次。如果假设是对的,就能得到预期的正确结果,就可以接着调试下一个Bug,一步一步逼近正确的程序;如果假设错误,只好另外再找思路再做假设。“当你把不可能的全部剔除,剩下的——即使看起来再怎么不可能——就一定是事实。”(即使你没看过福尔摩斯也该看过柯南吧) 235 | 236 | 也有一种观点认为,编程和调试是一回事,编程的过程就是逐步调试直到获得期望的结果为止。你应该总是从一个能正确运行的小规模程序开始,每做一步小的改动就立刻进行调试,这样的好处是总有一个正确的程序做参考:如果正确就继续编程,如果不正确,那么一定是刚才的小改动出了问题。例如,Linux操作系统包含了成千上万行代码,但它也不是一开始就规划好了内存管理、设备管理、文件系统、网络等等大的模块,一开始它仅仅是Linus Torvalds用来琢磨Intel 80386芯片而写的小程序。据Larry Greenfield说,“Linus的早期工程之一是编写一个交替打印AAAA和BBBB的程序,这玩意儿后来进化成了Linux。”(引自The Linux User's Guide Beta1版)在后面的章节中会给出更多关于调试和编程实践的建议。 237 | 238 | .. _intro.firstprogram: 239 | 240 | 第一个程序 241 | ------------ 242 | 243 | 在开始写程序之前首先要搭建开发环境,安装编译器、头文件、库文件、开发文档等。在Linux系统下如何安装软件包和搭建开发环境不是本书的重点,这些问题需要读者自己解决,但我在这里简单列出需要安装的软件包供参考(假定你用的是Debian或Ubuntu发行版): 244 | 245 | * gcc: The GNU C compiler 246 | * libc6-dev: GNU C Library: Development Libraries and Header Files 247 | * manpages-dev: Manual pages about using GNU/Linux for development 248 | * manpages-posix-dev: Manual pages about using a POSIX system for development 249 | * binutils: The GNU assembler, linker and binary utilities 250 | * gdb: The GNU Debugger 251 | * make: The GNU version of the "make" utility 252 | 253 | 本书所有代码都在Ubuntu 12.04 LTS(32位x86平台)发行版上编译测试通过。读者如果用其他Linux发行版,或者不使用发行版提供的软件包而是用自己从源代码编译出的软件包,则编译运行本书的代码得到的结果会有些不同,但不影响学习。 254 | 255 | 通常一本教编程的书中第一个例子都是打印Hello world,这个传统源自 [K&R]_ ,用C语言写这个程序可以这样写: 256 | 257 | .. code-block:: c 258 | :linenos: 259 | 260 | #include 261 | 262 | /* main: generate some simple output */ 263 | 264 | int main(void) 265 | { 266 | printf("Hello, world.\n"); 267 | return 0; 268 | } 269 | 270 | 将这个程序保存成主目录下的 :file:`main.c` ,然后编译运行:: 271 | 272 | $ gcc main.c 273 | $ ./a.out 274 | Hello, world. 275 | 276 | :command:`gcc` 是Linux平台的C编译器,编译后在当前目录下生成可执行文件 :file:`a.out` [#]_ ,直接在命令行输入这个可执行文件的路径就可以执行它。如果不想把文件名叫 :file:`a.out` ,可以用 :command:`gcc` 的 :option:`-o` 参数自己指定文件名:: 277 | 278 | $ gcc main.c -o main 279 | $ ./main 280 | Hello, world. 281 | 282 | .. [#] “a.out”是“Assembler Output”的缩写,实际上一个C程序要先被编译器翻译成汇编程序,再被汇编器翻译成机器指令,最后还要经过链接器的处理才能成为可执行文件,详见 :ref:`asmc.main` 。 283 | 284 | .. index:: Boilerplate 285 | 286 | 虽然这只是一个很小的程序,但我们目前暂时还不具备相关的知识来完全理解这个程序,比如程序的第一行,还有程序主体的 ``int main(void){...return 0;}`` 结构,这些部分我们暂时不详细解释,读者现在只需要把它们看成是每个程序按惯例必须要写的部分(Boilerplate)。但要注意 ``main`` 是一个特殊的名字,C程序总是从 ``main`` 里面的第一条语句开始执行的,在这个程序中是指 ``printf`` 这条语句。 287 | 288 | .. index:: z注释, Comment 289 | 290 | 第3行的 ``/* ... */`` 结构是一个注释(Comment),其中可以写一些描述性的话,解释这段程序在做什么。注释只是写给程序员看的,编译器会忽略从 ``/*`` 到 ``*/`` 的所有字符,所以写注释没有语法规则,爱怎么写就怎么写,并且不管写多少都不会被编译进可执行文件中。 291 | 292 | .. index:: ;号, Semicolon 293 | 294 | ``printf`` 语句的作用是把消息打印到屏幕。注意语句的末尾以;号(Semicolon)结束,下一条语句 ``return 0;`` 也是如此。 295 | 296 | .. index:: {}括号, Brace, Curly Brace, s缩进, Indent, k空格, Blank 297 | 298 | C语言用{}括号(Brace或Curly Brace)把语法结构分成组,在上面的程序中 ``printf`` 和 ``return`` 语句套在 ``main`` 的{}括号中,表示它们属于 ``main`` 的定义之中。我们看到这两句相比 ``main`` 那一行都缩进(Indent)了一些,在代码中可以用若干个空格(Blank)和Tab字符来缩进,缩进不是必须的,但这样使我们更容易看出这两行是属于 ``main`` 的定义之中的,要写出漂亮的程序必须有整齐的缩进, :ref:`codingstyle.indent` 将介绍推荐的缩进写法。 299 | 300 | 正如前面所说,编译器对于语法错误是毫不留情的,如果你的程序有一点拼写错误,例如第一行写成了 ``stdoi.h`` ,在编译时会得到错误提示:: 301 | 302 | $ gcc main.c 303 | main.c:1:19: fatal error: stdoi.h: No such file or directory 304 | compilation terminated. 305 | 306 | 这个错误提示非常紧凑,初学者不容易看明白出了什么错误,即使知道这个错误提示说的是第1行有错误,很多初学者对照着书看好几遍也看不出自己这一行哪里有错误,因为他们对符号和拼写不敏感(尤其是英文较差的初学者),他们还不知道这些符号是什么意思又如何能记住正确的拼写?对于初学者来说,最想看到的错误提示其实是这样的:“在main.c程序第1行的第19列,您试图包含一个叫做stdoi.h的文件,可惜我没有找到这个文件,但我却找到了一个叫stdio.h的文件,我猜这个才是您想要的,对吗?”可惜没有任何编译器会友善到这个程度,大多数时候你所得到的错误提示并不能直接指出谁是犯人,而只是一个线索,你需要根据这个线索做一些侦探和推理。 307 | 308 | 有些时候编译器的提示信息不是 ``error`` 而是 ``warning`` ,例如把上例中的 ``printf("Hello, world.\n");`` 改成 ``printf(1);`` 然后编译运行:: 309 | 310 | $ gcc main.c 311 | main.c: In function ‘main’: 312 | main.c:7:9: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [enabled by default] 313 | /usr/include/stdio.h:363:12: note: expected ‘const char * __restrict__’ but argument is of type ‘int’ 314 | main.c:7:9: warning: format not a string literal and no format arguments [-Wformat-security] 315 | $ ./a.out 316 | Segmentation fault (core dumped) 317 | 318 | .. index:: d段错误, Segmentation fault, b崩溃, Crash 319 | 320 | 这个警告信息是说类型不匹配,但勉强还能配得上。警告信息不是致命错误,编译仍然可以继续,如果整个编译过程只有警告信息而没有错误信息,仍然可以生成可执行文件。但是,警告信息也是不容忽视的。出警告信息说明你的程序写得不够规范,可能有Bug,虽然能编译生成可执行文件,但程序的运行结果往往是不正确的,例如上面的程序运行时出了一个段错误(Segmentation fault),段错误是程序崩溃(Crash)的一种表现,这属于运行时错误。 321 | 322 | 各种警告信息的严重程度不同,像上面这种警告几乎一定表明程序中有Bug,而另外一些警告只表明程序写得不够规范,一般还是能正确运行的。有些不重要的警告信息 :command:`gcc` 默认是不提示的,但这些警告信息也有可能表明程序中有Bug, **一个好的习惯是打开gcc的-Wall选项,让gcc提示所有的警告信息--不管是严重的还是不严重的--然后把这些问题从代码中全部消灭** 。比如把上例中的 ``printf("Hello, world.\n");`` 改成 ``printf(0);`` 然后编译运行:: 323 | 324 | $ gcc main.c 325 | $ ./a.out 326 | 327 | 编译既不报错也不报警告,一切正常,但是运行程序什么也不打印。如果打开 :option:`-Wall` 选项编译就会报警告了:: 328 | 329 | $ gcc -Wall main.c 330 | main.c: In function ‘main’: 331 | main.c:7:9: warning: null argument where non-null required (argument 1) [-Wnonnull] 332 | 333 | 如果 ``printf`` 后面的 ``0`` 是你不小心写上去的(例如错误地使用了编辑器的查找替换功能),这个警告就能帮助你发现错误。虽然本书的命令行为了突出重点通常省略 :option:`-Wall` 选项,但是强烈建议你写每一个编译命令时都加上 :option:`-Wall` 选项。 334 | 335 | .. rubric:: 习题 336 | 337 | #. 尽管编译器的错误提示不够友好,但仍然是学习过程中一个很有用的工具。你可以像上面那样,从一个正确的程序开始每次改动一小点,然后编译看是什么结果,如果出错了,就尽量记住编译器给出的错误提示并把改动还原。因为错误是你改出来的,你已经知道错误原因是什么了,所以能很容易地把错误原因和错误提示信息对应起来记住,这样下次你在毫无防备的情况下撞到这个错误提示时就会很容易想到错误原因是什么了。这样反复练习,有了一定的经验积累之后面对编译器的错误提示就会从容得多了。 338 | 339 | -------------------------------------------------------------------------------- /zh/link.rst: -------------------------------------------------------------------------------- 1 | 链接详解 2 | ============== 3 | 4 | .. _link.declaration: 5 | 6 | 声明和定义 7 | ----------------- 8 | -------------------------------------------------------------------------------- /zh/linkedlist.rst: -------------------------------------------------------------------------------- 1 | 链表、二叉树和哈希表 2 | ===================== 3 | 4 | .. _linkedlist.linkedlist: 5 | 6 | 链表 7 | ------- 8 | 9 | .. _linkedlist.binarytree: 10 | 11 | 二叉树 12 | ------- 13 | 14 | 哈希表 15 | ------- 16 | 17 | -------------------------------------------------------------------------------- /zh/number.rst: -------------------------------------------------------------------------------- 1 | 计算机中数的表示 2 | =================== 3 | 4 | .. _number.binary: 5 | 6 | 为什么计算机用二进制计数 7 | --------------------------------- 8 | 9 | .. index:: s十进制, Decimal 10 | 11 | 人类的计数方式通常是“逢十进一”,称为十进制(Decimal),大概因为人有十个手指,所以十进制是最自然的计数方式,很多民族的语言文字中都有十个数字,而阿拉伯数字0~9是目前最广泛采用的。 12 | 13 | .. index:: e二进制, Binary, w位, bit 14 | 15 | 计算机是用数字电路搭成的,数字电路中只有1和0两种状态,所以对计算机来说二进制(Binary)是最自然的计数方式。根据“逢二进一”的原则,十进制的1、2、3、4分别对应二进制的1、10、11、100。二进制的一位数字称为一个位(bit) [#]_ ,三个bit能够表示的最大的二进制数是111,也就是十进制的7。不管用哪种计数方式,数的大小并没有变,十进制的1+1等于2,二进制的1+1等于10,二进制10和十进制2的大小是相等的。计算机采用如下的逻辑电路计算两个bit的加法: 16 | 17 | .. [#] bit通常首字母小写,因为bit简写为b,而Byte简写为B 18 | 19 | .. figure:: ../images/number.digitallogic.png 20 | 21 | 1-bit Full Adder 22 | 23 | .. index:: y一位全加器, 1-bit Full Adder, m门电路, Gate, d导线, Wire, f反相器, Inverter, y异或, XOR, eXclusive OR 24 | 25 | 图的上半部分(出自Wikipedia)的电路称为一位全加器(1-bit Full Adder),图的下半部分是一些逻辑电路符号的图例。我们首先解释这些图例,逻辑电路由门电路(Gate)和导线(Wire)组成,同一条导线上在某一时刻的电压值只能是高和低两种状态之一,分别用1和0表示。如果两条导线短接在一起则它们的电压值相同,在接点处画一个黑点,如果接点处没有画黑点则表示这两条线并没有短接在一起,只是在画图时无法避免交叉而已。导线的电压值进入门电路的输入端,经过逻辑运算后在门电路的输出端输出运算结果的电压值,任何复杂的加减乘除运算都可以分解成简单的逻辑运算。AND、OR和NOT运算在 :ref:`cond.bool` 中讲过了,这三种逻辑运算分别用与门、或门和反相器(Inverter)实现。另外几种逻辑运算在这里补充一下。异或(XOR,eXclusive OR)运算的真值表如下: 26 | 27 | .. table:: XOR的真值表 28 | 29 | = = ======= 30 | A B A XOR B 31 | = = ======= 32 | 0 0 0 33 | 0 1 1 34 | 1 0 1 35 | 1 1 0 36 | = = ======= 37 | 38 | .. index:: y与非, NAND, h或非, NOR 39 | 40 | 异或运算用一句话概括就是:两个操作数相同则结果为0,两个操作数不同则结果为1。与非(NAND)和或非(NOR)运算就是在与、或运算的基础上取反: 41 | 42 | .. table:: NAND的真值表 43 | 44 | = = ======== 45 | A B A NAND B 46 | = = ======== 47 | 0 0 1 48 | 0 1 1 49 | 1 0 1 50 | 1 1 0 51 | = = ======== 52 | 53 | .. table:: NOR的真值表 54 | 55 | = = ======= 56 | A B A NOR B 57 | = = ======= 58 | 0 0 1 59 | 0 1 0 60 | 1 0 0 61 | 1 1 0 62 | = = ======= 63 | 64 | .. index:: j进位, Carry 65 | 66 | 如果把与门、或门和反相器组合来实现NAND和NOR运算,则电路过于复杂了,因此逻辑电路中通常有专用的与非门和或非门。现在我们看看上图中的AND、OR、XOR是怎么实现两个bit的加法的。A、B是两个加数,C\ :sub:`in` 是低位传上来的进位(Carry),相当于三个加数求和,三个加数都是0则结果为0,三个加数都是1则结果为11,即输出位S是1,产生的进位C\ :sub:`out` 也是1。下面根据加法的规则用真值表列出所有可能的情况: 67 | 68 | .. table:: 1-bit Full Adder的真值表 69 | 70 | = = ============ ============= = 71 | A B C\ :sub:`in` C\ :sub:`out` S 72 | = = ============ ============= = 73 | 0 0 0 0 0 74 | 0 0 1 0 1 75 | 0 1 0 0 1 76 | 0 1 1 1 0 77 | 1 0 0 0 1 78 | 1 0 1 1 0 79 | 1 1 0 1 0 80 | 1 1 1 1 1 81 | = = ============ ============= = 82 | 83 | 请读者自己验算上面的电路图是否正确实现了这个真值表。如果把很多个一位全加器串接起来,就成了多位加法器,如下图所示(该图出自Wikipedia): 84 | 85 | .. figure:: ../images/number.fulladder.png 86 | 87 | 4-bit Ripple Carry Adder 88 | 89 | .. index:: Ripple Carry Adder 90 | 91 | 图中的一位全加器用方框表示,上一级全加器的C\ :sub:`out` 连接到下一级全加器的C\ :sub:`in` ,让进位像涟漪一样一级一级传开,所以叫做Ripple Carry Adder,这样就可以把两个4 bit二进制数A\ :sub:`3`\ A\ :sub:`2`\ A\ :sub:`1`\ A\ :sub:`0` 和B\ :sub:`3`\ B\ :sub:`2`\ B\ :sub:`1`\ B\ :sub:`0` 加起来了。在这里介绍Ripple Carry Adder只是为了让读者理解计算机是如何通过逻辑运算来做算术运算的,实际上这种加法器效率很低,只能加完一位再加下一位,更实用、更复杂的加法器可以多个位一起计算,有兴趣的读者可参考 [数字逻辑基础]_ 的5.4节。 92 | 93 | .. _number.conversion: 94 | 95 | 不同进制之间的换算 96 | -------------------------- 97 | 98 | 在十进制中,个位的1代表10\ :sup:`0`\ =1,十位的1代表10\ :sup:`1`\ =10,百位的1代表10\ :sup:`2`\ =100,所以 99 | 100 | 123=1×10\ :sup:`2`\ +2×10\ :sup:`1`\ +3×10\ :sup:`0` 101 | 102 | 同理,在二进制中,个位的1代表2\ :sup:`0`\ =1,十位的1代表2\ :sup:`1`\ =2,百位的1代表2\ :sup:`2`\ =4,所以 103 | 104 | (A\ :sub:`3`\ A\ :sub:`2`\ A\ :sub:`1`\ A\ :sub:`0`)\ :sub:`2`\ =A\ :sub:`3`\ ×2\ :sup:`3`\ +A\ :sub:`2`\ ×2\ :sup:`2`\ +A\ :sub:`1`\ ×2\ :sup:`1`\ +A\ :sub:`0`\ ×2\ :sup:`0` 105 | 106 | .. index:: z最高位, MSB, Most Significant Bit, z最低位, LSB, Least Significant Bit 107 | 108 | 如果二进制和十进制数出现在同一个等式中,为了区别我们用(A\ :sub:`3`\ A\ :sub:`2`\ A\ :sub:`1`\ A\ :sub:`0`)\ :sub:`2` 这种形式表示A\ :sub:`3`\ A\ :sub:`2`\ A\ :sub:`1`\ A\ :sub:`0` 是二进制数,每个数字只能是0或1,其他没有套括号加下标的数仍表示十进制数。对于(A\ :sub:`3`\ A\ :sub:`2`\ A\ :sub:`1`\ A\ :sub:`0`)\ :sub:`2` 这样一个二进制数,最左边的A\ :sub:`3` 位称为最高位(MSB,Most Significant Bit),最右边的A\ :sub:`0` 位称为最低位(LSB,Least Significant Bit)。以后我们遵循这样的惯例:假设一个数是32位的,则LSB称为第0位而不是第1位(这一位上的1表示2\ :sup:`0` ),MSB称为第31位(这一位上的1表示2\ :sup:`31` )。上式就是从二进制到十进制的换算公式。作为练习,请读者算一下(1011)\ :sub:`2` 和(1111)\ :sub:`2` 换算成十进制分别是多少。 109 | 110 | 下面来看十进制怎么换算成二进制。我们知道 111 | 112 | 13=1×2\ :sup:`3`\ +1×2\ :sup:`2`\ +0×2\ :sup:`1`\ +1×2\ :sup:`0` 113 | 114 | 所以13换算成二进制应该是(1101)\ :sub:`2` 。问题是怎么把13分解成等号右边的形式呢?注意到等号右边可以写成 115 | 116 | 13=(((0×2+1\ :sub:`3`)×2+1\ :sub:`2`)×2+0\ :sub:`1`)×2+1\ :sub:`0` 117 | 118 | 我们将13反复除以2取余数就可以提取出上式中的1101四个数字,为了让读者更容易看清楚是哪个1和哪个0,上式和下式中对应的数字都加了下标: 119 | 120 | 13÷2=6...1\ :sub:`0` 121 | 122 | 6÷2=3...0\ :sub:`1` 123 | 124 | 3÷2=1...1\ :sub:`2` 125 | 126 | 1÷2=0...1\ :sub:`3` 127 | 128 | 把这四步得到的余数按相反的顺序排列就是13的二进制表示,因此这种方法称为除二反序取余法。 129 | 130 | .. index:: b八进制, Octal, s十六进制, Hexadecimal 131 | 132 | 计算机用二进制表示数,程序员也必须习惯使用二进制,但二进制写起来太啰嗦了,所以通常将二进制数分成每三位一组或者每四位一组,每组用一个数字表示。比如把(10100011)\ :sub:`2` 从最低位开始每三位分成一组,即(10,100,011)\ :sub:`2` ,然后把每组写成一个0~7的数字,就是(243)\ :sub:`8` ,这种表示法的特点是逢八进一,称为八进制(Octal)。类似地,我们也可以把(10100011)\ :sub:`2` 按每四位分成一组,即(1010,0011)\ :sub:`2` ,然后把每组写成一个数字,这个数的低位是3,高位已经大于9了,我们规定用字母A~F表示10~15,则这个数可以写成(A3)\ :sub:`16` ,每一位数字的取值范围是0~F,逢十六进一,称为十六进制(Hexadecimal)。所以,八进制、十六进制是程序员为了书写二进制方便而发明的简便写法,就像草书和正楷的关系一样。 133 | 134 | .. rubric:: 习题 135 | 136 | #. 二进制小数可以这样定义: 137 | 138 | (0.A\ :sub:`-1`\ A\ :sub:`-2`\ A\ :sub:`-3`...)\ :sub:`2`\ =A\ :sub:`-1`\ ×2\ :sup:`-1`\ +A\ :sub:`-2`\ ×2\ :sup:`-2`\ +A\ :sub:`-3`\ ×2\ :sup:`-3`\ +... 139 | 140 | 这个定义同时也是从二进制小数到十进制小数的换算公式。从本节讲的十进制转二进制的推导过程出发类比一下,十进制小数换算成二进制小数应该怎么算? 141 | 142 | #. 思考一下,八进制(或十六进制)与十进制之间如何相互换算? 143 | 144 | 整数的加减运算 145 | --------------------- 146 | 147 | 我们已经了解了计算机中正整数如何表示,加法如何计算,那么负数如何表示,减法又如何计算呢?本节讨论这些问题。为了书写方便,本节举的例子都是8位二进制数的计算,实际计算机做整数加减运算的操作数可以是8位、16位、32位或64位的。 148 | 149 | Sign and Magnitude表示法 150 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 151 | 152 | .. index:: f符号位, Sign Bit, Sign and Magnitude 153 | 154 | 要用8位二进制数表示正数和负数,一种简单的想法是把最高位规定为符号位(Sign Bit),0表示正1表示负,剩下的7位表示绝对值的大小,这称为Sign and Magnitude表示法,例如-1表示成10000001,+1表示成00000001。这样用8位二进制数可以表示的整数的取值范围是-(2\ :sup:`7`\ -1)~2\ :sup:`7`\ -1,即-127~127。 155 | 156 | 采用这种表示法,计算机做加法运算需要处理以下逻辑: 157 | 158 | .. index:: y溢出, s上溢, Overflow, y溢出标志, Overflow Flag, x下溢, Underflow 159 | 160 | #. 如果两数符号位相同,就把它们的低7位相加,符号位不变。如果低7位相加时在最高位产生进位,说明结果的绝对值大于127,超出7位所能表示的数值范围,这称为溢出(Overflow) [#]_ ,这时通常把计算机中的一个溢出标志(Overflow Flag)置1,读一下这个标志就知道当前运算是否产生了溢出。 161 | #. 如果两数符号位不同,首先比较它们的低7位(即绝对值)谁大,然后用较大的绝对值减较小的绝对值,结果的符号位和绝对值大的数相同。 162 | 163 | .. [#] 溢出可以进一步细分,正整数溢出称为上溢(Overflow),负整数溢出称为下溢(Underflow)。 164 | 165 | 那么减法如何计算呢?由于我们规定了负数的表示,可以把减法改写成加法,要计算a-b,可以先把b变号然后和a相加,相当于计算a+(-b)。但如果两个加数的符号位不同就要用较大的绝对值减较小的绝对值,这一步减法计算仍然是免不了的。我们知道加法要进位,减法要借位,计算过程是不同的,我们在 :ref:`number.binary` 简单介绍了加法器电路,现在看来还需要再设计一套减法器电路。 166 | 167 | 如果采用Sign and Magnitude表示法,计算机做加减运算需要处理很多逻辑:比较符号位,比较绝对值,加法变减法,减法变加法,小数减大数变成大数减小数……这是非常低效率的。还有一个缺点是0的表示不唯一,既可以表示成10000000也可以表示成00000000,这进一步增加了逻辑的复杂性,所以我们迫切需要重新设计整数的表示方法使计算过程更简单。 168 | 169 | 1's Complement表示法 170 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 171 | 172 | 本节介绍一种二进制补码表示法,为了便于理解,先从我们熟悉的十进制数的补码表示法开始讲起。现在我们用3位十进制数字做为补码来表示-499~499之间的数,具体规定如下: 173 | 174 | .. table:: 9的补码表示法 175 | 176 | ==== ======= 177 | 数值 补码表示 178 | ==== ======= 179 | -499 500 180 | -498 501 181 | ... ... 182 | -1 998 183 | 0 999 184 | 0 0 185 | 1 1 186 | ... ... 187 | 498 498 188 | 499 499 189 | ==== ======= 190 | 191 | .. index:: 9的补码, 9's Complement 192 | 193 | 也就是说,正数的补码就是它本身,负数的补码是999加上该负数(或者说减去该负数的绝对值),0有两个补码--0和999。这种补码表示称为9的补码(9's Complement)。采用9的补码表示,可以把取值范围内(-499~499)任意正负数的加减法运算转化成正数的加法运算来做。我们看一个例子。 194 | 195 | #. 要计算:167-59 196 | #. 减法改写成加法:167+(-59) 197 | #. 取9的补码表示:167+940 198 | #. 两个补码相加的结果是:107进1 199 | #. 高位进的1加到低位上去,结果是:108(本来应该加1000,结果加了1,少加了999,正好把先前取9的补码多加的999抵消掉了) 200 | #. 注意,计算结果也是9的补码,如果计算结果在500~999之间,说明它是负数或0,需要转换回原数才是最终结果 201 | #. 以上计算过程的证明:167-59=167+(-59)=167+(999-59)-1000+1=167+940-1000+1=1107-1000+1=107+1=108 202 | 203 | 在上述计算步骤中其实我们还是做了一次减法运算--取-59的补码需要计算999-59,这个减法没有借位,比较容易算。看到这里,读者最放心不下的恐怕是这个步骤:计算结果的最高位如果有进位则要加回到最低位上去,如果没有进位就不做任何处理。虽然上面给出了证明,但只是证明了“在167-59的计算过程中这么处理是正确的”。完整的证明需要考虑五种情况: 204 | 205 | * 两个正数相加 206 | * 两个负数相加 207 | * 一正一负相加得正 208 | * 一正一负相加得负 209 | * 任何数加0(补码可以用0表示也可以用999表示),结果不变 210 | 211 | 我们举的例子验证了第三种情况,另外四种情况请读者自己验证。注意,如果计算结果超出了取值范围-499~499则产生溢出,我们暂时不考虑溢出的情况,下一节会讲到如何判定溢出。 212 | 213 | .. index:: 1的补码, 1's Complement 214 | 215 | 现在我们把上述补码表示法推广到8位二进制数,这种补码表示称为1的补码(1's Complement): 216 | 217 | #. 正数的补码就是它本身,取值范围是00000001~01111111(1~127)。 218 | #. 负数的补码是11111111减去该负数的绝对值,取值范围是10000000~11111110(-127~-1)。负数取补码非常简单,连减法都不用做,因为1-1=0,1-0=1,所以取补码就是把每个bit取反。 219 | #. 正数补码的最高位都是0,负数补码的最高位都是1,所以最高位仍可以看作符号位。 220 | #. 0有两个补码--0和11111111。 221 | #. 如果是减法运算,先改写成加法运算,然后把各加数取补码后相加,计算结果的最高位如果有进位则要加回到最低位上去,计算结果也是补码表示的。 222 | 223 | 举个例子: 224 | 225 | #. 要计算:00001000-00000100 226 | #. 减法改写成加法:00001000+(-00000100) 227 | #. 取1的补码表示:00001000+11111011 228 | #. 两个补码相加的结果是:00000011进1 229 | #. 高位进的1加到低位上去,结果是:00000100 230 | 231 | 1's Complement表示法相对于Sign and Magnitude表示法的优势是非常明显的:不需要把符号和绝对值分开考虑,计算逻辑很简单,甚至连减法器电路都省了,只要有一套加法器电路,再有一套把每个bit取反的电路,就可以做取值范围内任意正负数的加减法运算。美中不足的是0的补码表示仍然不唯一,既可以是11111111也可以是00000000,为了解决这最后一个问题,我们引入2's Complement表示法。 232 | 233 | 2's Complement表示法 234 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 235 | 236 | .. index:: 2的补码, 2's Complement 237 | 238 | 上一节以8位二进制数为例介绍了1's Complement表示法,我们在此基础上稍加修改,得到2's Complement表示法: 239 | 240 | #. 0和正数的补码就是它本身,取值范围是0~01111111(0~127)。 241 | #. 负数的补码是在1's Complement表示法的基础上再加1,取值范围是10000000~11111111(-128~-1)。 242 | #. 0和正数补码的最高位都是0,负数补码的最高位都是1,所以最高位仍可以看作符号位。 243 | #. 如果是减法运算,先改写成加法运算,然后把各加数取补码后相加,忽略计算结果最高位的进位 [#]_ ,计算结果也是补码表示的。 244 | 245 | .. [#] 读者如果对“忽略计算结果最高位的进位”这个步骤放心不下,仍可以分几种情况自行验证:两个正数相加;两个负数相加;一正一负相加得正;一正一负相加得负;任何数加0结果不变(这个显然,因为0现在只有一种补码表示就是0)。 246 | 247 | 目前绝大多数计算机都采用这种补码表示法。为什么称为“2的补码”呢?我们看一个例子,用8位二进制数来表示-4的补码是这样算的: 248 | 249 | 11111111-00000100+1=100000000-00000100=2\ :sup:`8`\ -4 250 | 251 | 同理,如果用32位二进制数来表示-4的补码,应该等于2\ :sup:`32`\ -4。 252 | 253 | 采用2's Complement表示法,8位二进制数可以表示的取值范围是-128~127,如果计算结果超出这个范围就会产生溢出,例如: 254 | 255 | .. figure:: ../images/number.overflow.png 256 | 257 | 有符号数加法溢出 258 | 259 | 如何判定产生了溢出呢?我们还是分几种情况讨论:如果两个正数相加溢出,结果一定是负数;如果两个负数相加溢出,结果一定是正数;一正一负相加,无论结果是正是负都不可能溢出。 260 | 261 | .. figure:: ../images/number.overflowp.png 262 | 263 | 如何判定溢出 264 | 265 | 从上图可以得出结论:在相加过程中最高位产生的进位和次高位产生的进位如果相同则表示没有溢出,如果不同则表示有溢出。在逻辑电路中可以把这两个进位连接到一个异或门,把异或门的输出连接到溢出标志。 266 | 267 | 有符号数和无符号数 268 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 269 | 270 | .. index:: y有符号数, Signed Number, w无符号数, Unsigned Number 271 | 272 | 前面几节我们用8位二进制数表示正数和负数,讲了三种表示法,每种表示法对应一种计算规则,取值范围也略有不同,这称为有符号数(Signed Number);如果直接用8位二进制数表示0~255,这称为无符号数(Unsigned Number)。其实计算机做加法时并不区分操作数是有符号数还是无符号数,计算过程都一样,比如上一节的例子也可以看作无符号数的加法: 273 | 274 | .. figure:: ../images/number.carry.png 275 | 276 | 无符号数加法进位 277 | 278 | .. index:: j进位标志, Carry Flag 279 | 280 | 如果把这两个操作数看作有符号数-126和-8相加,计算结果是错的,因为产生了溢出;但如果看作无符号数130和248相加,计算结果是122进1,也就是122+256,这个结果是对的。计算机的加法器在做完计算之后,根据最高位产生的进位设置进位标志(Carry Flag),同时根据最高位和次高位产生的进位的异或设置溢出标志。至于这个加法到底是有符号数加法还是无符号数加法则取决于程序怎么理解了,如果程序把它理解成有符号数加法,下一步就要检查溢出标志,如果程序把它理解成无符号数加法,下一步就要检查进位标志。 281 | 282 | .. index:: l零标志, Zero Flag, f负数标志, Negative Flag, Sign Flag 283 | 284 | 通常计算机在做算术运算之后还可能设置另外两个标志:如果计算结果的所有bit都是零则设置零标志(Zero Flag),如果计算结果的最高位是1则设置负数标志(Negative Flag或Sign Flag)。如果程序把计算结果理解成有符号数,也可以检查负数标志判断结果是正是负。 285 | 286 | .. _number.float: 287 | 288 | 浮点数 289 | -------------- 290 | 291 | .. index:: k科学计数法, Scientific Notation, w尾数, Mantissa, Significand, z指数, Exponent, j基数, Radix 292 | 293 | 浮点数在计算机中的表示是基于科学计数法(Scientific Notation)的,我们知道32767这个数用科学计数法可以写成3.2767×10\ :sup:`4` ,3.2767称为尾数(Mantissa,或者叫Significand),4称为指数(Exponent)。浮点数在计算机中的表示与此类似,只不过基数(Radix)是2而不是10。下面我们用一个简单的模型来解释浮点数的基本概念。我们的模型由三部分组成:符号位、指数部分(表示2的多少次方)和尾数部分(小数点前面总是0,尾数部分只表示小数点后的数字)。 294 | 295 | .. figure:: ../images/number.float.png 296 | 297 | 一种浮点数格式 298 | 299 | 如果要表示17这个数,我们知道17=17.0×100=0.17×10\ :sup:`2` ,类似地,17=(10001)\ :sub:`2`\ ×2\ :sup:`0`\ =(0.10001)\ :sub:`2`\ ×2\ :sup:`5` ,这样就可以表示为: 300 | 301 | .. figure:: ../images/number.float17.png 302 | 303 | 17的浮点数表示 304 | 305 | .. index:: p偏移的指数, Biased Exponent 306 | 307 | 如果我们要表示0.25就遇到新的困难了,因为0.25=1×2\ :sup:`-2`\ =(0.1)\ :sub:`2`\ ×2\ :sup:`-1` ,而我们的模型中指数部分没有规定如何表示负数。我们可以在指数部分规定一个符号位,然而更广泛采用的办法是使用偏移的指数(Biased Exponent)。规定一个偏移值,比如16,实际的指数要加上这个偏移值再填写到指数部分,这样比16大的就表示正指数,比16小的就表示负指数。要表示0.25,指数部分应该填16-1=15: 308 | 309 | .. figure:: ../images/number.biasfloat025.png 310 | 311 | 0.25的偏移指数浮点数表示 312 | 313 | .. index:: z正规化, Normalize, y隐含的, Implied 314 | 315 | 现在还有一个问题需要解决:每个浮点数的表示都不唯一,例如17=(0.10001)\ :sub:`2`\ ×2\ :sup:`5`\ =(0.010001)\ :sub:`2`\ ×2\ :sup:`6` ,这样给计算机处理增加了复杂性。为了解决这个问题,我们规定尾数部分的最高位必须是1,也就是说尾数必须以0.1开头,对指数做相应的调整,这称为正规化(Normalize)。由于尾数部分的最高位必须是1,这个1就不必保存了,可以节省出一位来用于提高精度,我们说最高位的1是隐含的(Implied)。这样17就只有一种表示方法了,指数部分应该是16+5=21=(10101)\ :sub:`2` ,尾数部分去掉最高位的1是0001: 316 | 317 | .. figure:: ../images/number.normalfloat17.png 318 | 319 | 17的正规化尾数浮点数表示 320 | 321 | 两个浮点数相加,首先把小数点对齐然后相加: 322 | 323 | .. figure:: ../images/number.addfloat.png 324 | 325 | 浮点数相加 326 | 327 | .. index:: j精度损失, Significance Loss 328 | 329 | 由于浮点数表示的精度有限,计算结果末尾的10两位被舍去了。做浮点运算时要注意精度损失(Significance Loss)的问题,有时计算顺序不同也会导致不同的结果。比如: 330 | 331 | 11.0010000+0.00000001+0.00000001=11.0010000+0.00000001=11.0010000 332 | 333 | 后面加的两个很小的数全被舍去了,没有对计算结果产生任何影响。但如果调一下计算顺序它们就能影响到计算结果了: 334 | 335 | 0.00000001+0.00000001+11.0010000=0.00000010+11.0010000=11.0010001 336 | 337 | 再比如128.25=(10000000.01)\ :sub:`2` ,需要10个有效位,而我们的模型中尾数部分是8位,算上隐含的最高位1一共有9个有效位,那么128.25的浮点数表示只能舍去末尾的1,表示成(10000000.0)\ :sub:`2` ,其实跟128相等了。 338 | 339 | .. index:: s上溢, x下溢 340 | 341 | 整数运算会产生溢出,浮点运算也会产生溢出,浮点运算的溢出也分上溢和下溢两种,但和整数运算的定义不同。假设整数采用8位2's Complement表示法,取值范围是-128~127,如果计算结果是-130则称为下溢,计算结果是130则称为上溢。假设按本节介绍的浮点数表示法,取值范围是-(0.111111111)\ :sub:`2`\ ×2\ :sup:`15`\ ~(0.111111111)\ :sub:`2`\ ×2\ :sup:`15` ,如果计算结果超出这个范围则称为上溢;如果计算结果未超出这个范围但绝对值太小了,在-(0.1)\ :sub:`2`\ ×2\ :sup:`-16`\ ~(0.1)\ :sub:`2`\ ×2\ :sup:`-16` 之间,那么也同样无法表示,称为下溢。 342 | 343 | .. index:: IEEE, Institute of Electrical and Electronics Engineers, IEEE 754 344 | 345 | 浮点数是一个相当复杂的话题,不同平台的浮点数表示和浮点运算在实现上也有较大差异,本节只是通过这个简单的模型介绍一些基本概念而不深入讨论,理解了这些基本概念有助于你理解浮点数标准,目前业界广泛采用的浮点数标准是由IEEE(Institute of Electrical and Electronics Engineers)制定的IEEE 754。 346 | 347 | 在 :ref:`cond.bool` 讲过浮点数不能做精确比较,现在读者应该知道为什么不能精确比较了:首先,浮点数的精度有限;其次,浮点数是用二进制的科学计数法表示的,通常不能精确地表示十进制数的小数部分。本节前面举例的时候总是用十进制的0.25,如果换成十进制的0.1就没法精确地用二进制小数来表示了。我们把 :ref:`cond.bool` 的例子拿过来再研究一下: 348 | 349 | .. code-block:: c 350 | :linenos: 351 | 352 | #include 353 | 354 | int main(void) 355 | { 356 | double a = 0.3 - 0.2; 357 | double b = 0.2 - 0.1; 358 | if (a == b) 359 | printf("Equal\n"); 360 | else 361 | printf("Unequal\n"); 362 | 363 | printf("a=%.18f\n", a); 364 | printf("b=%.18f\n", b); 365 | return 0; 366 | } 367 | 368 | 其中 ``printf`` 的转换说明 ``%.18f`` 表示打印时保留小数点后18位的精度。运行结果是:: 369 | 370 | Unequal 371 | a=0.099999999999999978 372 | b=0.100000000000000006 373 | 374 | 最后讨论一个细节问题。我们知道,定义全局变量时如果没有Initializer就用0初始化,定义数组时如果Initializer中提供的元素不够那么剩下的元素也用0初始化。例如: 375 | 376 | .. code-block:: c 377 | :linenos: 378 | 379 | int i; 380 | double d; 381 | double a[10] = { 1.0 }; 382 | 383 | “用0初始化”的意思是变量 ``i`` 、变量 ``d`` 和数组元素 ``a[1]~a[9]`` 的所有字节都用0填充,或者说所有bit都是0。无论是用Sign and Magnitude表示法、1's Complement表示法还是2's Complement表示法,一个整数的所有bit是0都表示0值,但一个浮点数的所有bit是0一定表示0值吗?严格来说不一定,某种平台可能会规定一个浮点数的所有bit是0并不表示0值,但 [C99Rationale]_ 第6.7.8节提到: 384 | 385 | As far as the committee knows, all machines treat all bits zero as a representation of floating-point zero. But, all bits zero might not be the canonical representation of zero. 386 | 387 | 因此在绝大多数平台上,一个浮点数的所有bit是0就表示0值。 388 | -------------------------------------------------------------------------------- /zh/op.rst: -------------------------------------------------------------------------------- 1 | 运算符详解 2 | ================ 3 | 4 | .. _op.bitop: 5 | 6 | 位运算 7 | ----------- 8 | 9 | .. _op.compoundassign: 10 | 11 | 复合赋值运算符 12 | --------------- 13 | 14 | .. _op.op: 15 | 16 | 运算符总结 17 | -------------- 18 | -------------------------------------------------------------------------------- /zh/pointer.rst: -------------------------------------------------------------------------------- 1 | 指针 2 | ======= 3 | 4 | .. _pointer.pointer: 5 | 6 | 指针的基本概念 7 | ----------------- 8 | 9 | .. _pointer.pointertoconst: 10 | 11 | 指针与const限定符 12 | ------------------------ 13 | 14 | .. _pointer.pointertopointer: 15 | 16 | 指向指针的指针与指针数组 17 | ------------------------- 18 | 19 | .. _pointer.type: 20 | 21 | .. figure:: ../images/pointer.type.gif 22 | 23 | C语言类型总结 24 | -------------------------------------------------------------------------------- /zh/preface.rst: -------------------------------------------------------------------------------- 1 | 前言 2 | ========= 3 | 4 | 本书最初是为北京亚嵌教育研究中心的嵌入式Linux系统工程师就业班课程量身定做的教材之一。该课程是为期四个月的全日制职业培训,要求学员毕业时具备非常扎实的C编程能力,能熟练地使用Linux系统,同时对计算机体系结构与指令集、操作系统原理和设备驱动程序都有较深入的了解。然而学员入学时的水平是非常初级而且参差不齐的:学历有专科、本科也有研究生,专业有和计算机相关的也有很不相关的(例如会计专业),以前从事的职业有和技术相关的也有完全不相关的(例如HR),年龄从二十出头到三十五六岁的都有。这么多背景完全不同、基础完全不同、思维习惯和理解能力完全不同的人来听同一堂课,大家都迫切希望学会嵌入式开发技术,投身IT行业,这就是职业教育的特点,也是我编写本书时需要考虑的主要问题。 5 | 6 | 学习编程绝不是一件简单的事,尤其对于零基础的初学者来说更不简单。大学的计算机专业有四年时间从零基础开始培养一个人,微积分、线性代数、随机数学、离散数学、组合数学、自动机、编译原理、操作系统、计算机组成原理等等一堆基础课,再加上C/C++、Java、数据库、网络、软件工程、计算机图形学等等一堆专业课,最后培养出一个能找到工作的学生。很遗憾这最后一条很多学校没有做好,来亚嵌培训的很多学生就是四年这么学过来的,却还是需要参加一个职业培训才能找到比较理想的工作。与之形成鲜明对比的是,只给我们四个月的时间,同样要求从零基础开始,最后培养出一个能找到工作的学生,而且还要保证他找到工作,这就是职业教育的特点。 7 | 8 | 为什么我说“只给我们四个月的时间”?我们倒是想教四年呢,但学时的长短我们做不了主,是由市场规律决定的。四年的任务要求四个月做好,要怎么完成这样一个几乎不可能的任务?有些职业教育给出的答案是“实用主义”,打出了“有用就学,没有用就不学”的口号,大肆贬低说大学里教的基础课都是过时的、无用的,只有他们教的技术才是实用的,这种炒作很不好,我认为大学里教的每一门课都是非常有用的,基础知识在任何时候都不会过时,倒是那些时髦的“实用技术”有可能很快就过时了。 9 | 10 | 四年的任务怎么才能用四个月做好?我们给出的答案是“优化”。 11 | 12 | 现在大学里安排的课程体系最大的缺点就是根本不考虑优化。每个过来人都会有这样的感觉:大一大二学了好多数学课,却不知道都是干什么用的,为什么要学。连它有什么用都不知道怎么能有兴趣学好呢?然后到大三大四学专业课时,用到以前的知识了,才发现以前学的数学是多么有用,然而早就忘得一干二净了,考完试都还给老师了,回头重新学吧,这时候才发现很多东西以前根本没学明白,现在才真的学明白了,那么前两年的时间岂不是都浪费了? 13 | 14 | 大学里的课程体系还有一个缺点就是不灵活,每门课必须占一个学期,必须由一个老师教,不同课程的老师之间没有任何沟通和衔接。其实这些课程之间是相互依赖的,把它们强行拆开是不符合认知规律的。比如我刚上大学的时候,大一上半学期就被逼着学C语言,其实C语言是一门很难的编程语言,不懂编译原理、操作系统和计算机体系结构根本不可能学明白,那半个学期自然就浪费掉了。当时几乎所有学校的计算机相关专业都是这样,大一上来就学C语言(有的学校更疯狂,大一上来就学C++),学生们都在大一时就“学会”了C语言,但其实都是半吊子水平,到了大三大四真正写代码的时候经常为一个Bug搞得焦头烂额,却没有机会再系统地学一遍C语言,因为在学校看来,C语言课早在大一就给你“上完了”,就像一顿饭已经吃完了,不管你吃饱没吃饱,不会再让你重吃一遍了。 15 | 16 | 显而易见,如果认真地对这些计算机课程做优化,理清各门课程之间的依赖关系,的确是有很多水份可以挤的。我们正是致力于缩短学生的学习时间,而学习效果不仅不打折扣,还要更好,因为我们把各门课程的知识系统地串联起来了。 17 | 18 | 本书有什么特点 19 | --------------- 20 | 21 | * 本书不是孤立地讲C语言,而是和编译原理、操作系统、计算机体系结构结合起来讲。或者说,本书的内容只是以C语言为载体,真正讲的是计算机的原理和程序的原理。 22 | 23 | * 强调基本概念和基本原理,在编排顺序上非常重视概念之间的依赖关系,每次引入一个新的概念,只依赖于前面章节已经讲过的概念,而绝不会依赖后面章节要讲的概念。有些地方为了叙述得完整,也会引用后面要讲的内容,比如说“有关XX我们到第X章再仔细讲解”,凡是这种引用都不是必要的依赖,可以当它不存在,只管继续往下看就行了。 24 | 25 | * 尽量做到每个知识点直到要用的时候才引入。过早引入一个知识点,讲完了又不用它,读者很快就会遗忘,这是不符合认知规律的。 26 | 27 | 本书面向什么样的读者 28 | -------------------- 29 | 30 | 这是一本从零基础开始学习编程的书,不要求读者有任何编程经验,但读者至少需要具备以下素质: 31 | 32 | * 熟悉Linux系统的基本操作。如果不具备这一点,请先参考其他教材学习Linux系统的基本操作,熟练之后再学习本书,比如《鸟哥的Linux私房菜》就是Linux系统管理和应用方面比较好的一本书。但学习本书并不需要会很多系统管理技术,只要会用基本命令,会自己安装系统和软件包就足够了。 33 | 34 | * 具有高中毕业的数学水平。本书会用到高中的数学知识。事实上,如果不具有高中毕业的数学水平,也不必考虑做程序员了。但并不是说只要具有高中毕业的数学水平就足够做程序员了,只能说看这本书应该没有问题,数学是程序员最重要的修养,计算机科学其实就是数学的一个分支,如果你的数学功底很差,日后还需恶补一下。 35 | 36 | * 具有高中毕业的英文水平。理由同上。 37 | 38 | * 对计算机的原理和本质深感兴趣,不是为就业而学习,不是为拿高薪而学习,而是真的感兴趣,想把一切来龙去脉搞得清清楚楚而学习。 39 | 40 | * 勤于思考。本书尽最大努力理清概念之间的依赖关系,力求一站式学习,读者不需要为了找一个概念的定义去翻其他书,也不需要为了搞清楚一个概念在本书中前后一通乱翻,只需从前到后按顺序学习即可。但一站式学习并不等于傻瓜式学习,有些章节有一定的难度,需要积极思考才能领会。本书可以替你节省时间,但不能替你思考,不要指望像看小说一样走马观花看一遍就能学会。 41 | 42 | 为什么要学这本书而不是K&R? 43 | ------------------------------- 44 | 45 | [K&R]_ 是公认的世界上最经典的C语言教程,这点毫无疑问。在C标准出台之前, [K&R]_ 第一版就是事实上的C标准。C89标准出台之后, [K&R]_ 跟着标准推出了第二版,可惜此后就没有更新过了,所以不能反映C89之后C语言的发展以及最新的C99标准,本书在这方面做了很多补充。上面我说过了,这本书与其说是讲C语言,不如说是以C语言为载体讲计算机和操作系统的原理,而 [K&R]_ 只是为了讲C语言而讲C语言,侧重点不同,内容编排也很不相同。 [K&R]_ 写得非常好,代码和语言都非常简洁,但很可惜,只有已经学会了C语言的人才懂得欣赏它, [K&R]_ 是非常不适合入门学习的,尤其不适合零基础的学生入门学习。 46 | 47 | 本书“是什么”和“不是什么” 48 | ------------------------- 49 | 50 | .. 按目前的规划,已经不止两部分了 51 | 52 | 本书包括两大部分: 53 | 54 | * C语言入门。介绍基本的C语法,帮助没有任何编程经验的读者理解什么是程序,怎么写程序,培养程序员的思维习惯,找到编程的感觉。其中前六章改编自 [ThinkCpp]_ 。 55 | 56 | * C语言本质。结合计算机和操作系统的原理讲解C程序是怎么编译、链接、运行的,同时全面介绍C的语法。 :ref:`op.bitop` 改编自林小竹老师的讲义, :ref:`linkedlist.linkedlist` 和 :ref:`linkedlist.binarytree` 改编自朱仲涛老师的讲义, :doc:`asm` 改编自 [GroundUp]_ 。 57 | 58 | 这本书定位在入门级,虽然内容很多,但不是一本百科全书,除了把C语言基本讲透之外其他内容都不深入,书后列出了很多参考书目,是读者进一步学习的起点。很多初学者看到网上一些老鸟推荐的书单,以为只要一本一本啃下来就能成为高手了,但很快就发现想迈出第一步都很难--该从哪一本开始啃起呢?本书的参考书目也是我列的书单,但我所做的不只是列书单而已。 [K&R]_ 的第1章是一个Whirlwind Tour,把全书的内容简单过一遍,然后再逐个深入进去讲解,本书也可以看作是计算机专业课程体系的一个Whirlwind Tour,学完之后读者应该会有一个全局观,下一步该啃哪本书自己心里就有数了。 59 | 60 | 为什么要在Linux平台上学C语言?用Windows学C语言不好吗? 61 | ------------------------------------------------------- 62 | 63 | 用Windows还真的是学不好C语言。C语言是一种面向底层的编程语言,要写好C程序,必须对操作系统的工作原理非常清楚,因为操作系统也是用C写的,我们用C写应用程序直接使用操作系统提供的接口。Linux是一种开源的操作系统,你有任何疑问都可以从源代码和文档中找到答案,即使你看不懂源代码也找不到文档,也很容易找个高手教你,各种邮件列表、新闻组和论坛上从来都不缺乐于助人的高手;而Windows是一种封闭的操作系统,除了微软的员工别人都看不到它的源代码,只能通过文档去猜测它的工作原理,更糟糕的是,微软向来喜欢藏着掖着,好用的功能留着自己用,而不会写到文档里公开。 64 | 65 | .. index:: j集成开发环境, IDE, Integrated Development Environment 66 | 67 | Windows平台上的开发工具往往和各种集成开发环境(IDE,Integrated Development Environment)绑在一起,例如Visual Studio、Eclipse等。使用IDE确实很便捷,但IDE对于初学者绝对不是好东西。微软喜欢宣扬傻瓜式编程的理念,告诉你用鼠标拖几个控件然后点一个按钮就可以编译出程序来,但是真正有用的程序有哪个是这么拖出来的?很多从Windows平台入门学编程的人,编了好几年程序,还是只知道编完程序点一个按钮就可以跑了,把几个源文件拖到一个项目里就可以编译到一起了,如果有更复杂的需求他们就傻眼了,因为他们脑子里只有按钮、菜单的概念,根本没有编译器、链接器、Makefile的概念,甚至连命令行都没用过,然而这些都是初学编程就应该建立起来的基本概念。另一方面,编译器、链接器和C语言的语法有密切的关系,不了解编译器、链接器的工作原理就不可能真正掌握C的语法。 68 | 69 | 所以,IDE并没有帮助你学习,反而阻碍了你学习,本来要学好C编程只要把语法和编译命令学会就行了,现在有了IDE,除了学会语法和编译命令,你还得弄清楚编译命令和IDE是怎么集成的,这才算学明白了,本来就很复杂的学习任务被IDE搞得更加复杂了。Linux用户的使用习惯从来都是以敲命令为主,以鼠标操作为辅,从学编程的第一天起就要敲命令编译程序,等到你把这些基本概念都搞清楚了,你觉得哪个IDE好用你再去用,不过到那时候你可能会更喜欢vi或emacs而不是IDE了。 70 | 71 | 体例说明 72 | ---------- 73 | 74 | 像 ``The quick brown fox jumps over the lazy dog`` 这样的字体在本书中是代码字体。这种字体的名称是Dejavu Sans Mono(很遗憾本书的在线版本无法保证读者的浏览器看到的字体一定是这种),为什么我要提倡用这种字体呢? 75 | 76 | #. 它是等宽字体,因此适合做代码字体。 77 | #. 它的数字 ``1`` 和小写字母 ``l`` 、数字 ``0`` 和大写字母 ``O`` 区分得非常清楚(我在教学中发现初学者很容易把这些字符抄错),因此它比Courier New更适合做代码字体。 78 | #. 它是我的Linux图形终端的默认字体,采用这种字体排版可以使得看书和看屏幕的感觉很一致,希望读者在看这本书时也会有这种Dejavu(似曾相识)的感觉。 79 | 80 | 代码的示例如下: 81 | 82 | .. code-block:: bash 83 | :linenos: 84 | 85 | #! /bin/sh 86 | VAR=1 87 | VAR=$(($VAR+1)) 88 | echo $VAR 89 | 90 | 终端显示的示例如下,包括Shell提示符、输入的命令和运行结果:: 91 | 92 | $ /bin/sh script.sh 93 | 2 94 | 95 | 本书中统一用$表示Shell提示符。 96 | 97 | **加粗** 的字句表示强调。 98 | 99 | 在定义一个名词时会给出它的英文名称,例如“集成开发环境(IDE,Integrated Development Environment)”,通过书后的索引可以找到每个名词在书中的哪个位置被定义或者首次出现。 100 | 101 | 致谢 102 | ------- 103 | 104 | 本书的写作得到北京亚嵌教育研究中心的全力支持,尤其感谢李明老师和何家胜老师,没有公司的支持,我不可能有时间有条件写这本书,也不可能有机会将这本书公开在网上。 105 | 106 | 然后要感谢亚嵌教育的历届学员和各位老师,在教学和讨论过程中我经常会得到有益的启发,这些都促使这本书更加完善。在本书的写作过程中,很多读者为本书提出很有价值的建议,很多建议是热心网友通过在线评论提的,在此向他们表示感谢。 107 | 108 | 感谢帮助过我的老师们:李明,何家胜,邸海霞,郎铁山,朱仲涛,廖文江,韩超,秦蔚,吴岳,张頔,邢文鹏,何晓龙,林小竹,卫剑钒,郭同彬,王波,王磊,洪峰。 109 | 110 | 还要感谢电子工业出版社博文视点资讯有限公司李冰老师的大力支持。 111 | 112 | 在写作过程中我遇到过很多困难,工作繁忙,对未来迷茫,生活压力大,缺乏安全感,个人琐事等等。然而有这么多热心的同学、老师、朋友、网友在等着看我的书在线更新,给我提建议希望我把书改得更完善,这是我坚持写下去的最大的动力。谢谢你们! 113 | 114 | 由于我的水平十分有限,没写过C编译器和C标准库,所以疏漏之处在所难免,如有错误欢迎批评指正。写书是一件严肃的事,书中的错误所有人都看得见,白纸黑字赖不掉的。我教过的很多学生都在大学里学过C语言,甚至考过二级,但程序写得一塌糊涂,连最基本的概念都搞错了,以前学过的C语言教材中的错误在他们脑子里根深蒂固,即使我纠正多次,他们仍然只记得以前学过的错误概念,这种有基础的学生还不如没有任何基础的学生教起来容易。我非常害怕我教给别人的仍然是错的,所以我仔细研究了C99之后才敢动笔写书。这本书涵盖的话题比较广泛,我竭尽全力也不足以保证书中的内容全部正确,还要依靠社区的力量一起来完善这本书,这样才能真正对读者负责,所以我选择将这本书开源。 115 | 116 | 希望本书成为你求学道路上的第一个伙伴。 117 | 118 | 宋劲杉 北京 2012年7月10日 119 | -------------------------------------------------------------------------------- /zh/prep.rst: -------------------------------------------------------------------------------- 1 | 预处理 2 | =============== 3 | 4 | 5 | .. _prep.otherfeature: 6 | 7 | 其它预处理特性 8 | ----------------------- 9 | -------------------------------------------------------------------------------- /zh/stdlib.rst: -------------------------------------------------------------------------------- 1 | C标准库 2 | ============= 3 | 4 | 字符串操作函数 5 | ---------------- 6 | 7 | .. _stdlib.comparestring: 8 | 9 | 比较字符串 10 | ^^^^^^^^^^^^^^^^^^^^^^^^ 11 | 12 | .. _stdlib.errno: 13 | 14 | errno与perror/strerror函数 15 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 16 | 17 | .. _stdlib.formattedio: 18 | 19 | 格式化I/O函数 20 | ^^^^^^^^^^^^^^^^^^^^^^^^ 21 | -------------------------------------------------------------------------------- /zh/summary1.rst: -------------------------------------------------------------------------------- 1 | 本阶段总结 2 | ===================== 3 | 4 | 善于学习的人都应该善于总结。本书的编排顺序充分考虑到知识的前后依赖关系,保证在讲解每个新知识点的时候都只用到前面章节讲过的知识,但正因为如此,很多相互关联的知识点被拆散到多个章节中了。我们一章一章地纵向学习过来之后,应该理出几个横切面,把拆散到各章节中的知识点串起来。请从以下几个方面整理和复习。 5 | 6 | #. C的语法规则 7 | 8 | * 源文件中所有函数定义之外可以出现哪些语法元素? 9 | * 函数定义之中可以出现哪些语法元素? 10 | * 语句有哪几种? 11 | * 哪些语法元素需要遵循标识符的命名规则? 12 | * 表达式由哪些语法元素组成? 13 | * 到目前为止学过哪些运算符?它们的优先级和结合性是怎样的? 14 | * 哪些运算符取操作数的左值?哪些运算符有Side Effect? 15 | * 哪些运算符的操作数必须是整型?哪些运算符的操作数必须是算术类型?哪些运算符的操作数必须是标量类型? 16 | * 哪些表达式可以做左值?哪些表达式只能做右值? 17 | * 哪些地方必须用常量表达式?哪些地方必须用整型常量表达式? 18 | 19 | #. 思维方法与编程思想 20 | 21 | * 组合规则, :ref:`expr.expression` 22 | * Least Surprise, :ref:`func.parameter` 23 | * 充分条件与必要条件, :ref:`func.scope` 24 | * 封装, :ref:`cond.ifelse` 25 | * 布尔逻辑, :ref:`cond.bool` 26 | * 递归, :ref:`func2.recurse` 27 | * 函数式编程, :ref:`iter.while` 28 | * 迭代( :doc:`iter` )与增量式求解( :ref:`sortsearch.insertion` ) 29 | * 抽象, :ref:`struct.dataabstraction` 30 | * 避免硬编码, :ref:`array.statrandom` 31 | * 数据驱动, :ref:`array.multidim` 32 | * 分而治之, :ref:`sortsearch.mergesort` 33 | * 折半查找, :ref:`sortsearch.binarysearch` 34 | * 回溯, :ref:`stackqueue.dfs` 35 | 36 | #. 调试方法 37 | 38 | * 编译错误、运行时错误与语义错误, :ref:`intro.debug` 39 | * 增量式开发, :ref:`func2.incremental` 40 | * 打印语句与Scaffold, :ref:`func2.incremental` 41 | * gdb, :doc:`gdb` 42 | * DbC与Assertion, :ref:`sortsearch.binarysearch` 43 | -------------------------------------------------------------------------------- /zh/type.rst: -------------------------------------------------------------------------------- 1 | 数据类型详解 2 | ================ 3 | 4 | .. _type.integer: 5 | 6 | 整型 7 | --------- 8 | 9 | .. _type.conversion: 10 | 11 | 类型转换 12 | ------------- 13 | --------------------------------------------------------------------------------