PPS - Unit 3
PPS - Unit 3
Fundamentals of C
A sample C Program
#include<stdio.h>
int main()
{
int a; //variable declaration
--other statements
}
Header Files
5
◻ Type a program
◻ Save it
◻ Compile the program – This will generate an
exe file (executable)
◻ Run the program (Actually the exe created out
of compilation will run and not the .c file)
◻ In different compiler we have different option
for compiling and running. We give only the
concepts.
Comments
10
Note: Number of bytes and range given to each data type is platform
dependent
Data Types in C
15
Constants
16
Ex. PI=3.142
Variables
17
Ex :
int a; //-----variable declaration
auto if
break int
case long
char register
const return
continue short
default signed
do sizeof
double static
else struct
enum switch
extern typedef
float union
for unsigned
goto void
volatile while
Operators In C
23
◻ Assignment operator ( = )
◻ Arithmetic operators ( +, -, *, /, % )
◻ Relational operators ( >, >=, <, <=, == , != )
◻ Logical operators ( !, &&, || )
◻ Address operator ( & )
◻ Increment and Decrement operators ( ++, -- )
◻ Compound Assignment Operators ( =, +=, -=, /=, *=, %= )
◻ sizeof operator
Use of Modulus (%) Operator
24
Operator Priority
* , / and % Highest
+ and - Lowest
The expression that is written within parenthesis is given highest priority
◻ Prefix operator first increments / decrements and then makes the assignment
Example:
int iValue;
int iResult;
iValue = 5;
iResult = ++iValue;
◻ Post fix operator makes the assignment and then increments/decrements the
Value
Example:
int iValue;
int iResult;
iValue = 5;
iResult = iValue++;
Address of Operator
30
Example:
int EmployeeId = 1001;
double Salary = 7600.00;
printf(“Employee Id %d” , EmployeeId);
printf(“Salary %lf”, Salary);
Formatted Output Using printf (cont’d)
36
Example:
int EmployeeId = 1001;
double Salary = 7600.00;
printf(“Employee Id %d\n”, EmployeeId);
printf(“Salary %lf\n”, Salary);
Declaring and Using Character Variables
38
Syntax:
char variablename1, variablename2,….;
Example:
char Alphabet;
char Status, Number ;
◻ The value can be assigned by enclosing it in a single quote (‘ ‘)
else Statement
◻ In simple ‘if’ statement, when the condition is true, a set of statements are
executed. But when it is false, there is no alternate set of statements
◻ The statement ‘else’ provides the same
Syntax:
if (testExpression)
{
// codes inside the body of if
}
else
{
// codes inside the body of else
}
If-else Statement (Cont’d)
43
else Statement
Example:
if (Duration >= 3) {
RateOfInterest = 6.0;
}
else {
RateOfInterest = 5.5;
}
else if Statement
◻ The ‘else if’ statement is to check for a sequence of conditions
◻ When one condition is false, it checks for the next condition and so on
◻ When all the conditions are false the ‘else’ block is executed
◻ The statements in that conditional block are executed and the other ‘if’ statements
are skipped
Nested if Statement
44
Syntax:
Nested if Statement
if (condition-1)
• An ‘if’ statement embedded within another ‘if’
{
statement is called as nested ‘if’
Statement 1;
• Example:
if (condition-2)
if (iDuration > 6 )
{
{
Statement 2;
if (dPrincipalAmount > 25000)
}
{
else
printf(“Your percentage of incentive is 4%”);
{
}
Statement n;
else
}
{
}
printf(“Your percentage of incentive is 2%”);
else {
}}
Statement x;
else {
}
printf(“No incentive”);
Next Statement;
}
Example
45
}
}
Output: Choice is 2
48
Iterational Control Structures
49
◻ A ‘while’ loop is used to repeat certain statements as long as the condition is true
◻ When the condition becomes false, the ‘while’ loop is quitted
◻ This loop control structure is called as an entry-controlled loop because, only
when the condition is true, are the statements executed
Syntax:
while (condition)
{
Set of statements;
}
Next Statement;
Example:
unsigned int Count = 1;
while (Count <= 3)
{
printf(“%d\n”,Count);
}
What is the output of the following code?
51
Example 1 Example 2
unsigned int Count=3; int Count = 0;
while (Count<=5) while(Count<=10)
{ {
printf(“%u\n”,Count); printf(“\n MITWPU”);
Count++; Count++;
} }
do while Loop Control Structure
52
◻ The ‘do while’ loop is very similar to ‘while’ loop. In ‘do while’ loop, the
condition is tested at the end of the loop.
◻ Because of this, even when the condition is false, the body of the loop is
executed at least once.
◻ This is an exit-controlled loop.
Syntax:
do
{
Set of statement(s);
} while (condition);
Next Statement;
do while Loop Control Structure Example
53
◻ The ‘for’ loops are similar to the other loop control structures
◻ The ‘for’ loops are generally used when certain statements have to be
executed a specific number of times
◻ Advantage of for loops:
⬜ All the three parts of a loop (initialization, condition , increment) can be
given in a single statement
⬜ Because of this, there is no chance of user missing out initialization or
increment steps which is the common programming error in ‘while’ and ‘do
while’ loops
Syntax:
for (Initialization; Termination-Condition; Increment-Step)
{
Set of statement(s);
}
Next Statement;
for Loop Control Structure (cont’d)
56
Example:
int Count;
for (int Count = 1; Count <= 5; Count++)
{
printf(“%d\n”,Count);
}
Output:
1
2
3
4
5
What is the output of the following code?
57
int Num;
int Counter;
int Product;
for(Counter=1; Counter<= 3; Counter++)
{
Product = Product * Counter;
}
printf("%d", Product);
for(int Count=0;Count<10;Count++);
{
printf("%d\n",Count);
}
The output is 10
for and while loops
59
Given Sum=0,Ctr=0;
int sum,Ctr=0;
while(Ctr<10)
for(int Sum=0,Ctr=0;Ctr<10;Ctr=Ctr+1)
{
{
scanf(“%d”,&Num);
scanf(“%d”,&Num);
Sum=Sum+Num;
Sum=Sum+Num;
Ctr=Ctr+1;
}
}
printf(“%d”,Sum);
printf(”%d”,Sum);
Nested Loops
60
int Counter1=0;
int Counter2;
while(Counter1 < 3) {
for (Counter2 = 0; Counter2 < 5; Counter2++) {
printf("%d\t",Counter2);
if (Counter2 == 2){
break;
}
}
printf("\n");
Counter1 += 1;
} 0 1 2 is printed 3 times
Continuing the Loops - continue Statement
63
◻ ‘continue’ statement forces the next iteration of the loop to take place and skips the
code between continue statement and the end of the loop
◻ In case of for loop, continue makes the execution of the increment portion of the
statement and then evaluates the conditional part.
◻ In case of while and do-while loops, continue makes the conditional statement to
be executed.
Example:
int count;
for( Count = 0 ; Count < 10; Count++) {
if (Count == 4) {
continue;
}
printf(“%d\n”, Count);
} The above code displays numbers from 1 to 9 except 4.
Terminating the program using exit() function
64
void main()
{
int i,j; for(j=0;j<=i;j++)
int space=4; {
//run loop (parent loop) till number of rows printf("* ");
for(i=0;i< 5;i++) }
{ printf("\n");
//loop for initially space, before star space--; // decrement one space after one
printing row
for(j=0;j< space;j++) }
{ }
printf(" ");
}
Arrays in C
68
float Salary[6];
Contd…
70
◻ Example:
int aiEmployeeNumbers[ ] = {15090, 15091,
15092, 15093,15094, 15095};
#include<stdio.h>
int main()
{
#define ARRAYSIZE 10
❑ The array has two subscripts. One subscript denotes the row
& the other the column. The declaration of two dimension
arrays is as follows:
❑ int arr[2][3];
❑ Syntax :
data_type array_name[row_size][column_size];
int m[10][20];
◻ In memory, a string ends with a null character ‘\0’ and it occupies 1 byte
of memory.
◻ char acItemCategory[3]=“Books”;
◻ Here the size specified is 3. But the number of characters in
the string is 5. This is invalid.
◻ char acItemCategory[ ]={'s','t','a','t','i','o','n','a','r','y','\0'};
◻ Here the character constants are supplied to initialize the
string.
Reading and Printing Strings -
Example
84
◻ Example:
strlen(“Programming Fundamentals”);
/*returns 24*/
strlen(acItemCategory);
◻ returns the number of characters in the character
array ‘acItemCategory’
Example: C strlen() function
88
#include <stdio.h>
#include <string.h>
int main() {
char a[20]="Program";
char b[20]={'P','r','o','g','r','a','m','\0'};
char c[20];
printf("Enter string: ");
gets(c); //scanf(“%s”,c);
printf("Length of string a = %d \n",strlen(a));
#include <stdio.h>
#include <string.h>
int main()
{
char str1[10]= "awesome";
char str2[10];
char str3[10];
strcpy(str2, str1); //function
call
strcpy(str3, "well");
puts(str2);
puts(str3);
return 0;
}
strcat() Function
92
◻ Example:
◻ char acTraineeFpCourse [50] = “The course is “;
◻ strcat(acTraineeFpCourse,”Oracle 8i”);
◻ The resultant string in acTraineeFPCourse will be
◻ “The course is Oracle 8i”
Example: C strcat() function
94
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "This is", str2[] = "MIT-WPU";
//concatenates str1 and str2 and resultant string is stored in str1.
strcat(str1,str2);
puts(str1);
puts(str2);
return 0;
}
strcmp() Function
95
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int result;