Tuesday, December 21, 2010

Paypal in Bangladesh

Bangladesh doesn't support the most popular internet transfer medium paypal still now. So Bangladeshi Freelancers facing problems in paypal transaction. Thinking about paypal solution in bangladesh. You can trust on Paypal Bangladesh . Paypal bangladesh is a reliable and instant organization of Bangladesh providing paypal solution.

Monday, December 20, 2010

cbulk link partners

Web Design | Web Development | Mobile Applications

At Kinsh Technologies, we are aware how important it is to have a successful business. We implement web-solutions as well as software development for you to match your business needs. Joomla, Wordpress, Drupal, Magento, Zen-Cart, OS-Commerce, PHP, Dot Net Technologies, Mobile Technologies are the areas of our expertise and we assure quality and timely work.



SEO Indian Experts - Search Engine Optimization Services and Online Marketing

Read more: http://www.seoindianexperts.com


Tuesday, December 7, 2010

Useful link,article and source code directories

Millions of peoples are trying to get traffic to their websites and spending lots of money in this. Submit article to article directory is a good and popular way of getting SEO and traffic to website.There are lots of paid article directories available in web but a very few free. This software give you chance to submit your article to world's best free article directory article.ognilab.com totally free of cost.

Submit Your article to world's best free article directory

This softwre can help you to find your most needed C,C++ source code from the C,C++ source code directory cprogram.org.You can also submit your source code in web to spread and test your knowledge on programming.Submit C,C++ source code to this directory to make it rich with source code and welfare of other programmers.


Millions of peoples are trying to get traffic to their websites and spending lots of money in this. Submit website link to Web directory is a good and popular way of getting SEO and traffic to website.There are lots of paid web directories available in web but a very few free. This software give you chance to submit your website link to world's best free web directory dir.ognilab.com totally free of cost.

Monday, October 4, 2010

C,C++ Tutorial : Unions

generally of different types, at different times. Declaring a union is similar to
declaring a structure. Its general form is

union union-type-name {
type member-name;
type member-name;
type member-name;
} union-variables;

For example:

union u_type {
int i;
char ch;
};

This declaration does not create any variables. You may declare a variable either
by placing its name at the end of the declaration or by using a separate declaration
statement. In C, to declare a union variable called cnvt of type u_type using the
definition just given, write
union u_type cnvt;
When declaring union variables in C++, you need use only the type name—
you don't need to precede it with the keyword union. For example, this is how
cnvt is declared in C++:
u_type cnvt;
In C++, preceding this declaration with the keyword union is allowed, but redundant.
In C++, the name of a union defines a complete type name. In C, a union name is its
tag and it must be preceded by the keyword union. (This is similar to the situation
with structures described earlier.) However, since the programs in this chapter are
valid for both C and C++, the C-style declaration form will be used.
In cnvt, both integer i and character ch share the same memory location. Of
course, i occupies 2 bytes (assuming 2-byte integers) and ch uses only 1. Figure 7-2
shows how i and ch share the same address. At any point in your program, you can
refer to the data stored in a cnvt as either an integer or a character.
When a union variable is declared, the compiler automatically allocates enough
storage to hold the largest member of the union. For example (assuming 2-byte
integers), cnvt is 2 bytes long so that it can hold i, even though ch requires only
1 byte.

To access a member of a union, use the same syntax that you would use for
structures: the dot and arrow operators. If you are operating on the union directly,
use the dot operator. If the union is accessed through a pointer, use the arrow
operator. For example, to assign the integer 10 to element i of cnvt, write
cnvt.i = 10;
In the next example, a pointer to cnvt is passed to a function:
void func1(union u_type *un)
{
un->i = 10; /* assign 10 to cnvt using
function */
}
Using a union can aid in the production of machine-independent (portable)
code. Because the compiler keeps track of the actual sizes of the union members,
no unnecessary machine dependencies are produced. That is, you need not worry
about the size of an int, long, float, or whatever.
Unions are used frequently when specialized type conversions are needed
because you can refer to the data held in the union in fundamentally different
ways. For example, you may use a union to manipulate the bytes that comprise a
double in order to alter its precision or to perform some unusual type of rounding.To get an idea of the usefulness of a union when nonstandard type conversions
are needed, consider the problem of writing a short integer to a disk file. The C/C++
standard library defines no function specifically designed to write a short integer to
a file. While you can write any type of data to a file using fwrite(), using fwrite()
incurs excessive overhead for such a simple operation. However, using a union you
can easily create a function called putw() , which writes the binary representation of
a short integer to a file one byte at a time. (This example assumes that short integers
are 2 bytes long.) To see how, first create a union consisting of one short integer and
a 2-byte character array:
union pw {
short int i;
char ch[2];
};
Now, you can use pw to create the version of putw() shown in the following program.
#include <stdio.h>
union pw {
short int i;
char ch[2];
};
int putw(short int num, FILE *fp);
int main(void)
{
FILE *fp;
fp = fopen("test.tmp", "wb+");
putw(1000, fp); /* write the value 1000 as an integer */
fclose(fp);
return 0;
}
int putw(short int num, FILE *fp)
{
union pw word;word.i = num;
putc(word.ch[0], fp); /* write first half */
return putc(word.ch[1], fp); /* write second half */
}
Although putw() is called with a short integer, it can still use the standard function
putc() to write each byte in the integer to a disk file one byte at a time.
 

**Note :C++ supports a special type of union called an anonymous union which is
discussed in Part Two of this book.

Bit-Fields in C, C++

Unlike some other computer languages, C/C++ has a built-in feature called a bit-field
that allows you to access a single bit. Bit-fields can be useful for a number of reasons,
such as:
If storage is limited, you can store several Boolean (true/false) variables in
one byte.
Certain devices transmit status information encoded into one or more bits
within a byte.
Certain encryption routines need to access the bits within a byte.
Although these tasks can be performed using the bitwise operators, a bit-field can
add more structure (and possibly efficiency) to your code.
To access individual bits, C/C++ uses a method based on the structure. In fact,
a bit-field is really just a special type of structure member that defines how long,
in bits, the field is to be. The general form of a bit-field definition is
struct struct-type-name {
type name1 : length;
type name2 : length;
..
.
type nameN : length;
} variable_list;
Here, type is the type of the bit-field and length is the number of bits in the field.
A bit-field must be declared as an integral or enumeration type. Bit-fields of length
1 should be declared as unsigned, because a single bit cannot have a sign.
Bit-fields are frequently used when analyzing input from a hardware device.
For example, the status port of a serial communications adapter might return a
status byte organized like this:

Bit    Meaning When Set

0        Change in clear-to-send line
1         Change in data-set-ready
2         Trailing edge detected
3        Change in receive line
4       Clear-to-send
5        Data-set-ready
6      Telephone ringing
7       Received signal

You can represent the information in a status byte using the following bit-field:

struct status_type {
unsigned delta_cts: 1;
unsigned delta_dsr: 1;
unsigned tr_edge: 1;
unsigned delta_rec: 1;
unsigned cts: 1;
unsigned dsr: 1;
unsigned ring: 1;
unsigned rec_line: 1;
} status;

You might use a routine similar to that shown here to enable a program to determine
when it can send or receive data.
status = get_port_status();
if(status.cts) printf("clear to send");
if(status.dsr) printf("data ready");
To assign a value to a bit-field, simply use the form you would use for any other type
of structure element. For example, this code fragment clears the ring field:
status.ring = 0;
As you can see from this example, each bit-field is accessed with the dot operator.
However, if the structure is referenced through a pointer, you must use the −> operator.
You do not have to name each bit-field. This makes it easy to reach the bit you
want, bypassing unused ones. For example, if you only care about the cts and dsr
bits, you could declare the status_type structure like this:
struct status_type {
unsigned : 4;
unsigned cts: 1;
unsigned dsr: 1;
} status;
Also, notice that the bits after dsr do not need to be specified if they are not used.
It is valid to mix normal structure members with bit-fields. For example,
struct emp {
struct addr address;
float pay;
unsigned lay_off: 1; /* lay off or active */
unsigned hourly: 1; /* hourly pay or wage */
unsigned deductions: 3; /* IRS deductions */
};
defines an employee record that uses only 1 byte to hold three pieces of information:
the employee's status, whether the employee is salaried, and the number of deductions.
Without the bit-field, this information would have taken 3 bytes.
Bit-fields have certain restrictions. You cannot take the address of a bit-field. Bitfields
cannot be arrayed. They cannot be declared as static. You cannot know, from
machine to machine, whether the fields will run from right to left or from left to right;
this implies that any code using bit-fields may have some machine dependencies.
Other restrictions may be imposed by various specific implementations, so check the
user manual for your compiler.

Arrays and Structures Within Structures

A member of a structure may be either a simple or compound type. A simple
member is one that is of any of the built-in data types, such as integer or character.
You have already seen one type of compound element: the character arrays used in
addr. Other compound data types include one-dimensional and multidimensional
arrays of the other data types and structures.
A member of a structure that is an array is treated as you might expect from
the earlier examples. For example, consider this structure:
struct x {
int a[10][10]; /* 10 x 10 array of ints */
float b;
} y;
To reference integer 3,7 in a of structure y, write
y.a[3][7]
When a structure is a member of another structure, it is called a nested structure.
For example, the structure address is nested inside emp in this example:
struct emp {
struct addr address; /* nested structure */
float wage;
} worker;
Here, structure emp has been defined as having two members. The first is a structure
of type addr, which contains an employee's address. The other is wage, which holds
the employee's wage. The following code fragment assigns 93456 to the zip element
of address.

worker.address.zip = 93456;
As you can see, the members of each structure are referenced from outermost to
innermost. Standard C specifies that structures may be nested to at least 15 levels.
Standard C++ suggests that at least 256 levels of nesting be allowed.

Friday, October 1, 2010

Using Structure Pointers in C,C++

There are two primary uses for structure pointers: to pass a structure to a function
using call by reference, and to create linked lists and other dynamic data structures that
rely on dynamic allocation. This chapter covers the first use.
There is one major drawback to passing all but the simplest structures to functions:
the overhead needed to push the structure onto the stack when the function call is
executed. (Recall that arguments are passed to functions on the stack.) For simple
structures with few members, this overhead is not too great. If the structure contains
many members, however, or if some of its members are arrays, run-time performance
may degrade to unacceptable levels. The solution to this problem is to pass only a
pointer to the function.
When a pointer to a structure is passed to a function, only the address of the
structure is pushed on the stack. This makes for very fast function calls. A second
advantage, in some cases, is when a function needs to reference the actual structure
used as the argument, instead of a copy. By passing a pointer, the function can
modify the contents of the structure used in the call.
To find the address of a structure variable, place the & operator before the
structure's name. For example, given the following fragment:
struct bal {
float balance;
char name[80];
} person;
struct bal *p; /* declare a structure pointer */
then
p = &person;

places the address of the structure person into the pointer p.
To access the members of a structure using a pointer to that structure, you must
use the −> operator. For example, this references the balance field:
p->balance
The −> is usually called the arrow operator, and consists of the minus sign followed
by a greater-than sign. The arrow is used in place of the dot operator when you are
accessing a structure member through a pointer to the structure.
To see how a structure pointer can be used, examine this simple program, which
prints the hours, minutes, and seconds on your screen using a software timer.
/* Display a software timer. */
#include <stdio.h>
#define DELAY 128000
struct my_time {
int hours;
int minutes;
int seconds;
} ;
void display(struct my_time *t);
void update(struct my_time *t);
void delay(void);
int main(void)
{
struct my_time systime;
systime.hours = 0;
systime.minutes = 0;
systime.seconds = 0;
for(;;) {
update(&systime);
display(&systime);
}
return 0;
}

void update(struct my_time *t)
{
t->seconds++;
if(t->seconds==60) {
t->seconds = 0;
t->minutes++;
}
if(t->minutes==60) {
t->minutes = 0;
t->hours++;
}
if(t->hours==24) t->hours = 0;
delay();
}
void display(struct my_time *t)
{
printf("%02d:", t->hours);
printf("%02d:", t->minutes);
printf("%02d\n", t->seconds);
}
void delay(void)
{
long int t;
/* change this as needed */
for(t=1; t<DELAY; ++t) ;
}
The timing of this program is adjusted by changing the definition of DELAY.
As you can see, a global structure called my_time is defined but no variable is
declared. Inside main() , the structure systime is declared and initialized to 00:00:00.
This means that systime is known directly only to the main() function.
The functions update() (which changes the time) and display() (which prints
the time) are passed the address of systime. In both functions, their arguments are
declared as a pointer to a my_time structure.
Inside update() and display() , each member of systime is accessed via a pointer.
Because update() receives a pointer to the systime structure, it can update its value.

For example, to set the hours back to 0 when 24:00:00 is reached, update() contains
this line of code:
if(t->hours==24) t->hours = 0;
This tells the compiler to take the address of t (which points to systime in main() )
and to reset hours to zero.
Remember, use the dot operator to access structure elements when operating on
the structure itself. When you have a pointer to a structure, use the arrow operator.

Structure Pointers in C, C++

C/C++ allows pointers to structures just as it allows pointers to any other type
of variable. However, there are some special aspects to structure pointers that
you should know.


Declaring a Structure Pointer
 
Like other pointers, structure pointers are declared by placing * in front of a structure
variable's name. For example, assuming the previously defined structure addr, the
following declares addr_pointer as a pointer to data of that type:
struct addr *addr_pointer;
Remember, in C++ it is not necessary to precede this declaration with the keyword
struct.

Passing Entire Structures to Functions in C,C++

When a structure is used as an argument to a function, the entire structure is passed
using the standard call-by-value method. Of course, this means that any changes 

made to the contents of the structure inside the function to which it is passed do not
affect the structure used as an argument.
When using a structure as a parameter, remember that the type of the argument
must match the type of the parameter. For example, in the following program both the
argument arg and the parameter parm are declared as the same type of structure.
#include <stdio.h>
/* Define a structure type. */
struct struct_type {
int a, b;
char ch;
} ;
void f1(struct struct_type parm);
int main(void)
{
struct struct_type arg;
arg.a = 1000;
f1(arg);
return 0;
}
void f1(struct struct_type parm)
{
printf("%d", parm.a);
}
As this program illustrates, if you will be declaring parameters that are structures,
you must make the declaration of the structure type global so that all parts of your
program can use it. For example, had struct_type been declared inside main() (for
example), then it would not have been visible to f1().
As just stated, when passing structures, the type of the argument must match
the type of the parameter. It is not sufficient for them to simply be physically similar;
their type names must match. For example, the following version of the preceding
program is incorrect and will not compile because the type name of the argument
used to call f1() differs from the type name of its parameter.

/* This program is incorrect and will not compile. */
#include <stdio.h>
/* Define a structure type. */
struct struct_type {
int a, b;
char ch;
} ;
/* Define a structure similar to struct_type,
but with a different name. */
struct struct_type2 {
int a, b;
char ch;
} ;
void f1(struct struct_type2 parm);
int main(void)
{
struct struct_type arg;
arg.a = 1000;
f1(arg); /* type mismatch */
return 0;
}
void f1(struct struct_type2 parm)
{
printf("%d", parm.a);

Passing Structure Members to Functions in C,C++

When you pass a member of a structure to a function, you are actually passing
the value of that member to the function. Therefore, you are passing a simple
variable (unless, of course, that element is compound, such as an array). For
example, consider this structure:
struct fred
{
char x;
int y;
float z;
char s[10];
} mike;
Here are examples of each member being passed to a function:
func(mike.x); /* passes character value of x */
func2(mike.y); /* passes integer value of y */
func3(mike.z); /* passes float value of z */
func4(mike.s); /* passes address of string s */
func(mike.s[2]); /* passes character value of s[2] */
If you wish to pass the address of an individual structure member, put the & operator
before the structure name. For example, to pass the address of the members of the
structure mike, write
func(&mike.x); /* passes address of character x */
func2(&mike.y); /* passes address of integer y */
func3(&mike.z); /* passes address of float z */
func4(mike.s); /* passes address of string s */
func(&mike.s[2]); /* passes address of character s[2] */
Remember that the & operator precedes the structure name, not the individual
member name. Note also that s already signifies an address, so no & is required.

Thursday, September 30, 2010

Structure Assignments in C,C++

The information contained in one structure may be assigned to another structure of the
same type using a single assignment statement. That is, you do not need to assign the
value of each member separately. The following program illustrates structure
assignments:
#include <stdio.h>
int main(void)
 {
struct {
int a;
int b;
} x, y;
x.a = 10;
y = x; /* assign one structure to another */
printf("%d", y.a);
return 0;
}
After the assignment, y.a will contain the value 10.

Accessing Structure Members in C, C++

Individual members of a structure are accessed through the use of the . operator
(usually called the dot operator). For example, the following code assigns the ZIP
code 12345 to the zip field of the structure variable addr_info declared earlier:
addr_info.zip = 12345;
The structure variable name followed by a period and the member name references
that individual member. The general form for accessing a member of a structure is
structure-name.member-name
Therefore, to print the ZIP code on the screen, write
printf("%d", addr_info.zip);
This prints the ZIP code contained in the zip member of the structure variable
addr_info.
In the same fashion, the character array addr_info.name can be used to call
gets() , as shown here:
gets(addr_info.name);
This passes a character pointer to the start of name.
Since name is a character array, you can access the individual characters of
addr_info.name by indexing name. For example, you can print the contents of
addr_info.name one character at a time by using the following code:
register int t;
for(t=0; addr_info.name[t]; ++t)
putchar(addr_info.name[t]);

 

Structures in C, C++

A structure is a collection of variables referenced under one name, providing a
convenient means of keeping related information together. A structure declaration
forms a template that may be used to create structure objects (that is, instances of
a structure). The variables that make up the structure are called members. (Structure
members are also commonly referred to as elements or fields.)
Generally, all of the members of a structure are logically related. For example, the
name and address information in a mailing list would normally be represented in a
structure. The following code fragment shows how to declare a structure that defines
the name and address fields. The keyword struct tells the compiler that a structure is
being declared.
struct addr
{
char name[30];
char street[40];
char city[20];
char state[3];
unsigned long int zip;
};
Notice that the declaration is terminated by a semicolon. This is because a structure
declaration is a statement. The type name of the structure is addr. As such, addr
identifies this particular data structure and is its type specifier.
At this point, no variable has actually been created. Only the form of the data has been
defined. When you define a structure, you are defining a compound variable type, not
a variable. Not until you declare a variable of that type does one actually exist. In C, to
declare a variable (i.e., a physical object) of type addr, write
struct addr addr_info;
This declares a variable of type addr called addr_info. In C++, you may use this shorter
form.
addr addr_info;
As you can see, the keyword struct is not needed. In C++, once a structure
has been declared, you may declare variables of its type using only its type name,
without preceding it with the keyword struct. The reason for this difference is that
in C, a structure's name does not define a complete type name. In fact, Standard C
refers to a structure's name as a tag. In C, you must precede the tag with the keyword
struct when declaring variables. However, in C++, a structure's name is a complete
type name and may be used by itself to define variables. Keep in mind, however,
that it is still perfectly legal to use the C-style declaration in a C++ program. Since the
programs in Part One of this book are valid for both C and C++, they will use the C
declaration method. Just remember that C++ allows the shorter form.
When a structure variable (such as addr_info) is declared, the compiler automatically
allocates sufficient memory to accommodate all of its members. Figure 7-1
shows how addr_info appears in memory assuming 1-byte characters and 4-byte
long integers.
You may also declare one or more structure variables when you declare a structure.
For example,
struct addr {
char name[30];
char street[40];
char city[20];
char state[3];
unsigned long int zip;
} addr_info, binfo, cinfo;
defines a structure type called addr and declares variables addr_info, binfo, and cinfo
of that type.
If you only need one structure variable, the structure type name is not needed. That
means that
struct {
char name[30];
char street[40];
char city[20];
char state[3];
unsigned long int zip;
} addr_info;
declares one variable named addr_info as defined by the structure preceding it.
The general form of a structure declaration is
struct struct-type-name {
type member-name;
type member-name;
type member-name;
.. .
} structure-variables;
where either struct-type-name or structure-variables may be omitted, but not both.

Wednesday, September 29, 2010

Implementation Issues Of Functions in C,C++

There are a few important things to remember about functions that affect their
efficiency and usability. These issues are the subject of this section.

Parameters and General-Purpose Functions
A general-purpose function is one that will be used in a variety of situations, perhaps
by many different programmers. Typically, you should not base general-purpose
functions on global data. All of the information a function needs should be passed
to it by its parameters. When this is not possible, you should use static variables.
Besides making your functions general purpose, parameters keep your code
readable and less susceptible to bugs resulting from side effects.

Efficiency
Functions are the building blocks of C/C++ and are crucial to all but the simplest
programs. However, in certain specialized applications, you may need to eliminate
a function and replace it with inline code. Inline code performs the same actions as a
function, but without the overhead associated with a function call. For this reason,
inline code is often used instead of function calls when execution time is critical.
Inline code is faster than a function call for two reasons. First, a CALL instruction
takes time to execute. Second, if there are arguments to pass, these have to be placed
on the stack, which also takes time. For most applications, this very slight increase in
execution time is of no significance. But if it is, remember that each function call uses
time that would be saved if the function's code were placed in line. For example, the
following are two versions of a program that prints the square of the numbers from 1
to 10. The inline version runs faster than the other because the function call adds time.
in line function call
#include <stdio.h> #include <stdio.h>
int sqr(int a);
int main(void) int main(void)
{ {
int x; int x;
for(x=1; x<11; ++x) for(x=1; x<11; ++x)
printf("%d", x*x); printf("%d", sqr(x));
return 0; return 0;
} }
int sqr(int a)
{
return a*a;
}
 
Note : In C++, the concept of inline functions is expanded and formalized. In fact, inline
functions are an important component of the C++ language.

Old-Style Versus Modern Function Parameter Declarations

Early versions of C used a different parameter declaration method than does either
Standard C or Standard C++. This early approach is sometimes called the classic form.
This book uses a declaration approach called the modern form. Standard C supports
both forms, but strongly recommends the modern form. Standard C++ only supports
the modern parameter declaration method. However, you should know the old-style
form because many older C programs still use it.
The old-style function parameter declaration consists of two parts: a parameter
list, which goes inside the parentheses that follow the function name, and the actual
parameter declarations, which go between the closing parentheses and the function's
opening curly brace. The general form of the old-style parameter definition is
type func_name(parm1, parm2, . . .parmN)
type parm1;
type parm2;
.
..
type parmN;
{
function code
}

For example, this modern declaration:
float f(int a, int b, char ch)
{
/* ... */
}
will look like this in its old-style form:
float f(a, b, ch)
int a, b;
char ch;
{
/* ... */
}
Notice that the old-style form allows the declaration of more than one parameter in a
list after the type name.
 

Note :The old-style form of parameter declaration is designated as obsolete by the C
language and is not supported by C++.

Declaring Variable-Length Parameter Lists Of function in C, C++

You can specify a function that has a variable number of parameters. The most
common example is printf() . To tell the compiler that an unknown number of
arguments may be passed to a function, you must end the declaration of its
parameters using three periods. For example, this prototype specifies that func()
will have at least two integer parameters and an unknown number (including 0)
of parameters after that.
int func(int a, int b, ...);
This form of declaration is also used by a function's definition.
Any function that uses a variable number of parameters must have at least one
actual parameter. For example, this is incorrect:
int func(...); /* illegal */

Standard Library Function Prototypes

Any standard library function used by your program must be prototyped. To
accomplish this, you must include the appropriate header for each library function.
All necessary headers are provided by the C/C++ compiler. In C, all headers are files
that use the .H extension. In C++, headers may be either separate files or built into
the compiler itself. In either case, a header contains two main elements: any definitions
used by the library functions and the prototypes for the library functions. For example,
stdio.h is included in almost all programs in this part of the book because it contains
the prototype for printf() . The headers for the standard library are described in
Part Three.

Function Prototypes in C, C++

In C++ all functions must be declared before they are used. This is normally
accomplished using a function prototype. Function prototypes were not part of the
original C language. They were, however, added when C was standardized. While
prototypes are not technically required by Standard C, their use is strongly encouraged.
Prototypes have always been required by C++. In this book, all examples include full
function prototypes. Prototypes enable both C and C++ to provide stronger type
checking, somewhat like that provided by languages such as Pascal. When you use
prototypes, the compiler can find and report any illegal type conversions between the
type of arguments used to call a function and the type definition of its parameters. The
compiler will also catch differences between the number of arguments used to call a
function and the number of parameters in the function.
The general form of a function prototype is
type func_name(type parm_name1, type parm_name2,. . .,
type parm_nameN);
The use of parameter names is optional. However, they enable the compiler to identify
any type mismatches by name when an error occurs, so it is a good idea to include
them.
The following program illustrates the value of function prototypes. It produces an
error message because it contains an attempt to call sqr_it() with an integer argument
instead of the integer pointer required. (It is illegal to convert an integer into a pointer.)
/* This program uses a function prototype to
enforce strong type checking. */
void sqr_it(int *i); /* prototype */
int main(void)
{

int x;
x = 10;
sqr_it(x); /* type mismatch */
return 0;
}
void sqr_it(int *i)
{
*i = *i * *i;
}
A function's definition can also serve as its prototype if the definition occurs prior
to the function's first use in the program. For example, this is a valid program.
#include <stdio.h>
/* This definition will also serve
as a prototype within this program. */
void f(int a, int b)
{
printf("%d ", a % b);
}
int main(void)
{
f(10,3);
return 0;
}
In this example, since f() is defined prior to its use in main(), no separate
prototype is required. While it is possible for a function's definition to serve as its
prototype in small programs, it is seldom possible in large onesespecially when
several files are used. The programs in this book include a separate prototype for
each function because that is the way C/C++ code is normally written in practice.
The only function that does not require a prototype is main(), since it is the first
function called when your program begins.
Because of the need for compatibility with the original version of C, there is a
small but important difference between how C and C++ handle the prototyping of a

function that has no parameters. In C++, an empty parameter list is simply indicated
in the prototype by the absence of any parameters. For example,
int f(); /* C++ prototype for a function with no parameters */
However, in C this prototype means something different. For historical reasons,
an empty parameter list simply says that no parameter information is given. As far as the
compiler is concerned, the function could have several parameters or no parameters. In
C, when a function has no parameters, its prototype uses void inside the parameter list.
For example, here is f() 's prototype as it would appear in a C program.
float f(void);
This tells the compiler that the function has no parameters, and any call to that function
that has parameters is an error. In C++, the use of void inside an empty parameter list
is still allowed, but is redundant.
In C++, f( ) and f(void) are equivalent.
Function prototypes help you trap bugs before they occur. In addition, they help
verify that your program is working correctly by not allowing functions to be called
with mismatched arguments.
One last point: Since early versions of C did not support the full prototype syntax,
prototypes are technically optional in C. This is necessary to support pre-prototype
C code. If you are porting older C code to C++, you may need to add full function
prototypes before it will compile. Remember: Although prototypes are optional in C,
they are required by C++. This means that every function in a C++ program must be
fully prototyped.

Recursion In C/C++

In C/C++, a function can call itself. A function is said to be recursive if a statement in
the body of the function calls itself. Recursion is the process of defining something in
terms of itself, and is sometimes called circular definition.
A simple example of a recursive function is factr() , which computes the factorial of
an integer. The factorial of a number n is the product of all the whole numbers between
1 and n. For example, 3 factorial is 1 x 2 x 3, or 6. B oth factr() and its iterative
equivalent are shown here:
/* recursive */
int factr(int n) {
int answer;
if(n==1) return(1);
answer = factr(n-1)*n; /* recursive call */
return(answer);

}
/* non-recursive */
int fact(int n) {
int t, answer;
answer = 1;
for(t=1; t<=n; t++)
answer=answer*(t);
return(answer);
}
The nonrecursive version of fact() should be clear. It uses a loop that runs from 1 to
n and progressively multiplies each number by the moving product.
The operation of the recursive factr() is a little more complex. When factr() is
called with an argument of 1, the function returns 1. Otherwise, it returns the product
of factr(n−1)*n. To evaluate this expression, factr() is called with n−1. This happens
until n equals 1 and the calls to the function begin returning.
Computing the factorial of 2, the first call to factr() causes a second, recursive call
with the argument of 1. This call returns 1, which is then multiplied by 2 (the original
n value). The answer is then 2. Try working through the computation of 3 factorial on
your own. (You might want to insert printf() statements into factr() to see the level of
each call and what the intermediate answers are.)
When a function calls itself, a new set of local variables and parameters are
allocated storage on the stack, and the function code is executed from the top with
these new variables. A recursive call does not make a new copy of the function. Only
the values being operated upon are new. As each recursive call returns, the old local
variables and parameters are removed from the stack and execution resumes at the
point of the function call inside the function. Recursive functions could be said to
"telescope" out and back.
Most recursive routines do not significantly reduce code size or improve memory
utilization. Also, the recursive versions of most routines may execute a bit slower than
their iterative equivalents because of the overhead of the repeated function calls. In
fact, many recursive calls to a function could cause a stack overrun. Because storage for
function parameters and local variables is on the stack and each new call creates a new
copy of these variables, the stack could be overrun. However, you probably will not
have to worry about this unless a recursive function runs wild.
The main advantage to recursive functions is that you can use them to create clearer
and simpler versions of several algorithms. For example, the quicksort algorithm is
difficult to implement in an iterative way. Also, some problems, especially ones related

to artificial intelligence, lend themselves to recursive solutions. Finally, some people
seem to think recursively more easily than iteratively.
When writing recursive functions, you must have a conditional statement, such
as an if, somewhere to force the function to return without the recursive call being
executed. If you don't, the function will never return once you call it. Omitting the
conditional statement is a common error when writing recursive functions. Use
printf() liberally during program development so that you can watch what is going
on and abort execution if you see a mistake

What Does main( ) Return?

The main() function returns an integer to the calling process, which is generally the
operating system. Returning a value from main() is the equivalent of calling exit()
with the same value. If main() does not explicitly return a value, the value passed
to the calling process is technically undefined. In practice, most C/C++ compilers
automatically return 0, but do not rely on this if portability is a concern.

Functions of Type void

One of void's uses is to explicitly declare functions that do not return values. This
prevents their use in any expression and helps avert accidental misuse. For example,
the function print_vertical() prints its string argument vertically down the side of
the screen. Since it returns no value, it is declared as void.
void print_vertical(char *str)
{
while(*str)
printf("%c\n", *str++);
}
Here is an example that uses print_vertical() .
#include <stdio.h>
void print_vertical(char *str); /* prototype */

int main(int argc, char *argv[])
{
if(argc > 1) print_vertical(argv[1]);
return 0;
}
void print_vertical(char *str)
{
while(*str)
printf("%c\n", *str++);
}
One last point: Early versions of C did not define the void keyword. Thus, in
early C programs, functions that did not return values simply defaulted to type int.
Therefore, don't be surprised to see many examples of this in older code.

Returning Pointers from Functions

Although functions that return pointers are handled just like any other type of
function, a few important concepts need to be discussed.
Pointers to variables are neither integers nor unsigned integers. They are the
memory addresses of a certain type of data. The reason for this distinction is because
pointer arithmetic is relative to the base type. For example, if an integer pointer is
incremented, it will contain a value that is 4 greater than its previous value (assuming
4-byte integers). In general, each time a pointer is incremented (or decremented), it
points to the next (or previous) item of its type. Since the length of different data types
may differ, the compiler must know what type of data the pointer is pointing to. For
this reason, a function that returns a pointer must declare explicitly what type of
pointer it is returning. For example, you should not use a return type of int * to return
a char * pointer!
To return a pointer, a function must be declared as having a pointer return type.
For example, this function returns a pointer to the first occurrence of the character c
in string s:
/* Return pointer of first occurrence of c in s. */
char *match(char c, char *s)
{
while(c!=*s && *s) s++;
return(s);
}
If no match is found, a pointer to the null terminator is returned. Here is a short
program that uses match() :

#include <stdio.h>
char *match(char c, char *s); /* prototype */
int main(void)
{
char s[80], *p, ch;
gets(s);
ch = getchar();
p = match(ch, s);
if(*p) /* there is a match */
printf("%s ", p);
else
printf("No match found.");
return 0;
}
This program reads a string and then a character. If the character is in the string, the
program prints the string from the point of match. Otherwise, it prints No match found.

Returning Values in C, C+

All functions, except those of type void, return a value. This value is specified by the
return statement. In C, if a non-void function does not explicitly return a value via a
return statement, then a garbage value is returned. In C++, a non-void function must
contain a return statement that returns a value. That is, in C++, if a function is specified
as returning a value, any return statement within it must have a value associated with
it. However, if execution reaches the end of a non-void function, then a garbage value
is returned. Although this condition is not a syntax error, it is still a fundamental error
and should be avoided.
As long as a function is not declared as void, you may use it as an operand in an
expression. Therefore, each of the following expressions is valid:
x = power(y);
if(max(x,y) > 100) printf("greater");
for(ch=getchar(); isdigit(ch); ) ... ;
As a general rule, a function cannot be the target of an assignment. A statement
such as swap(x,y) = 100; /* incorrect statement */
is wrong. The C/C++ compiler will flag it as an error and will not compile a program
that contains it. (As is discussed in Part Two, C++ allows some interesting exceptions
to this general rule, enabling some types of functions to occur on the left side of an
assignment.)
When you write programs, your functions generally will be of three types. The
first type is simply computational. These functions are specifically designed to
perform operations on their arguments and return a value based on that operation.
A computational function is a "pure" function. Examples are the standard library
functions sqrt() and sin() , which compute the square root and sine of their arguments.
The second type of function manipulates information and returns a value that
simply indicates the success or failure of that manipulation. An example is the library
function fclose() , which is used to close a file. If the close operation is successful, the
function returns 0; if the operation is unsuccessful, it returns EOF.
The last type of function has no explicit return value. In essence, the function is
strictly procedural and produces no value. An example is exit() , which terminates a
program. All functions that do not return values should be declared as returning type
void. By declaring a function as void, you keep it from being used in an expression,
thus preventing accidental misuse.
Sometimes, functions that really don't produce an interesting result return
something anyway. For example, printf() returns the number of characters written.
Yet it would be unusual to find a program that actually checked this. In other words,
although all functions, except those of type void, return values, you don't have to use
the return value for anything. A common question concerning function return values
is, "Don't I have to assign this value to some variable since a value is being returned?"
The answer is no. If there is no assignment specified, the return value is simply
discarded. Consider the following program, which uses the function mul() :
#include <stdio.h>
int mul(int a, int b);
int main(void)
{
int x, y, z;
x = 10; y = 20;
z = mul(x, y); /* 1 */
printf("%d", mul(x,y)); /* 2 */
mul(x, y); /* 3 */

return 0;
}
int mul(int a, int b)
{
return a*b;
}
In line 1, the return value of mul() is assigned to z. In line 2, the return value is not
actually assigned, but it is used by the printf() function. Finally, in line 3, the return
value is lost because it is neither assigned to another variable nor used as part of an
expression.

Returning from a Function in C, C++

There are two ways that a function terminates execution and returns to the caller. The
first occurs when the last statement in the function has executed and, conceptually, the function's ending curly brace (}) is encountered. (Of course, the curly brace isn't
actually present in the object code, but you can think of it in this way.) For example, the
pr_reverse() function in this program simply prints the string "I like C++" backwards
on the screen and then returns.
#include <string.h>
#include <stdio.h>
void pr_reverse(char *s);
int main(void)
{
pr_reverse("I like C++");
return 0;
}
void pr_reverse(char *s)
{
register int t;
for(t=strlen(s)-1; t>=0; t--) putchar(s[t]);
}
Once the string has been displayed, there is nothing left for pr_reverse() to do, so it
returns to the place from which it was called.
Actually, not many functions use this default method of terminating their
execution. Most functions rely on the return statement to stop execution either
because a value must be returned or to make a function's code simpler and more
efficient.
A function may contain several return statements. For example, the find_substr()
function in the following program returns the starting position of a substring within
a string, or returns −1 if no match is found.
#include <stdio.h>
int find_substr(char *s1, char *s2);
int main(void)
{
if(find_substr("C++ is fun", "is") != -1)
printf("substring is found");return 0;
}
/* Return index of first match of s2 in s1. */
int find_substr(char *s1, char *s2)
{
register int t;
char *p, *p2;
for(t=0; s1[t]; t++) {
p = &s1[t];
p2 = s2;
while(*p2 && *p2==*p) {
p++;
p2++;
}
if(!*p2) return t; /* 1st return */
}
return -1; /* 2nd return */
}

argc and argv—Arguments to main( )

Sometimes it is useful to pass information into a program when you run it. Generally,
you pass information into the main() function via command line arguments. A
command line argument is the information that follows the program's name on the
command line of the operating system. For example, when you compile a program,
you might type something like the following after the command prompt:
cc program_name
where program_name is a command line argument that specifies the name of the
program you wish to compile.
There are two special built-in arguments, argv and argc, that are used to receive
command line arguments. The argc parameter holds the number of arguments on

the command line and is an integer. It is always at least 1 because the name of the
program qualifies as the first argument. The argv parameter is a pointer to an array of
character pointers. Each element in this array points to a command line argument. All
command line arguments are strings—any numbers will have to be converted by the
program into the proper internal format. For example, this simple program prints
Hello and your name on the screen if you type it directly after the program name.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
if(argc!=2) {
printf("You forgot to type your name.\n");
exit(1);
}
printf("Hello %s", argv[1]);
return 0;
}
If you called this program name and your name were Tom, you would type name Tom
to run the program. The output from the program would be Hello Tom.
In many environments, each command line argument must be separated by a space
or a tab. Commas, semicolons, and the like are not considered separators. For example,
run Spot, run
is made up of three strings, while
Herb,Rick,Fred
is a single string since commas are not generally legal separators.
Some environments allow you to enclose within double quotes a string containing
spaces. This causes the entire string to be treated as a single argument. Check your
operating system documentation for details on the definition of command line
parameters for your system.
You must declare argv properly. The most common method is
char *argv[];

The empty brackets indicate that the array is of undetermined length. You can now
access the individual arguments by indexing argv. For example, argv[0] points to the
first string, which is always the program's name; argv[1] points to the first argument,
and so on.
Another short example using command line arguments is the program called
countdown, shown here. It counts down from a starting value (which is specified on
the command line) and beeps when it reaches 0. Notice that the first argument
containing the number is converted into an integer by the standard function atoi() .If
the string "display" is the second command line argument, the countdown will also be
displayed on the screen.
/* Countdown program. */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(int argc, char *argv[])
{
int disp, count;
if(argc<2) {
printf("You must enter the length of the count\n");
printf("on the command line. Try again.\n");
exit(1);
}
if(argc==3 && !strcmp(argv[2], "display")) disp = 1;
else disp = 0;
for(count=atoi(argv[1]); count; --count)
if(disp) printf("%d\n", count);
putchar('\a'); /* this will ring the bell */
printf("Done");
return 0;
}
Notice that if no command line arguments have been specified, an error message is
printed. A program with command line arguments often issues instructions if the
user attempts to run the program without entering the proper information.
To access an individual character in one of the command line arguments, add a
second index to argv. For example, the next program displays all of the arguments
with which it was called, one character at a time:

#include <stdio.h>
int main(int argc, char *argv[])
{
int t, i;
for(t=0; t<argc; ++t) {
i = 0;
while(argv[t][i]) {
putchar(argv[t][i]);
++i;
}
printf("\n");
}
return 0;
}
Remember, the first index accesses the string, and the second index accesses the
individual characters of the string.
Normally, you use argc and argv to get initial commands into your program. In
theory, you can have up to 32,767 arguments, but most operating systems do not allow
more than a few. You typically use these arguments to indicate a filename or an option.
Using command line arguments gives your program a professional appearance and
facilitates its use in batch files.
When a program does not require command line parameters, it is common
practice to explicitly declare main() as having no parameters. For C programs this is
accomplished by using the void keyword in its parameter list. (This is the approach
used by the programs in Part One of this book.) However, for C++ programs you
may simply specify an empty parameter list. In C++, the use of void to indicate an
empty parameter list is allowed, but redundant.
The names argc and argv are traditional but arbitrary. You may name these two
parameters to main() anything you like. Also, some compilers may support additional
arguments to main() , so be sure to check your user's manual.

Calling Functions with Arrays

Arrays are covered in detail in Chapter 4. However, this section discusses passing
arrays as arguments to functions because it is an exception to the normal call-by-value
parameter passing.
When an array is used as a function argument, its address is passed to a function.
This is an exception to the call-by-value parameter passing convention. In this case, the
code inside the function is operating on, and potentially altering, the actual contents of
the array used to call the function. For example, consider the function print_upper() ,
which prints its string argument in uppercase:
#include <stdio.h>
#include <ctype.h>
void print_upper(char *string);
int main(void)
{
char s[80];
gets(s);
print_upper(s);
printf("\ns is now uppercase: %s", s);
return 0;
}
/* Print a string in uppercase. */
void print_upper(char *string)
{
register int t;
for(t=0; string[t]; ++t) {
string[t] = toupper(string[t]);
putchar(string[t]);
}
}
After the call to print_upper() , the contents of array s in main() will change to
uppercase. If this is not what you want, you could write the program like this:
#include <stdio.h>
#include <ctype.h>

void print_upper(char *string);
int main(void)
{
char s[80];
gets(s);
print_upper(s);
printf("\ns is unchanged: %s", s);
return 0;
}
void print_upper(char *string)
{
register int t;
for(t=0; string[t]; ++t)
putchar(toupper(string[t]));
}
In this version, the contents of array s remain unchanged because its values are not
altered inside print_upper() .
The standard library function gets() is a classic example of passing arrays into
functions. Although the gets() in your standard library is more sophisticated, the
following simpler version, called xgets() , will give you an idea of how it works.
/* A simple version of the standard
gets() library function. */
char *xgets(char *s)
{
char ch, *p;
int t;
p = s; /* gets() returns a pointer to s */
for(t=0; t<80; ++t){
ch = getchar();
switch(ch) {

case '\n':
s[t] = '\0'; /* terminate the string */
return p;
case '\b':
if(t>0) t--;
break;
default:
s[t] = ch;
}
}
s[79] = '\0';
return p;
}
The xgets() function must be called with a character pointer. This, of course, can
be the name of a character array, which by definition is a character pointer. Upon entry,
xgets() establishes a for loop from 0 to 79. This prevents larger strings from being
entered at the keyboard. If more than 80 characters are entered, the function returns.
(The real gets() function does not have this restriction.) Because C/C++ has no built-in
bounds checking, you should make sure that any array used to call xgets() can accept
at least 80 characters. As you type characters on the keyboard, they are placed in the
string. If you type a backspace, the counter t is reduced by 1, effectively removing the
previous character from the array. When you press ENTER, a null is placed at the end
of the string, signaling its termination. Because the actual array used to call xgets() is
modified, upon return it contains the characters that you type.

 

Creating a Call by Reference

Even though C/C++ uses call by value for passing parameters, you can create a
call by reference by passing a pointer to an argument, instead of the argument itself.
Since the address of the argument is passed to the function, code within the function
can change the value of the argument outside the function.
Pointers are passed to functions just like any other value. Of course, you need
to declare the parameters as pointer types. For example, the function swap() , which exchanges the values of the two integer variables pointed to by its arguments,
shows how.
void swap(int *x, int *y)
{
int temp;
temp = *x; /* save the value at address x */
*x = *y; /* put y into x */
*y = temp; /* put x into y */
}
swap() is able to exchange the values of the two variables pointed to by x and y
because their addresses (not their values) are passed. Thus, within the function,
the contents of the variables can be accessed using standard pointer operations, and
the contents of the variables used to call the function are swapped.
Remember that swap() (or any other function that uses pointer parameters) must
be called with the addresses of the arguments. The following program shows the correct
way to call swap() :
void swap(int *x, int *y);
int main(void)
{
int i, j;
i = 10;
j = 20;
swap(&i, &j); /* pass the addresses of i and j */
return 0;
}
In this example, the variable i is assigned the value 10 and j is assigned the value
20. Then swap() is called with the addresses of i and j. (The unary operator & is used
to produce the address of the variables.) Therefore, the addresses of i and j, not their
values, are passed into the function swap() .


Note
:C++ allows you to fully automate a call by reference through the use of reference
parameters. This feature is described in Part Two.

Call by Value, Call by Reference

In a computer language, there are two ways that arguments can be passed to a
subroutine. The first is known as call by value. This method copies the value of an argument into the formal parameter of the subroutine. In this case, changes made to
the parameter have no effect on the argument.
Call by reference is the second way of passing arguments to a subroutine. In this
method, the address of an argument is copied into the parameter. Inside the subroutine,
the address is used to access the actual argument used in the call. This means that
changes made to the parameter affect the argument.
By default, C/C++ uses call by value to pass arguments. In general, this means that
code within a function cannot alter the arguments used to call the function. Consider
the following program:
#include <stdio.h>
int sqr(int x);
int main(void)
{
int t=10;
printf("%d %d", sqr(t), t);
return 0;
}
int sqr(int x)
{
x = x*x;
return(x);
}
In this example, the value of the argument to sqr() , 10, is copied into the parameter
x. When the assignment x = x*x takes place, only the local variable x is modified. The
variable t, used to call sqr() , still has the value 10. Hence, the output is 100 10.
Remember that it is a copy of the value of the argument that is passed into the
function. What occurs inside the function has no effect on the variable used in the call.

Function Arguments in C,C++

If a function is to use arguments, it must declare variables that accept the values
of the arguments. These variables are called the formal parameters of the function.
They behave like other local variables inside the function and are created upon entry
into the function and destroyed upon exit. As shown in the following function, the
parameter declarations occur after the function name:
/* Return 1 if c is part of string s; 0 otherwise. */
int is_in(char *s, char c)
{
while(*s)
if(*s==c) return 1;
else s++;
return 0;
}
The function is_in() has two parameters: s and c. This function returns 1 if the
character c is part of the string s; otherwise, it returns 0.
As with local variables, you may make assignments to a function's formal
parameters or use them in an expression. Even though these variables perform
the special task of receiving the value of the arguments passed to the function,
you can use them as you do any other local variable.

Monday, September 27, 2010

Scope Rules of Functions in C C++

The scope rules of a language are the rules that govern whether a piece of code knows
about or has access to another piece of code or data.
Each function is a discrete block of code. A function's code is private to that function
and cannot be accessed by any statement in any other function except through a call to
that function. (For instance, you cannot use goto to jump into the middle of another
function.) The code that constitutes the body of a function is hidden from the rest of the
program and, unless it uses global variables or data, it can neither affect nor be affected by other parts of the program. Stated another way, the code and data that are defined
within one function cannot interact with the code or data defined in another function
because the two functions have a different scope.
Variables that are defined within a function are called local variables. A local
variable comes into existence when the function is entered and is destroyed upon
exit. That is, local variables cannot hold their value between function calls. The only
exception to this rule is when the variable is declared with the static storage class
specifier. This causes the compiler to treat the variable as if it were a global variable
for storage purposes, but limits its scope to within the function. (Chapter 2 covers
global and local variables in depth.)
In C (and C++) you cannot define a function within a function. This is why neither
C nor C++ are technically block-structured languages.

Call by Value, Call by Reference

In a computer language, there are two ways that arguments can be passed to a
subroutine. The first is known as call by value. This method copies the value of an argument into the formal parameter of the subroutine. In this case, changes made to
the parameter have no effect on the argument.
Call by reference is the second way of passing arguments to a subroutine. In this
method, the address of an argument is copied into the parameter. Inside the subroutine,
the address is used to access the actual argument used in the call. This means that
changes made to the parameter affect the argument.
By default, C/C++ uses call by value to pass arguments. In general, this means that
code within a function cannot alter the arguments used to call the function. Consider
the following program:
#include <stdio.h>
int sqr(int x);
int main(void)
{
int t=10;
printf("%d %d", sqr(t), t);
return 0;
}
int sqr(int x)
{
x = x*x;
return(x);
}
In this example, the value of the argument to sqr() , 10, is copied into the parameter
x. When the assignment x = x*x takes place, only the local variable x is modified. The
variable t, used to call sqr() , still has the value 10. Hence, the output is 100 10.
Remember that it is a copy of the value of the argument that is passed into the
function. What occurs inside the function has no effect on the variable used in the call.

argc and argv—Arguments to main( )

Sometimes it is useful to pass information into a program when you run it. Generally,
you pass information into the main() function via command line arguments. A
command line argument is the information that follows the program's name on the
command line of the operating system. For example, when you compile a program,
you might type something like the following after the command prompt:
cc program_name
where program_name is a command line argument that specifies the name of the
program you wish to compile.
There are two special built-in arguments, argv and argc, that are used to receive
command line arguments. The argc parameter holds the number of arguments on
the command line and is an integer. It is always at least 1 because the name of the
program qualifies as the first argument. The argv parameter is a pointer to an array of
character pointers. Each element in this array points to a command line argument. All
command line arguments are strings—any numbers will have to be converted by the
program into the proper internal format. For example, this simple program prints
Hello and your name on the screen if you type it directly after the program name.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
if(argc!=2) {
printf("You forgot to type your name.\n");
exit(1);
}
printf("Hello %s", argv[1]);
return 0;
}
If you called this program name and your name were Tom, you would type name Tom
to run the program. The output from the program would be Hello Tom.
In many environments, each command line argument must be separated by a space
or a tab. Commas, semicolons, and the like are not considered separators. For example,
run Spot, run
is made up of three strings, while
Herb,Rick,Fred
is a single string since commas are not generally legal separators.
Some environments allow you to enclose within double quotes a string containing
spaces. This causes the entire string to be treated as a single argument. Check your
operating system documentation for details on the definition of command line
parameters for your system.
You must declare argv properly. The most common method is
char *argv[];
The empty brackets indicate that the array is of undetermined length. You can now
access the individual arguments by indexing argv. For example, argv[0] points to the
first string, which is always the program's name; argv[1] points to the first argument,
and so on.
Another short example using command line arguments is the program called
countdown, shown here. It counts down from a starting value (which is specified on
the command line) and beeps when it reaches 0. Notice that the first argument
containing the number is converted into an integer by the standard function atoi() .If
the string "display" is the second command line argument, the countdown will also be
displayed on the screen.
/* Countdown program. */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(int argc, char *argv[])
{
int disp, count;
if(argc<2) {
printf("You must enter the length of the count\n");
printf("on the command line. Try again.\n");
exit(1);
}
if(argc==3 && !strcmp(argv[2], "display")) disp = 1;
else disp = 0;
for(count=atoi(argv[1]); count; --count)
if(disp) printf("%d\n", count);
putchar('\a'); /* this will ring the bell */
printf("Done");
return 0;
}
Notice that if no command line arguments have been specified, an error message is
printed. A program with command line arguments often issues instructions if the
user attempts to run the program without entering the proper information.
To access an individual character in one of the command line arguments, add a
second index to argv. For example, the next program displays all of the arguments
with which it was called, one character at a time:


#include <stdio.h>
int main(int argc, char *argv[])
{
int t, i;
for(t=0; t<argc; ++t) {
i = 0;
while(argv[t][i]) {
putchar(argv[t][i]);
++i;
}
printf("\n");
}
return 0;
}
Remember, the first index accesses the string, and the second index accesses the
individual characters of the string.
Normally, you use argc and argv to get initial commands into your program. In
theory, you can have up to 32,767 arguments, but most operating systems do not allow
more than a few. You typically use these arguments to indicate a filename or an option.
Using command line arguments gives your program a professional appearance and
facilitates its use in batch files.
When a program does not require command line parameters, it is common
practice to explicitly declare main() as having no parameters. For C programs this is
accomplished by using the void keyword in its parameter list. (This is the approach
used by the programs in Part One of this book.) However, for C++ programs you
may simply specify an empty parameter list. In C++, the use of void to indicate an
empty parameter list is allowed, but redundant.
The names argc and argv are traditional but arbitrary. You may name these two
parameters to main() anything you like. Also, some compilers may support additional
arguments to main() , so be sure to check your user's manual.

Implementation Issues of functions C,C++

There are a few important things to remember about functions that affect their
efficiency and usability. These issues are the subject of this section.

Parameters and General-Purpose Functions
A general-purpose function is one that will be used in a variety of situations, perhaps
by many different programmers. Typically, you should not base general-purpose
functions on global data. All of the information a function needs should be passed
to it by its parameters. When this is not possible, you should use static variables.
Besides making your functions general purpose, parameters keep your code
readable and less susceptible to bugs resulting from side effects.

Efficiency
Functions are the building blocks of C/C++ and are crucial to all but the simplest
programs. However, in certain specialized applications, you may need to eliminate
a function and replace it with inline code. Inline code performs the same actions as a

function, but without the overhead associated with a function call. For this reason,
inline code is often used instead of function calls when execution time is critical.
Inline code is faster than a function call for two reasons. First, a CALL instruction
takes time to execute. Second, if there are arguments to pass, these have to be placed
on the stack, which also takes time. For most applications, this very slight increase in
execution time is of no significance. But if it is, remember that each function call uses
time that would be saved if the function's code were placed in line. For example, the
following are two versions of a program that prints the square of the numbers from 1
to 10. The inline version runs faster than the other because the function call adds time.
in line function call
#include <stdio.h> #include <stdio.h>
int sqr(int a);
int main(void) int main(void)
{ {
int x; int x;
for(x=1; x<11; ++x) for(x=1; x<11; ++x)
printf("%d", x*x); printf("%d", sqr(x));
return 0; return 0;
} }
int sqr(int a)
{
return a*a;
}
In C++, the concept of inline functions is expanded and formalized. In fact, inline
functions are an important component of the C++ language.

Old-Style Versus Modern Function Parameter Declarations

Early versions of C used a different parameter declaration method than does either
Standard C or Standard C++. This early approach is sometimes called the classic form.
This book uses a declaration approach called the modern form. Standard C supports
both forms, but strongly recommends the modern form. Standard C++ only supports
the modern parameter declaration method. However, you should know the old-style
form because many older C programs still use it.
The old-style function parameter declaration consists of two parts: a parameter
list, which goes inside the parentheses that follow the function name, and the actual
parameter declarations, which go between the closing parentheses and the function's
opening curly brace. The general form of the old-style parameter definition is
type func_name(parm1, parm2, . . .parmN)
type parm1;
type parm2;
.
..
type parmN;
{
function code
}
For example, this modern declaration:
float f(int a, int b, char ch)
{
/* ... */
}
will look like this in its old-style form:
float f(a, b, ch)
int a, b;
char ch;
{
/* ... */
}
Notice that the old-style form allows the declaration of more than one parameter in a
list after the type name.
The old-style form of parameter declaration is designated as obsolete by the C
language and is not supported by C++.

Declaring Variable-Length Parameter Lists

You can specify a function that has a variable number of parameters. The most
common example is printf() . To tell the compiler that an unknown number of
arguments may be passed to a function, you must end the declaration of its
parameters using three periods. For example, this prototype specifies that func()
will have at least two integer parameters and an unknown number (including 0)
of parameters after that.
int func(int a, int b, ...);
This form of declaration is also used by a function's definition.
Any function that uses a variable number of parameters must have at least one
actual parameter. For example, this is incorrect:
int func(...); /* illegal */

Standard Library Function Prototypes

Any standard library function used by your program must be prototyped. To
accomplish this, you must include the appropriate header for each library function.
All necessary headers are provided by the C/C++ compiler. In C, all headers are files
that use the .H extension. In C++, headers may be either separate files or built into
the compiler itself. In either case, a header contains two main elements: any definitions
used by the library functions and the prototypes for the library functions. For example,
stdio.h is included in almost all programs in this part of the book because it contains
the prototype for printf() . The headers for the standard library are described in
Part Three.

Function Prototypes in C,C++

In C++ all functions must be declared before they are used. This is normally
accomplished using a function prototype. Function prototypes were not part of the
original C language. They were, however, added when C was standardized. While
prototypes are not technically required by Standard C, their use is strongly encouraged.
Prototypes have always been required by C++. In this book, all examples include full
function prototypes. Prototypes enable both C and C++ to provide stronger type
checking, somewhat like that provided by languages such as Pascal. When you use
prototypes, the compiler can find and report any illegal type conversions between the
type of arguments used to call a function and the type definition of its parameters. The
compiler will also catch differences between the number of arguments used to call a
function and the number of parameters in the function.
The general form of a function prototype is
type func_name(type parm_name1, type parm_name2,. . .,
type parm_nameN);
The use of parameter names is optional. However, they enable the compiler to identify
any type mismatches by name when an error occurs, so it is a good idea to include
them.
The following program illustrates the value of function prototypes. It produces an
error message because it contains an attempt to call sqr_it() with an integer argument
instead of the integer pointer required. (It is illegal to convert an integer into a pointer.)
/* This program uses a function prototype to
enforce strong type checking. */
void sqr_it(int *i); /* prototype */
int main(void)
{
int x;
x = 10;
sqr_it(x); /* type mismatch */
return 0;
}
void sqr_it(int *i)
{
*i = *i * *i;
}
A function's definition can also serve as its prototype if the definition occurs prior
to the function's first use in the program. For example, this is a valid program.
#include <stdio.h>
/* This definition will also serve
as a prototype within this program. */
void f(int a, int b)
{
printf("%d ", a % b);
}
int main(void)
{
f(10,3);
return 0;
}
In this example, since f() is defined prior to its use in main(), no separate
prototype is required. While it is possible for a function's definition to serve as its
prototype in small programs, it is seldom possible in large onesespecially when
several files are used. The programs in this book include a separate prototype for
each function because that is the way C/C++ code is normally written in practice.
The only function that does not require a prototype is main(), since it is the first
function called when your program begins.
Because of the need for compatibility with the original version of C, there is a
small but important difference between how C and C++ handle the prototyping of a

function that has no parameters. In C++, an empty parameter list is simply indicated
in the prototype by the absence of any parameters. For example,
int f(); /* C++ prototype for a function with no parameters */
However, in C this prototype means something different. For historical reasons,
an empty parameter list simply says that no parameter information is given. As far as the
compiler is concerned, the function could have several parameters or no parameters. In
C, when a function has no parameters, its prototype uses void inside the parameter list.
For example, here is f() 's prototype as it would appear in a C program.
float f(void);
This tells the compiler that the function has no parameters, and any call to that function
that has parameters is an error. In C++, the use of void inside an empty parameter list
is still allowed, but is redundant.
In C++, f( ) and f(void) are equivalent.
Function prototypes help you trap bugs before they occur. In addition, they help
verify that your program is working correctly by not allowing functions to be called
with mismatched arguments.
One last point: Since early versions of C did not support the full prototype syntax,
prototypes are technically optional in C. This is necessary to support pre-prototype
C code. If you are porting older C code to C++, you may need to add full function
prototypes before it will compile. Remember: Although prototypes are optional in C,
they are required by C++. This means that every function in a C++ program must be
fully prototyped.

Recursion, In, C/C++,

In C/C++, a function can call itself. A function is said to be recursive if a statement in
the body of the function calls itself. Recursion is the process of defining something in
terms of itself, and is sometimes called circular definition.
A simple example of a recursive function is factr() , which computes the factorial of
an integer. The factorial of a number n is the product of all the whole numbers between
1 and n. For example, 3 factorial is 1 x 2 x 3, or 6. B oth factr() and its iterative
equivalent are shown here:
/* recursive */
int factr(int n) {
int answer;
if(n==1) return(1);
answer = factr(n-1)*n; /* recursive call */
return(answer);
}
/* non-recursive */
int fact(int n) {
int t, answer;
answer = 1;
for(t=1; t<=n; t++)
answer=answer*(t);
return(answer);
}
The nonrecursive version of fact() should be clear. It uses a loop that runs from 1 to
n and progressively multiplies each number by the moving product.
The operation of the recursive factr() is a little more complex. When factr() is
called with an argument of 1, the function returns 1. Otherwise, it returns the product
of factr(n−1)*n. To evaluate this expression, factr() is called with n−1. This happens
until n equals 1 and the calls to the function begin returning.
Computing the factorial of 2, the first call to factr() causes a second, recursive call
with the argument of 1. This call returns 1, which is then multiplied by 2 (the original
n value). The answer is then 2. Try working through the computation of 3 factorial on
your own. (You might want to insert printf() statements into factr() to see the level of
each call and what the intermediate answers are.)
When a function calls itself, a new set of local variables and parameters are
allocated storage on the stack, and the function code is executed from the top with
these new variables. A recursive call does not make a new copy of the function. Only
the values being operated upon are new. As each recursive call returns, the old local
variables and parameters are removed from the stack and execution resumes at the
point of the function call inside the function. Recursive functions could be said to
"telescope" out and back.
Most recursive routines do not significantly reduce code size or improve memory
utilization. Also, the recursive versions of most routines may execute a bit slower than
their iterative equivalents because of the overhead of the repeated function calls. In
fact, many recursive calls to a function could cause a stack overrun. Because storage for
function parameters and local variables is on the stack and each new call creates a new
copy of these variables, the stack could be overrun. However, you probably will not
have to worry about this unless a recursive function runs wild.
The main advantage to recursive functions is that you can use them to create clearer
and simpler versions of several algorithms. For example, the quicksort algorithm is
difficult to implement in an iterative way. Also, some problems, especially ones related

to artificial intelligence, lend themselves to recursive solutions. Finally, some people
seem to think recursively more easily than iteratively.
When writing recursive functions, you must have a conditional statement, such
as an if, somewhere to force the function to return without the recursive call being
executed. If you don't, the function will never return once you call it. Omitting the
conditional statement is a common error when writing recursive functions. Use
printf() liberally during program development so that you can watch what is going
on and abort execution if you see a mistake.

What Does main( ) Return?

The main() function returns an integer to the calling process, which is generally the
operating system. Returning a value from main() is the equivalent of calling exit()
with the same value. If main() does not explicitly return a value, the value passed
to the calling process is technically undefined. In practice, most C/C++ compilers
automatically return 0, but do not rely on this if portability is a concern.

Functions of Type void in C,C++

One of void's uses is to explicitly declare functions that do not return values. This
prevents their use in any expression and helps avert accidental misuse. For example,
the function print_vertical() prints its string argument vertically down the side of
the screen. Since it returns no value, it is declared as void.
void print_vertical(char *str)
{
while(*str)
printf("%c\n", *str++);
}
Here is an example that uses print_vertical() .
#include <stdio.h>
void print_vertical(char *str); /* prototype */

int main(int argc, char *argv[])
{
if(argc > 1) print_vertical(argv[1]);
return 0;
}
void print_vertical(char *str)
{
while(*str)
printf("%c\n", *str++);
}
One last point: Early versions of C did not define the void keyword. Thus, in
early C programs, functions that did not return values simply defaulted to type int.
Therefore, don't be surprised to see many examples of this in older code.