Académique Documents
Professionnel Documents
Culture Documents
Day 2
by Supreet Singh
Arrays
Arrays
• An array is a series of variables, all being same
type and size
• Each variable in an array is called an array element
• All the elements are of same type, but may contain
different values
• The entire array is contiguously stored in memory
• The position of each array element is known as
array index or subscript
• An array can either be one dimensional (1-D) or two
dimensional (2-D) or Multi-dimensional
Declaring a 1-D Array
Syntax:
data-type arrayname[size];
Example:
int aiEmployeeNumbers[6];
float afSalary[6];
When an array is declared inside a function without initializing it, the elements
have unknown (garbage) values and outside the function the elements have
zero/default values
Declaring and Initializing arrays (1 of 2)
Arrays can be initialized as they are declared
Example:
int aiEmployeeNumbers[] = {15090, 15091, 15092,
15093,15094, 15095};
A 2-D array should be declared by specifying the row size and the
column size
To access the individual elements, the row and the column should
be supplied
The table type visualization of rows and columns is therefore only for
convenience
Declaring and using 2-D Arrays
A 2-D array can be declared by specifying the maximum size for rows and
columns
Syntax:
data type arrayname [Row Size][Column Size];
Example:
int aiEmployeeInfo[3][2];
The above declaration declares aiEmployeeInfo with 3 rows and 2 columns
To access the individual elements row index (starts from zero) and the
corresponding column index (starts from zero) should be supplied
Example:
printf(“%d”,aiEmployeeInfo[0][1]);
The above printf references the information at 0th row 1st column
Initializing 2-D Arrays(1 of 2)
A 2-D array can be initialized as given below:
Example:
int aiEmployeeInfo[][3]= {101,1,102,1,103,2};
Since there are six initial values and the column size is 3, the
number of
rows is taken as 2
8FFE 5
&iNumber
Pointers - Address of Operator(3 of 5)
To print the address of a variable, precede the variable with an
Since an address is an unsigned integer,
ampersand (&) %u is used as a conversion specifier
Example:
int iNumber = 100;
printf(“The value is %d\n”,iNumber);
printf(“The address is %u\n”,&iNumber);
printf(“The address in hexa decimal is
%x\n”,&iNumber);
Example:
/* Initializing pointer to NULL */
int *piCount = NULL;
Strings
Strings (1 of 2)
A string is a series of characters in a group that
occupy contiguous memory
Example:
“CDAC”
“Information Technology”
In
memory, a string ends with a null
character ‘\0’
Syntax:
unsigned int strlen (char string[]);
Here string[ ] can be a string constant or a character pointer or a character
Example:
strlen(“Programming Fundamentals”); returns 24
scanf(“%s”, acItemCategory);
scanf(“%s”, &acItemCategory[0]);
Both the input functions are valid. The first one passes the base
address (Address of the first element) implicitly
The second function passes the address of the first element explicitly
gets(acItemCategory);
gets(&acItemCategory[0]);
Syntax:
strcpy (Dest_String , Source_String);
Here Dest_string should always be variable
Example:
char acCourseName[40];
strcpy(acCourseName , “C Programming”);
Syntax
strcat( Dest_String_Variable , Source_String ) ;
In this, the Destination should be a variable and Source_String can
either be a string constant or a variable.
The contents of Dest_String is concatenated with Source_String
contents and the resultant string is stored into Dest_String variable.
Example:
char acTraineeFpCourse [50] = “The course is “;
strcat(acTraineeFpCourse,”Oracle 10G”);
Syntax:
int strcmp( String1 , String2 );
Here both String1 and String2 can either be a variable or a string
constant
strcmp() Function (2 of 2)
strcmp() function returns an integer value
Example:
strcmp(“My Work”, “My Job”); returns a positive
value
strcmpi() Function
strcmpi()function is same as strcmp()
function but it is case insensitive
Example:
strcmpi(“My WoRk” , “MY work”); returns zero
Functions
Functions
A function is a section of a program that performs a specific task
Function
call
Advantages of Functions
The functions can be developed by different people and can
be combined together as one application
After the function performs the task, it may send back the results to
the calling function
A function can return back only one value to the calling function
through a return statement
Function Definition :
Implementing the function or writing the task of the function
Consists of
• Function Header
• Function Body
Syntax:
Return_data_type FunctionName (data_type arg1,
data_type arg2,...,data_type argn );
Example:
int fnValidateDate(int iDay,int iMonth, int iYear);
In the above example, iDay, iMonth and iYear are optional. The
same can also be written as:
Syntax:
return (expression) ;
Example:
return(iNumber * iNumber);
return 0;
return (3);
return;
return (10 * iNumber);
Calling User-Defined Functions (1 of 2)
A function is called by giving its name and passing the
required arguments
Calling
a function which does not return
any value
/* Calling a function */
fnDisplayPattern(15);
Calling
a function that do not take any
arguments and do not return anything
/* Calling a function */
fnCompanyNameDisplay();
Function Terminologies
Function Prototype
void fnDisplay() ;
Calling Function
int main(int argc, char **argv){
fnDisplay();
return 0;
} Function Call Statement
void fnDisplay(){
printf(“Hello World”);
}
Called Function
Function Definition
Formal and Actual Parameters
The variables declared in the function header
are called as formal parameters
Actual Arguments
Functions – Example (2 of 2)
Formal Arguments
Return value
Parameter Passing Techniques
When a function is called and if the
function accepts some parameters, then
there are two ways by which the function
can receive parameters
Pass by value
Pass by reference
Pass by Value (1 of 3)
When parameters are passed from the
called function to a calling function, the
value of the actual argument is copied onto
the formal argument
main() fnUpdateValues()
100 115
200 240
void fnUpdateValues(int
*piNumber1, int *piNumber2){
*piNumber1 = *piNumber1 + 15;
a fnUpdateValues()
main()
115 Address of
iValue1
240 Address of
iValue2