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. [Read more…] about C Program to find out root using Bisection
C Program for Return Value of main
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. [Read more…] about C Program for Return Value of main
C Program using a function declaration with an undefined parameter list
#include <stdio.h> #include <Stdlib.h> int ff(); int main() { clrscr(); ff(); ff(2); ff(2,3,4); return 3; } int ff(int i,int j,int k) { printf(" \n Hello world %d %d %d\n",i,j,k); return 3; }
[Read more…] about C Program using a function declaration with an undefined parameter list
Function strftime () in C
The syntax of the function is as follows:
size_t strftime(char* s, size_t maxsize, const char* format, const struct tm * Tmptr);
The function strftime () formats the broken-down time tm into an array pointed to by s according to the specified formatting string pointed to by format. The size of array is limited by maxsize. The formatting string consists of 0 or more conversion Specifiers and ordinary characters. The ordinary characters placed in the formatting string are copied into s without any conversion. Each specifier consists of a percent character(%) followed by a character that determines the action of conversion specifier. Some conversion Specifiers may have an additional modifier character E or 0 placed between% symbol and the conversion specifier character. The strftime() function returns the number of characters placed in the arrays, not including the terminating Null character.
Illustrates application of function strftime (). [Read more…] about Function strftime () in C
Function asctime () in C
The syntax of the function asctime () is written as follows: [Read more…] about Function asctime () in C
Function ctime () in C
The function prototype of ctime () is written in the following manner: [Read more…] about Function ctime () in C
Function localtime () in C
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: [Read more…] about Function localtime () in C
Function mktime () in C
Function difftime () in C
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: [Read more…] about Function difftime () in C
Function time () in C
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: [Read more…] about Function time () in C
Function clock () in C
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. [Read more…] about Function clock () in C
void Pointers in C
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. [Read more…] about void Pointers in C
typedef for Pointers to Functions in C
We may make use of typedef for declaring pointers to functions. Examine the following code: [Read more…] about typedef for Pointers to Functions in C
Pointer to Functions as Parameter in C
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. [Read more…] about Pointer to Functions as Parameter in C
Pointer to Three-Dimensional Arrays in C
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. [Read more…] about Pointer to Three-Dimensional Arrays in C
C Program for memchr () and memcmp () functions.
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. [Read more…] about C Program for memchr () and memcmp () functions.
Passing arguments by value and by Pointers in C
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. [Read more…] about Passing arguments by value and by Pointers in C
Pointers to Strings in C
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. [Read more…] about Pointers to Strings in C
C Program manipulating array elements with pointers
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. [Read more…] about C Program manipulating array elements with pointers
C Program pointers for traversing an array
One-Dimensional Array with Pointer in C
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. [Read more…] about One-Dimensional Array with Pointer in C
C Program to computes the area and perimeter of a rectangle using pointers.
C Program to size of pointers to all data types is same
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. [Read more…] about C Program to size of pointers to all data types is same
C Program to use typedef in pointer
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. [Read more…] about C Program to use typedef in pointer
Type Casting Of Pointers in C
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. [Read more…] about Type Casting Of Pointers in C
Declaration and Initialization of Pointers in C
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: [Read more…] about Declaration and Initialization of Pointers in C
Advantages of using pointers in C
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: [Read more…] about Advantages of using pointers in C
Inline Functions in C
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. [Read more…] about Inline Functions in C
C Program use of time() as seed number
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>. [Read more…] about C Program use of time() as seed number
Void Functions in C
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. [Read more…] about Void Functions in C