Beruflich Dokumente
Kultur Dokumente
The functions can be developed by different people and can be combined together as one
application.
Easy to code and debug.
Functions support reusability. That is, once a function is written, it can be called from any other
module without having to rewrite the same. This saves time in rewriting the same code.
A function prototype is the information to the compiler regarding the user-defined function
name, the data type and the number of values to be passed to the function and the return data type
from the function. This is required because the user-defined function is written towards the end of
the program and the main does not have any information regarding these functions.
The function prototypes are generally written before main. A function prototype should
end with a semicolon
Function Prototypes declare only the signature of the function before actually defining the
function. The signature includes function name, return type, list of parameter data types and
optional names of formal parameters.
General Form
An array is a collection of similar data types in which each element is located in separate
memory locations. It is a linear and homogeneous data structure. It means similar data types are
stored contiguously in memory and that too under one variable name.
1. One-dimensional arrays
2. Two-dimensional arrays
3. Multi-dimensional arrays
We can initialize the elements of array in the same way as the ordinary variables when they
are declared. The general form of initialization array is:
General Form
Examlpe
The statement:
will declare the variable number as an array of size 3 and will assign zero to each element.
Like any other variable, array must be declared before they are used so that the compiler can
allocate space for them in memory.
General Form
data_type array_name[row_size][column_size];
Where data_type specifies the type of element that will be contained in the array, such as int, float
or char. The row_size indicates the maximum number of rows and the column_size indicates the
maximum number of columns, and the product of row_size x column_size represents the maximum
number of elements that can be stored inside the array.
Example
int value[4][3];
declares the value to be an array containing 12 (4 x 3) integer elements. Any subscript [0][0]
to [3][2] are valid. As with the single-dimensional arrays, each dimension of the array is indexed
from zero to its maximum minus one; the first index selects the row and the second index selects the
column within that row.
General Form
data_type array_name[s1][s2][s3]...[sn];
Examples
int sales[4][5][12];
float table[5][4][5][3];
Here, sales is a three-dimensional array declared to contain 240 integer type elements.
Similarly table is a four-dimensional array containing 300 elements of floating-point type.
Define string.
In C language, the group of characters, digits, and symbols enclosed within double quotation
marks is called string. The string is always declared as character arrays. In other words, character
arrays are called strings.
C does not support strings as a data type. However, it allows us to represent strings as
character arrays. In C, therefore, a string variable is any valid C variable name and is always
declared as an array of characters.
char varname[size];
Examples
char city[10];
char name[30];
When the compiler assigns a character string to a character array, it automatically supplies a
null character (\0 ) at the end of the string. Therefore, the size should be equal to the maximum
number of characters in the string plus one.
Like numeric arrays, character arrays may be initialized when they are declared. C permits a
character array to be initialized in either of the following two forms:
Function Action
strcat() Appends one string to another.
strchr() Scans a string for the first occurrence of a given character.
strcmp() Compare two strings.
stricmp() Compare two strings without case sensitivity.
strcpy() Copies string src to dest.
strlen() Calculates length of a string.
strlwr() Converts s to all lowercase.
strupr() Converts s to all uppercase.
strncat() Appends a portion of one string to another.
strncmp() Compare portions of two strings.
strnicmp() Compare portions of two strings, without case sensitivity.
strncpy() Copies at most maxlen characters of src to dest.
strrev() Reverses all characters in s (except for the terminating null).
Directive Function
#define Defines a macro substitution
#undef Undefines a macro
#include Specifies the files to be included
#ifdef Test for a macro definition
#endif Specifies the end of #if
#ifndef Test whether a macro is not defined
#if Test a compile-time condition
#else Specifies alternatives when #if test fails
General Form
The preprocessor permits us to define more complex and more useful form of replacements.
General Form
Notice that there is no space between the macro identifier and the left parentheses. The
identifiers f1, f2,.......,fn are the formal macro arguments that are analogous to the formal arguments
in a function definition.
We can also use one macro in the definition of another macro. That is, macro definitions
may be nested.
Examples
The preprocessor expands each #define macro, until no more macros appear in the text. For
example, the last definition is first expanded into:
(((()*())*())*((()*())*()))
General Form
#undef identifier
Example
#undef TEST
This is useful when we want to restrict the definition only to a particular part of the program.
Define pointer and explain how to declare and assign pointer to an integer.
A pointer is a data type which stores the address of a variable. A variable is a name given to
a set of memory locations allocated to it. For every variable there is an address, the starting address
of its set of memory locations. If a variable called p holds the address of another variable i then p is
called as a pointer variable and p is said to point to i.
Example
int qty;
int *ptr; /* declaration */
ptr = &qty; /* initialization */
In C, it is possible to access and display the address of the memory location of a variable
using the & operator with variable name.
The pointer variable is needed to store the memory address of any variable and is denoted by
an (*) asterisk symbol.
Using a pointer without initializing it to any data may result in data corruption or even
program crash. To differentiate between initialized and uninitialized pointers, we usually set an
uninitialized pointer to NULL.
A pointer can be checked whether it is pointing to NULL using the == (Equal to) operator.
Program:
#include <stdio.h>
int main()
{
int i, fno, sno, res = 0;
printf("Enter the first number : ");
scanf("%d", &fno);
printf("Enter the second number : ");
scanf("%d", &sno);
for(i = 1; i <= fno; i++)
res = res + sno;
printf("Result = %d", res);
return 0;
}
Output:
It is possible to make a pointer to point to another pointer, thus creating a chain of pointers
as shown.
Here, the pointer variable p2 contains the address of the pointer variable p1, which points to
the location that contains the desired value. This is known as multiple indirections.
int **p2;
This declaration tells the compiler that p2 is a pointer to a pointer of int type. Remember, the
pointer p2 is not a pointer to an integer, but rather a pointer to an integer pointer.
We can access the target value indirectly pointed to by pointer to a pointer by applying the
indirection operator twice.