introduction to c programming - wordpress.com€¦ · introduction to c programming by avani m....

24
Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

Upload: others

Post on 15-Jul-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to C programming - WordPress.com€¦ · Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE . Classification of Software Computer Software

Introduction to C programming

By Avani M. Sakhapara

Asst Professor, IT Dept,

KJSCE

Page 2: Introduction to C programming - WordPress.com€¦ · Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE . Classification of Software Computer Software

Classification of Software

Computer Software

System Software Application Software

Page 3: Introduction to C programming - WordPress.com€¦ · Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE . Classification of Software Computer Software

Growth of Programming Languages

Page 4: Introduction to C programming - WordPress.com€¦ · Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE . Classification of Software Computer Software

History • ALGOL 60 – too abstract and generic • CPL (Combined Programming Language) at

Cambridge University – too big, many features, difficult to learn and implement

• BCPL (Basic Combined Programming Language) at Cambridge university by Martin Richards – too specific, less powerful

• B language at AT & T’s Bell Lab by Kem Thompson – too specific

• C language = B language + BPCL + new features • C language at AT & T’s Bell Lab in USA by Dennis

M. Ritchie in 1972

Page 5: Introduction to C programming - WordPress.com€¦ · Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE . Classification of Software Computer Software

Features of C

• Robust language – can be used to write any complex program

• Has rich set of built-in functions and operators

• well suited for writing both, system s/w and application s/w

• Dynamic Allocation of Memory

Page 6: Introduction to C programming - WordPress.com€¦ · Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE . Classification of Software Computer Software

Structure of a C Program

#include<stdio.h>

/*multiline

Comment*/

void main()

{

//single line comment

printf(“Hello all”);

}

• #include->preprocessor directive

• stdio.h->standard input output header file which has “printf function”

• program execution always start from main() function

• printf() function is used to display some text on the output screen

Page 7: Introduction to C programming - WordPress.com€¦ · Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE . Classification of Software Computer Software

Structure of a C Program

#include<stdio.h>

/*multiline

Comment*/

void main()

{

//single line comment

printf(“Hello all”);

}

Page 8: Introduction to C programming - WordPress.com€¦ · Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE . Classification of Software Computer Software

Escape Characters

• These are non-printing characters and are represented by escape sequences consisting of a backslash (\) followed by a letter

Page 9: Introduction to C programming - WordPress.com€¦ · Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE . Classification of Software Computer Software

List of Escape Characters

Page 10: Introduction to C programming - WordPress.com€¦ · Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE . Classification of Software Computer Software

Exercise

• Write a C program to display the following

1. Display the below message

Favourite colour:

‘yellow’

2. Display the table

Roll no Class Div

123 SE A

124 FE B

Page 11: Introduction to C programming - WordPress.com€¦ · Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE . Classification of Software Computer Software

Variables

• A variable is an entity used by the program to store values used in the computation

• Variable names are the names given to the memory location where the values are stored.

• The type of the variable depends on the type of the value it stores.

Page 12: Introduction to C programming - WordPress.com€¦ · Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE . Classification of Software Computer Software

Rules for forming variable names

• It should begin with a letter or underscore( _ )

• Followed by any combination of letters, underscores and digits 0-9

• Eg: sum, item_price1, _sys – valid names

price$, #num – invalid names

• The uppercase and lowercase letters are distinct in C. The variable names “sum” and “Sum” refers to different variables

• No commas or blank spaces allowed

• Length of variables depends on the compiler

Page 13: Introduction to C programming - WordPress.com€¦ · Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE . Classification of Software Computer Software

Data types

Page 14: Introduction to C programming - WordPress.com€¦ · Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE . Classification of Software Computer Software

Primary Data Types

Data type keyword Size(in bytes)

Range Purpose Format Specifier

Character char 1 -27 to 27 -1 = -128 to +127

Store single character

%c

Integer int 2 -215 to 215 -1 = -32768 to +32767

Store numbers

%d

Float float 4 -3.4e38 to +3.4e38 Store fractions

%f

Double double 8 -1.7e308 to +1.7e308

Store smaller fractions

%lf

Page 15: Introduction to C programming - WordPress.com€¦ · Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE . Classification of Software Computer Software

Qualifiers

• Qualifiers can be used with basic datatypes

• short and long

– vary the size

- short->used with int

- long-> used with int and double

• signed and unsigned – vary the range

- used with char and int

Page 16: Introduction to C programming - WordPress.com€¦ · Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE . Classification of Software Computer Software

Data types with Qualifiers Data type with qualifier

Size(in bytes)

Range Purpose Format Specifier

signed short int /short int/int

2 -215 to 215 - 1 = -32768 to +32767

Store numbers %d

signed long int/long int/long

4 -231 to 231 - 1

Store large numbers

%ld

unsigned short int /unsigned short/unsigned int

2 0 to 216 - 1 =0 to 65535

store positive numbers and zero

%u

unsigned long int/unsigned long

4 0 to 232 - 1

store large positive numbers and zero

%lu

unsigned char 1 0 to 28 – 1 =0 to 255

store positive bytes

%c

long double 10 store small fractional values

%Lf

Page 17: Introduction to C programming - WordPress.com€¦ · Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE . Classification of Software Computer Software

Variable Declaration and Initialization

• All the variables must be declared at the beginning of the function before using it.

• Syntax: datatype var1, var2;

• Eg: int number, sum;

float interest;

• Variable Initialization: var1=value1;

var2=value2;

• Eg: number = 23;

interest = 2.3f;

Page 18: Introduction to C programming - WordPress.com€¦ · Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE . Classification of Software Computer Software

Variable Declaration and Initialization

• Variables can be initialized at the time of variable declaration using the following

• Syntax: datatype var1= value1;

• Eg: int number = 23;

Page 19: Introduction to C programming - WordPress.com€¦ · Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE . Classification of Software Computer Software

Specifications of Different Constants

Page 20: Introduction to C programming - WordPress.com€¦ · Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE . Classification of Software Computer Software

Example: Program to add two numbers

#include<stdio.h>

void main()

{

int a, b=6, c;

a=3;

c = a + b;

printf(“sum = 9”);

printf(“sum = %d”, c);

}

#include<stdio.h>

void main()

{

int a, b=6, c;

a = 3;

printf(“sum = %d”, a + b);

}

Page 21: Introduction to C programming - WordPress.com€¦ · Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE . Classification of Software Computer Software

Using scanf() for input

• scanf() function is defined in <stdio.h> header file

• Syntax: scanf(“format specifier”,&varname);

• &varname – address of(&) varname

• Eg: scanf (“%d”,&a);

• Getting multiple values using scanf()

- scanf (“%d %f”, &a, &b);

Page 22: Introduction to C programming - WordPress.com€¦ · Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE . Classification of Software Computer Software

Excercise

• Write a C program to accept two numbers from the user and swap them using

i) third variable

ii) Without using third variable

• Write a C program to accept the radius of the circle from the user and calculate the circumference and the area of the circle and display it.

Page 23: Introduction to C programming - WordPress.com€¦ · Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE . Classification of Software Computer Software

Defining Constants

• Two ways for defining constants

i) Using #define directive

ii) Using const keyword

1) Using #define directive

• Syntax: # define identifier token-string

• The directive substitutes token-string for all subsequent occurrences of an identifier

• Called symbolic constants

• Usually written in uppercase to differentiate it from variables

Page 24: Introduction to C programming - WordPress.com€¦ · Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE . Classification of Software Computer Software

Examples

# include<stdio.h> #define SIZE 5 void main() { int a=3; //SIZE replaced by 5 a = SIZE * 2; printf(“result= %d“, a); SIZE= 10; //error }

# include<stdio.h> void main() { const int size = 5; int a=3; //size replaced by value 5 a = size * 2; printf(“result= %d“, a); size= 10; //error }