rhl6u300

Trochę ciekawostek – na weekend (czego to ludzie nie wymyślą ...


Red Hat® Linux 6 Unleashed










Chapter 30: C and C++ Programming





Previous
ChapterNext
Chapter










Sections in this Chapter:












Background
on the C Language





Project
Management Tools





GNU C/C++
Compiler Command-Line Switches






Programming in C: Basic Concepts





Elements
of the C++ Language





New Features
of the GNU egcs Compiler System






Creating,
Compiling, and Executing Your First Program





Programming
in C++: Basic Concepts





Additional
Resources






Elements
of the C Language





Classes
in C++











 

Previous
SectionNext
Section





Programming in C: Basic Concepts



Functions in
C Programs












C is a compiled,
third-generation procedural
language. Compiled means that C code is analyzed, interpreted, and
translated into machine instructions at some time prior to the execution of the
C program. These steps are carried out by the C compiler and, depending on the
complexity of the C program, by the make utility.
After the program is compiled, it can be executed over and over without
recompilation.The phrase third-generation
procedural
describes computer languages that clearly distinguish the data used in a program
from the actions performed on that data. Programs written in third-generation
languages take the form of a series of explicit processing steps, or procedures.
These procedures manipulate the contents of data structures by means of explicit
references to their locations in memory and manipulate the computer's
hardware in response to
hardware
interrupts.



Functions in C Programs
In the C language,
all
procedures take the form of functions. Just as a mathematical function
transforms input data, a C function is typically a procedure that transforms
some value (or input values, known as arguments)
or performs some other action and returns the results. The act of invoking the
transformation is known as calling the
function.Mathematical
function calls can be nested, as can function calls in C. When function calls
are nested, the results of the innermost function are passed as input to the
next function, and so on. Table 30.1 shows how nested calls to the square root
function are evaluated arithmetically.



TABLE 30.1  Nested
Operations
in Mathematics



Function




Value






sqrt(256)




16






sqrt( sqrt(256) ) =
sqrt(16)




4






sqrt( sqrt( sqrt(256) ) ) =
sqrt(4)




2






shows the
way function calls are nested within C programs. In the figure, the Main
function calls Function 1, which calls Function
2. Function 2 is evaluated first, and its
results are passed back to Function 1. When Function
1 completes its operations, its results are passed back to the Main
function.


FIGURE
30.1
Nesting
function
calls within C programs.
Nonfunctional procedures in other languages often operate on
data variables that are shared with other code in the program. For instance, a
nonfunctional procedure might update a program-wide
COUNT_OF_ERRORS whenever a user makes a keyboard
mistake. Such procedures must be carefully written, and they are usually
specific to the program for which they were first created because they reference
particular shared data variables within the wider
program.A function, however, receives all the
information it needs when it is called (including the location of data variables
to use in each instance). The function neither knows nor cares about the wider
program context that calls it. It simply transforms the values found within the
input variables (parameters), whatever they might be, and returns the results to
whichever other function invokes it.
Because procedures written in C are implemented as functions,
they don't need to know whether (or how deeply) they will be nested inside
other function calls. This enables you to reuse C functions in many different
programs without modifying them. For example, Function
2 in might be called
directly by the Main logic in a different C program.
An entire C program is itself a
function that returns a result code, when executed, to the program that invokes
it. This is usually a shell in the case of applications, but it might also be
any other part of the operating system or any other UNIX program. Because C
programs are all structured as functions, they can be invoked by other programs
or nested inside larger programs without needing to be rewritten
in
any way.


Note -
C's featuring of structuring programs as functions has heavily shaped the
look and feel of UNIX. More than in most other operating environments, UNIX
systems consist of many
small
C programs that call one another, are combined into larger programs, and are
invoked by the user as needed. Instead of using monolithic, integrated
applications, UNIX typically hosts many small, flexible programs. Users can
customize their working environments by combining these tools to do new
tasks.


Data in C Programs
The two kinds
of
data that are manipulated within C programs are literal
values and
variables. Literal
values are specific, actual numbers or characters, such as
1, 4.35, or
a. Variables are names associated with a
place in memory that can hold data values. Each variable in C is typed; that is,
it can hold only one kind of value. The basic datatypes include integers,
floating-point (real) numbers, characters, and arrays. An array is a
series of data elements of the same type; the elements are identified by the
order in which they appear (in other words, by their place within the
series).You can define complex data structures as
well. Complex data structures are used to gather a number of related data items
together under one name. A terminal communications program, for example, might
have a terminal control block
(TCB) associated with each
user who is logged on. The TCB typically contains data elements identifying the
communications port, the active application process, and other information
associated with that terminal session.
All the variables in a C program must
be explicitly defined before they can be used.





Red Hat® Linux 6 Unleashed










Chapter 30: C and C++ Programming





Previous
ChapterNext
Chapter










Sections in this Chapter:












Background
on the C Language





Project
Management Tools





GNU C/C++
Compiler Command-Line Switches






Programming in C: Basic Concepts





Elements
of the C++ Language





New Features
of the GNU egcs Compiler System






Creating,
Compiling, and Executing Your First Program





Programming
in C++: Basic Concepts





Additional
Resources






Elements
of the C Language





Classes
in C++











 

Previous
SectionNext
Section





© Copyright Macmillan USA. All rights reserved.

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • strefamiszcza.htw.pl
  • Copyright (c) 2009 Trochę ciekawostek – na weekend (czego to ludzie nie wymyślą ... | Powered by Wordpress. Fresh News Theme by WooThemes - Premium Wordpress Themes.