C Programming [Lee 2008].pdf

(2166 KB) Pobierz
First Edition, 2007
ISBN 978 81 89940 36 2
© All rights reserved.
Published by:
Global Media
1819, Bhagirath Palace,
Chandni Chowk, Delhi-110 006
Email:
globalmedia@dkpd.com
Table of Contents
1.
Introduction
2.
Basic Components
3.
Simple Output and Input
4.
C's Built-In Functions
5.
Standard Libraries
6.
Some Tips for C
7.
In Depth C Ideas
8.
Memory Management
9.
Networking in Unix
10.
FAQ's
11.
Macro Conventions
12.
Code Library
Introduction
Why Learn C?
The most popular Operating Systems right now are Microsoft Windows, Mac OS X, and /Linux.
Each is written in C. Why? Because Operating Systems run directly on top of the hardware. There
is no lower layer to mediate their requests. Originally, this OS software was written in the
Assembly language, which results in very fast and efficient code. However, writing an OS in
Assembly is a tedious process, and produces code that will only run on one CPU architecture,
such as the Intel X86 or AMD64. Writing the OS in a higher level language, such as C, lets
programmers re-target the OS to other architectures without re-writing the entire code.
But why 'C' and not Java or Basic or Perl? Mostly because of memory allocation. Unlike most
computer languages, C allows the programmer to address memory the way he/she would using
assembly language. Languages like Java and Perl shield the programmer from having to worry
about memory allocation and pointers. This is usually a good thing. It's quite tedious to deal with
memory allocation when building a high-level program like a quarterly income statement report.
However, when dealing with low level code such as that part of the OS that moves the string of
bytes that makes up that quarterly income report from the computer's memory to the network
card's buffer so they can be shipped to the network printer, direct access to memory is critical --
something you just can't do with Java. C can be compiled into fast and efficient machine code.
So is it any wonder that C is such a popular language?
Like toppling dominoes, the next generation of programs follows the trend of its ancestors.
Operating Systems designed in C always have system libraries designed in C. Those system
libraries are in turn used to create higher-level libraries (like OpenGL, or GTK), and the designers
of those libraries often decide to use the language the system libraries used. Application
developers use the higher-level libraries to design word processors, games, media players, and the
like. Many of them will choose to program in the language that higher-level library uses. And the
pattern continues on and on and on...
Why learn C? | What you need before you can learn
History of the C Programming Language
In 1947, three scientists at Bell Telephone Laboratories, William Shockley, Walter Brattain, and
John Bardeen created the transistor. Modern computing was beginning. In 1956 at MIT the first
fully transistor based computer was completed, the TX-0. In 1958 at Texas Instruments, Jack
Kilby created the first integrated circuit. But even before the first integrated circuit existed, the
first high level language had already been written.
In 1954 Fortran, the Formula Translator, had been written. It began as Fortran I in 1956. Fortran
begot Algol 58, the Algorithmic Language, in 1958. Algol 58 begot Algol 60 in 1960. Algol 60
begot CPL, the Combined Programming Language, in 1963. CPL begot BCPL, Basic CPL, in
1967. BCPL begot B in 1969. B begot C in 1971.
B was the first language in the C lineage directly, having been created at Bell Labs by Ken
Thompson. B was an interpreted language, used in early, internal versions of the UNIX operating
system. Thompson and Dennis Ritchie, also of Bell Labs, improved B, calling it NB; further
extensions to NB created C, a compiled language. Most of UNIX was rewritten in NB and then C,
which led to a more portable operating system.
B was of course named after BCPL, and C was its logical successor.
The portability of UNIX was the main reason for the initial popularity of both UNIX and C. So
rather than creating a new operating system for each new machine, system programmers could
simply write the few system-dependent parts required for the machine, and write a C compiler for
the new system. Thereafter since most of the system utilities were written in C, it simply made
sense to also write new utilities in that language.
History | Using a Compiler
Getting Started
This book is intended to be an introduction to C programming. Although some basic computer
literacy is assumed, no special knowledge is needed.
The minimum software required to start programming in C is a
text editor
to create C source
files, and
C compiler
to turn those source files into executable programs.
Many programmers, however, prefer to use a
IDE
(Integrated development environments). This
is a program which combines editing, compiling and debugging into a convenient all-in-one
interface. There are a variety of these available on almost every computer platform. Some can be
downloaded free-of-charge while others are commercial products.
C Compilers:
OpenWatcom
Borland C
Compiler
C Compiler
Platform
DOS, Windows, Netware,
[1]
OS/2
[2] Windows
[3]
License
Open
source
Freeware
Extra
DOS, Cygwin (w32), MinGW Open
(w32)OS/2, Mac OS X, Unix source
Tiny C Compiler
Open
[4] /Linux, Windows
(tcc)
source
IDEs:
De facto standard. Ships
with most Unix systems.
Small, fast compiler
CDT
Platform License
Extra
Windows,
A C/C++ plug-in for Eclipse, a popular open
[5]
Open source
Mac OS X,
source IDE.
Zgłoś jeśli naruszono regulamin