Professional Documents
Culture Documents
Objectives
Ver. 1.0
Slide 1 of 53
Programming in C
Identifying the Benefits and Features of C Language
Ver. 1.0
Slide 2 of 53
Programming in C
C as a Second and Third Generation Language
C language:
Possesses powerful low-level features of second generation
languages.
Provides loops and constructs available in third generation
languages.
Is very powerful and flexible.
Ver. 1.0
Slide 3 of 53
Programming in C
Block Structured Language - An Advantage for Modular Programming
C language:
Offers all essentials of structured programming.
Has functions that work along with other user-developed
functions and can be used as building blocks for advanced
functions.
Offers only a handful of functions, which form the core of the
language.
Has rest of the functions available in libraries. These functions
are developed using the core functions.
Ver. 1.0
Slide 4 of 53
Programming in C
Features of the C Language
Ver. 1.0
Slide 5 of 53
Programming in C
Using the Data Types Available in C language
Ver. 1.0
Slide 6 of 53
Programming in C
Fundamental Data Types
Ver. 1.0
Slide 7 of 53
Programming in C
Fundamental Data Types (Contd.)
Ver. 1.0
Data
Number of bytes on a
32-byte machine
Minimum
Maximum
char
-128
127
int
-2^31
(2^31) - 1
float
6 digits of precision
6 digits of precision
Slide 8 of 53
Programming in C
Derived Data Types
Ver. 1.0
Slide 9 of 53
Programming in C
Derived Data Types (Contd.)
Ver. 1.0
Data
Number of bytes
on a 32-byte
machine
Minimum
Maximum
short int
-2^15
(2^15) - 1
long int
-2^31
(2^31) - 1
double float
12 digits of precision
6 digits of
precision
Slide 10 of 53
Programming in C
Defining Data
Ver. 1.0
Data definition
Data type
Memory defined
Size (bytes)
Value assigned
char a, c;
char
a
c
1
1
char a = 'Z';
char
int count;
int
count
int
a
count
4
4
10
float fnum;
float
fnum
float fnum1,
fnum2 = 93.63;
float
fnum1
fnum2
4
4
93.63
Slide 11 of 53
Programming in C
Practice: 1.1
Ver. 1.0
Slide 12 of 53
Programming in C
Practice: 1.1 (Contd.)
Solution:
1. int num;
2. char chr=Z;
3. float num = 8.93;
4. int i, j=0;
Ver. 1.0
Slide 13 of 53
Programming in C
Defining Data (Contd.)
Defining Strings:
Syntax:
char (variable) [(number of bytes)];
Here number of bytes is one more than the number of
characters to store.
To define a memory location of 10 bytes or to store 9 valid
characters, the string will be defined as follows:
char string [10];
Ver. 1.0
Slide 14 of 53
Programming in C
Practice: 1.2
Ver. 1.0
Slide 15 of 53
Programming in C
Practice: 1.2 (Contd.)
Solution:
1. char addrs[31];
2. char head[15];
Ver. 1.0
Slide 16 of 53
Programming in C
Identifying the Structure of C Functions
Ver. 1.0
Slide 17 of 53
Programming in C
Single Level Functions
Ver. 1.0
Slide 18 of 53
Programming in C
Single Level Functions (Contd.)
Ver. 1.0
Slide 19 of 53
Programming in C
Practice: 1.3
Ver. 1.0
Slide 20 of 53
Programming in C
Practice: 1.3 (Contd.)
3. main()
}
printf("This has got to be right");
{
4. main()
{
This is a perfect comment line
printf("Is it okay?");
}
Ver. 1.0
Slide 21 of 53
Programming in C
Practice: 1.3 (Contd.)
Solution:
1. man instead of main() and semi-colon missing at the end of
the printf() function.
2. mam instead of main() and ) missing at the end of the
printf() function.
3. } instead of { for marking the beginning of the function and
{ instead of } for marking the end of the function.
4. Comment line should be enclose between /* and */.
Ver. 1.0
Slide 22 of 53
Programming in C
Multiple Level Functions
Slide 23 of 53
Programming in C
Multiple Level Functions (Contd.)
Ver. 1.0
Slide 24 of 53
Programming in C
Practice: 1.4
Slide 25 of 53
Programming in C
Practice: 1.4 (Contd.)
Solution:
a. main() is always the first function to be executed. Further
execution of the program depends on functions invoked from
main(). Here, after executing printf(), the program
terminates as no other function is invoked. The function
print_msg is not invoked, hence it is not executed.
b. The two functions, dis_error() and disp_error, are not
the same because the function names are different.
Ver. 1.0
Slide 26 of 53
Programming in C
Using the Input-Output Functions
C Environment
Ver. 1.0
Slide 27 of 53
Programming in C
Using the Input-Output Functions (Contd.)
Ver. 1.0
Slide 28 of 53
Programming in C
Character-Based Input-Output Functions
Slide 29 of 53
Programming in C
Character-Based Input-Output Functions (Contd.)
Ver. 1.0
Slide 30 of 53
Programming in C
Practice: 1.5
1.
Ver. 1.0
Slide 31 of 53
Programming in C
Practice: 1.5 (Contd.)
Solution:
Ver. 1.0
Slide 32 of 53
Programming in C
Practice: 1.6
1.
Ver. 1.0
Slide 33 of 53
Programming in C
Practice: 1.6 (Contd.)
Solution:
/* function to accept and display two characters*/
#include<stdio.h>
main()
{
char a, b;
a=getchar();
fflush(stdin);
b=getchar();
fflush(stdin);
putchar(a);
putchar(b);
}
Ver. 1.0
Slide 34 of 53
Programming in C
String-Based Input-Output Functions
Slide 35 of 53
Programming in C
Practice: 1.7
Ver. 1.0
Slide 36 of 53
Programming in C
Practice: 1.7 (Contd.)
Solution:
Ver. 1.0
Slide 37 of 53
Programming in C
Using Constructs
Ver. 1.0
Slide 38 of 53
Programming in C
Conditional Constructs
Conditional Constructs:
Requires relation operators as in other programming language
with a slight change in symbols used for relational operators.
The two types of conditional constructs in C are:
if..else construct
switchcase construct
Ver. 1.0
Slide 39 of 53
Programming in C
Conditional Constructs (Contd.)
Ver. 1.0
Slide 40 of 53
Programming in C
Practice: 1.8
Ver. 1.0
Grade
HRA %
45%
40%
30%
25%
Slide 41 of 53
Programming in C
Practice: 1.8 (Contd.)
Slide 42 of 53
Programming in C
Practice: 1.8 (Contd.)
Solution:
Ver. 1.0
Slide 43 of 53
Programming in C
Conditional Constructs (Contd.)
Syntax of switchcase construct:
switch (variable)
{
case 1 :
statement1 ;
break ;
case 2 :
statement 2 ;
:
:
break;
default :
statement
}
Ver. 1.0
Slide 44 of 53
Programming in C
Practice: 1.9
Ver. 1.0
Slide 45 of 53
Programming in C
Practice: 1.9 (Contd.)
Solution:
Ver. 1.0
Slide 46 of 53
Programming in C
Loop Constructs
loop
body
Ver. 1.0
Slide 47 of 53
Programming in C
Loop Constructs (Contd.)
Evaluate
Condition
do while
Execute
Body of
Loop
False
True
Execute
Body of
Loop
Evaluate
Condition
False
True
Ver. 1.0
Slide 48 of 53
Programming in C
Practice: 1.10
Ver. 1.0
Slide 49 of 53
Programming in C
Practice: 1.10 (Contd.)
Solution:
Ver. 1.0
Slide 50 of 53
Programming in C
Summary
Ver. 1.0
Slide 51 of 53
Programming in C
Summary (Contd.)
Ver. 1.0
Slide 52 of 53
Programming in C
Summary (Contd.)
Ver. 1.0
Slide 53 of 53
Programming in C
Objectives
Ver. 1.0
Slide 54 of 53
Programming in C
Working with Operators
An operator:
Is a symbol used to command the computer to do
mathematical or logical manipulations.
Operates on data and variables.
Ver. 1.0
Slide 55 of 53
Programming in C
Practice: 2.1
Ver. 1.0
Slide 56 of 53
Programming in C
Practice: 2.1 (Contd.)
Solution:
#include<stdio.h>
main()
{ char yn;
do {
puts(Enter y/n (Yes/No));
yn=getchar ();
fflush (stdin);
if(yn!=y && yn!=n)
puts(Invalid input);
}
while(yn!=y && yn!=n);
}
Ver. 1.0
Slide 57 of 53
Programming in C
Unary Operators
Unary Operators:
Operates on a single operand.
Prefixed to an integer constant.
Tells the compiler to reverse the sign by subtracting the value
(or variable) from zero.
Has the same effect as the sign, which is used to indicate a
number less than zero, for example -12.
Ver. 1.0
Slide 58 of 53
Programming in C
Practice: 2.2
Ver. 1.0
Slide 59 of 53
Programming in C
Practice: 2.2 (Contd.)
Solution:
1. Valid
2. Valid
Ver. 1.0
Slide 60 of 53
Programming in C
Binary Operators
Binary Operators:
Operate on two operands.
Are as follows:
+ (add)
- (subtract)
* (multiply)
/ (divide)
% (modulo)
Ver. 1.0
Slide 61 of 53
Programming in C
Practice: 2.3
/*statement A*/
/*statement B*/
Ver. 1.0
Slide 62 of 53
Programming in C
Practice: 2.3 (Contd.)
Solution:
1. ch is equal to s. Note that a-A gives 32 after statement 1 is
executed.
2. ch is back to S after statement 2 is executed.
Ver. 1.0
Slide 63 of 53
Programming in C
Binary Operators (Contd.)
Ver. 1.0
Slide 64 of 53
Programming in C
Practice: 2.4
Ver. 1.0
Slide 65 of 53
Programming in C
Practice: 2.4 (Contd.)
Solution:
1. a. Loss of data. int set equal to a double.
b. No loss of data. double set equal to a float.
c. Loss of data. float set equal to a double.
d. Loss of data. Right-hand result is a double while left-hand
side is just an int.
e. No loss of data. Right-hand result is a double and
left-hand side is also a double.
2. Not necessary. The ints will automatically be converted to
doubles (following the conversion of the float to a double).
Ver. 1.0
Slide 66 of 53
Programming in C
Ternary Operator
Ternary Operator:
Is a shorthand method for writing if.else conditional
construct.
Helps in reducing lines of code.
Has the following form for the expression using the ternary
operator:
(test-expression) ? T-expression : F-expression;
Ver. 1.0
Slide 67 of 53
Programming in C
Ternary Operator (Contd.)
Ver. 1.0
Slide 68 of 53
Programming in C
Practice: 2.5
Ver. 1.0
Slide 69 of 53
Programming in C
Practice: 2.5 (Contd.)
Solution:
1. True.
2. If b is non-zero, it will determine the quotient of a and b. If b
equals zero, quotient is set to 0.
3. Yes. Note that if b is non-zero, the test expression (b)
evaluates to true and hence quotient is set to (a/b).
4. The variable always_negative will always take on a nonnegative value, i.e. it will be assigned the absolute value of j.
The name of the variable always_negative is just a red
herring. Remember that self-documenting variable names will
help in writing programs that are readable. Note the unary
operator (-j).
Ver. 1.0
Slide 70 of 53
Programming in C
Compound Assignment Operators
Ver. 1.0
Slide 71 of 53
Programming in C
Increment / Decrement Operators
Ver. 1.0
Slide 72 of 53
Programming in C
Increment / Decrement Operators (Contd.)
/* statement A */
/* statement B */
Ver. 1.0
Slide 73 of 53
Programming in C
Practice: 2.6
= 3, total = 5;
sum++;
++sum;
/*statement A */
sum
--sum;
/*statement B */
Ver. 1.0
Slide 74 of 53
Programming in C
Practice: 2.6 (Contd.)
Ver. 1.0
Slide 75 of 53
Programming in C
Practice: 2.6 (Contd.)
Ver. 1.0
Slide 76 of 53
Programming in C
Practice: 2.6 (Contd.)
Solution:
1. total=5, sum=5
total=3, sum=3
quite a complicated way of reducing total by 2.
2. True. i+1 is equivalent to i++.
3. True. i+1 is equivalent to 1+i.
4.
Ver. 1.0
Slide 77 of 53
Programming in C
Using Loops
The while and dowhile looping constructs are generally
used in situations where the number of execution of the loop
is not known.
The for loop construct is used when the number of
execution of the loop is known.
Ver. 1.0
Slide 78 of 53
Programming in C
The for Loop Construct
Ver. 1.0
Slide 79 of 53
Programming in C
The for Loop Construct (Contd.)
FALSE
EVALUATE
CONDITION
TRUE
BODY OF LOOP
REINITIALIZATION
Ver. 1.0
Slide 80 of 53
Programming in C
The for Loop Construct (Contd.)
Ver. 1.0
Slide 81 of 53
Programming in C
Practice: 2.7
Ver. 1.0
Slide 82 of 53
Programming in C
Practice: 2.7 (Contd.)
Solution:
1.
2.
Ver. 1.0
Slide 83 of 53
Programming in C
Controlling the Loop Execution
Ver. 1.0
Slide 84 of 53
Programming in C
Practice: 2.8
Ver. 1.0
Slide 85 of 53
Programming in C
Practice: 2.8 (Contd.)
Solution:
Ver. 1.0
Slide 86 of 53
Programming in C
Using Formatted Input-Output Functions
Ver. 1.0
Slide 87 of 53
Programming in C
Formatted Output
Ver. 1.0
Slide 88 of 53
Programming in C
Formatted Output (Contd.)
Ver. 1.0
Meaning
Slide 89 of 53
Programming in C
Practice: 2.9
Ver. 1.0
Slide 90 of 53
Programming in C
Practice: 2.9 (Contd.)
Solution:
Integer is: 15; Alphabet is Z.
Ver. 1.0
Slide 91 of 53
Programming in C
Formatted Input
Here
format
- The format-specification string
data1, data2 - Data names where the input data
is to be stored as per the
format-specification string
Ver. 1.0
Slide 92 of 53
Programming in C
Formatted Input (Contd.)
Ver. 1.0
Slide 93 of 53
Programming in C
Practice: 2.10
Ver. 1.0
Slide 94 of 53
Programming in C
Practice: 2.10 (Contd.)
Solution:
#include<stdio.h>
main()
{
int e_num;
float e_wt;
printf(Enter the Element No. and Weight of a
Proton\n);
scanf(%d %f, &e_num, &e_wt);
fflush(stdin);
printf(The Element No. is: , e_num);
printf(The weight of a Proton is: %f\n,
e_wt);
}
Ver. 1.0
Slide 95 of 53
Programming in C
Practice: 2.11
Number of
characters
Type of argument to
assign
int
float
char (string)
int
The function should also print out each of the assigned data
items in separate lines.
Ver. 1.0
Slide 96 of 53
Programming in C
Practice: 2.11
Solution:
#include<stdio.h>
main()
{
int i,j;
char str[3];
float fnum;
printf(Enter a string of 11 chrs\n);
/*why
11: because 11 is the total length of */
/*input.*/
scanf(%2d %4f %2s %3d,&i, &fnum, str, &j);
fflush(stdin);
printf(%2d\n %4f\n %2s\n %3d\n, i, fnum,
str, j);
}
Ver. 1.0
Slide 97 of 53
Programming in C
Summary
Ver. 1.0
Slide 98 of 53
Programming in C
Summary (Contd.)
Notation
OR
||
AND
&&
NOT
Ver. 1.0
Slide 99 of 53
Programming in C
Summary (Contd.)
Ver. 1.0
Slide 100 of 53
Programming in C
Summary (Contd.)
Ver. 1.0
Slide 101 of 53
Programming in C
Summary (Contd.)
Ver. 1.0
Slide 102 of 53
Programming in C
Objectives
Ver. 1.0
Slide 103 of 53
Programming in C
Chapter 1
Ver. 1.0
Slide 104 of 53
Programming in C
Chapter 2
Ver. 1.0
Slide 105 of 53
Programming in C
Chapter 2
Ver. 1.0
Slide 106 of 53
Programming in C
Objectives
Ver. 1.0
Slide 107 of 53
Programming in C
Working with Arrays
Arrays:
Are a group of similar objects.
Can be defined as a contiguous area in memory.
Can be referred to by a common name.
Has a unique identifier for each element, called as a subscript
or an index.
Can be categorized as:
One-dimensional/Single-dimensional arrays
Multidimensional arrays
Ver. 1.0
Slide 108 of 53
Programming in C
One-Dimensional Arrays
For Example:
char string [11];
Defines a character array named string to store 10
characters.
string[0] to string[9] are for valid characters.
string[10] for the string-terminator character, \0 (NULL).
Similarly:
int numbers [11];
Ver. 1.0
Slide 109 of 53
Programming in C
Practice: 3.1
Ver. 1.0
Slide 110 of 53
Programming in C
Practice: 3.1 (Contd.)
Solution:
a. float fnum[10];
b. int age[5], qty[6];
c. char name[31];
Ver. 1.0
Slide 111 of 53
Programming in C
One-Dimensional Arrays (Contd.)
Initializing Arrays:
An array can be initialized, when declared.
Initialization at the time of declaration is possible only outside a
function unless it is declared static.
Direct initialization is possible in the following ways:
char strl[7]={E,X,O,D,U,S,\0};
char str2[7]={"EXODUS"};
In the first case, individual elements have been moved into the
array. Therefore, it is essential that the string terminator be
specified explicitly. While in the second case, the string
terminator gets attached automatically because a string has
been assigned.
Ver. 1.0
Slide 112 of 53
Programming in C
Practice: 3.2
Ver. 1.0
Slide 113 of 53
Programming in C
Practice: 3.2 (Contd.)
Solution:
Ver. 1.0
Slide 114 of 53
Programming in C
One-Dimensional Arrays (Contd.)
Array elements:
Are referenced by using subscripts.
Are integers, therefore array manipulation is possible through
the use of variables.
Ver. 1.0
Slide 115 of 53
Programming in C
Practice: 3.3
Ver. 1.0
Slide 116 of 53
Programming in C
Practice: 3.3 (Contd.)
Solution:
Ver. 1.0
Slide 117 of 53
Programming in C
Practice: 3.4
Ver. 1.0
Slide 118 of 53
Programming in C
Practice: 3.4 (Contd.)
Solution:
Ver. 1.0
Slide 119 of 53
Programming in C
One-Dimensional Arrays (Contd.)
Array Addressing:
To arrive at a particular element, the following formula is
applied:
Starting address + ( Offset number * Scaling
factor) of the array
Here,
Scaling factor is the number of bytes of storage space required
by the specific data type.
Offset number * Scaling factor gives the number of bytes
to be added to the starting address of the array to get to a desired
element.
Ver. 1.0
Slide 120 of 53
Programming in C
Practice: 3.5
Ver. 1.0
Slide 121 of 53
Programming in C
Practice: 3.5 (Contd.)
Solution:
Ver. 1.0
Slide 122 of 53
Programming in C
Multidimensional Arrays
Ver. 1.0
Slide 123 of 53
Programming in C
Practice: 3.6
s_p_array [5][4] = {
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
};
Ver. 1.0
Slide 124 of 53
Programming in C
Practice: 3.6 (Contd.)
Solution:
1. False. Note that since there are 5 salesman with 4 products,
there should be 5 sets of {}, each containing four zeroes. The
correct initialization is:
int s_p_array [5][4] = {
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
};
Ver. 1.0
Slide 125 of 53
Programming in C
Multidimensional Arrays (Contd.)
Ver. 1.0
Slide 126 of 53
Programming in C
Practice: 3.7
Ver. 1.0
Slide 127 of 53
Programming in C
Practice: 3.7 (Contd.)
Solution:
a. M
b. Birds, Beasts, and Relatives
c. 97
Ver. 1.0
Slide 128 of 53
Programming in C
Practice: 3.8
Ver. 1.0
Slide 129 of 53
Programming in C
Practice: 3.8 (Contd.)
Ver. 1.0
ACTION
a.
b.
c.
d.
Slide 130 of 53
Programming in C
Practice: 3.8 (Contd.)
Solution:
Ver. 1.0
Slide 131 of 53
Programming in C
Appreciating Preprocessor Directives
Preprocessor directives:
Are instructions to the compiler in the source code of a C
program.
Are not actually a part of the C language.
Expand the scope of the C programming environment.
Begin with a #.
Ver. 1.0
Slide 132 of 53
Programming in C
Appreciating Preprocessor Directives (Contd.)
Ver. 1.0
Slide 133 of 53
Programming in C
Summary
Slide 134 of 53
Programming in C
Summary (Contd.)
Slide 135 of 53
Programming in C
Summary (Contd.)
Ver. 1.0
Slide 136 of 53
Programming in C
Summary (Contd.)
Ver. 1.0
Slide 137 of 53
Programming in C
Objectives
Ver. 1.0
Slide 138 of 53
Programming in C
Declaring and Manipulating Pointers
Ver. 1.0
Slide 139 of 53
Programming in C
Declaring Pointers
Ver. 1.0
Slide 140 of 53
Programming in C
Practice: 4.1
Ver. 1.0
Slide 141 of 53
Programming in C
Practice: 4.1 (Contd.)
Solution:
1. float
2. No. When a pointer variable is being declared, the type of
variable to which it is pointing to (int, float, or char)
should also be indicated.
3. False. A pointer to an integer is being declared and not an
integer.
4. Yes. It is okay to club declaration of a certain type along with
pointers to the same type.
Ver. 1.0
Slide 142 of 53
Programming in C
Manipulating Pointers
Ver. 1.0
Slide 143 of 53
Programming in C
Practice: 4.2
Ver. 1.0
x = y;
y=*ptr;
x = ptr;
x = &.ptr;
ptr = &x;
x = &y;
Slide 144 of 53
Programming in C
Practice: 4.2 (Contd.)
After statement
A
B
C
Ver. 1.0
&x
1006
&y
1018
ptr1
ptr2
Slide 145 of 53
Programming in C
Practice: 4.2 (Contd.)
Ver. 1.0
Slide 146 of 53
Programming in C
Practice: 4.2 (Contd.)
Solution:
Ver. 1.0
Slide 147 of 53
Programming in C
Pointer Arithmetic
Pointer Arithmetic:
Arithmetic operations can be performed on pointers.
Therefore, it is essential to declare a pointer as pointing to a
certain datatype so that when the pointer is incremented or
decremented, it moves by the appropriate number of bytes.
Consider the following statement:
ptr++;
It does not necessarily mean that ptr now points to the next
memory location. The memory location it will point to will
depend upon the datatype to which the pointer points.
May be initialized when declared if done outside main().
Consider the following example:
#include <stdio.h>
char movie[]= Jurassic Park;
main() {
char *ptr;
Ver. 1.0
Slide 148 of 53
Programming in C
Pointer Arithmetic (Contd.)
Ver. 1.0
Slide 149 of 53
Programming in C
Practice: 4.3
Ver. 1.0
Slide 150 of 53
Programming in C
Practice: 4.3 (Contd.)
Solution:
a. 12 ( Size of integer = 4*3)
b. False. Note that ptr is now pointing past the one-d array. So,
whatever is stored (junk or some value) at this address is
printed out. Again, note the dangers of arbitrary assignments
to pointer variables.
Ver. 1.0
Slide 151 of 53
Programming in C
Using Pointers to Manipulate Character Arrays
Ver. 1.0
Slide 152 of 53
Programming in C
One-Dimensional Arrays and Pointers
Ver. 1.0
Slide 153 of 53
Programming in C
One-Dimensional Arrays and Pointers (Contd.)
Gives the impression that the two pointers are equal. However,
there is a very subtle difference between str and ptr. str is a
static pointer, which means that the address contained in str
cannot be changed. While ptr is a dynamic pointer. The address
in ptr can be changed.
Ver. 1.0
Slide 154 of 53
Programming in C
Practice: 4.4
Ver. 1.0
Slide 155 of 53
Programming in C
Practice: 4.4 (Contd.)
Solution:
1. some_string [0]
2. True
3. True. Note that one does not have to count the size of the
error message in the first declaration.
Ver. 1.0
Slide 156 of 53
Programming in C
Two-Dimensional Arrays and Pointers
Slide 157 of 53
Programming in C
Two-Dimensional Arrays and Pointers (Contd.)
Ver. 1.0
Slide 158 of 53
Programming in C
Practice: 4.5
Ver. 1.0
Slide 159 of 53
Programming in C
Practice: 4.5 (Contd.)
Solution:
1. True. New strings can be typed straight away within the {}. As
in:
char *err_msg_msg[]= {
All's well,
File not found,
No read permission for file,
Insufficient memory,
No write permission for file
};
Ver. 1.0
Slide 160 of 53
Programming in C
Practice: 4.5 (Contd.)
2.
Ver. 1.0
Slide 161 of 53
Programming in C
Two-Dimensional Arrays and Pointers (Contd.)
Here,
*num is equal to num[0] because num[0] points to num[0][0].
*(*num) will give the value 3.
*num is equal to num[0].
Ver. 1.0
Slide 162 of 53
Programming in C
String-Handling Functions Using Pointers
Ver. 1.0
Slide 163 of 53
Programming in C
Using Pointers to Manipulate Character Arrays (Contd.)
Slide 164 of 53
Programming in C
Summary
Ver. 1.0
Slide 165 of 53
Programming in C
Summary (Contd.)
Ver. 1.0
Slide 166 of 53
Programming in C
Objectives
Ver. 1.0
Slide 167 of 53
Programming in C
Implementing Modular Approach in C Programs
Ver. 1.0
Slide 168 of 53
Programming in C
Advantages of Functions
Functions:
Allow reusability of code and structuring of programs.
Provide programmers a convenient way of designing
programs.
Ver. 1.0
Slide 169 of 53
Programming in C
Parameters of Functions
A parameter:
Is the data that the function must receive when called from
another function.
May or may not be present in a function.
Of a user-defined function is declared outside the {} of that
function.
Ver. 1.0
Slide 170 of 53
Programming in C
Practice: 5.1
Ver. 1.0
Slide 171 of 53
Programming in C
Practice: 5.1 (Contd.)
Solution:
1. The standard functions used in this program within main() are
as follows:
scanf() parameters are format of input, and pointers to the
variable(s) in which the input must be stored
fflush() parameter is stdin
The user-defined functions are:
output() no parameters
calc() one parameter, g, an int type data
Ver. 1.0
Slide 172 of 53
Programming in C
Invoking Functions
Ver. 1.0
Slide 173 of 53
Programming in C
Passing Arrays to Functions
Ver. 1.0
Slide 174 of 53
Programming in C
Practice: 5.2
Ver. 1.0
Slide 175 of 53
Programming in C
Practice: 5.2 (Contd.)
Ver. 1.0
Slide 176 of 53
Programming in C
Practice: 5.2 (Contd.)
Ver. 1.0
Slide 177 of 53
Programming in C
Practice: 5.2 (Contd.)
Solution:
1. Yes, because the addresses of the variables are passed in by
using call by reference, the memory locations of the variables
are known to the function. Using pointers to the variables, their
values can be modified.
2. False. Values of variables are copied into the parameters only
in the case of a call by value.
3. a. max()
b. min()
c. iii
d. ii
e. After max() is executed, the values of i and j printed out
would be the same as those entered during execution. After
min() is executed, the value of i would still be the same, but
j would increase by 5 (since b is a pointer to the variable j).
Ver. 1.0
Slide 178 of 53
Programming in C
Practice: 5.2 (Contd.)
4.main() {
int x, y;
printf(Enter Number: );
scanf(%d, &x);
fflush(stdin);
printf(Enter power raise to : );
scanf(%d, &y);
fflush(stdin);
power(&x, &y); }
power(m,n)
int *m, *n; /* power is pointed to by n,
value is pointed to by m */
{ int i=1,val=1;
while(i++<= *n)
val = val ** m;
printf(%d the power of %d is %d\n, *n,*m,
val);}
Ver. 1.0
Slide 179 of 53
Programming in C
Returning Values from a Function
Ver. 1.0
Slide 180 of 53
Programming in C
Practice: 5.3
Ver. 1.0
Slide 181 of 53
Programming in C
Practice: 5.3 (Contd.)
Ver. 1.0
Slide 182 of 53
Programming in C
Practice: 5.3 (Contd.)
Solution:
Ver. 1.0
Slide 183 of 53
Programming in C
Command-Line Arguments
Command-line arguments:
Are the parameters that the main() function can receive from
the command line.
Are passed as information from the OS prompt to a program.
Slide 184 of 53
Programming in C
Practice: 5.4
value of argc
argv [0]
argv [1]
argv[2]
1.
2.
3.
4.
Slide 185 of 53
Programming in C
Practice: 5.4 (Contd.)
Ver. 1.0
Slide 186 of 53
Programming in C
Practice: 5.4 (Contd.)
Solution:
Ver. 1.0
Slide 187 of 53
Programming in C
Using Library Functions for String Manipulation
Library functions:
Are also known as built-in functions.
Can be used by including the concerned header files.
Ver. 1.0
Slide 188 of 53
Programming in C
Standard String-Handling Functions
Ver. 1.0
Slide 189 of 53
Programming in C
Practice: 5.5
Ver. 1.0
Slide 190 of 53
Programming in C
Practice: 5.5 (Contd.)
Solution:
1. Value returned - 32
Declaration - int x;
2. 846*>
3. False
Ver. 1.0
Slide 191 of 53
Programming in C
String to Numeric Conversion Functions
Conversion functions:
Are available as a part of the standard library.
Are used to convert one data type into another.
Ver. 1.0
Slide 192 of 53
Programming in C
Practice: 5.6
Ver. 1.0
Slide 193 of 53
Programming in C
Practice: 5.6 (Contd.)
Solution:
1. a. 0
b. 345.000000
Ver. 1.0
Slide 194 of 53
Programming in C
Functions for Formatting Data in Memory
sscanf():
Performs formatted input from a string.
Has the following syntax:
sscanf(string, format-specification, data, .);
Ver. 1.0
Slide 195 of 53
Programming in C
Practice: 5.7
Ver. 1.0
Purpose
Instruction
printf(%s, name);
Slide 196 of 53
Programming in C
Practice: 5.7 (Contd.)
Solution:
Ver. 1.0
Slide 197 of 53
Programming in C
Working with Data Storage Types
Ver. 1.0
Slide 198 of 53
Programming in C
Practice: 5.8
2. A numeric array has to store 4 values - 2.5, 6,3, 7.0 and 8.0.
This array is to be declared and used in a function called
compute(). Which of the following is/are correct
declarations of this array?
a.
b.
c.
d.
Ver. 1.0
Programming in C
Practice: 5.8 (Contd.)
Solution:
1. a. area, number
b. val
2. (a) Is invalid because the array should be float or double type.
(b) Is invalid because it is declared as auto type.
(c) Is invalid because it is declared as auto type.
(d) Is correct.
.
Ver. 1.0
Slide 200 of 53
Programming in C
Practice: 5.9
Program B
char numarray[5];
main() {
char c ;
: }
Ver. 1.0
Slide 201 of 53
Programming in C
Practice: 5.9 (Contd.)
Ver. 1.0
Slide 202 of 53
Programming in C
Practice: 5.9 (Contd.)
Solution:
1. In program B, val would be declared as follows:
int val;
calc(){
:}
In program A, the declaration would be as follows:
main()
{ extern int val;
:}
Ver. 1.0
Slide 203 of 53
Programming in C
Practice: 5.9 (Contd.)
Ver. 1.0
Slide 204 of 53
Programming in C
Practice: 5.10
Ver. 1.0
Slide 205 of 53
Programming in C
Practice: 5.10 (Contd.)
Ver. 1.0
Slide 206 of 53
Programming in C
Practice: 5.10 (Contd.)
Solution:
Work out your answers. A discussion on these follows in the
Classroom.
Ver. 1.0
Slide 207 of 53
Programming in C
Summary
Call by value means that the called function creates its own
copy of the values in different variables.
Call by reference means that the called function should be able
to refer to the variables of the caller function directly, and does
not create its own copy of the values in different variables.
Ver. 1.0
Slide 208 of 53
Programming in C
Summary (Contd.)
Ver. 1.0
Slide 209 of 53
Programming in C
Summary (Contd.)
Ver. 1.0
Slide 210 of 53
Programming in C
Objectives
Ver. 1.0
Slide 211 of 53
Programming in C
Chapter 4
Ver. 1.0
Slide 212 of 53
Programming in C
Chapter 4 (Contd.)
Ver. 1.0
Slide 213 of 53
Programming in C
Chapter 4 (Contd.)
Ver. 1.0
Slide 214 of 53
Programming in C
Chapter 5
Ver. 1.0
Slide 215 of 53
Programming in C
Chapter 5 (Contd.)
Ver. 1.0
Slide 216 of 53
Programming in C
Chapter 5 (Contd.)
Ver. 1.0
Slide 217 of 53
Programming in C
Objectives
Ver. 1.0
Slide 218 of 53
Programming in C
Reading and Writing Contents in a File
Ver. 1.0
Slide 219 of 53
Programming in C
Opening Files
Ver. 1.0
Slide 220 of 53
Programming in C
FILE Type Pointers
Ver. 1.0
Slide 221 of 53
Programming in C
The exit() Function
Ver. 1.0
Slide 222 of 53
Programming in C
Character Input-Output with Files
Ver. 1.0
Slide 223 of 53
Programming in C
Closing Files
Ver. 1.0
Slide 224 of 53
Programming in C
Practice: 6.1
Slide 225 of 53
Programming in C
Practice: 6.1 (Contd.)
Solution:
Ver. 1.0
Slide 226 of 53
Programming in C
Practice: 6.2
Slide 227 of 53
Programming in C
Practice: 6.2 (Contd.)
Slide 228 of 53
Programming in C
Practice: 6.2 (Contd.)
Ver. 1.0
Slide 229 of 53
Programming in C
Practice: 6.2 (Contd.)
Solution:
Ver. 1.0
Slide 230 of 53
Programming in C
Line Input and Output with Files
The functions used for line input and output with files are:
fgets():
Is used to read number of specified characters from a stream.
Reads number of characters specified 1 characters.
Has the following syntax:
fgets(str, 181, ptr1);
Str Character array for storing the string
181 Length of the string to be read
ptr1- FILE pointer
fputs():
Is used to output number of specified characters to a stream.
Has the following syntax:
fputs(str, ptr1);
Str Character array to be written
ptr1- FILE pointer
Ver. 1.0
Slide 231 of 53
Programming in C
Practice: 6.3
Given that MAXLEN is a #define and that all lines in the file
number-list are 25 characters long what will the declaration
statements for the parameters of fgets() be?
3. Assume that the file number_list contains the following
records:
120
1305
Ver. 1.0
Slide 232 of 53
Programming in C
Practice: 6.3 (Contd.)
Given that the file has been opened and the first input
statement executed is as follows:
fgets(line, 3, file_ind);
Ver. 1.0
Slide 233 of 53
Programming in C
Practice: 6.3 (Contd.)
Ver. 1.0
Slide 234 of 53
Programming in C
Practice: 6.3 (Contd.)
Solution:
1. False. fputs() writes the contents of a string onto a file. So
even if a string has size 100, but contains only 20 characters
before a \0, only 20 characters get written.
2. The declarations are:
#define MAXLEN 26/* macro definition outside
main() */
char line[26];
3. b. fgets() will read either 3 - 1 characters , i.e. 2 characters,
or until it comes across a newline character. Since the newline
occurs after the third character, it will read in 2 characters from
the first record.
Ver. 1.0
Slide 235 of 53
Programming in C
Practice: 6.3 (Contd.)
4. a. 1
b. 2
c. 1 and 3. (NULL in case file cannot be opened; FILE type
pointer in case of successful open)
5. The answer to this practice will be discussed in class. Work
out your solution.
Ver. 1.0
Slide 236 of 53
Programming in C
Formatted Input and Output with Files
The functions for formatted input and output with files are:
fscanf():
Scans and formats input from a stream.
Is similar to scanf().
Has the following syntax:
int fscanf(FILE *Stream, const char
*format[,address,..]);
fprintf():
Sends formatted output to a stream.
Is similar to printf().
Has the following syntax:
int fprintf(FILE *Stream, const char
*format[,address,..]);
Ver. 1.0
Slide 237 of 53
Programming in C
Practice: 6.4
It is rewritten as:
fscanf(%6s%d, array, &num);
Ver. 1.0
Slide 238 of 53
Programming in C
Practice: 6.4 (Contd.)
Ver. 1.0
Slide 239 of 53
Programming in C
Practice: 6.4 (Contd.)
Size
4
20
60
Slide 240 of 53
Programming in C
Practice: 6.4 (Contd.)
Solution:
Ver. 1.0
Slide 241 of 53
Programming in C
Using Random Access in Files
Ver. 1.0
Slide 242 of 53
Programming in C
The fseek () Function
Here:
int rtn is the value returned by fseek()(0 if successful and 1 if
unsuccessful).
file-pointer is the pointer to the file.
offset is the number of bytes that the current position will shift on a
file.
from-where is the position on the file from where the offset would be
effective.
Ver. 1.0
Slide 243 of 53
Programming in C
The rewind () Function
Here:
file-pointer is the pointer returned by the function fopen().
After rewind() is executed, current position is always 1,
i.e. beginning of file.
Ver. 1.0
Slide 244 of 53
Programming in C
Practice: 6.5
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
Ver. 1.0
Slide 245 of 53
Programming in C
Practice: 6.5 (Contd.)
Ver. 1.0
Slide 246 of 53
Programming in C
Practice: 6.5 (Contd.)
Solution:
1. fseek(fp, 0L, 0);
2. The following current positions are relative to the beginning of
the file:
a.
b.
c.
d.
e.
Ver. 1.0
1
30
1
20
24
Slide 247 of 53
Programming in C
Practice: 6.6
Increment balance by
Rs 150.50
Rs 200.00
Rs 300.40
Ver. 1.0
Balance
Slide 248 of 53
Programming in C
Practice: 6.6 (Contd.)
Solution:
Ver. 1.0
Slide 249 of 53
Programming in C
Practice: 6.7
Ver. 1.0
Slide 250 of 53
Programming in C
Practice: 6.7 (Contd.)
Error listing:
Ver. 1.0
Slide 251 of 53
Programming in C
Practice: 6.7 (Contd.)
Solution:
1. Work out for your answer. The solution will be discussed in the
classroom session.
Ver. 1.0
Slide 252 of 53
Programming in C
Summary
Ver. 1.0
Slide 253 of 53
Programming in C
Summary (Contd.)
Ver. 1.0
Slide 254 of 53
Programming in C
Objectives
Ver. 1.0
Slide 255 of 53
Programming in C
Working with Structures
Structures:
Are collection of heterogeneous data types.
Are also known as records.
Are used to define new data types.
Are defined using the struct keyword.
Ver. 1.0
Slide 256 of 53
Programming in C
Defining Structures
Ver. 1.0
Slide 257 of 53
Programming in C
Practice: 7.1
Ver. 1.0
Slide 258 of 53
Programming in C
Practice: 7.1 (Contd.)
Solution:
1. False
2. a. The structure declaration should be:
struct {
int day;
int month;
int year;
} date;
Ver. 1.0
Slide 259 of 53
Programming in C
Defining Structures (Contd.)
Ver. 1.0
Slide 260 of 53
Programming in C
Practice: 7.2
int day;
int month;
int year;
date;
Declaration 1
Declaration 2
Answer the following questions:
1. Memory is allocated for the structure (date_type/ date).
2. Which of the following is/are the correct way(s) of referring to
the variable day?
a. day.date
b. date_type.day
Ver. 1.0
Slide 261 of 53
Programming in C
Practice: 7.2 (Contd.)
Ver. 1.0
Slide 262 of 53
Programming in C
Practice: 7.2 (Contd.)
Solution:
1. date (date_type is only a structure type)
2. a. Invalid because the structure name precedes the variable
name.
b. Invalid because date_type is not actually created in
memory, it is only a label.
3. The following statement should be added after the struct
declaration:
struct date_type date;
Ver. 1.0
Slide 263 of 53
Programming in C
Passing Structures to Functions
Ver. 1.0
Slide 264 of 53
Programming in C
Practice: 7.3
*ptr;
Slide 265 of 53
Programming in C
Practice: 7.3 (Contd.)
Ver. 1.0
Slide 266 of 53
Programming in C
Practice: 7.3 (Contd.)
Solution:
1. a. By using the following statement:
ptr = &date;
Slide 267 of 53
Programming in C
Arrays of Structures
Ver. 1.0
Slide 268 of 53
Programming in C
Practice: 7.4
(3 characters)
(20 characters)
(1 character)
(20 characters)
Ver. 1.0
Slide 269 of 53
Programming in C
Practice: 7.4 (Contd.)
Solution:
Ver. 1.0
Slide 270 of 53
Programming in C
Working with Structures (Contd.)
date{
dd;
mm;
yy;
Slide 271 of 53
Programming in C
Practice: 7.5
Ver. 1.0
Slide 272 of 53
Programming in C
Practice: 7.5 (Contd.)
Solution:
1. The first statement defines sentence as a data type consisting
of an array of 50 characters. The second statement declares
complex as a two-dimensional array, (an array of ten arrays of
50 characters each).
Ver. 1.0
Slide 273 of 53
Programming in C
Using Structures in File Handling
Ver. 1.0
Slide 274 of 53
Programming in C
Writing Records onto a File Using Structures
Ver. 1.0
Slide 275 of 53
Programming in C
Practice: 7.6
Slide 276 of 53
Programming in C
Practice: 7.6 (Contd.)
The program should allow the user to indicate when he
wants to stop data entry (i.e. it should keep accepting
records until the user indicates that there are no more
records).
After all records have been entered, a report on the total
number of sales and the total sale value for each product is
to be printed in the following format (for each product).
Product number
Product name
Total number of sales
Total sale value
: ___________________
: ___________________
: ___________________
: ___________________
Slide 277 of 53
Programming in C
Practice: 7.6 (Contd.)
Solution:
Ver. 1.0
Slide 278 of 53
Programming in C
Reading Records from Files Using Structures
Ver. 1.0
Slide 279 of 53
Programming in C
Practice: 7.7
Slide 280 of 53
Programming in C
Practice: 7.7 (Contd.)
Solution:
Ver. 1.0
Slide 281 of 53
Programming in C
Practice: 7.8
Ver. 1.0
Slide 282 of 53
Programming in C
Practice: 7.8 (Contd.)
Solution:
1. The solution to this practice will be discussed in class. Work
out your answer.
Ver. 1.0
Slide 283 of 53
Programming in C
Summary
Slide 284 of 53
Programming in C
Summary (Contd.)
Ver. 1.0
Slide 285 of 53
Programming in C
Objectives
Ver. 1.0
Slide 286 of 53
Programming in C
Differentiating Between High-Level and Low-Level Input/Output
Ver. 1.0
Slide 287 of 53
Programming in C
Difference Between High-level I/O and Low-level I/O
Low-level I/O:
Provides direct access to files and devices.
Is complex (buffer management is to be done by the
programmer).
Is faster.
Uses a file descriptor to track the status of the file.
Ver. 1.0
Slide 288 of 53
Programming in C
Practice: 8.1
Ver. 1.0
Slide 289 of 53
Programming in C
Practice: 8.1 (Contd.)
Solution:
A file descriptor is a non-negative integer.
Ver. 1.0
Slide 290 of 53
Programming in C
Uses of Low-Level Input/Output
Ver. 1.0
Slide 291 of 53
Programming in C
Working with Low-Level Input/Output Functions
Ver. 1.0
Slide 292 of 53
Programming in C
The open() Function
Ver. 1.0
Slide 293 of 53
Programming in C
The close() Function
Ver. 1.0
Slide 294 of 53
Programming in C
The read() function
Ver. 1.0
Slide 295 of 53
Programming in C
The write() function
Ver. 1.0
Slide 296 of 53
Programming in C
Practice: 8.2
Ver. 1.0
Slide 297 of 53
Programming in C
Practice: 8.2 (Contd.)
Solution:
1. In a read() function, the value of zero indicates end-of-file.
2. If you do not call write() function in a loop then the entire
data would not be written.
Ver. 1.0
Slide 298 of 53
Programming in C
Error Handling
Error Handling:
Some of the low-level I/O functions return error flags when
they fail to perform a specified task.
You can find these types of errors using a variable, errno.
The following table lists some values of errno that are
common to the open(), close(), read(), and write()
functions.
errno values
Ver. 1.0
Description
EACCES
Specifies that the program has failed to access one of the directories
in the file.
ENAMETOOLONG
ENOSPC
Specifies that the disc is out of space and the file can not be created.
EIO
EBADF
Specifies that the file descriptor passed to read, write, or close the file
is invalid.
Slide 299 of 53
Programming in C
Error Handling (Contd.)
Ver. 1.0
Description
EEXIST
Specifies that if File already exists, and O_CREAT and O_EXCL are
set, then opening the file would conflict with the existing file and the
file will not open.
EISDIR
ENOENT
EMFILE
EROFS
Specifies that the file is on a read only systembut either one of the
write permissions O_WRONLY, O_RDWR, or O_TRUNC is set.
Slide 300 of 53
Programming in C
Error Handling (Contd.)
Ver. 1.0
Description
EFBIG
EINTR
Slide 301 of 53
Programming in C
Using Random Access Seek in Files
Ver. 1.0
Slide 302 of 53
Programming in C
The lseek() Function
Ver. 1.0
Slide 303 of 53
Programming in C
The lseek() Function (Contd.)
Ver. 1.0
Description
SEEK_SET or 0
SEEK_CUR or 1
SEEK_END or 2
Slide 304 of 53
Programming in C
The lseek() Function (Contd.)
Ver. 1.0
Instruction
Function used
lseek(filedes,0,2);
lseek(filedes,0,0);
Slide 305 of 53
Programming in C
Summary
Ver. 1.0
Slide 306 of 53
Programming in C
Summary (Contd.)
Ver. 1.0
Slide 307 of 53
Programming in C
Practice Questions
Ver. 1.0
Slide 308 of 53
Programming in C
Chapter 7
Ver. 1.0
Slide 309 of 53
Programming in C
Chapter 8
Ver. 1.0
Slide 310 of 53