c programming language - iiitkalyani.files.wordpress.com · int struct break else long switch case...

38
C Programming Language Madhumita Sengupta IIIT Kalyani [email protected]

Upload: dinhliem

Post on 04-Apr-2018

229 views

Category:

Documents


2 download

TRANSCRIPT

C Programming Language

Madhumita Sengupta

IIIT – Kalyani

[email protected]

TOPICS

C Introduction

C Flow Control

C Functions

C Programming Arrays

C Programming Pointers

C Programming Strings

Structure And Union

C Programming Files

Additional Topics

2

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

FACTS ABOUT C

C was developed by Dennis M. Ritchie at Bell Labs.

C was invented to write an operating system called UNIX.

C is a successor of B language which was introduced around 1970

The UNIX OS was totally written in C by 1973.

The language was formalized in 1988 by the American National Standard Institute (ANSI).

Today C is the most widely used and popular System Programming Language.

Most of the state-of-the-art software's have been implemented using C.

Today's most popular Linux OS and RBDMS MySQL have been written in C. 3

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

WHERE C USED AND WHY

C was adopted as a system development language because it produces code that runs nearly as fast as code written in assembly language.

Operating Systems

Language Compilers

Assemblers

Text Editors

Print Spoolers

Network Drivers

Modern Programs

Databases

Language Interpreters

Utilities 4

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

FIRST PROGRAM

#include <stdio.h>

main ()

{

printf(“Hello World”);

}

5

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

C INTRODUCTION

Keywords & Identifier

Variables & Constants

C Data Types

C Input/Output

C Operators

C Introduction Examples

6

[email protected]

KEYWORDS

Keywords are the reserved words used in programming.

Each keywords has fixed meaning and that cannot be

changed by user.

For example:

7

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

auto

double

int

struct

break

else

long

switch

case

enum

register

typedef

char

extern

return

union

continue

for

signed

void

do

if

static

while

default

goto

sizeof

volatile

const float

short

unsigned

IDENTIFIER

In C programming, identifiers are names given to C entities, such as variables, functions, structures etc. Identifier are created to give unique name to C entities to identify it during the execution of program. For

example:

int age;

void function() ;

Rules for writing identifier

1. An identifier can be composed of letters (both uppercase and lowercase letters), digits and underscore '_' only.

2. The first letter of identifier should be either a letter or an underscore. But, it is discouraged to start an identifier name with an underscore though it is legal. It is because, identifier that starts with underscore can conflict with system names. In such cases, compiler will complain about it. Some system names that start with underscore are _fileno, _iob, _wfopen etc.

3. There is no rule for the length of an identifier. However, the first 31 characters of an identifier are discriminated by the compiler. So, the first 31 letters of two identifiers in a program should be different.

8

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

VARIABLES

Variables are memory location in computer's

memory to store data.

Rules for writing variable name in C

is same as writing identifier

Example:

int age;

int Student_name;

float salary;

char choice;

double number1, number2;

9

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

CONSTANTS

Constants are the terms that can't be changed during the

execution of a program.

Integer constants

Decimal digits: 0 1 2 3 4 5 6 7 8 9

Octal digits: 0 1 2 3 4 5 6 7

Hexadecimal digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F.

Notes:

1. You can use small caps a, b, c, d, e, f instead of uppercase

letters while writing a hexadecimal constant.

2. Every octal constant starts with 0 and hexadecimal

constant starts with 0x in C programming.

10

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

CONT...

Floating-point constants

-2.0

0.0000234

-0.22E-5

Character constants

'a',

l',

'm',

'F' etc.

Enumeration constants

enum color {yellow, green, black, white}; 11

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

DATA TYPE

Fundamental Data Types

Integer types

Floating Type

Character types

Derived Data Types

Arrays

Pointers

Structures

Enumeration

12

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

DATA TYPE

13

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

CONT...

14

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

SAMPLE CODE

#include <stdio.h>

main ()

{

int sum=33;

float money=44.12;

char letter;

double pressure;

letter='E';

pressure=2.01e-10;

printf("value of sum is %d\n", sum);

printf("value of money is %f\n", money);

printf("value of letter is %c\n", letter);

printf("value of pressure is %e\n", pressure);

} 15

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

OUTPUT OF SAMPLE CODE

value of sum is 33

value of money is 44.119999

value of letter is E

value of pressure is 2.010000e-10

16

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

#DEFINE PREPROCESSOR

Names given to values that cannot be changed.

Implemented with the

#define preprocessor directive.

Example

#define N 3000

#define FALSE 0

#define PI 3.14159

#define FIGURE "triangle"

17

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

THE #DEFINE PREPROCESSOR CONT...

#define identifier value

#include <stdio.h>

#define LENGTH 10

#define WIDTH 5

#define NEWLINE '\n'

int main()

{

int area;

area = LENGTH * WIDTH;

printf("value of area : %d", area);

printf("%c", NEWLINE);

return 0;

} 18

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

REDEFINING PREPROCESSOR DIRECTIVE

ANYWHERE IN THE PROGRAM IS LEGAL

#include<stdio.h>

#define num 5

main()

{

#define num 100

printf(“%d”, num);

}

Output : 10 (with warning)

19

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

THE CONST KEYWORD

const type variable = value;

#include <stdio.h>

int main()

{

const int LENGTH = 10;

const int WIDTH = 5;

int area; area = LENGTH * WIDTH; printf("value of

area : %d", area);

return 0;

} 20

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

ENUMERATION

By default, value1 will be equal to 0, value2 will be 1

and so on but, the programmer can change the

default value.

21

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

EXAMPLE OF EXTERN

First File: main.c

#include <stdio.h>

int count ;

extern void write_extern();

main()

{

count = 5;

write_extern();

}

22

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

EXAMPLE OF EXTERN CONT..

Second File: support.c

#include <stdio.h>

extern int count;

void write_extern(void)

{

printf("count is %d\n", count);

}

Here, extern keyword is being used to declare count in the second file where as it has its definition in the first file, main.c. Now, compile these two files as follows:

$gcc main.c support.c

This will produce a.out executable program, when this program is executed, it produces the following result:

5

23

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

INPUT / OUTPUT

ESCAPE SEQUENCE

1. \n Newline

2. \r Carriage return

3. \t Horizontal tab

4. \v Vertical tab

5. \\ \ character

6. \’ ' character

7. \“ " character

8. \? ? character

24

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

JUST A LOOK

#include <stdio.h>

int main()

{

/* my first program in C */

printf("Hello, World! \n");

return 0;

}

A C program basically consists of the following parts:

Preprocessor Commands

Functions

Variables

Statements & Expressions

Comments

25

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

HOW TO PRINT NUMBER

#include<stdio.h>

int main()

{

int c=5;

printf("Number=%d", c);

return 0;

}

26

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

HOW TO TAKE INPUT FROM USER

#include<stdio.h>

int main()

{

int c;

printf("Enter a number\n");

scanf("%d",&c);

printf("Number=%d",c);

return 0;

}

27

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

TEST YOUR KNOWLEDGE ? LAB EXEC 2

1. Write a program to print “Hello Friends”.

2. Write a program to accept two numbers from user

and display the result of [+, -, /, %, *].

3. Write a program to accept a two digit number and

find out reverse of that number.

4. WAP to find out the total income of husband and

wife by taking input in double.

5. WAP to take two number and swap then with the

help of third variable.

6. WAP to take two number and swap then without

the help of third variable.

7. WAP to accept a number and check for even and

odd.

28

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

HOW TO ENTER CHARACTER

#include <stdio.h>

int main()

{

char var1;

printf("Enter character: ");

scanf("%c",&var1);

printf("You entered %c.",var1);

return 0;

}

29

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

ASCII CODE

#include <stdio.h>

int main()

{

char var1;

printf("Enter character: ");

scanf("%c",&var1);

printf("You entered %c.\n",var1);

printf("ASCII value of %d",var1);

return 0;

}

30

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

VARIATIONS IN OUTPUT FOR INTEGER AN FLOATS

#include<stdio.h>

int main()

{

printf("Case 1:%6d\n",9876);

/* Prints the number right justified within 6 columns */

printf("Case 2:%3d\n",9876);

/* Prints the number to be right justified to 3 columns but, there are 4 digits so number is not right justified */

printf("Case 3:%.2f\n",987.6543);

/* Prints the number rounded to two decimal places */

printf("Case 4:%.f\n",987.6543);

/* Prints the number rounded to 0 decimal place, i.e, rounded to integer */

printf("Case 5:%e\n",987.6543);

/* Prints the number in exponential notation(scientific notation) */

return 0;

}

31

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

OUTPUT

Case 1: 9876

Case 2: 9876

Case 3: 987.65

Case 4: 988

Case 5: 9.876543e+002

32

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

OPERATORS

Arithmetic Operators

Increment and Decrement Operators

Assignment Operators

Relational Operators

Logical Operators

Conditional Operators

Bitwise Operators

Special Operators

33

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

ARITHMETIC OPERATORS

Operator Meaning of Operator

+ addition or unary plus

- subtraction or unary minus

* multiplication

/ division

34

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

/* Program to demonstrate the working of arithmetic

operators in C. */

#include <stdio.h>

int main()

{

int a=9, b=4, c;

c = a + b;

printf("a+b=%d\n",c);

c = a - b;

printf("a-b=%d\n",c);

c = a * b;

printf("a*b=%d\n",c);

c = a / b;

printf("a/b=%d\n",c);

c = a % b;

printf("Remainder when a divided by b=%d\n",c);

return 0;

}

35

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

INCREMENT AND DECREMENT OPERATORS

++ , --

(Prefix, Postfix)

{--a, ++a} {a--, a++}

Let a=5

a++; //a becomes 6

a--; //a becomes 5

++a; //a becomes 6

--a; //a becomes 5

36

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

CONT....

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

37

Prefix

int x =4, y;

y=++x;

printf(“x=%d,y=%d”,x,y);

Postfix

int x =4, y;

y=x++;

printf(“x=%d,y=%d”,x,y);

END

ma

dh

um

ita.sn

gp

t@gm

ail.co

m

38