34
C programming Language and Data Structure For DIT Students

C programming Language and Data Structure For DIT Students

Embed Size (px)

Citation preview

Page 1: C programming Language and Data Structure For DIT Students

C programming Language and

Data Structure

For DIT Students

Page 2: C programming Language and Data Structure For DIT Students

Course Detail

Theory 100

Piratical 100

Total 200

Page 3: C programming Language and Data Structure For DIT Students

Introduction to C Programming

Introduction

Page 4: C programming Language and Data Structure For DIT Students

Books

“Turbo C Programming for PC”, Robert Lafore, SAMS

“Data Structure” ,Schaum’s Series

Page 5: C programming Language and Data Structure For DIT Students

Language?

Source of Communication between two persons.– Example: Human –To- Human

OR Source of Communication between User

and Computer is called programming language.– Example: Human –To- Machine

Page 6: C programming Language and Data Structure For DIT Students

Program

Set of instructions which perform any specific task is called program.

Page 7: C programming Language and Data Structure For DIT Students

What is programming? Series of instructions to a computer to achieve a task

Instructions must be written in a way the computer can understand

Programming languages are used to write programs

Once the code (language) of a program has been written, it must be executed (run, started).

Some programming languages (like C, C++ or Java) require the code to be compiled (translated to binary) before it can be started.

Page 8: C programming Language and Data Structure For DIT Students

History of C language

Year Language Developer1960

ALGOL(Algorithmic Language) Internal Committee

1967BCPL(Basic Combined Programming

Language)Martin Richards

1970 B Ken Thompson

1972 C Dennis Ritchie

Page 9: C programming Language and Data Structure For DIT Students

What is C?

C A language written by Dennis

Ritchie in 1972 at AT&T Bell Labs USA. This was to be the language that UNIX was written in to become the first "portable" language.

In recent years C has been used as a general-purpose language because of its popularity withprogrammers.

AT&T-American Telephone and Telegraph UNIX-Uniplexed Information and Computing System or Service

Page 10: C programming Language and Data Structure For DIT Students

Why C Still Useful?

C provides: Efficiency, high performance and high quality Provide functionality through rich set of function libraries Gateway for other professional languages like C C++ Java

C is used: System software, Compilers, Editors Graphics and Geometry Databases, operating systems, device drivers Also used in application programs

Page 11: C programming Language and Data Structure For DIT Students

Software Development Method

Requirement Specification – Problem Definition

Analysis – Refine, Generalize the problem definition

Design – Develop Algorithm: (A formula or set of steps for solving a

particular problem)

Implementation – Write Code

Verification and Testing – Test and Debug the code

Page 12: C programming Language and Data Structure For DIT Students

How do you write a program?

Decide what steps are needed to complete the task

Write the steps (Algorithm) in pseudo code (written in English) or as a flowchart (graphic symbols)

Translate into the programming language

Try out the program and “debug”.

Page 13: C programming Language and Data Structure For DIT Students

Sample Pseudo code

Task: add two numbers Pseudo code (Algorithm) :

1. Start

2. Get two numbers

3. Add them (a + b)

4. Print the answer

5. End

Page 14: C programming Language and Data Structure For DIT Students

What does a flowchart look like?

Pseudo code (Algorithm) :

1. Start

2. Get two numbers

3. Add them (A + B)

4. Print the answer

5. End

Start

Get 2 numbers

A+B

Print answer

End

Page 15: C programming Language and Data Structure For DIT Students

START/END

INPUT/OUTPUT

PROCESS

DECISION

Flow Chart symbols

Page 16: C programming Language and Data Structure For DIT Students

Integrated Development Environments

An integrated development environment (IDE) is a software package that makes it possible to edit, compile, link, execute, and debug a program without leaving the environment.

16

Page 17: C programming Language and Data Structure For DIT Students

Simple C Program

/* A first C Program*/

#include <stdio.h>

void main()

{     printf("Hello World");

}

Page 18: C programming Language and Data Structure For DIT Students

Simple C Program

/* A first C Program*/

#include <stdio.h>

void main()

{     printf("Hello World");

}

Header File

Main Function

Opening bracket

Statement Part

Closing Bracket

Preprocessor directive

Page 19: C programming Language and Data Structure For DIT Students

Simple C Program

Line 1: #include <stdio.h>

As part of compilation, the C compiler runs a program called the C preprocessor. The preprocessor is able to add and remove code from your source file.

In this case, the directive #include tells the preprocessor to include code from the file stdio.h.

This file contains declarations for functions that the program needs to use. A declaration for the printf function is in this file.

Page 20: C programming Language and Data Structure For DIT Students

Simple C Program

Line 2: void main()

This statement declares the main function. A C program can contain many functions but must

always have one main function. A function is a self-contained module of code that can

finish some task. The "void" specifies the return type of main. In this case,

nothing is returned to the operating system.

Page 21: C programming Language and Data Structure For DIT Students

Simple C Program

Line 3: {

This opening bracket denotes the start of the program.

Page 22: C programming Language and Data Structure For DIT Students

Simple C Program

Line 4: printf("Hello World ");

Printf is a function from a standard C library that is used to print strings to the standard output, normally your screen.

The compiler links code from these standard libraries to the code you have written to produce the final executable.

If there were another printf in this program, its string would print on the next line.

Page 23: C programming Language and Data Structure For DIT Students

Simple C Program

Line 5: } This closing bracket denotes the end of the program.

Page 24: C programming Language and Data Structure For DIT Students

Comment Comment should be enclosed between /* */ It is used to increase the readability of the program. Any number of comments can be given at any place

in the program. Comment cannot be nested

example:

/* A first C Program*/

Page 25: C programming Language and Data Structure For DIT Students

Steps in learning English language

Steps in learning C

AlphabetsDigitsSpecial-symbols

ConstantsVariablesKeywords

Instruction Program

Getting started with C

Alphabets Sentences ParagraphWords

Page 26: C programming Language and Data Structure For DIT Students

The C character Set

• A character denotes any alphabet, digit or special symbol used to represent information.

Alphabets A,B, …. ,Y, Z a,b, ….. ,y, z

Digits 0,1,2,3,4,5,6,7,8,9

Special Symbols ~ ‘ ! @ # % ^ & * ( ) _ - + = | \ { } [ ] : ; “ ‘ < > , . ? /

Page 27: C programming Language and Data Structure For DIT Students

Constants, Variable and keywords The alphabets, numbers and special symbol

when properly combined form constants, variables and keywords

A constant is a quantity that doesn’t change

A variable is a name given to the location in memory where the constant is stored

Example: 3x + y = 20 3 & 20 are constants, which cannot changex & y can vary or change hence are called variables

Page 28: C programming Language and Data Structure For DIT Students

Keywords

• Keywords are the words whose meaning has already been explained to the C compiler

• Sometimes called reserved words.• They cannot be used as variable names.• There are only 32 keywords available in c

auto double if static dobreak else int struct gotocase enum long switch signedchar extern near typedef whileconst float register union defaultcontinue far return unsigned forshort void

Page 29: C programming Language and Data Structure For DIT Students

Escape Sequence

Certain non printing characters can be expressed in terms of escape sequences

\n new line \t tab \a alert \\ backslash \” double quote \0 Null \b Backspace

Page 30: C programming Language and Data Structure For DIT Students

Data Types

• C Supports several different types of data, each of which may be represented differently within the computers memory.

• Basic data types are listed below: Data Type Description Typical Memory

int integer quantity 2 bytes char single character 1 bytes float floating point number 4 bytes

Page 31: C programming Language and Data Structure For DIT Students

Escape Sequences in C Certain non printing characters can be expressed in terms of

escape sequences

Character Escape Sequence

bell \a backspace \b horizontal tab \t vertical tab \v newline \n carriage return \r quotation mark (“) \” question mark(?) \? backslash (\) \\ null \0

Page 32: C programming Language and Data Structure For DIT Students

Development with C

Four stages

Editing: Writing the source code by using some IDE or editor

Preprocessing or libraries: Already available routines

compiling: translates or converts source to object code for a

specific platform source code -> object code– linking: The object code of a program is linked with libraries

that are needed for execution of a program. The linker is used to link the program with libraries. It creates a file with '*.exe' extension.

Page 33: C programming Language and Data Structure For DIT Students

Program Development

Source File

Program Object Code File

Executable File

Preprocessor

Modified Source Code in RAM

Compiler

Linker

Editor

Page 34: C programming Language and Data Structure For DIT Students

Keywords

Sometimes called reserved words.

Are defined as a part of the C language.

Can not be used for anything else!

32 keywords in C

Examples:

– Int

– void

– while

– for