Macros are small functions (generally single line functions) which may be dealt with the help of preprocessor directive #define. Here, we shall discuss only the directive #define which is also used to define constants. A macro may or may not have parameters. An advantage of using a macro is that if a program involves a large number of the function calls of a small function the overburden of function calls can make the program inefficient; in case of macro, the code is substituted wherever the macro occurs. Thus, a programmer does not have to repeat the code again and again in the source code of the program while the function call is eliminated. However, a disadvantage of using macro is that data types are not included in the macro nor are these checked by the compiler. A few illustrations of macros are given below. [Read more…] about Pre-Processor Directive in C
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
Nested FOR Loops in C
Nested for loops have many applications, particularly, in programs dealing with sorting of lists, input/output of multi-dimensional arrays, etc. and also in the evaluation of expressions involving more than one parameter. The code for nested/or loops is given below. [Read more…] about Nested FOR Loops in C
Compound Conditions In For loop in C
So far, we have used only one variable in a for loop; however, more than one variable with different end values and with different modes of increments/decrements may also be used. In a compound for expression, the variables may be separated by a comma as illustrated for i and j below. [Read more…] about Compound Conditions In For loop in C
Endless for loop in C
In the expression for for loop the inclusion of the expressions are optional. However, two semicolons must be included. An endless for loop may be written as shown below. [Read more…] about Endless for loop in C
Compound Conditions in a While in C
The while expression may consist of a single expression (as it is generally done in most of the programs); however, we may also use compound conditions or expressions. Multiple expressions may be connected by a comma operator or by Boolean operators. If the expressions are simply connected by comma, it is the last expression that is evaluated. The expressions preceding the last are ignored. In the following while expression the first expression, i.e., j <4 is neglected. [Read more…] about Compound Conditions in a While in C
Nested While Statements in C
There are several situations where more than one parameter need to be varied over a range of values to obtain the desired results. For example, there may be a function having two variables, say x and y and it is desired to evaluate the function for different values of x and y. In such cases, for every value of x the values of y are varied over the range of values of y. This calls for nested while expressions as illustrated below. [Read more…] about Nested While Statements in C
Boolean Operators in C
Boolean operators AND, OR, and NOT are used to manipulate logical statements. Boolean operators are the core operators used in digital control systems as well as computer systems. AND and OR are binary operators, while NOT is a unary operator. Let A and B be two logical statements or variables representing logical statements. If a logical statement is true it may be assigned the value 1, and if a logical statement is false it may be assigned the value 0. Table gives details of the three operators AND, OR, and NOT. [Read more…] about Boolean Operators in C
Composite Assignment Operators in C
Arithmetic operators may be combined with the assignment operator to obtain composite assignment operators. An arithmetic operator is written first followed by the assignment operator but not vice versa. In all the composite operators there should not be any blank space between the symbols. For instance, in+= if we give space between+ and= (such as+ =),it may result in an error. [Read more…] about Composite Assignment Operators in C
Precision for Characters and Strings in C
The field width and precision setting may be used for characters and strings as well. However, these have different meaning. See the following code: [Read more…] about Precision for Characters and Strings in C
Explicit Display of + and – Signs in Output in C
In general, the sign is not displayed with positive values. However, if we desire to display the + sign, we may add it in the control string. If the display is desired to be left justified as well as with+ sign, add + or ++between the % sign and the conversion character; for example, the following code for integers. [Read more…] about Explicit Display of + and – Signs in Output in C
Precision Setting in C
Precision is specified by the number of digits after the decimal point for the outputs for float as well as double numbers. If precision is not specified, it would be according to the default setting in the computer which is generally 6 digits. The precision may be specified by a period(.) followed by a positive number equal to the number of digits desired. An illustration is given below. [Read more…] about Precision Setting in C
Type Casting in C
Type casting or type conversion refers to change of type of variables or pointers or user-defined objects from one type to another type. The need for type conversion arises in some operations involving two operands of different types, or even of same type. The example given below illustrates this concept. [Read more…] about Type Casting in C
C storage class Specifiers
An object is a space in memory with a name or an identifier. The lifetime of an object is the storage duration of the object in a program, that is, lifetime is the portion of program execution during which the object exists with a constant address in the program and retains the last stored value. Three types of storage durations are defined in C, that is, automatic, static, and allocated. [Read more…] about C storage class Specifiers
Arithmetic Operators in C
There are five arithmetic operators, +, -, *, I, and %, which respectively represent the processes of addition, subtraction, multiplication, division, and modulus. The modulus operator (%) gives the remainder when one integer is divided by another integer. All of the five operators have been described with examples of codes in Table. [Read more…] about Arithmetic Operators in C
sizeof() operator in C
The sizeof operator is another method to determine the storage requirements of any data type of variable during the execution of a program. For example, we can use the expression. [Read more…] about sizeof() operator in C
Data Types in C
Data types in C are specified or identified as the data storage format that tells the compiler or interpreter how the programmer enters the data and what type of data they enter into the program. Data types are used to define a variable before use in a program. Data types determine the size of the variable, space it occupies in storage. [Read more…] about Data Types in C
Function of printf and scanf in C
The input into a program and output from a program is the basic requirements of any useful program. For input from standard input device, i.e., keyboard, the function scanf () is used and for output to the standard output device, i.e., monitor, the function printf () is used. Both these functions can take any number of arguments. The first argument is the formatting string enclosed between double quotes and consists of conversion characters. For printf () function, the formatting string may also contain any text that needs to be displayed on the monitor along with the values of variables. For example, if we want to display the value of an integer variable n, we may write the code as [Read more…] about Function of printf and scanf in C
Identifiers in C Language
When we declare a variable name and its type, the compiler allocates a block of memory for placing its value. In fact, for the computer, this allocated block of memory is the variable and it recognizes it by its name. The sizes of memory blocks allocated for different types of data may vary on different computers depending on the hardware, the operating system, and the compiler used. The names of variables or identifiers should be carefully selected. The general guidelines are as follows: [Read more…] about Identifiers in C Language
Escape Sequences in C
Escape sequences are basically control characters used for formatting the output. These are combinations of a backslash and a character in lowercase. We have already used “\n”, which shifts the curser to the next line. Table lists the escape sequences defined in C language. [Read more…] about Escape Sequences in C
C Program to use ferror, perror and fputs
To read a string in a file we can use fgets () whose prototype is: [Read more…] about C Program to use ferror, perror and fputs
Pointers to Functions in C
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.
C allows variables and functions accessed through pointers! We can then do things like, for example, pass a function as an argument to another function. A pointer to a function has the following statement:
A pointer to a function may be declared as below.
type ( *ientifier_for _pointer ) ( types_of_parameters_of_function);
The first word type in the above declaration refers to the type of data that the function returns. Then the parentheses contain the indirection operator (*) followed by the name or identifier of pointer. The next pair of parentheses contains the types of the parameters of the function. The names of parameters may not be given in the declaration of a prototype. The names of parameters are required when the function is being defined. Let pFunction be the name of pointer to a double function and has a parameter of type int. The pointer to such a function may be declared as below.
double (*pFunction ) (int)
In the above declarations, *pFunction has been enclosed in parentheses because otherwise there would be only one parentheses that encloses the parameter and which will have higher precedence than(*) operator. Thus, an expression such as double * pFunction (int) ; would mean that function pFunction takes an int arguments and returns a pointer to a double number. Therefore, it is necessary to enclose* pFunction in parentheses.
You should note that the above declaration of pointer is not tied to any particular function. The declaration of pFunction is declaration for pointer to any function that is of type double and has an int as its parameter. It will be pointer to a particular function only after it has been so initialized with address of that function.
Like an array the name of a function is also a constant pointer to the function. Therefore, a pointer to a function may be initialized by the name of the function. Let Function1, and Function2 be any two functions which are of type double and take an int as parameter. Thus, their prototypes may be written in the following manner:
double Function1 (int);
double Function2 (int);
Now we may initialize the pointer pFunction as below.
pFunction = Function1;
With this initialization, the pointer points to Function1. Also, we may as well assign the following value to pFunction:
pFunction = Function2;
After the above assignment, the pointer pFunction now points to Function2.
A function may be called by dereferencing its pointer. Thus, let n be an integer. We may call the function Function2 (after the above assignment) as given below.
int n =10;
(*pFunction)(n); II calling the Function2
Since the pointer identifier pFunction and function name both carry the address of function, the dereference operator is in fact not necessary when calling a function. The function may as well be called by pointer identifier (name) itself as shown below.
pFunction (n);
pFunction (n) is a call to the function whose address is currently held by pFunction.
Program illustrates the use of pointers to functions. The user has a choice of three functions and choice is facilitated by the pointer and a switch statement.
#include <stdio.h>
#include <string.h>
#include <conio.h>
void PrintString (char * str, int (* func) (const char *));
main (void)
{
char String [20] = "C Programming";
int (*p) (const char *); /* Statement pointer to function Function pointed and whole and receives as parameter a constant string */
clrscr();
p = puts; /* P The pointer changes to point to the function puts which has the following prototype: int puts (const char *) */
PrintString (String, p); /* The pointer is passed as parameter to PrintString */
return 0;
}
void PrintString (char * str, int (* func) (const char *))
{
(* func) (str); /* Call the function through the function pointer */
func (str); /* Also valid way of making the call to function puts through pointer to function func */
}
See who made the assignment puts a p simply using:
p = puts;
From this we conclude that the name of a function (without the parentheses) is in fact the address of that function! Notice, too, the two alternative ways of calling a function through a pointer. In the above program, we made this call by:
(* func) (str);
and
func (str);
These forms are equivalent to each other.
In addition, the program, the PrintString () function uses any function func to print the string on the screen. The programmer can then provide not only the string but also the function that will be used to print it. In main () we see how we can allocate the pointer to p functions, the address of the puts () function C.
In short, to declare a pointer to function, we can assign to this pointer the address of a function and can also call the function pointed through it. We can not do some things we did with “normal” pointers, for example, increment or decrement a pointer to function.
Union in c
As we already know, a structure is an ordered list of elements of either the same or different types. A union is similar to a structure but with one major difference that the structure stores all its members one after another, whereas the union can store only one member at a time, since all the members in a union are stored beginning from the same memory location. Thus, the total memory required to store a union is the same as that required for the largest member in it. The union is useful when we have to store one of the alternative values of different types and yet conserve space. [Read more…] about Union in c
typedef in c
The typedef feature allows us to give an alternative (possibly short and more meaningful) name to an existing data type and improve program readability. For example, instead of using the int data type to declare variables to represent marks in three subjects, we can associate a more meaningful name (say Marks)for the int data type using typedef as: [Read more…] about typedef in c
Enumerated Types in C
Consider that we need to work with the colours in a rainbow, e. g., to paint a rainbow on the screen. We thus have to work with seven colours, namely, violet, indigo, blue, green, yellow, orange and red. These colours can be represented using integer values starting with 0. This enables us to use various program constructs such as conditions or loops to process these colours. However, programs written using such code often become difficult to understand as can be seen from the statement given below. [Read more…] about Enumerated Types in C
Bitwise Operators in C
All the objects stored in a computer are ultimately converted into binary numbers, sequences of 0s and 1s. Each digit in a binary number store on one bit of the computer memory. A bit define as the smallest unit of memory in a computer. The computer manipulates a number by manipulating the bits on which the number stored. In control systems also we often need to use operators to manage bits. [Read more…] about Bitwise Operators in C
C Program to create HSC examination marks data file
Let us define a structure student to store the HSC marks data of a student as follows: [Read more…] about C Program to create HSC examination marks data file
C Program to Text file containing prime numbers in a given range
The fscanf () function works as scanf (). The difference is that fscanf () reads a file and not the computer keyboard. prototype: [Read more…] about C Program to Text file containing prime numbers in a given range
The ungetc Function in C
If a character read from an input stream is not appropriate for the current operation, we can push it back to the stream using the ungetc function. A subsequent character read operation (such as fgetc or getc) on that stream will return this character. The prototype of the ungetc function is given below. [Read more…] about The ungetc Function in C
The fgets and fputs Functions in C
The fgets function reads a sequence of character, i. e., a character string from an input stream. Its prototype is given below. [Read more…] about The fgets and fputs Functions in C