0% found this document useful (0 votes)
18 views25 pages

DSA4-9

This document introduces software concepts and the basics of the C programming language, covering topics such as data types, memory allocation, and programming structures. It includes programming exercises and sample code to illustrate key features of C, as well as guidance for those looking to pursue a career in IT. The document also discusses recursion, conditional statements, and various programming constructs in C.

Uploaded by

kashyapishu1611
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views25 pages

DSA4-9

This document introduces software concepts and the basics of the C programming language, covering topics such as data types, memory allocation, and programming structures. It includes programming exercises and sample code to illustrate key features of C, as well as guidance for those looking to pursue a career in IT. The document also discusses recursion, conditional statements, and various programming constructs in C.

Uploaded by

kashyapishu1611
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

LECTURE 4-9 INTRODUCTION

THIS SERIES WILL COVER SOFTWARE CONCEPTS AND BASIC C


PROGRAMMING LANGUAGE.

FOLLOWING SLIDES ARE ONLY AN INDICATION OF THE TOPICS TO BE


COVERED IN THESE LECTURES.

A NUMBER OF PROGRAMMING EXERCISES WILL BE DONE ONLINE TO


ILLUSTRATE DIFFERENT FEATURES OF C LANGUAGE.

STEP BY STEP METHOD TO DEVELOP PROGRAMS WILL BE INTRODUCED


TO MAKE PROGRAMMING EASIER.
SOFTWARE
WHAT IS SOFTWARE
-A COMPUTER EXECUTABLE PROGRAM TO DO SOME JOB

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

Pointers int *a,


Address of a variable: use of &
arrays:
int a[10];
Memory allotment:
Access function formula:
For one dimension array:
&a[i]=a+i*sizeof(type)
Programming Concepts of C Language
For two dimension array:
int a[10][20]; where m=10, n=20.
&a[i][j]= a+ (…………….. )*sizeof(type)
Methods of storing 2-dimension arrays:
Row major format
Column major format
C uses row major format
FORTRAN uses column major format

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;

And items of struct can be accessed as


x.rollno
x.name
x.marks
Discussions Lecture 4-5:
SAMPLE PROGRAM TO CHECK (ON LINUX UBUNTU 64 BIT):
#include<stdio.h>
int main()
{
int a[10],b;
b=15;
printf("value of a:%d, value of &a[0]:%d\n",a,&a[0]);
printf("value of b:%d, value of a[-1]:%d\n",b,a[-1]);
}

value of a:156338880, value of &a[0]:156338880


value of b:15, value of a[-1]:15.
Discussions Lecture 4-5:
SAMPLE PROGRAM TO CHECK (ON LINUX UBUNTU 64 BIT):
#include<stdio.h>
int main()
{
int a,b; char c;
printf("give first integer:");
scanf("%d",&a);
printf("give character:");
scanf("%c",&c);
printf("give second integer:");
scanf("%d",&b);
}

give first integer:45


give character:give second integer:34
Discussions Lecture 4-5:
SAMPLE PROGRAM TO CHECK (ON LINUX UBUNTU 64 BIT):
#include<stdio.h>
int main()
{
int a,b; char c;
printf("give first integer:");
scanf("%d",&a);
printf("give character:");
//fflush(stdin);
scanf ("%*[^\n]");
scanf ("%*c");
scanf("%c",&c);
printf("the char is %d %c\n",c,c);
printf("give second integer:");
scanf("%d",&b);
}
give first integer:34
give character:q
the char is 113 q
give second integer:45
Discussions Lecture 4-5:
scanf ("%*[^\n]");
scanf ("%*c");

● %*[^\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();

How program is loaded in memory:


- Code area, data area, stack area and heap (area)
- Concept of segment and its size 64KiB(2^16) 64 Kilobytes

- Code area: code in machine language goes here


- Data area: all variables declared in the program are stored here
- Stack area: used to implement function calls and returns
- Heap : Whatever memory remains is heap.
Programming Concepts of C Language
Static and Dynamic Memory Allocation:
Static: Memory allocation at compile time and it goes to data area.
#include<stdio.h>
int main()
{
int a[1025][1025];
printf("memory allocated successfully\n");
}
memory allocated successfully
#include<stdio.h>
int main()
{
int a[2000][2000];
printf("memory allocated successfully\n");
}
Segmentation fault (core dumped)
Programming Concepts of C Language
Dynamic: Memory allocation at run time and it goes to heap area.

malloc(no_of_bytes); returns a pointer of type void

free(pointer); frees the memory allocated by dynamic memory allocation and


attached to this pointer.

To make things compiler independent, normally we do not provide the number of


bytes directly in malloc().

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\n",p[0],a[0]);
}
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);
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.
1. Start learning and using “Sublime Text” for all your text manipulation needs.
2. Start using “Visual Studio Code” for all your coding.
3. Learn language “Go”. See online tutorials for the same.
4. 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

Book: C: A Software Engineering Approach by Darnell and Margolis - Springer

The C Programming Language by Kernighan and Ritchie - vPearson Rs. 274/-


Programming Concepts of C Language ……….
Recursion:
Feature of structured programming languages
They allow the a function to call itself.
Based on Rule of Mathematical Induction.
Properties for recursion to work:
1. Function should be definable in terms of itself.
2. While defining in terms of itself, the dimension of the problem should change.
3. There must be a terminating condition.
4. Change of dimension of step 2 should approach step 3.

Examples:
Factorial
Summing the series
Tower of Hanoi
Jumping Car problem
Programming Concepts of C Language ……….
Recursion:
General format:
If (term_cond)
{
terminating_assignment;
}
else
{
recursive_definition:
}

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;
}
Reserved words in C language:

You might also like