Professional Documents
Culture Documents
CLASSIFICATION
-SYSTEM SOFTWARE
-APPLICATION SOFTWARE
SYSTEM SOFTWARE:
OPERATING SYSTEMS, COMPILERS, LOADERS, LINKERS, ASSEMBLERS,
DEVICE DRIVERS ETC.
APPLICATION SOFTWARE:
WHEN THE PROBLEM TO BE SOLVED IS TOO COMPLEX
WHEN DATA TO BE PROCESSED IS LARGE/ HUGE
Programming Concepts of C Language
Case sensitive language
Performs very little (almost none) limits or type checking
Highly syntax dependent (use of ;)
Provides ample opportunities to experiment and develop new styles
Preprocessor directives:
#include <stdio.h>
#include<conio.h>
#define directive
Macros:
Inline Macros
#define N 10
#define SQR(x) x*x
Multiline macros
Programming Concepts of C Language
main() : int main()/ void main() / main()
Scalar Data types:
5 basic: char,int,float,double,enum
4 modifiers: short,long,signed,unsigned
Aggregate Data types:
Arrays, structures and Unions
So in C language &a[i][j]=a+(i*n+j)*sizeof(type)
-----------------------------------------------------------------------
struct
scanf() and printf():
scanf(“%d%c%d”,&x,&y,&z);
char a[10];
scanf(“%s”,a);
Discussions Lecture 4-5:
Arrays: Collection of similar items
Struct: Collection of different but related items.
struct stu_rec{
int rollno;
char name[40];
int marks;
};
typedef struct stu_rec student;
Now we can use student in place of struct stu_rec while declaring variables.
Example: student x;
● %*[^\n] scans everything until a \n, but doesn't scan in the \n. The asterisk(*)
tells it to discard whatever was scanned.
● %*c scans a single character, which will be the \n left over by %*[^\n] in this
case. The asterisk instructs scanf to discard the scanned character.
● Both %[ and %c are format specifiers. You can see what they do. The asterisks in both
the specifiers tell scanf , not to store the data read by these format specifiers.
● fflush(stdout) is permitted to be used.
● However fflush(stdin) in undefined in ANSI C and therefore it may or may not
work.
● Very good reference material for scanf():
http://www.cplusplus.com/reference/cstdio/scanf/
Programming Concepts of C Language
Use of escape sequences in printf():
\n,\t etc….
Use of getch() & getche() in Windows C compilers
#include<conio.h>
char x;
x=getch();
Instead, we use sizeof() function to allocate bytes based on the type of the data so
that it can run on any machine.
Discussions Lecture-6
Array space allocation example:
#include<stdio.h>
int main()
{
int a[10];
int *p;
a[0]=10;
p=a;
printf("%d %d %d\n",p[0],a[0],*a);
}
10 10 10
Discussions Lecture-6
Password program using getch(): (for Windows compiler only)
#include<stdio.h>
#include<conio.h> x This line not required in LINUX
int main()
{
char p[20],x;
int i=0,j;
printf("give password:");
do
{
x=getch(); x=getc(stdin); in LINUX
if (x!=13) 10 instead of 13 in LINUX (10 is LF in LINUX,
{ while13,10 is CRLF in Windows)
p[i]=x;
//printf("*");
}
i++;
}
while(x!=13); 10 instead of 13 in LINUX
printf("\nthe password is:");
for(j=0;j<i;j++)
printf("%c",p[j]);
}
Discussions Lecture-6
How to make a career in IT Industry
(For those who are comfortable with C programming. Those who are not, please
don’t try these).
1. Start using LINUX instead of Windows environment. I will suggest Ubuntu
Latest release.
2. Learn Java (OO Programming language) and JavaScript (OO Scripting
Language) Both these languages have C type syntax.
- Java creates applications that run in a virtual machine or browser.
- JavaScript runs in a browser only and makes your web pages more
interactive.
3. Start learning and using “Sublime Text” for all your text manipulation needs.
4. Start using “Visual Studio Code” for all your coding.
5. Learn language “Go”. See online tutorials for the same.
6. If you have learned these and use them regularly, 50% of your work for
getting a job in IT industry is done.
Programming Concepts of C Language
Conditional statements:
if ….... else …………
switch()
Break;
switch (expression)
{
case constant1:
// statements
break;
case constant2:
// statements
break;
.
.
.
default:
// default statements
}
Programming Concepts of C Language
Looping:
for(i=0; i<10; i++)
{
}
while( )
{
}
do {
} while( );
Programming Concepts of C Language ……….
Functional types of variables:
auto, static, register, extern
Functions:
-Role and concepts
-Procedures and Functions
-Parameter passing mechanisms (by value, by address/reference)
-use of Pointers
scanf and printf :
Use of & in scanf and not in printf
Global and local variables, Formal parameter
Thus, despite this apparent equivalence between array name and pointer variables, there is still a
distinction to be made between them. Even though the name of an array is, in most expression contexts,
converted into a pointer (to its first element), this pointer does not itself occupy any storage; the array
name is not an l-value, and its address is a constant, unlike a pointer variable. Consequently, what an
array "points to" cannot be changed, and it is impossible to assign a new address to an array name.
Head recursion
Tail recursion
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
printf("%d\n",fact(5));
getch();
}
int fact(int n)
{
int f;
if (n==1)
f=1;
else
{
f=n*fact(n-1);
}
return f;
}
DISCUSSIONS LECTURE-8
Functions & Procedures:
1. Whenever an array is declared, in addition to the storage space for actual elements, an
additional constant with array name of type pointer is created with address of first
element stored in it.
2. Arrays are always passed to functions as address. This is called call by reference.
3. For variables declared as global or static, storage space is provided on data area; while
for auto variables storage space is provided on the stack.
4. No global variables to be used. Globals will be used only for macros and definitions.
5. The compiler implements functions by just creating variables as defined in formal
parameter list and passing values to them from calling program. Whether it will be call
by value or call by reference will be decided how variables have been defined in formal
parameter list.
6. In C, it is possible to write functions behaving as both classical functions and classical
procedures.
7. We will try to write functions in C which are totally independent. This will make our
programming fully modular. All values used in the function will either be in formal
parameter list or defined locally in the function.
DISCUSSIONS LECTURE-8
Recursion:
O(log2N) 4 7 10 14
{ ¥, / /
The objective of the problem is as follows:
N disks
A
1. There are three pegs named A.B and C as shown above.
¥5 c-
2. N disks are present on peg A in decreasing order from bottom to top.
3. These N disks are to be moved from peg A (source) to peg C (destination) (using peg B
called intermediate) following two rules in minimum number of steps:
a. Move only one disk at a time.
b. At no time, on no peg, a larger disk should come over a smaller disk.
As can be seen, a solution to this problem is impossible; unless we think recursively.
RECURSIVE SOLUTION TO PROBLEM OF TOWER OF HANOI:
To move N disks from peg A to peg C, do the following:
1. Move top N-1 disks from peg A to peg B (using peg C as intermediate).
2. Now move one disk from peg A to peg C. (of course no intermediate peg is required).
3. Now move N-1 disks from peg B to peg C (using peg A as intermediate).
How to move N-1 disks as mentioned above:
Think recursively. Do the same with N-2 disks.
The disk will actually be moved when N=1. (Terminating Condition)
RECURSIVE FUNCTION FOR TOWER OF HANOI PROBLEM:
Let’s say, this will be called for four disks in main() as:
move(4,1,2,3);
Let’s see how it will work:
else part else part else part term. Cond.
------------- ------------- -------------- ----------------
move(4,1,2,3): see move(3,1,3,2); → move(2,1,2,3); → move(1,1,3,2); 1 to 2
move(1,1,2,3); move(1,1,3,2); move(1,1,2,3); →→ 1 to 3
I
tell
move(3,2,1,3); move(2,3,1,2); move(1,2,1,3); 2 to 3
1 to 2
move(1,3,2,1); 3 to 1
move(1,3,1,2); 3 to 2
move(1,1,3,2); 1 to 2
1 to 3
move(2,2,3,1); move(1,2,1,3); 2 to 3
move(1,2,1,3); move(1,2,3,1); 2 to 1
←÷
-
move(2,1,2,3); move(1,3,2,1) 3 to 1
2 to 3
move(1,1,3,2); 1 to 2
move(1,1,2,3); 1 to 3
move(1,2,1,3); → 2 to 3
Time complexity of Tower of Hanoi Problem:
# i¥ ¥
1. On a single lane road, N cars from left side and N cars from right side approach
each other; touching bumper to bumper. They stop with exactly a gap of one car
in between as shown above. (It can be different number of cars on both the
sides).
2. None of these cars have reverse gears. The can’t go reverse.
3. All these cars have a push button called HOP. When pressed, the car can hop one
car at a time in forward direction.
4. The rule of mathematical induction says: if somethin proved for N=1 and N=2, it
should be true for all values of N.
5. In this case it can be seen that cars can pass for N=1 as well as N=2.
6. Try solving the problem for N=4. Actually any number of cars can pass through in
this problem.
7. GOOD LUCK.
Reserved words in C language: