Basic C Programming
Education & Research
For Infosys internal use
© 2009 Infosys Technologies Limited
© 2009 Infosys Technologies Limited
Confidential Information
•
•
•
•
•
•
This Document is confidential to Infosys Technologies Limited. This document contains
information and data that Infosys considers confidential and proprietary (“Confidential
Information”).
Confidential Information includes, but is not limited to, the following:
– Corporate and Infrastructure information about Infosys;
– Infosys’ project management and quality processes;
– Project experiences provided included as illustrative case studies.
Any disclosure of Confidential Information to, or use of it by a third party, will be damaging to
Infosys.
Ownership of all Infosys Confidential Information, no matter in what media it resides,
remains with Infosys.
Confidential information in this document shall not be disclosed, duplicated or used – in
whole or in part – for any purpose without specific written permission of an authorized
representative of Infosys.
This document also contains third party confidential and proprietary information. Such third
party information has been included by Infosys after receiving due written permissions and
authorizations from the party/ies. Such third party confidential and proprietary information
shall not be disclosed, duplicated or used – in whole or in part – for any purpose without
specific written permission of an authorized representative of Infosys.
For Infosys internal use
© 2009 Infosys Technologies Limited
2
Important Note:
• For better understanding of concepts you are advised to go through prerequisites (Programming Fundamentals), pre-reading/demoprograms/assignments of this course available on certification portal. Prereading, Demo-programs and assignments are gradable components of this
course.
• Knowledge of UNIX or GNU/Linux commands is essential as all demo
programs are tested using gcc 4.0
• You may find attending classroom/virtual classroom sessions, e-learning
modules (available on TAL), helpful in preparation.
• In case of any technical doubts, write to Smita_Khode or Archana_Dhande
For Infosys internal use
© 2009 Infosys Technologies Limited
3
Topics Covered
• Programming Methodologies
• Array
• single dimensional
• Multi-dimensional
• Strings
• Structures, Unions and Enums
• Functions in C
• Scope of Variables
• Program Stack and Heap
• Parameter Passing Techniques
• The concept of Recursion
• Command line arguments
For Infosys internal use
© 2009 Infosys Technologies Limited
4
Topics Covered(cont…)
• Pointers
• NULL pointer
• Pointer-to-pointer
• Function pointers
• String Functions
• Control Structures
For Infosys internal use
© 2009 Infosys Technologies Limited
5
Programming Methodologies
• Classification based on behavior
• Sequential Approach
• Event Driven Approach
• Classification based on Structure
• Structured Programming
• Object Oriented Programming
For Infosys internal use
© 2009 Infosys Technologies Limited
6
Different Stages of a Program Life Cycle
C Files
prog.c
help.c
(Source Code)
strtest.c
Header Files
stdio.h
func.h
Text Editor
C COMPILER
Pre-Processor
COMPILER
Windows: cl.exe
UNIX: cc
LINUX: gcc
Intermediate
Code
Software Developer
Compiler
1011
010101
110
Object Files
0101
101
11
prog.obj help.obj
strtest.obj
101101
101010
011101 10101 010
1010101
101
10011
111
Runtime
Libraries
LINKER:
Windows: link.exe
UNIX: ld
LINUX: ld
Linker
Executable File
(prog.exe)
For Infosys internal use
© 2009 Infosys Technologies Limited
7
Arrays
• An array is a homogeneous collection of variables.
• Each variable in an array is called an element of the array
• 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 integer array looks like this:
1
5
1509
-255
125
0
For Infosys internal use
1
2
3
4
1
5090
2
1509
1023
© 2009 Infosys Technologies Limited
8
Declaring Arrays
• In C, an array can be of any basic data type
Example:
int aiEmployeeNumbers[6];
float afSalary[6];
• The array index starts with zero
• The valid array indexes for the above declared array is 0 to 5
• When an array is declared without initializing it, the elements have
unknown (garbage) values
For Infosys internal use
© 2009 Infosys Technologies Limited
9
Memory Representation of an Integer Array
Memory
Contents of
Memory
Location
Address
5
4
3
2
1
0
00
03
FF
00
00
00
7D
Integer (5)
How an integer array
looks in memory?
2A 3025
2A 3026
2A 3027
2A3028
2A 3029
2A 302A
2A 302B
Integer (4)
Uninitialized Integer Array
(Conceptual)
Integer (3)
ay
Arr ex
Ind
Integer (2)
8
125
Integer (1)
1209
-255
15
1023
00
00
04
B9
00
00
00
08
00
00
00
0F
80
00
00
7C
00
Integer (0)
/* Declare array */
int aiEmployeeNumbers [6];
2A 3014
2A 3015
2A 3016
2A 3017
2A 3018
2A 3019
2A 301A
2A 301B
2A 301C
2A 301D
2A 301E
2A 301F
2A3020
2A 3021
2A 3022
2A 3023
2A 3024
For Infosys internal use
© 2009 Infosys Technologies Limited
10
Declaring and Initializing arrays (1 of 2)
• Arrays can be initialized as they are declared
• Example:
int aiEmployeeNumbers[ ] = {15090, 15091, 15092, 15093,15094, 15095};
• The size in the above case is optional and it is automatically computed
• In the above example, size of the array is 6 and it occupies 6 * 4 = 24 bytes
1
1
4
2
3
4
5
1509
2
1509
5
1
0
1
5090
1509
5093
1509
For Infosys internal use
© 2009 Infosys Technologies Limited
11
Declaring and Initializing arrays (2 of 2)
• When an array is partially initialized, the remaining elements will be zero or
garbage values
Example:
int aiEmployeeNumbers[6] = {15090, 15091, 15092};
• In the above example, the array indexes from 3 to 5 may contain zero or
garbage values
e
itializ
Unin lues
va
1509
1
1023
-255
125
0
1
2
3
4
5
0
1509
2
1509
d
For Infosys internal use
© 2009 Infosys Technologies Limited
12
Using Array Elements
• An array can be accessed by giving the respective index
• Syntax:ArrayName[index]
• Example:
float afSalaries[6]; /* Declare an array of six floats */
afSalaries[0] = 12500.00; /* Assign a value to element at index 0 */
afSalaries[1] = 15000.00; /* Assign a value to element at index 1 */
afSalaries[5] = 25000.00; /* Assign a value to element at index 5 */
For Infosys internal use
© 2009 Infosys Technologies Limited
13
Using Array Elements ….
/* Print the salary at array index 0 */
printf (“Salary at Index 0 is %f \n”, afSalaries[0]);
• Arrays can also be referenced as index[ArrayName]
• The statement 5[afSalaries] is a valid statement
For Infosys internal use
© 2009 Infosys Technologies Limited
14
Two Dimensional Array
Two Dimensional arrays are described as array of arrays.
A two dimensional array can be imagined as a bi-dimensional table of a
concrete data type which is unique.
•
0 1
2
0
1
2
For Infosys internal use
© 2009 Infosys Technologies Limited
15
Declaration
Syntax
datatype name[rows][columns];
Ex: float MATRIX [3][5];
Matrix represents a two dimensional array of 3 rows ,5 columns having
values of type float.
For Infosys internal use
© 2009 Infosys Technologies Limited
16
Initialization
Since two dimensional arrays are treated as array of arrays, it can be
initialized as follows:
float MATRIX[3][5] =
{ {12.89, 11.23, 34.89, 11.23, 34.56 },
{1.9, 2.9, 3.9, 4.9, 5.9 },
{2.9, 3.9 , 4.9 , 5.9 , 6.9 }
};
For Infosys internal use
© 2009 Infosys Technologies Limited
17
Multi Dimensional Arrays
For Infosys internal use
© 2009 Infosys Technologies Limited
18
Multi Dimensional Arrays
Consider
char *p[4];
‘p’ is an
“Array of
Pointers”
Starting
Addresses
........
p[0]
p[1]
p[2]
p[3]
2000
3400
5200
1800
w
e
/0
t
o
o
/0
l
o
v
e
/0
~
w
e
e
.
.
/0
~
For Infosys internal use
© 2009 Infosys Technologies Limited
19
Multidimensional Arrays and Functions
• A simplest way of passing a multidimensional array to a function is to
declare it in exactly the same way in the function as it was declared in the
caller.
If we were to call void func(MATRIX);
then we might declare
void func(int
{
...
}
Mult[30][50])
For Infosys internal use
© 2009 Infosys Technologies Limited
20
Multidimensional Arrays and Functions
• When multidimensional array is declared in the function definition, first
dimension of rows are optional
•
void func(float M[][7]) {
...
}
For Infosys internal use
© 2009 Infosys Technologies Limited
21
Thoughts
•
•
•
•
•
•
•
•
What are the different approaches of programming?
How is linear approach different from event approach?
How is structured approach different from other approaches?
What are the different stages in program development?
What is array?
How do you initialize and access array?
How is single dimensional array different from 2D array?
How are the array elements are stored in memory?
For Infosys internal use
© 2009 Infosys Technologies Limited
22
Strings
• A string is a series of characters in a group that occupy contiguous memory
Example:
“My Training”
“Programming Fundamentals”
• A string should always be enclosed within double quotes (“”)
• In memory, a string ends with a null character ‘\0’
• Space should be allocated to store ‘\0’ as part of the string
• A null character (\0) occupies 1 byte of memory
For Infosys internal use
© 2009 Infosys Technologies Limited
23
Declaration of Strings (1 of 2)
char variablename [Number_of_characters ];
• Example:
char acEmployeeName[20];
Here 20 implies that the maximum number of characters can be 19 and one
position is reserved for ‘\0’
Since a character occupies one byte, the above array occupies 20 bytes (19
bytes for the employee name and one byte for ‘\0’)
/* Declaring a string as a character pointer */
char *pcInfy = “Infosys Tech”;
/* Declaring a string as a character array */
char acInfy[ ] = “Infosys Tech”;
Think why ‘\0’ requires only one byte!!
For Infosys internal use
© 2009 Infosys Technologies Limited
24
Declaration of Strings (2 of 2)
acInfy
char acInfy[] = “Infosys Tech”;
2A0E34
(OR)
Points
to
char *pcInfy = “Infosys Tech”;
(OR)
char acInfy[13] = “Infosys Tech”;
g
Strin
End
g
Strin
Start
Po
int
to s
2A0E34
0
o
f
n
I
1
2
3
4
5
c h
T e
s
s y
7
6
y
(Arra
8
\0
9
10
11
12
es)
Indic
How a String
looks in Memory
acInfy
Memory
Address
Data
Decimal (Hex)
Character/
Glyph
2A0E34
073 (0x49)
2A0E35
110 (0x6E)
2A0E36
102 (0x66)
2A0E37
111 (0x6F)
2A0E38
115 (0x73)
2A0E39
121 (0x79)
2A0E3A
115 (0x73)
2A0E3B
032 (0x20)
2A0E3C
084 (0x54)
2A0E3D
101 (0x65)
2A0E3E
099 (0x63)
2A0E3F
104 (0x68)
e
c
h
2A0E40
000 (0x00)
\0
I
n
f
o
s
y
s
(Space)
T
For Infosys internal use
© 2009 Infosys Technologies Limited
25
Storage of strings in memory
acItemCategory
char acItemCategory[15]= “Books”;
66(B)
111(o)
111(o)
107(k)
115(s)
0(\0)
Garbage value
Garbage value
Garbage value
Garbage
Garbage
Garbage
Garbage
Garbage
Garbage
value
value
value
value
value
value
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
800A
800B
800C
800D
800E
For Infosys internal use
© 2009 Infosys Technologies Limited
26
Null Terminators in Strings
• A null character marks the end of the string
acInfy
2A0E34
g
Strin
End
g
Strin
Start
Po
int
to s
0
y s
s
o
f
n
I
1
2
3
4
5
c h
T e
6
7
8
9
10
Points
to
\0
11
12
Memory
Address
Data
Decimal (Hex)
2A0E34
073 (0x49)
2A0E35
110 (0x6E)
2A0E36
102 (0x66)
2A0E37
111 (0x6F)
2A0E38
115 (0x73)
2A0E39
121 (0x79)
2A0E3A
115 (0x73)
2A0E3B
032 (0x20)
2A0E3C
084 (0x54)
2A0E3D
101 (0x65)
2A0E3E
099 (0x63)
2A0E3F
104 (0x68)
s
(Space)
T
e
c
h
2A0E40
000 (0x00)
\0
2A0E34
acInfy
Character/
Glyph
I
n
f
o
s
y
For Infosys internal use
© 2009 Infosys Technologies Limited
27
Static initialization of Strings
• char acItemCategory[15]=“Greeting Cards”;
In the above declaration, array size is specified according to the number
of characters in the string. It includes one space for ‘\0’
• char acItemCategory[15] =“Ornaments” ;
Here the size is more than the number of characters which is valid.
• char acItemCategory[ ]=“Groceries”;
Here the size of the array is computed automatically which is 10. String
contains a 9 characters totally and 1 for ‘\0’;
For Infosys internal use
© 2009 Infosys Technologies Limited
28
Static initialization of Strings
• char acItemCategory[3]=“Books”;
Here the size specified is 3. But the number of characters in the string is 5.
This is invalid.
• char acCompany[ ]={‘I',‘n',‘f',‘o’,’s',‘y',‘s’,'\0'};
Here the character constants are supplied to initialize the string.
.
For Infosys internal use
© 2009 Infosys Technologies Limited
29
Printing Strings to console
• Using Character pointer:
char *pcProg = “Programming Fundamentals”;
printf(pcProg);
printf(“This is %s course”,pcProg);
• Using Character Array:
char acProg[ ] = “Programming Fundamentals”;
printf(acProg);
OR
printf(“%s”, acProg );
printf(“This is %s course”,acProg);
• Printing a string as part of a formatted string
int iCourseId = 27;
char *pcProg = “Programming Fundamentals”;
/* print the courseId (Int) and Course name (String – char*) */
printf(“The Id of this course is %d and this course is %s \n”,iCourseId,
pcProg);
For Infosys internal use
© 2009 Infosys Technologies Limited
30
Thoughts
•
•
•
•
•
•
•
What is string?
How is string represented or stored in memory?
What is null character?
How the strings are initialized?
Can size of string can be smaller than the array size?
Which function is used to print Strings to console?
Which header file contains string related functions?
For Infosys internal use
© 2009 Infosys Technologies Limited
31
Structures (1 of 2)
• Data used in real life is complex
• The primitive data types which are provided by all programming languages
are not adequate enough to handle the complexities of real life data
• Examples:
Date: A date is a combination of day of month, month and year
Address: Address of a person can consist of name, plot number, street, city,
pin (zip) code and state.
Account Details: Bank account information can contain the account number,
customer ID and Balance
For Infosys internal use
© 2009 Infosys Technologies Limited
32
Structures (2 of 2)
• A structure is a set of interrelated data
• A structure (also known as data structure or struct in short) in C, is a set of
primitive data types which are related to business and are grouped together
to form a new data type
• A structure is a mechanism provided by the language to create custom and
complex data types
For Infosys internal use
© 2009 Infosys Technologies Limited
33
Declaring a Structure (1 of 3)
• In C, a structure can be declared using the ‘struct’ keyword
• The set of variables that form the structure must be declared with a valid
name similar to declaring variables
• Each variable inside a structure can be of different data type
• Syntax:
struct structure_name
{
data type member 1;
data type member 2;
……
data type member n;
};
• Semicolon is required in the end of the structure
For Infosys internal use
© 2009 Infosys Technologies Limited
34
Declaring a Structure (2 of 3)
• Example:
Date is a simple data structure, but not available as a built-in data type in C
A date has three components:
• Day of month (int, Range: 1 to31)
• Month (int, Range: 1to 12)
• Year (int, four digits)
struct
short
short
short
};
date {
iDay;
iMonth;
iYear;
• In the date structure, iDay, iMonth and iYear are member variables
For Infosys internal use
© 2009 Infosys Technologies Limited
35
Declaring a Structure (3 of 3)
• A structure is generally declared globally above function ‘main’
• The member variables cannot be initialized within a structure
• The structure is allocated memory only after declaring a variable of type
structure
For Infosys internal use
© 2009 Infosys Technologies Limited
36
Accessing Member Variables of a Structure (1 of 2)
• Each member variable in a structure can be accessed individually
• Once a structure is declared, it can be used just like any primitive data type
• Inorder to access the structure members, a variable of structure should be
created
• Example:
struct date sToday;
• To access individual members of the structure, the ‘.’ operator is used as
shown below :
sToday.iDay = 30;
sToday.iMonth = 6;
sToday.iYear = 2005;
For Infosys internal use
© 2009 Infosys Technologies Limited
37
Accessing Member Variables of a Structure (2 of 2)
int main (int argc, char** argv) {
/* Declare two instances of date structure */
struct date sToday, sTomorrow;
/* Set 'day', 'month' and 'year' in instance sToday */
sToday.iDay = 30;
sToday.iMonth = 6;
sToday.iYear = 2005;
/* Set sTomorrow's values */
sTomorrow.iDay = 1;
sTomorrow.iMonth = 7;
sTomorrow.iYear = 2005;
/* Print the contents of structure */
printf ("Today, the date is: %d/%d/%d\n",
sToday.iDay, sToday.iMonth, sToday.iYear);
printf ("Tomorrow,the date will be: %d/%d/%d\n",
sTomorrow.iDay, sTomorrow.iMonth, sTomorrow.iYear);
}
}
For Infosys internal use
© 2009 Infosys Technologies Limited
38
typedef Keyword (1 of 2)
• C allows one type of data to be renamed with a different name using the
‘typedef’ keyword (typedef is a short form of ‘define type’)
• A struct date had to be instantiated by using:
/* Create an instance of date structure */
struct date today;
• Alternately, typedef can be used to create the date structure
• It is a good practice while defining the structure and doing a typedef to keep
the tag name same as the structure’s name with an underscore (‘_’) prefix
For Infosys internal use
© 2009 Infosys Technologies Limited
39
typedef Keyword (2 of 2)
• Example:
/* Declare the structure date */
struct _date {
short iDay;
short iMonth;
short iYear;
};
/* Define the structure ‘_date’ as a new data type ‘date’ */
typedef struct _date date;
/* Create an instance of date structure */
date today;
For Infosys internal use
© 2009 Infosys Technologies Limited
40
Coding Standards and Best Practices for Structures
• Each structure should have a small header on the similar lines of a function
header
/*************************************************************
*******
* Structure: <structure name>
* Description: <brief description of its use
* Member Variables:
* <variable name> - <description of the variable>
* ... ...
**************************************************************
*******/
• The name of the structure should all be in lowercase
• Examples: date, address, accountdetails
• The names of structure instance variables should have a prefix of ‘s’
For Infosys internal use
© 2009 Infosys Technologies Limited
41
Structures in Memory
• A structure instance occupies memory space
• Sum of sizes of all member variables is the minimum amount of memory
occupied by a structure
• The members of a structure are stored in contiguous locations
struct date {
short iDay;
short iMonth;
short iYear;
};
…
…
struct date sToday;
Memory
Address
2A3080
2A3081
2A3082
2A3083
2A3084
2A3085
short iDay;
short iMonth;
short iYear;
For Infosys internal use
© 2009 Infosys Technologies Limited
42
Structure within a Structure (1 of 3)
• typedef struct _accountdetails {
•
int iAccountNumber;
•
char cAccountType;
•
char acCustomerName[10];
•
date sOpenDate;
•
double dBalance;
•
} accountdetails;
• /* Declare an instance of accountdetails */
•
accountdetails sAccount;
•
sAccount.iAccountNumber = 702984;
•
sAccount.cAccountType = 'S';
•
sAccount.dBalance = 5000.0;
•
/* Populating the date sturucture within the accountdetails structure */
•
•
•
sAccount.sOpenDate.iDay = 1;
sAccount.sOpenDate.iMonth = 6;
sAccount.sOpenDate.iYear = 2005;
For Infosys internal use
© 2009 Infosys Technologies Limited
43
Structure within a Structure (3 of 3)
Memory
Address
int iAccountNumber;
char cAccountType;
short iDay;
short iMonth;
short iYear;
double dBalance;
2A30A0
2A30A1
2A30A2
2A30A3
2A30A4
2A30A5
2A30A6
2A30A7
2A30A8
2A30A9
2A30AA
2A30AB
2A30AC
2A30AD
2A30AE
2A30AF
2A30B0
2A30B1
2A30B2
2A30B3
2A30B4
2A30B5
2A30B6
2A30B7
2A30B8
2A30B9
2A30BA
2A30BB
2A30BC
char acCustomerName[10];
date sOpenDate;
(10 bytes)
typedef struct _accountdetails {
int iAccountNumber;
char cAccountType;
char acCustomerName[10];
date sOpenDate;
double dBalance;
} accountdetails;
…
…
accountdetails account;
For Infosys internal use
© 2009 Infosys Technologies Limited
44
Union
• Union
• mechanism to create user defined data types
• single piece of memory that is shared
• The variables in the union can be of dissimilar data types
• At any point of time, only one variable in the union can be in use
For Infosys internal use
© 2009 Infosys Technologies Limited
45
Union…
• Size of the union is fixed at compile time and it’s large enough to
accommodate the largest element in the union
union encrypt
{
int iIntVar;
char acArrayVar[2];
}uEnc;
• Accessing elements of a union is similar to accessing elements of a structure
uEnc.iIntVar=10;
uEnc.acArrayVar[0]=‘a’;
uEnc.acArrayVar[1]=‘b’;
For Infosys internal use
© 2009 Infosys Technologies Limited
46
Enum
• enum is the short form for enumerate
• It is used to declare and initialize integer constants
enum colors {RED, YELLOW, GREEN, BLUE};
• In this example the colors are set of constants
• Integer values are related with the constants declared in the enum
• The default value starts with 0 and gets increased by 1 as we move to next
element in the list.
• We can also assign values to the elements
enum colors { RED=1, YELLOW, GREEN=5, BLUE };
What is the value of BLUE?
For Infosys internal use
© 2009 Infosys Technologies Limited
47
Enum …
• Consider the code below
#include <stdio.h>
void main()
{
enum {RED, GREEN=22, BLACK};
printf(“\nRED = %d\t GREEN = %d\t BLUE = %d\n ", RED, GREEN,
BLACK);
}
• The output is:
RED = 0
GREEN = 22
BLUE = 23
For Infosys internal use
48
© 2009 Infosys Technologies Limited
48
Thoughts
•
•
•
•
•
•
•
•
•
•
•
•
How structures different from arrays?
How do you declare structures?
Can you initialize members within structure?
When memory is allocated for structure?
How do you access structure members?
What is the use of typedef?
How do structure are stored in memory?
What is the size of structure ?
What is the difference between union and structure?
What is the size of union in memory?
What is enum?
How do you declare enum data type?
For Infosys internal use
© 2009 Infosys Technologies Limited
49
Program stack and heap
• During execution of a program, the storage of program and data is as
follows:
• The executable code is stored into the code /Text segment
• The global variables are stored into data segment
• The heap memory is used for dynamic memory allocation
• The local variables are stored into the stack
• Heap: ity is a part of memory for dynamic memory allocation for user
programs.
For Infosys internal use
© 2009 Infosys Technologies Limited
50
Functions
• A function is a piece of a code that performs a specific task
• Solving a problem using different functions makes programming much
simpler with fewer defects
• Values can be passed to functions so that the function performs the task on
these values
• Values passed to the function are called arguments
• After the function performs the task, it can send back the results to the
function who called it.
• return value is the value sent by the called function to the calling function
• A function can return back only one value to the calling function
For Infosys internal use
© 2009 Infosys Technologies Limited
51
Advantages of Functions (1 of 3)
• 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
For Infosys internal use
© 2009 Infosys Technologies Limited
52
Identifying Functions
Monthly Salary
Calculation function
Overtime Calculation
function
Bonus Calculation
function
Key Task
Annual Increment
Calculation function
Sub Tasks
For Infosys internal use
© 2009 Infosys Technologies Limited
53
Classification of Functions
Library functions
- defined in the language
- provided along with the compiler
Example:
printf(), scanf() etc.
User Defined functions
- written by the user
Example: main() or any other
user-defined function
For Infosys internal use
© 2009 Infosys Technologies Limited
54
Coding Standards for Writing Functions
• The function should begin with a header which describes about the
function. It is written as follows:
/*************************************************************
***
* Function:
fnFactorial()
* Description: Accepts an integer and finds the factorial
* Input Parameters:
*
int - Number for which factorial to be found
* Returns: int - Factorial of the given integer
**************************************************************
**/
For Infosys internal use
© 2009 Infosys Technologies Limited
55
Elements of a Function
 Function Declaration or Function Prototype :
 The function should be declared prior to its usage
 Function Definition :
 Implementing the function or writing the task of the function
 Consists of
• Function Header
• Function Body
 Function Invocation or Function call:
 To utilize a function’s service, the function have to be invoked (called)
For Infosys internal use
© 2009 Infosys Technologies Limited
56
Declaring Function Prototypes
• A function prototype is the information to the compiler regarding the userdefined function name, the data type and the number of values to pass 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
For Infosys internal use
© 2009 Infosys Technologies Limited
57
Declaring Function Prototypes (2 of 2)
• Function Prototypes declare ONLY the signature of the function before
actually defining the function
• Here signature includes function name, list of parameter data types and
optional names of formal parameters
• Syntax:
returnDataType FunctionName
(
data_type
arg2,...,data_type argn );
• Example:
int iValidate_Date(int iDay,int iMonth, int iYear);
arg1,
data_type
For Infosys internal use
© 2009 Infosys Technologies Limited
58
Writing User-Defined Functions
• A function header and body looks like this:
Return-data-type function-name(data-type argument-1,…) {
Local variable declarations;
/* Write the body of the function here */
Statement(s);
return (expression);
}
• The return data type can be any valid data type in C
• The return type of the function is void if it is not returning anything
• A function header does not end with a semicolon
• The return statement is optional. It is required only when a value has to be
returned
For Infosys internal use
© 2009 Infosys Technologies Limited
59
Writing User-Defined Functions
Return data type
Arguments (Parameters)
int fnAdd(int iNumber1, int iNumber2)
{
/* Variable declaration*/
int iSum;
/* Find the sum */
iSum = iNumber1 + iNumber2;
/* Return the result */
return (iSum);
Function header
Function Body
Can also be written as
return isum;
}
For Infosys internal use
© 2009 Infosys Technologies Limited
60
Returning values
• The result of the function can be given back to the calling functions
• Return statement is used to return a value to the calling function
• Syntax:
return (expression) ;
• Example:
return(iNumber * iNumber);
return 0;
return (3);
return;
return (10 * i);
For Infosys internal use
© 2009 Infosys Technologies Limited
61
Calling User-Defined Functions
• A function is called by giving its name and passing the required arguments
• The constants can be sent as arguments to functions
iResult = fnAdd(10, 15);
• The variables can also be sent as arguments to functions
int iResult,iNumber1=10, iNumber2=20;
iResult = fnAdd(iNumber1, iNumber2);
• Calling a function with void return data type
fnDisplayPattern(15);
• Calling a function that do not take any arguments and do not return
anything
fnCompanyNameDisplay();
For Infosys internal use
© 2009 Infosys Technologies Limited
62
Formal and Actual Parameters
• The variables declared in the function header are called as formal
parameters
• The variables or constants that are passed in the function call are called as
actual parameters
• The formal parameter names and actual parameters names can be the same
or different
For Infosys internal use
© 2009 Infosys Technologies Limited
63
Functions – Example
int fnAdd(int iNumber1, int iNumber2) ;
int main() {
int iResult,iValue1, iValue2;
Actual Argument
/* Function is called here */
iResult = fnAdd(iValue1, iValue2);
printf(“Sum of %d and %d is %d\n”,iValue1, iValue2, iResult);
return 0;
}
/* Function to add two integers */
Formal Argument
int fnAdd(int iNumber1, int iNumber2)
{
/* Variable declaration*/
Return value
int iSum;
iSum = iNumber1 + iNumber2; /* Find the sum */
return (iSum); /* Return the result */
}
For Infosys internal use
© 2009 Infosys Technologies Limited
64
Function Calls and Stack
Local variables of function
Arguments to function
User-Defined Function
Local variables of main
Arguments to main
Function main
STACK
For Infosys internal use
© 2009 Infosys Technologies Limited
65
Function calls and Stack
/* Find the sum of two integers */
void fnSumPrint(int iValue1, int iValue2);
int main(int argc, char **argv)
{
int iNumber1=10, iNumber2=20;
scanf(“%d %d”,&iNumber1,&iNumber2);
fnSumPrint(iNumber1,iNumber2);
return 0;
}
void fnSumPrint(int iValue1, int iValue2)
{
int iResult;
/* Find the sum */
iResult = iValue1 + iValue2;
printf(“%d”,iResult);
}
For Infosys internal use
© 2009 Infosys Technologies Limited
66
Local Variables
Formal
Parameters of
main()
iValue1=10
iValue2=20
iValue1=10
iValue2=20
DA
TE
iResult=30
LI
iValue1=10
iValue2=20
This area of stack
accessible only to
fnSumPrint
iResult=30
fnSumPrint()
fnSumPrint()
IN
Formal Parameters of
fnSumPrint()
iResult
VA
Local Variables of
fnSumPrint()
D
Function calls and Stack (4 of 4)
iNumber1=10
iNumber2=20
iNumber1=10
iNumber2=20
iNumber1=10
iNumber2=20
iNumber1=10
iNumber2=20
int argc
char **argv
int argc
char **argv
int argc
char **argv
int argc
char **argv
main()
1. Program Stack
When executing main
main()
2. Program Stack
when executing
fnSumPrint()
(iValue1 and
iValue2 are
copies of
iNumber1 and
iNumber2)
This area of
stack accessible
only to main
main()
3. Program Stack
when executing
iResult= iValue1+ iValue2
(code in fnSumPrint act
upon copies --iNumber1
and iNumber2 only)
fnSumPrint()
main()
4. Program Stack
after returning
from fnSumPrint()
back to main
How a function call reflects on program stack
For Infosys internal use
© 2009 Infosys Technologies Limited
67
Scope of Variables
• The scope of variables refers to that portion of the program where the
variables can be accessed
• They are accessible in some portion of the program and in the other they
are not accessible
• scope of a variable identify the part of the program in which the set of
variables can be referenced and manipulated
• When a variable is required in a program, it can be declared as:
• Local variable
• Global variable
For Infosys internal use
© 2009 Infosys Technologies Limited
68
Local Variables (1 of 2)
• The variables that are declared inside a function are called as local variables
• Their scope is only inside the function where they are declared
• These variables cannot be accessed outside the function
• Local variables exist only till the function terminates its execution
• The initial values of local variables are garbage values
For Infosys internal use
© 2009 Infosys Technologies Limited
69
Local Variables (2 of 2)
#include <stdio.h>
void fnSumOfNumbers(); /* Function prototype */
Variables ‘iNumber1’
int main(int argc, char **argv)
and ‘iNumber2’ are
{
local to function ‘main’
int Number1, iNumber2;
printf("Enter the anumber ");
scanf("%d",&iNumber1);
printf("Enter the next number ");
scanf("%d",&iNumber2);
fnSumOfNumbers();
/* Call the function */
}
void fnSumOfNumbers()
Variable ‘iResult’ is
local to function
{
‘fnSumOfNumbers’
int iResult;
iResult = iNumber1 + iNumber2; /* Find the sum */
printf("The sum is %d",iResult);
}
For Infosys internal use
© 2009 Infosys Technologies Limited
70
Global Variables (1 of 2)
• The variables that are declared outside all the functions (above ‘main’ ) are
called as global variables
• These variables can be accessed by all the functions
• The global variables exist for the entire life-cycle of the program
• The global variables are by default initialized to zero
• Coding Standard:
• Each global variable should start with the alphabet ‘g’
• Example:
int giValue;
float gfSalary;
For Infosys internal use
© 2009 Infosys Technologies Limited
71
Global Variables (2 of 2)
#include <stdio.h>
int giValue1, giValue2; /* Global variables declaration */
void fnSumOfNumbers(); /* Function prototype */
int main(){
printf("Enter the first number ");
scanf("%d",&giValue1);
printf("Enter the second number ");
scanf("%d",&giValue2);
fnSumOfNumbers();
/* Call the function */
}
void fnSumOfNumbers(){
int iResult;
iResult = giValue1 + giValue2;
/* Find the sum */
printf("\nThe sum is %d\n",iResult);
}
For Infosys internal use
© 2009 Infosys Technologies Limited
72
Difference between Local and Global Variables
Variable
Scope
Local
Variable
Can be referenced only within
the function in which it is
declared
Global
Variable
Can be referenced in all the
functions
Value after
declaration
Garbage value
Zero
For Infosys internal use
© 2009 Infosys Technologies Limited
73
Disadvantages of Global Variables
• Lifetime of global variables is throughout the program.
• Hence usage of global variables leads to wastage of memory
• Scope of the global variable is throughout the program.
• Hence more than one function can change the value of the global
variable. This makes debugging difficult.
For Infosys internal use
© 2009 Infosys Technologies Limited
74
What is the output of the following code snippet?
int giGlobal;
int main(int argc, char **argv) {
int iLocal;
printf(“ Value of Local = %d \n, Value of Global = %d”, iLocal, giGlobal);
}
The output is:
Value of Local = <some garbage value>
Value of Global = 0
For Infosys internal use
© 2009 Infosys Technologies Limited
75
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 address / reference
For Infosys internal use
© 2009 Infosys Technologies Limited
76
Pass by Value (1 of 6)
• When parameters are passed from the called function to a calling function,
the value of the actual parameter is copied onto the formal argument
• Since the actual parameters and formal parameters are stored in different
memory locations, the changes in formal parameters do not alter the values
of actual parameters
For Infosys internal use
© 2009 Infosys Technologies Limited
77
Pass by Value (2 of 6)
int main(int argc, char ** argv)
{
int iValue=10;
printf("\nValue = %d",iValue);
fnIncrement(iValue);
printf("\nValue = %d",iValue);
return 0;
}
void fnIncrement(int iTemp)
{
iTemp++;
}
10
iValue = 10
iValue = 10
iTemp
10
11
For Infosys internal use
© 2009 Infosys Technologies Limited
78
Pass by Value (3 of 6)
#include<stdio.h>
void fnUpdateValues (int, int); /* function prototype*/
int main(int argc, char **argv) {
int iValue1=100, iValue2=250;
printf("\n\nBefore calling the function: ");
printf("ivalue1=%d iValue2=%d\n\n",iValue1,iValue2);
fnUpdateValues(iValue1, iValue2);/*function call*/
printf("After calling the function: ");
printf(" ivalue1=%d iValue2=%d\n\n",iValue1,iValue2);
return 0; }
/* Return 0 to the OS */
For Infosys internal use
© 2009 Infosys Technologies Limited
79
Pass by Value (4 of 6)
void fnUpdateValues(int iNumber1, int iNumber2)
{
iNumber1 = iNumber1 + 15;
/* Update the values */
iNumber2 = iNumber2 - 10;
}
For Infosys internal use
© 2009 Infosys Technologies Limited
80
Pass by Value (5 of 6)
fnUpdateValues()
main()
End
Call of function fnUpdateValues
For Infosys internal use
© 2009 Infosys Technologies Limited
81
Passing array elements to a function –
Pass by value
(6/6)
• Two ways to pass array elements to a function.
• Pass by Value & Pass by Reference/ Address
/* Demo of Pass by Value */
void fnDisplay(int iMarks);
int main(int argc, char **argv) {
int iIndex;
int aiMarks[] = {55,65};
for(iIndex=0;iIndex<=1;iIndex++) {
fnDisplay( aiMarks[iIndex] );
}
return 0;
}
void fnDisplay ( int iMarks) {
printf( “%d” , iMarks);
}
iIndex=0
55
iIndex=1
65
iMarks=55 iMarks=65
55
65
For Infosys internal use
© 2009 Infosys Technologies Limited
82
Recursive Functions
• When a function calls itself it is called as Recursion
• Many mathematical, searching and sorting algorithms, can be simply
expressed in terms of a recursive definition
• A recursive definition has two parts:
Base condition : When a function will terminate
Recursive condition :The invocation of a recursive call to the function
• When the problem is solved through recursion, the source code looks
elegant
For Infosys internal use
© 2009 Infosys Technologies Limited
83
Recursive Functions
/* Finding the factorial of an integer using a
recursive function */
int fnFact(int iNumber); /*Function Prototype*/
int main(int argc, char **argv)
{
int iFactorial;
iFactorial=fnFact(4);
printf("The factorial is %d\n\n",iFactorial);
return 0;
}
For Infosys internal use
© 2009 Infosys Technologies Limited
84
Recursive Functions
int fnFact(int iNumber)
{
int iFact;
if (iNumber <= 1) {
return 1;
}
else {
iFact = iNumber * fnFact(iNumber - 1);
}
return iFact;
}
For Infosys internal use
© 2009 Infosys Technologies Limited
85
Recursive Functions
• Find the output of the following code snippet when the function is called as
fnReverse(5);
void fnReverse(int iValue)
{
if (iValue > 0) {
fnReverse(iValue-1);
}
printf("%d\t",iValue);
}
Output will be
0 1 2 3 4 5
For Infosys internal use
© 2009 Infosys Technologies Limited
86
Recursive Functions
• Find the output of the following code snippet when the function is called as
fnReverse();
int giValue = 5;
/* Global Variable Declaration */
void fnReverse()
{
if (giValue > 0) {
giValue--;
fnReverse();
}
printf("%d\n",giValue);
Output: } prints 0 six times
For Infosys internal use
© 2009 Infosys Technologies Limited
87
Recursive Functions
• Find the output of the following code snippet when the function is called as
fnReverse();
char gacString[] = "Test";
int giIndex = 0;
void fnReverse()
{
if (gacString[giIndex] != '\0') {
giIndex++;
fnReverse();
}
giIndex--;
if (giIndex >= 0)
{
printf("%c",gacString[giIndex]);
}
}
/* Output: Prints the string “tseT” */
For Infosys internal use
© 2009 Infosys Technologies Limited
88
Thoughts
•
•
•
•
•
•
•
•
•
•
Where do the global variables are stored?
Where do the local variables are stored?
Where do the dynamic variables are stored?
What is a function?
How many values a function can return at a time?
When do you use functions?
Name few library functions?
Is it main library or user defined function?
What is function prototype and why do you require it?
What are the difference between formal and actual parameters?
For Infosys internal use
© 2009 Infosys Technologies Limited
89
Thoughts
•
•
•
•
•
•
How do the stacks are useful in function calling?
What is the difference between push and pop operations?
What is initial value of local and global variable when they are declared?
What is recursive function? When do you go for it?
What is the base condition and recursive condition?
What are the different techniques of argument passing?
For Infosys internal use
© 2009 Infosys Technologies Limited
90
Command line arguments
• It is useful to send data to a program while program starts executing.
• Data is passed to main( ) function via command line arguments.
• A command line argument is the data which we pass along with program
name from command prompt
• Example:
cc options program_name
where options and program_name is a command line argument that
specifies the name of the program you wish to compile.
For Infosys internal use
© 2009 Infosys Technologies Limited
91
Command Line Arguments…
• In a C program the function that accepts command line arguments is the
main() function
• To write the main() function to accept command-line arguments the syntax
is:
main( int argc, char *argv[] )
{
…
}
For Infosys internal use
© 2009 Infosys Technologies Limited
92
Command Line Arguments…
• argc is the number of elements on the command line i.e. the command and
the arguments
Example: > echo Hello World
• argc will have the value 3 for the command:
• if argc is 1, then no argument is passed to the program
• argv[] is an array of all the elements on the command line
For Infosys internal use
© 2009 Infosys Technologies Limited
93
Command line argument cont…..
Example: > echo Hello World
• argv[0] points to the string “echo”
• argv[1] points to the string “Hello”
• argv[2] points to the string “World”
• argv[3] is NULL value
• Note: “echo” is the command and the rest are arguments
For Infosys internal use
© 2009 Infosys Technologies Limited
94
Command Line Arguments…
/* Program to display arguments */
#include<stdio.h>
main(int argc, char *argv[])
{
/* Increment pointer to point to arguments*/
argv++;
/* Print the arguments on screen */
while (*argv != NULL)
{
printf("%s ", *argv);
argv++;
}
printf("\n");
}
For Infosys internal use
© 2009 Infosys Technologies Limited
95
© 2009 Infosys Technologies Limited
96
Thoughts
•
•
•
•
How do you provide information to running program?
Where do you store the command line arguments?
How do you determine the number of arguments passed?
Which function accepts command line arguments?
For Infosys internal use
© 2009 Infosys Technologies Limited
97
Pointers- Agenda
•
•
•
•
•
•
Pointers
Null Pointers
Constant Pointers
Pointer Arithmetic
Pointers to Pointers
Observations
For Infosys internal use
© 2009 Infosys Technologies Limited
98
Pointers in C
• Pointer is a derived data type
• stores the address of a memory location
• If p holds the address of another variable i
• p is a pointer variable and p is said to point to i
125
ess)
r
d
d
(A
A56
2
B
3
Integer
e
variabl
r to
e
t
n
Po i g e r
Inte
For Infosys internal use
© 2009 Infosys Technologies Limited
99
Why pointers?
•
•
•
•
•
•
•
•
•
•
To pass information back and forth between a function & its caller
To address different elements of arrays in alternate ways
To represent multidimensional arrays
Dynamic allocation of memory
pointers can improve the efficiency of certain routines
pointers provide support for dynamic data structures, such as binary trees
and linked lists.
we need to return multiple values to the caller function, the caller function
should pass the address of variables declared in it so that the called function
can write to these variables serving the purpose of multiple return values to
the calling environment
The size of the pointer variable is platform dependent.
Size of Pointer on Windows: 4 Bytes
Size of pointer on Unix :?
For Infosys internal use
© 2009 Infosys Technologies Limited
100
Pointers in C
• To declare a pointer variable,
Syntax:
data type *pointerName;
Example
1.int *piCount;
piCount will store the address of an integer value
2.float *pfBasic;
pfBasic contain the address of a float variable
Note:
The size of the pointer variable is platform dependent.
Size of Pointer on Windows: 4 Bytes
For Infosys internal use
© 2009 Infosys Technologies Limited
101
Pointers in C
• How to store the address of an integer into an integer pointer?
Syntax:
int *pointervariable = & integer_variable;
• Example
int iCount = 8;
int *piCount;
piCount = &iCount;
iCount is an integer variable
• piCount is an integer pointer
• & is the “address of” operator
For Infosys internal use
© 2009 Infosys Technologies Limited
102
Pointers in C
Memory
Contentsof
Address MemoryLocation
)
ress
(Add 18
2A 30
int iCount = 8;
int *piCount;
00
00
2A 301A
2A 301B
00
08
Integer
g er
n te
I
o
t
;
r
un t
nte
Po i t* p iCo
in
2A3018
2A3019
Integer Variable
intiCount;
Memory
Contentsof
Memory
Location
Address
piCount = &iCount;
Pointer to Integer
int * piCount;
o an
t
r
e
t
o in
A P Integ er nt)
u
iC o
p
*
t
(in
le
ar iab
v
r
e
Integ iCou n t)
(int
00
00
00
08
...
...
...
...
2A3018
2A3019
2A 301A
2A 301B
Pointsto…
)
ress
d
d
A
(
18
2A 30
Integer
08
00
2A
30
18
POINTER
01 2C70
01 2C71
01 2C72
01 2C73
Integer Variable
intiCount;
For Infosys internal use
© 2009 Infosys Technologies Limited
103
Reading Contents of Variable using Pointers
Accessing the value at the address stored in the pointer, use
*pointer variable
Example
printf(“%d”, *piCount);
“*” is called the indirection operator
It fetches the value at the address stored in piCount
For Infosys internal use
© 2009 Infosys Technologies Limited
104
NULL Pointers
• 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
• Example:
/* Initializing pointer to NULL */
int *piCount = NULL;
• A pointer can be checked whether it is pointing to NULL using the ‘==’ (Equal
to) operator
For Infosys internal use
© 2009 Infosys Technologies Limited
105
More on Pointers
•
Constant Pointers
Two ways to understand it
1. const data_type *var;
– pointer to constant integer
2. data_type *const var=& var1;
– constant pointer can not get its value changed
– must continue to point to var1 throughout the execution
Examples:
int i=0;
const int *pci; /* Pointer to a constant integer */
int *const cpi = &i; /* Constant Pointer to an integer */
For Infosys internal use
© 2009 Infosys Technologies Limited
106
More on Pointers (Pointer Assignments)
• We can use assignment operator to assign the value of one pointer to
another .
• Eg : pointers of the same type
#include <stdio.h>
printf(‘” *p1 and * p2: %d
%d\n", *p1, *p2);
printf(“ p1 and p2: %p %p",
p1, p2);
int main(void)
{
return 0;
int i = 9;
}
int *p1, *p2;
p1 = &i;
p2 = p1;
For Infosys internal use
© 2009 Infosys Technologies Limited
107
Pointer Conversions
• One type of pointer can be converted into another type of pointer.
• Two types of Conversions
• those that involve void * pointers,
• and those that don't.
For Infosys internal use
© 2009 Infosys Technologies Limited
108
Void pointers
• It is permissible to assign a void * pointer to any other type of pointer.
• It is also permissible toassign any other type of pointer to a void * pointer.
• A void * pointer is called a generic pointer.
• void * pointer is used to specify a pointer whose base type is unknown.
• void * can take any type of pointer argument
For Infosys internal use
© 2009 Infosys Technologies Limited
109
Void pointers
• It is also used to refer to raw memory (such as that returned by the malloc()
when the semantics of that memory are not known.
• No explicit cast is required to convert to or from a void * pointer.
• Eg
Void main()
{
……….
Void * ptr1;
int i=56,&ptr2;
ptr2=&I;
ptr1=ptr2;
……………
}
For Infosys internal use
© 2009 Infosys Technologies Limited
110
Pointer conversion other than void Pointers
• All other pointer conversions must be performed by using an explicit cast.
• However, the conversion of one type of pointer into another type may
create undefined behavior.
For Infosys internal use
© 2009 Infosys Technologies Limited
111
Pointer Arithmetic
• Valid operations
•
•
•
•
Assignment of 0(or NULL) to a pointer
Addition or subtraction of an integer to a pointer
Subtractions of two pointers pointing to the members of same array
Comparing a pointer to NULL
• Invalid operations
• Assignment of a nonzero integer to pointer
• Addition, multiplication, division of two pointers
• Comparisons of two pointers that do not point to the same members
For Infosys internal use
© 2009 Infosys Technologies Limited
112
Pointers and Arrays
• Pointers and Arrays
• Array name is the address of the 0th element of the array
• Array elements are allocated memory contiguously
• The name of the array acts like a pointer to the first element in the array
09C4
10
20
30
40
50
09C4
09C8
09CC
09D0
09D4
aiMarks
Elements
Address
For Infosys internal use
© 2009 Infosys Technologies Limited
113
Pointers and Arrays
• If we extract the base address of array into a pointer we can use it to access
the elements of the array
void main()
{
int iCount; /* Loop Counter */
int iaAr[5]={1,2,3,4,5}; /* Array of 5 integers */
int *piArr=iaAr; /* Pointer to array */
for(iCount=0;iCount<5;iCount++)
{
/* Print the content of array using the pointer */
printf("\n%d",*piArr);
piArr++;
}
}
NOTE: The above printf statement can be written using *(piArr + iCount) also
For Infosys internal use
© 2009 Infosys Technologies Limited
114
More on Pointers…
• Pointers and Arrays
/* Array Declaration */
int a[5];
a[0]
*a
a[1]
a[2]
a[3]
a[4]
*(a+1) *(a+2) *(a+3) *(a+4)
• a is a constant pointer = &a[0]
For Infosys internal use
© 2009 Infosys Technologies Limited
115
More on Pointers…
int a[5]={0,1,2,3,4};
int *pa;
pa=a;
pa+3 <=> pa + 3 *(sizeof(int))
pa = pa + 3;
printf(“%d %d\n”,*pa, a[3]);
a[0]
a[1]
a[2]
a[3]
*pa
*(pa+1)
*(pa+2)
*(pa+3)
a[4]
*(pa+4)
For Infosys internal use
© 2009 Infosys Technologies Limited
116
More on Pointers…
• Function returning a pointer
• Useful to return a string or an array
• Example:
/***********************************************
* Function Name: compstr
* Description: Compares two strings and returns
* the string which is lexicographically higher
* Input: char *, char *
* Output: char *
***********************************************/
char* compstr(char *pcString1, char *pcString2)
{
int iLex = 0;
iLex=strcmp(pcString1, pcString2);
if (iLex < 0) return pcString2;
if (iLex > 0) return pcString1;
if (iLex == 0) return pcString1;
For Infosys internal use
}
© 2009 Infosys Technologies Limited
117
Pass By Reference (1 of 2)
#include<stdio.h>
void fnUpdateValues(int *, int *);/* Function prototype */
int main(void)
{ int iValue1=100, iValue2=250;
printf("\n\nBefore calling the function: ");
printf("ivalue1=%d iValue2=%d\n\n",iValue1,iValue2);
/*Call the function and send the address of the variables */
fnUpdateValues(&iValue1, &iValue2);
printf("After calling the function: ");
printf(" ivalue1=%d iValue2=%d\n\n",iValue1,iValue2);
return 0; /* Return success code to the operating system */}
void fnUpdateValues(int *piNumber1, int *piNumber2)
{ *piNumber1 = *piNumber1 + 15; /* Update the values */
*piNumber2 = *piNumber2 - 10;
}
For Infosys internal use
© 2009 Infosys Technologies Limited
118
Local Variables
Formal
Parameters of
main()
iValue2=250
iValue1=100
int argc
char ** argv
main()
1. Program Stack
When executing main
800D
8009
fnUpdateValues()
800D
iValue2=250
8009
iValue1=100
This area of
stack accessible
int argc
only to main
char ** argv
main()
2. Program Stack
when executing
fnUpdateValues()
(iNumber1 and
iNumber2 are
pointers to
iValue1 and
iValue2)
(None)
*iNumber1=800D
*iNumber2=8009
*iNumber1=800D
*iNumber2=8009
fnUpdateValues()
iValue2=240
iValue1=115
fnUpdateValues()
int argc
char ** argv
int argc
char ** argv
main()
main()
3. Program Stack
when executing
*iNumber1 = *iNumber1+15
*iNumber2=*iNumber2-10
(code in fnUpdateValues
act directly on iNumber1
and iNumber2 )
LI
D
A
TE
(None)
A
*iNumber1=800D
*iNumber2=8009
This area of stack
accessible only to
fnUpdateValues
V
Formal Parameters of
fnUpdateValues()
(None)
IN
Local Variables of
fnUpdateValues()
D
Pass By Reference (2 of 2)
iValue2=240
iValue1=115
4. Program Stack
after returning
from
fnUpdateValues()
back to main
For Infosys internal use
© 2009 Infosys Technologies Limited
119
Difference between pass by value and pass by reference
Pass by value
Pass by reference
Consumes more memory space
because formal parameter also
occupies
memory space.
Consumes less memory space
Because irrespective of the actual
arguments data type, each pointer
occupies
only 4 bytes.
Takes more time for execution,
because the
values are copied
Takes less time because no values
are
copied
For Infosys internal use
© 2009 Infosys Technologies Limited
120
Example (Pass By Reference)
piVar
1000
void set(int *piVar)
{printf(“%d”,*piVar);
5000
-----4 bytes-----
0
*piVar=1; }
iVar1
void main()
5000
{ int iVar1=0;
01
<------2 bytes----->
set(&iVar1);
printf(“%d”,iVar1); }
1
For Infosys internal use
© 2009 Infosys Technologies Limited
121
What Does this Program Do?
void main ()
{
int iVar1 = 2, iVar2 = 4, iTmp;
iTmp = iVar1;
iVar1 = iVar2;
iVar2 = iTmp;
printf("Var1 = %d, Var2 = %d", iVar1, iVar2);
}
For Infosys internal use
© 2009 Infosys Technologies Limited
122
Pointers to pointers
• multiple indirection/pointers to pointers is nothing but a pointer pointing to
a pointer.
• A pointer to a pointer is declared as
<datatype> ** var1,**var2…..**varn;
Eg:
float **Val;
• Val is a pointer to a float pointer.
• To access the target value it is **Val
For Infosys internal use
© 2009 Infosys Technologies Limited
123
Pointers to pointers schema
For Infosys internal use
© 2009 Infosys Technologies Limited
124
Think ?
•In general How many multiple indirection are
supported by a C compiler ?
Answer : 12
void main()
{
int x=5;
int * ptr1=&x;
int **ptr2=&ptr1;
int ***ptr3=&ptr2;
….
…..
…..
}
For Infosys internal use
© 2009 Infosys Technologies Limited
125
Pointer to a pointer (Example)
#include <stdio.h>
int main (void)
{
int x, *p, **q;
x = 10;
p = &x;
q = &p;
printf ("%d", **q); /* print the value of x */
return 0;
}
For Infosys internal use
© 2009 Infosys Technologies Limited
126
Thoughts
•
•
•
•
•
•
•
•
•
•
What is pointer?
What are the advantages of pointers?
What are the usage of * and & operators?
What happens when the pointer variable is un initialized?
How do you initialize pointers?
What is the difference between constant pointer and pointer to constant?
Can you convert one type of pointer to another type?
Which is the generic pointer?
Can you use void pointer to store the address of float variable?
Does it requires explicit conversion when you convert to or from void
pointer?
For Infosys internal use
© 2009 Infosys Technologies Limited
127
Thoughts
•
•
•
•
How do you convert one type of pointer to another other than void pointer?
How the pass by value is different from pass by reference?
Array name specifies the address of which element of the array?
Which of the following operation on pointers is valid or invalid
• comparison of two pointers that do not point to the same member
• Addition or subtraction of an integer to a pointer
• Subtraction of two pointers which point to the members of the same
array
• Assignment of non-zero integer to pointer
• What is pointer-to-pointer?
For Infosys internal use
© 2009 Infosys Technologies Limited
128
Function Pointer
• A function pointer is a pointer variable
• When the compiler compiles a program it creates the entry point for each
function in the program
• A function pointer contains the address of the entry point of a function
• The entry point is the address to which execution transfers when a function
is called
For Infosys internal use
© 2009 Infosys Technologies Limited
129
Steps involved in usage of Function Pointer…
• int fnCube(int iIntVar);
• function accepts an integer and return an integer
• int (*pfFunc) (int)=NULL;
• pfFunc is a pointer to a function which can only point to those functions
which accept an integer and return a integer
• pfFunc=&fnCube;
• Assign the address of fncube to pfFunc
• iResult = pfFunc(atoi(argv[2]));
• call to fnCube() function using pointer to function
For Infosys internal use
© 2009 Infosys Technologies Limited
130
Function pointer Example
int fnCube(int iIntVar)
{
return(iIntVar* iIntVar * iIntVar);
}
int fnSquare(int iIntVar)
{
return(iIntVar * iIntVar);
}
/*int (*pfFunc) (int);*/
int (*pfFunc) (int)=NULL;
int main (int argc, char **argv)
{
int iResult;
if (!strcmp("square", argv[1]))
{
pfFunc=fnSquare;
}
else if (!strcmp("cube", argv[1]))
{
pfFunc=&fnCube;
}
else
{
printf("Error");
exit(1);
}
iResult = pfFunc(atoi(argv[2]));
printf(“%s of %s is %d\n”, argv[1], argv[2],
iResult );
return 0;
}
For Infosys internal use
© 2009 Infosys Technologies Limited
131
Passing Function Pointer as arguments…
• Passing function pointers as arguments to a another function
• The function to which the pointer is passed will call the function using the
pointer variable and not the function name
• Prototype of a function which accepts a function pointer
Example
• void F (void (*pf) (int) );
• The function pointer can point to any function which accepts an integer
and returns a void
For Infosys internal use
© 2009 Infosys Technologies Limited
132
E.g.: Passing Function Pointer as arguments..
#include <stdio.h>
#include <string.h>
void find(char *a, char *b,
int (*chk)(const char *, const char *))
void find(char *a, char *b,
int (*chk)(const char *, const char *));
int main(void)
{
char s1[80], s2[80];
/* function pointer */
int (*p) (const char *, const char *);
/* assign address of strchk to p */
p = strchk;
printf ("Enter two strings.\n");
gets(s1);
gets(s2);
/*pass address of strchk via p */
find(s1, s2, p); return 0;
}
{
printf("Testing for equality.\n");
if(!(*chk)(a, b)) {
printf("Equal");
}
else
{
printf("Not Equal");
}
}
For Infosys internal use
© 2009 Infosys Technologies Limited
133
Array of Function Pointer…
• Declaring a array of function pointers
• int Add (int, int);
• function Add accepts 2 integers and return a integer
• int Sub (int, int);
• function Sub accepts 2 integers and return a integer
• int (*pf[4])(int, int)={NULL};
• pf is a array of pointers to a functions
For Infosys internal use
© 2009 Infosys Technologies Limited
134
Array of Function Pointer…
• Initializing the array elements with functions addresses.
• pf[0]=&Add; -Assign the address of Add() to the zeroth element of the
array
• pf[1]=Sub; -Assign the address of Sub() to the first element of the array
• Calling the functions using function pointers stored as array elements
• int result = pf[0](10,20);
• This statement will call function Add()
• int result = pf[1](10,20);
• This statement will call function Sub()
For Infosys internal use
© 2009 Infosys Technologies Limited
135
Observations
• Using the comparison operator “= =”we could check if two pointers are
pointing to the same address location.
• Pointers when un-initialized may point to any memory location.
• One of the most common programming run time error that occurs is when
we use the asterisk operator on an un-initialized pointer.
• To avoid this problem the un-initialized pointer can be set to NULL.
• While programming we can check if a pointer is pointing to NULL by using
the comparison operator
• The unary operators such as ampersand’&’, asterisk’*’, increment ‘++’,
decrement’--‘, and NOT ‘!’ has a higher precedence than arithmetic
operators such as +,- and *.
For Infosys internal use
© 2009 Infosys Technologies Limited
136
Summary
•
•
•
•
•
•
•
Pointers
Null Pointers
Constant Pointers
Pointer Arithmetic
Pointers and arrays
Pointers to Pointers
Observations
For Infosys internal use
© 2009 Infosys Technologies Limited
137
String Functions
• C provides a wide range of string functions for performing different string
tasks
• Examples
strlen(str) - calculate string length
strcpy(dst,src) - copy string at src to dst
strcmp(str1,str2) - compare str1 to str2
• Functions come from the utility library string.h
• #include <string.h> to use
For Infosys internal use
© 2009 Infosys Technologies Limited
138
String Length
Syntax: int strlen(char *str)
• returns the length (integer) of the string argument
• counts the number of characters until an \0 is encountered
• does not count \0 char
Example:
char str1 = “hello”;
strlen(str1) would return 5
For Infosys internal use
© 2009 Infosys Technologies Limited
139
Copying a String
Syntax:
char *strcpy(char *dst, char *src)
• copies the characters (including the \0) from the source (src) to the
destination (dst).
• dst should have enough space to receive entire string (if not, other data
may get written over).
• If the two strings overlap (e.g., copying a string onto itself) the results are
unpredictable return value is the destination string (dst)
For Infosys internal use
© 2009 Infosys Technologies Limited
140
char *strncpy(char *str1, char *str2, int count)
•
•
•
•
The strncpy( ) function copies count characters from the string str2 to str1.
str2 must be a pointer to a null-terminated string.
If str1 and str2 overlap, the behavior of strncpy( ) is undefined.
If the string pointed to by str2 has less than count characters, nulls will be
appended to the end of str1 until count characters have been copied.
• Alternatively, if the string pointed to by str2 is longer than count characters,
the resultant array pointed to by str1 will not be null terminated.
• The strncpy( ) function returns a pointer to str1.
For Infosys internal use
© 2009 Infosys Technologies Limited
141
String Comparison
Syntax:
int strcmp(char *str1, char *str2)
compares str1 to str2, returns below values
<0
if ASCII value of the character they differ at is
smaller for str1
OR
if str1 starts the same as str2 (and str2 is longer)
>0
if ASCII value of the character they differ at is larger
for str1
OR
if str2 starts the same as str1 (and str1 is longer)
==0
if the two strings do not differ
For Infosys internal use
© 2009 Infosys Technologies Limited
142
String Comparison (cont)
strcmp examples:
strcmp(“hello”,”hello”) -- returns 0
strcmp(“yello”,”hello”) -- returns value > 0
strcmp(“Hello”,”hello”) -- returns value < 0
strcmp(“hello”,”hello there”) -- returns value < 0
strcmp(“some diff”,”some dift”) -- returns value < 0
expression for determining if two strings s1,s2 hold the same string value:
!strcmp(s1,s2)
For Infosys internal use
© 2009 Infosys Technologies Limited
143
String Comparison (cont)
Sometimes we only want to compare first n chars:
int strncmp(char *str1, char *str2, int n)
• The strncmp( ) function lexicographically compares not more than
count characters from the two null-terminated strings and returns
an integer based on the outcome, as shown here:
Value Meaning
• Less than zero str1 is less than str2
• Zero str1 is equal to str2
• Greater than zero str1 is greater than str2
• If there are less than count characters in either string, the
comparison ends when the first null is encountered.
For Infosys internal use
© 2009 Infosys Technologies Limited
144
String Comparison (cont)
Works the same as strcmp except that it stops at the nth character
looks at less than n characters if either string is shorter than n
strcmp(“some diff”,”some DIFF”) -- returns value > 0
strncmp(“some diff”,”some DIFF”,4) -- returns 0
For Infosys internal use
© 2009 Infosys Technologies Limited
145
memcmp( )
Syntax:
int memcmp(const void *buf1, const void *buf2, size_t count);
The memcmp( ) function compares the first count characters of the arrays
pointed to by buf1 and buf2.
The memcmp( ) function returns an integer
Value Meaning
Less than zero buf1 is less than buf2
Zero buf1 is equal to buf2
Greater than zero buf1 is greater than buf2
For Infosys internal use
© 2009 Infosys Technologies Limited
146
String Comparison (ignoring case)
Syntax:
int strcasecmp(char *str1, char *str2)
similar to strcmp except that upper and lower case characters (e.g., ‘a’ and ‘A’)
are considered to be equal
int strncasecmp(char *str1, char *str2, int n)
version of strncmp that ignores case
For Infosys internal use
© 2009 Infosys Technologies Limited
147
String Concatenation
Syntax:
char *strcat(char *dstS, char *addS)
appends the string at addS to the string dstS (after dstS’s delimiter)
returns the string dstS can cause problems if the resulting string is too long
to fit in dstS
char *strncat(char *dstS, char *addS, int n)
appends the first n characters of addS to dstS if less than n
characters in addS only the characters in addS appended
always appends a \0 character
For Infosys internal use
© 2009 Infosys Technologies Limited
148
Searching for a Character/String
Syntax:
char *strchr(char *str, int ch)
returns a pointer (a char *) to the first occurrence of ch in str
returns NULL if ch does not occur in str
can subtract original pointer from result pointer to determine which
character in array
char *strstr(char *str, char *searchstr)
similar to strchr, but looks for the first occurrence of the string searchstr
in str
char *strrchr(char *str, int ch)
similar to strchr except that the search starts from the end of string str
and works backward
For Infosys internal use
© 2009 Infosys Technologies Limited
149
strstr()
#include <string.h>
#include <stdio.h>
int main(void)
{
char *p;
p = strstr("this is a test", "is");
printf(p);
return 0;
}
For Infosys internal use
© 2009 Infosys Technologies Limited
150
Parsing Strings
Syntax:
char *strtok(char *str, const char *delimiters)
• strtok( ) is a function which returns a pointer to the next token in
the string pointed to by string1.
• The delimiters that determine the token are the characters making
up the string pointed to by string2.
• When there is no token to return , a null pointer will be returned
• To tokenize a string, the first call to strtok( ) must have string1
point to the string being tokenized.
• a null pointer for string1 is used in subsequent calls.
• Thus,the entire string is reduced to its tokens.
• Using a different set of delimiters for each call to strtok is also
possible
For Infosys internal use
© 2009 Infosys Technologies Limited
151
Printing to a String
The sprintf function allows us to print to a string argument using printf
formatting rules
First argument of sprintf is string to print to, remaining arguments are as in
printf
Example:
char buffer[100];
sprintf(buffer,”%s, %s”,LastName,FirstName);
if (strlen(buffer) > 15)
printf(“Long name %s %s\n”,FirstName,LastName);
For Infosys internal use
© 2009 Infosys Technologies Limited
152
Thoughts
•
•
•
•
•
•
•
•
•
•
Which function is used to find the length of the string?
Which function is used to compare two strings?
Which function is used to copy one string to another?
How do strncpy() differ from strcpy()?
How do strncmp() differ from strcmp()?
What is the use of memcmp()?
How do strncat() differ from strcat()?
How do strstr() differ from strchr()?
What is the use of strtok()?
What is the use of sprintf()?
For Infosys internal use
© 2009 Infosys Technologies Limited
153
Control Structures
For Infosys internal use
© 2009 Infosys Technologies Limited
154
Control Structures
• All programming languages would have:
• Assignments
• IF THEN ELSE
• GOTO
• LOOP
• While
• Do … while
• For(…)
• First three of them are sufficient to write the code, other two are there to
make programs more readable or comprehensible.
For Infosys internal use
© 2009 Infosys Technologies Limited
155
Control Structures
• In while or do… while constructs we have to increment/decrement loop
variants inside the grouped statements. There are chances that some one
might forgets to do that. Failure to do this might lead to a program going to
an infinite loops.
• In a for loop construct we are required to specify, initial setting, final
condition, and loop variant increment/decrement at one place. This reduces
chances of such mistakes
For Infosys internal use
© 2009 Infosys Technologies Limited
156
Control Structures
int num1;
int num2;
scanf(“%d”,&num1);
num2 = 1
while(num1 >1){
num2 = num2 * num1;
num1 = num1 -1;
}
printf(“%d”, num2);
int num1;
int num2;
scanf(“%d”, & num1);
for(num2 =1; num1 >1; num1 =
num1 – 1)
{
num2 = num2 * num1;
}
printf(“%d”, num2);
For Infosys internal use
© 2009 Infosys Technologies Limited
157
if and else if Statements
• Always use ‘else if’ when a sequence of conditions has to be tested.
• Using only ‘if’ will make the compiler to check all the conditions. This
increases the execution time
Amateur code
(Using only ‘if’)
if (iDuration >= 6) {
fRateOfInterest = 6.5;
}
if ((iDuration >= 3) &&
(iDuration < 6)) {
fRateOfInterest = 6.0;
}
if (iDuration == 2) {
fRateOfInterest = 5.5;
}
if (iDuration == 1) {
fRateOfInterest = 5.0;
}
P ro fe ssio n a l co d e
(U sin g ‘e lse if’)
if (iDuration >= 6) {
fRateOfInterest = 6.5;
}
else if ((iDuration >= 3) &&
(iDuration < 6)) {
fRateOfInterest = 6.0;
}
else if (iDuration == 2) {
fRateOfInterest = 5.5;
}
else {
fRateOfInterest = 5.0;
}
For Infosys internal use
© 2009 Infosys Technologies Limited
158
Assignment(=) vs. Equality Operator (==) (1 of 3)
• The operator ‘=’ is used for assignment purposes whereas the operator ‘==’
is used to check for equality
• It is a common mistake to use ‘=’ instead of ‘==’ to check for equality
• The compiler does not generate any error message
• Example:
if (fRateOfInterest = 6.5) {
printf(“Minimum Duration of deposit: 6 years”);
}
else if (fRateOfInterest = 6.0) {
printf(“Minimum Duration of deposit: 3 years”);
}
else {
printf(“No such interest rate is offered”);
}
The output of the above program will be “Minimum Duration of deposit: 6
years” irrespective of any input for ‘fRateOfInterest’
For Infosys internal use
© 2009 Infosys Technologies Limited
159
Assignment(=) vs. Equality Operator (==) (2 of 3)
• To overcome the problem, when constants are compared with variables for
equality, write the constant on the left hand side of the equality symbol
• Example:
if (6.5 = fRateOfInterest) {
printf(“Minimum Duration of deposit: 6 years”);
}
else if (6.0 = fRateOfInterest) {
printf(“Minimum Duration of deposit: 3 years”);
}
else {
printf(“No such interest rate is offered”);
}
• When the above code is compiled it generates compilation errors because
the variable’s value is being assigned to a constant
• This helps in trapping the error at compile time itself, even before it goes to
unit testing
For Infosys internal use
© 2009 Infosys Technologies Limited
160
Assignment(=) vs. Equality Operator (==) (3 of 3)
• Corrected Code:
if (6.5 == fRateOfInterest) {
printf(“Minimum Duration of deposit: 6 years”);
}
else if (6.0 == fRateOfInterest) {
printf(“Minimum Duration of deposit: 3 years”);
}
else {
printf(“No such interest rate is offered”);
}
For Infosys internal use
© 2009 Infosys Technologies Limited
161
What is the output of the following code snippet?
iResult = iNum % 2;
if ( iResult = 0) {
printf("The number is even");
}
else {
printf("The number is odd");
}
The output is
"The number is odd"
CASE 1: When iNum is 11
The output is
CASE 2: When iNum is 8
"The number is odd"
WHY???
For Infosys internal use
© 2009 Infosys Technologies Limited
162
What is the output of the following code snippet?
int iNum = 2;
switch(iNum){
case 1:
printf(“ONE-1”);
break;
case 2:
printf(“TWO-2”);
break;
case 3:
printf(“THREE-3”);
break;
default:
printf(“INVALID OPTION ”);
break;
For Infosys internal use
}
© 2009 Infosys Technologies Limited
TWO-2
163
What is the output of the following code snippet?
int iNum = 2;
switch(iNum) {
default:
printf(“INVALID”);
case 1:
printf(“ONE-1”);
case 2:
printf(“TWO-2”);
break;
case 3:
printf(“THREE-3”);
}
TWO-2
For Infosys internal use
© 2009 Infosys Technologies Limited
164
Comparison of break, continue and exit
break
Used to quit an innermost
loop or switch
Can be used only within loops
or switch
continue
Used to continue the
innermost loop
Can be used only within the
loops
exit()
Used to terminate the
program
Can be used anywhere in the
program
For Infosys internal use
© 2009 Infosys Technologies Limited
165
Selecting between while, do while and for loops
• If number of times to iterate a loop is well known, choose ‘for’ loop
• If number of times to iterate a loop is not known, choose ‘while’ loop and
the loop should not be executed when the condition is initially false
• If number of times to iterate a loop is not known, choose ‘do while’ loop
and the loop should be executed even when the condition is initially false
For Infosys internal use
© 2009 Infosys Technologies Limited
166
Control Structures
int main ( )
{ int iNum1 = 2;
while (iNum1 = 1)
{
printf { “ Hi “);
iNum1 --;
}
}
int main ( )
{ int iNum1 = 2;
while (iNum1 = 0 )
{
printf { “ Hi “);
iNum1 --;
}
}
Answer: Infinite Loop
Answer: Does not enter the loop
For Infosys internal use
© 2009 Infosys Technologies Limited
167
Strings
For Infosys internal use
© 2009 Infosys Technologies Limited
168
Strings
Function to initialize an integer array
void fvInitArray (int * piArray, int iSize, int iVal)
{ int iCount;
for (iCount = 0; iCount < iSize; iCount ++)
* (piArray + iCount) = iVal;
}
Function to calculate String length of a string
int fiStrlen (char * pcString )
{ int iCount = 0;
while ( * (pcString + iCount) != ‘\0’)
iCount++;
return (iCount +1);
}
For Infosys internal use
© 2009 Infosys Technologies Limited
169
Passing Strings to a Function
• By passing the base address of a string to a function the entire string can be
accessed. How??
• The function will take a pointer (char *s) to the address. *s will give the
contents at the first location. By incrementing s (++s) the contents at the
subsequent location can be obtained. End of the string is when *s equals
‘\0’
For Infosys internal use
© 2009 Infosys Technologies Limited
170
What does this program do ?
char *last_word(char *str)
{ int L; char *end;
L = strlen(str);
end = str + L;
while( *end != ' ')
end--;
return end+1;
}
•
LAST WORD IN A STRING
Find the BUG!
For Infosys internal use
© 2009 Infosys Technologies Limited
171
Tutorial Problem
• What is the output of the program?
main ()
{ int *piptr;
*pi ptr= 1;
printf("%d", *piptr);
}
For Infosys internal use
© 2009 Infosys Technologies Limited
172
Corrected Code
• What is the output of the program?
main ()
{ int *piptr;
piptr = (int *) malloc (sizeof(int))
*piptr= 1;
printf("%d", *piptr);
free (piptr);
}
For Infosys internal use
© 2009 Infosys Technologies Limited
173
Tutorial problem
• What is the output of the program:
main()
{ int a, *b, **i;
b = &a;
a = 1;
*i = b;
printf(“%d”, **i);
}
For Infosys internal use
© 2009 Infosys Technologies Limited
174
Tutorial problem Corrected
• What is the result of this program?
main()
{ int a, *b, **i;
b = &a;
a = 1;
i = &b;
printf(“%d”, **i);
}
For Infosys internal use
© 2009 Infosys Technologies Limited
175
References
•
•
•
•
•
•
http://enrcbt\cbtcontents
Kernighan & Ritchie, The C programming language, Prentice Hall India.
Yeshwant Kanetkar, Let Us C, BPB publication.
Mullish Cooper, The Spirit of C, Jaico.
Herbert Schildth, Teach Yourself C, Obsorne MCGraw – Hill.
Yeshwant Kanetkar, Understanding Pointers in C, BPB publication.
For Infosys internal use
© 2009 Infosys Technologies Limited
176
Thank You
www.infosys.com
For Infosys internal use
© 2009 Infosys Technologies Limited
© 2009 Infosys Technologies Limited
Descargar

Slide 1