Turn Desktop View Off
by Dinesh Thakur

Given a function f(x) which is continuous in the interval [a, b] and satisfying the property hat f(a)*f(b) < 0, there exists a root of the function f(x) in the interval [a, b]. The bisection algorithm works as follows where t: and 8 are small values specified by the user.

 
by Dinesh Thakur

We have been writing "return 0;" as the last statement of every main function we have developed so far. But since main is the first function to start, no function calls main. So, the question that arises is - "Where does the return value of main go?" The value is returned and stored by the operating system and can be used to guide further actions by the operating system using shell scripts/batch files.



 
by Dinesh Thakur

The syntax of the function is as follows:

size_t strftime(char* s, size_t maxsize, const char* format, const struct tm * Tmptr);

 
by Dinesh Thakur

The syntax of the function asctime () is written as follows:

char * asctime( const stuct tm * Tmptr);

 
by Dinesh Thakur

The function prototype of ctime () is written in the following manner:

char *ctime( const time_t *Timeptr);

 
by Dinesh Thakur

The function localtime () converts the calendar time, i.e., the output of function time (), into a broken-down time expressed as local time, and returns the pointer to broken-down time. If the conversion into local time fails, the function returns NULL pointer. The prototype of the function is written in the following manner:

 
by Dinesh Thakur

The prototype of the function is written as given below,

time_t mktime (struct tm * Timeptr) ;

 
by Dinesh Thakur

The function difftime ()returns the difference between two calendar times in seconds, i.e., number of seconds elapsed between two calendar times time2 and time]. The calendar time represents the time elapsed since 00:00:00 hours 01 January 1970, GMT. The function returns the difference in seconds as a double number. The function prototype is written as follows:

 
by Dinesh Thakur

The function time () returns the current calendar time, i.e., the number of seconds elapsed since 00:00:00 hour January 1, 1970 GMT (or gmt are used alternatively) up to the execution of the function. The function prototype is written as follows:

 
by Dinesh Thakur

The clock function is used to determine the processor time in executing a program or part of a program. The header file <time.h> should be included in the program for its application. The function prototype is given below.

 
by Dinesh Thakur

The assignment operator (=) may be used on pointers of the same type. However, if the types of pointers (types of variables to which they point) are not same then we will have to do type casting of one of these to the type of the other to use assignment operator. However, the void pointer (void * ) can represent any pointer type. Thus, any type of pointer may be assigned to a void pointer. However, the reverse is not valid. A void pointer cannot be assigned to any other type of pointer without first converting the void pointer to that type.

 
by Dinesh Thakur

We may make use of typedef for declaring pointers to functions. Examine the following code:

typedef void(*PF) ();

 
by Dinesh Thakur

A function may have another function as one of its parameters besides having parameters of other data types. A function is a derived type object; its type is derived from the type of data it returns. Like arrays, the name of a function also holds the address of the function.

 
by Dinesh Thakur

If elements of an array are two-dimensional arrays, the array is called a three-dimensional array. Therefore, a three-dimensional array may be considered as an array of matrices. Let Arm be a 3-dimensional array or an array of matrices. The declaration of pointer and its initialization is carried out as given below.

 
by Dinesh Thakur

memchr (): The function searches for first occurrence of c through the first n characters of the string pointed to by S. If successful it returns pointer to c in the string. If not successful, it returns NULL or 0.

 
by Dinesh Thakur

Passing Arguments by Value

The arguments to a function may be passed on by value or by pointers. In the first case the copies of values of parameters are passed on to the function. The function can only manipulate these copies. So, the original values of parameters are not affected. Their values cannot be changed by the function because function does not know where the parameters are stored in the memory. For example, if the function simply swapped the values of its parameters, it is only swapping the copies of the values of parameters. The original values of parameters are not affected. In Program, the arguments are passed on to the function by values. These values are changed by the function and then an expression is evaluated. The function may be called any numbers of times, the output of the program as well as the data fed to function do not change. This is the benefit of passing the arguments by value.



 
by Dinesh Thakur

Strings are character arrays terminated by null character. Pointer to a single string may be declared in the same way as the pointer to a one-dimensional array.

 
by Dinesh Thakur

An individual element of an array may be regarded as a variable of the type declared in the declaration of the array. All the operators that can be applied to a variable of that type are applicable to the elements of array as well. Program, the array elements are subjected to arithmetic operations with the help of pointers.

 
by Dinesh Thakur

The name or identifier of an array is itself a constant pointer to the array. Its value is the address of the first element of the array. Thus, a pointer to an array may be declared and assigned as shown below.

 
by Dinesh Thakur

The values of pointer variables are unsigned integer numbers which are addresses of other variables. An unsigned integer is allocated 4 bytes of memory for its storage on a typical 32-bit system. Thus, pointers to all types of data occupy the same size of memory because the value of a pointer is the memory address - an unsigned integer. However, the variables to which the pointers point to, of course, occupy different sizes of memory blocks according to their types.



 
by Dinesh Thakur

The typedef may be used in declaration of a number of pointers of the same type. It does not change the type, it only creates a synonym, i.e., another name for the same type as illustrated below.

 
by Dinesh Thakur

We saw that pointer values may be assigned to pointers of same type. However, pointers may be type cast from one type to another type. In the following code lines, A is an int type variable, D is variable of type double, and ch is a variable of type char. Pa is declared as a pointer to int variables, Pd is declared as a pointer to double type variables, and Pc is declared as pointer to character type variables. Pa is assigned the value &A.

 
by Dinesh Thakur

The declaration of a pointer variable in C, comprises the type of pointer followed by indirection operator (*) which is followed by an identifier for the pointer. The declaration is done as follows:

 
by Dinesh Thakur

Except a few, most of the programs in C may be written with or without pointers. Then the question arises "Why use pointers if you can do without them?” Pointers are considered to be useful tools in programming because of the following reasons:

 
by Dinesh Thakur

The qualifier inline has been introduced in C-99 in order to achieve better compatibility between C and C++. If a small function needs to be called a large number of times in a program, the overburden of function call can make the program inefficient. Also, if the function code is put in the listing of the program wherever it is needed, it will not only make the program lengthy but also clumsy. With qualifier inline we need to define the function only once and the compiler substitutes the code wherever the function is called in the program. This eliminates the multiple function call as well as the drudgery of typing the function code a large number of times in the program. Thus, the qualifier inline makes a program efficient and also lessens the work of the programmer. An illustration of its application is illustrated below.

 
by Dinesh Thakur

In many cases and particularly in computer games, a seed number cannot be provided on every event. We need some function whose return value is always changing. Time is always changing; so, we make use of function time() defined in the header file <time.h>.



 
by Dinesh Thakur

Functions may be return type functions and non-return type functions. The non-return type functions do not return any value to the calling function; the type of such functions is void. These functions may or may not have any argument to act upon. A few illustrations of such functions are given below.

 
by Dinesh Thakur

A function definition comprises a head (or header) and a body. The header gives the information about type of function, name of function, and a list of parameters. The list of parameters comprises types and names of parameters enclosed in parentheses and separated by commas. An illustration of function definition is given below.



 
by Dinesh Thakur

Just like every variable in a program has an address, every function in a program too has an address. The name of the function can be used to obtain the address of a function. This address can be stored in a special type of variable which are pointers to functions. We had seen that even though all addresses are similar, there exist different types of address variables to store the addresses of different data types. For example, an int * variable stores the address of an int type variable and a long * variable stores the address of a long type variable. In a similar fashion, we will need different types of pointers to functions depending on the different types of functions. 



 
by Dinesh Thakur

Since a pointer to type T is analogous to an array of type T, a pointer to a pointer (T**) is analogous to an array of type T*, i. e., an array of pointers to type T. Each element in this array of pointers can be used further to point to an array of type T. Thus, a pointer to a pointer can be used to represent two-dimensional arrays, as illustrated in Fig.

 
by Dinesh Thakur

When a C program is compiled, the compiler allocates memory to store different data elements such as constants, variables (including pointer variables), arrays and structures. This is referred to as compile-time or static memory allocation. There are several limitations in such static memory allocation:



 
by Dinesh Thakur

We have defined a pointer as a variable which keeps the address of another variable. Since pointer is also a variable and is allocated memory space in which its value is stored, so we can have another variable which keeps the address of the pointer. That would be a pointer to a pointer. Such a pointer to a pointer is depicted in Fig, in which pointer ppa points to pointer pa, which in turn points to variable a. These pointers can be initialized as shown below.



 
by Dinesh Thakur

For example, consider the declarations of a three-dimensional array of size 2 x 3 x 4 given below.

 
by Dinesh Thakur

As we know, the expression x [i] to access the ith element of vector x can be equivalently written using pointer notation as * (x+i) . Thus, the expression a [i] [j] to access the ijth element of matrix a can be equivalently written as * (*(a+i) +j). Observe that this expression is obtained by applying twice the construct used for a vector. The address of a [i][j] is thus given as * (a+i) +j. These results are summarized below.

 
by Dinesh Thakur

So far we have studied functions that either return a value or have a void return type. A function can also return a pointer to a data item of any type. However, we must be careful while returning pointers from a function. A common mistake would be to return a pointer to a local variable or value parameter in that function as they are destroyed when control returns to the calling function.

 
by Dinesh Thakur

We can use typecast operator to convert the type of value returned by a pointer dereference expression to another suitable type. For example, if pa is a pointer to a variable a of type int, we can use typecast as (double) *pa to convert the integer value of *pa to type double.

 
by Dinesh Thakur

We have studied that an array is a powerful built-in data structure in the C language. It is a collection of data items of the same type stored in consecutive memory locations. An element of an array can be accessed using subscript notation, as in a [i ] , b [ i ] [ j ] , etc. Also, we can process entire arrays using loops and pass them to functions.



 
by Dinesh Thakur

We know that when a function is called, the parameters are passed to it by value, i.e., the values of arguments in a function call are copied to the parameters of the called function. Since a function parameter is a copy of the argument variable and is local to the function, any change in its value in the body of the function modifies only the local copy and not the corresponding argument in the calling function.

 
by Dinesh Thakur

To manipulate data using pointers, the C language provides two operators: address (&) and dereference (*). These are unary prefix operators. Their precedence is the same as other unary operators which is higher than multiplicative operators.

 
by Dinesh Thakur

The C language permits a pointer to be declared for any data type. The declaration of a pointer variable takes the following general form:



 
by Dinesh Thakur

Every variable has a type associated with it which decides the values that can be assigned to it and the operations that can be performed on it. In addition, we can specify a storage class for a variable which decides the following:

 
by Dinesh Thakur

Sometimes we may want that a function should not modify the value of a parameter passed to it, either directly within that function or indirectly in some other function called form it. This can be achieved using const parameters. Consider, for example, the function given below to calculate the sum of the first n integer numbers.

 
by Dinesh Thakur

We know that the C language uses the call by value mechanism to pass parameters and that a function parameter is a copy of the argument specified in the function call.

 
by Dinesh Thakur

The Tower of Hanoi problem consists of three poles, left, middle, and right. One of the poles (say, the left) contains n disks of different sizes placed on each other, as shown in Fig. The objective of the problem is to transfer all the disksfrom the left pole to right pole such that only one disk can be moved at a time (to any pole) and a larger  disk cannot be placed on top of a smaller disk.

 
by Dinesh Thakur

We have seen that a function can be called from another function. In fact, the C language allows a function to be called from within itself. Such a function is called a recursive function.

 
by Dinesh Thakur

This program defines a function named func which has two parameters, a and b of type int. Within its body, this function first prints these parameter values, initializes them to 30 and prints them again. The main function declares two variables, a and b of type int. It first prints the values of these variables and then calls the function func with variables a and b as its arguments. Finally, the main function prints the values of variables a and b again.

 
by Dinesh Thakur

Let us write a program to calculate the area' and circumference of a circle using user-defined functions. A function area_circum to accept the radius of a circle and print its area and circumference is given in Example. However, rather than printing the results in a function, it is good practice to return the results to the calling function, where they can be printed or used for further processing, if required. Hence, let us return the results (area and circumference) to the calling function.

 
by Dinesh Thakur

Before a function is called in a program, the system should know where to look for the function definition. In case of functions belonging to C standard library we include the relevant header files in which the function is defined. This is done above the main() function. In case of user-defined functions, a function may be defined above or below the main function, because, a function cannot be defined inside another function. If a function is defined above the main function, there is no need of a separate declaration of function. However, if the function is defined below the main function, it is a good programming practice to declare the functions being used above the main. A function declaration may be done by the function header or by its prototype.

 
by Dinesh Thakur

It accepts three parameters  x,y, and z, each of type double and returns a value of type double.

 
by Dinesh Thakur

The return statement is used to terminate the execution of a function and transfer program control back to the calling function. In addition, it can specify a value to be returned by the function. A function may contain one or more return statements. The general format of the return statement is given below.

 
by Dinesh Thakur

We may often require a horizontal line of a dash, underline or some other character to be printed, particularly when printing results in a tabular format. Consider a situation in which we require a line containing a fixed number of a specific character (e.g., 65 dash characters) to be printed several times.

 
by Dinesh Thakur

C allows programmers to define their own functions. Such functions are called user defined functions. In fact, the main function that must be present in every C program is a user-defined function. A programmer may define additional functions in the following situations:

 
by Dinesh Thakur

Recall that a function call takes the form

func_ name ( arg_list )

 
by Dinesh Thakur

The exit function can be used to terminate program execution and return a specified value as program status to the calling program, usually the operating system. A zero value indicates success. An example of the exit function indicating unsuccessful termination of a program is shown below.



 
by Dinesh Thakur

Random number generation is a very useful facility in many programming situations, particularly gaming and simulation. We can also use it to generate test data for programs, particularly when a large amount of data is required. This saves a lot of time on data entry.

 
by Dinesh Thakur

The atoi, atof and atol utility functions are used to perform string to numeric conversion. As indicated by the last letter in function names, these functions convert the argument string to an integer, floating and long number. Note that the argument string should contain a number of appropriate type as text, optionally preceded by whitespace. For example, the atof function can successfully convert strings such as" 1.23", "3.21El0", etc. The conversion stops when an inappropriate character is encountered in the input string. Thus, these functions will also be able to convert strings such as "-1. 23ABC", "3. 21E10ABc". If conversion is successful, these functions return the converted value, otherwise zero.

 
by Dinesh Thakur

Another commonly required string operation is that of converting the case of a given string. The C standard library does not provide any function for case conversion. However, some C implementations provide the strlwr and strupr functions. The strlwr function converts all characters in a given string to lowercase, whereas the strupr function converts all characters to uppercase. Typical calls to these functions take the following forms:

 
by Dinesh Thakur

The length of a string is defined as the number of characters in it excluding the null terminator. The strlen function returns the length of a specified string. A typical call to this function takes the following form:

 
by Dinesh Thakur

String concatenation is another basic string manipulation operation. In this, all the characters in a string, including the null terminator, are appended to the target string (i. e., copied at the end) replacing the null terminator in it. Thus, after the concatenation operation, the target string contains all the characters originally contained in it followed by those in the appended string. For example, if string sl contains "Pine" and string s2 contains "apple", then appending string s2 to sl modifies string sl to "Pineapple". Note that sl must have enough character positions to accommodate all the appended characters, including the null terminator.

 
by Dinesh Thakur

A standard C header file contains the declarations or prototypes of functions of a particular category. A function 'prototype usually specifies the type of value returned by that function, the function name and a list specifying parameter types as

 
by Dinesh Thakur

The trigonometric and hyperbolic functions provided in the standard mathematical library are listed in Table. Except for the atan2 function, which takes two arguments, all other functions take a single argument and each function returns a single value. Note that the function parameters and return values are of type double.

 
by Dinesh Thakur

A function is a subprogram that is used to perform a predefined operation and optionally return a value. Using functions, we can avoid repetitive coding in programs and simplify as well as speed up program development.

 
by Dinesh Thakur

pointer is a very powerful and sophisticated feature provided in the C language. A variable defined in a program the compiler allocates a space in the memory to store its value. The number of bytes allocated to the variable depends on its type. For instance, a character is allocated 1 byte, an int is, in general, allocated 4 bytes, and a float is also allocated 4 bytes on a typical 32-bit system. The memory in RAM is grouped into bytes; each byte has 8 bits of memory. Bytes are sequentially numbered. Thus, each byte is associated with a number which is its address. When a variable is declared, a block of memory is allocated to store its value. The address of a variable is the byte number of the first byte of the memory block allocated for value storage. The value of a pointer to the variable is also the address where the value is stored, i.e., its value is the byte number of the first byte of the memory block where the value of variable is stored. A pointer to a data item is nothing but the address of that data item. Address of a variable may be determined by application of address-of operator (&).We can store this address in a variable, called the pointer variable, and use it to manipulate that data item. For instance, let Marks be a variable of type int. On a 32-bit system, Marks would be allocated a memory block of size 4 bytes for storing its value.



 
by Dinesh Thakur

It would not be wrong to say that C language is a function-based language. The C Standard Library contains a large number of predefined functions which may be called in programs for carrying out various processes. Related functions are grouped together under various header files.

 
by Dinesh Thakur

A goto statement implements a local jump of program execution, and the longjmp() and setjmp() functions implement a nonlocal, or far, jump of program execution. Generally, a jump in execution of any kind should be avoided because it is not considered good programming practice to use such statements as goto and longjmp in your program.

 
by Dinesh Thakur

exit () is used to exit the program as a whole. In other words it returns control to the operating system.

After exit () all memory and temporary storage areas are all flushed out and control goes out of program. In contrast the return statement is used to return from a function and return control to the calling function.



 
by Dinesh Thakur

There are several format Specifiers available in printf. The format specifier used varies depending on the data type used for printing. The given below are some of the format Specifiers used with printf in C program.



 
by Dinesh Thakur

Pass by Value: In this method, the value of each of the actual arguments in the calling function is copied into corresponding formal arguments of the called function. In pass by value, the changes made to formal arguments in the called function have no effect on the values of actual arguments in the calling function.

 
by Dinesh Thakur

Argument: An argument is an expression which is passed to a function by its caller (or macro by its invoker) in order for the function(or macro) to perform its task. It is an expression in the comma-separated list bound by the parentheses in a function call expression.

 
by Dinesh Thakur

In C, we can supply arguments to 'main' function. The arguments that we pass to main ( ) at command prompt are called command line arguments. These arguments are supplied at the time of invoking the program.

 
by Dinesh Thakur

In C, program execution starts from the main() function. Every C program must contain a main() function. The main function may contain any number of statements. These statements are executed sequentially in the order which they are written.

 
by Dinesh Thakur

The main () is a special function that marks the beginning of the program. The type int before main signifies that function main () returns an integer value to the system on its completion.

 
by Dinesh Thakur

Printf function always returns the number of characters printed by the printf function. Let us see this in brief with an example:

 
by Dinesh Thakur

The default returns value from a function in int. In other words generally unless explicitly specified the default return value by compiler would be integer value from function. So when a programmer wants other than integer values to be returned from function then it is essential that the programmer takes some steps in doing this namely: