c-introduction-c.sc dept, nit, raichur

Post on 04-Jun-2018

215 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 1/48

1

Objectives

Structure of a C-language program.

First C program.

Preprocessors.

Identifiers for objects in a program.

C basic Data Types.

Usage of Variables and Constants.

Input and Output concepts.

I ntroduction to the C Language

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 2/48

C History

Developed between 1969 and 1973 alongwith Unix

Due mostly to Dennis Ritchie

Designed for systems programming

Operating systems

Utility programs Compilers

Filters

Evolved from B, which evolved from BCPL

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 3/48

3

Background

C is a structured programming language. I t is

considered a high-level language because it allows the

programmer to concentrate on the problem at hand

and not worry about the machine that the program

will be using. While many languages claim to be

machine independent, C is one of the closest to

achieving that goal. That is another reason why it is

used by software developers whose applications have

to run on many different hardware platforms.

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 4/48

C History

Original machine (DECPDP-11) was very small

24K bytes of memory, 12Kused for operating system

Written when computerswere big, capital

equipment Group would get one,

develop new language, OS

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 5/48

5

Taxonomy of the C Language

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 6/48

6

C Programs

I t' s time to wr ite your f irst C program! This section

will take you through all the basic parts of a C

program so that you wil l be able to write it.

Structure of a C ProgramYour First C Program

Comments

The Greeting Program

Topics discussed here :

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 7/48

7

Structure of a C Program

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 8/48

8

The Greeting Program

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 9/48

9

Examples of Block Comments

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 10/48

10

Examples of Line Comments

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 11/48

11

Nested Block Comments Are Invalid

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 12/48

Computer Science: A Structured Programming Approach Using C 12

PROGRAM 2-1 The Greeting Program

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 13/48

13

2-3 Identifiers

One feature present in al l computer languages is the

identi f ier. I denti f iers allow us to name data and other

objects in the program. Each identi f ied object in the

computer is stored at a unique address. I f we didn’t  have identifiers that we could use to symbolically

represent data locations, we would have to know and

use object’s addresses. I nstead, we simply give data

identi f iers and let the compiler keep track of where

they are physical ly located.

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 14/48

14

Table 2-1 Rules for Identifiers

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 15/48

15

An identifier must start with a letter or underscore:

it may not have a space or a hyphen.

Note

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 16/48

16

C is a case-sensitive language.

Note

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 17/48

C Type Examplesint i;

int *j, k;

unsigned char *ch;

float f[10];

char nextChar(int, char*);

int a[3][5][10];

int *func1(float);

int (*func2)(void);

Integer

 j: pointer to integer, int k

ch: pointer to unsigned char

Array of 10 floats

2-Arguments function

Array of three arrays of five … 

function returning int *

pointer to function returning int

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 18/48

C Expression Classes arithmetic: + – * / % comparison: == != < <= > >= bitwise logical: & | ^ ~ shifting: << >>

lazy logical: && || ! conditional: ? : assignment: = += -=

increment/decrement: ++ -- sequencing: , pointer: * -> & []

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 19/48

19

Examples of Valid and Invalid Names

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 20/48

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 21/48

21

Data Types

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 22/48

22

Character Types

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 23/48

23

Integer Types

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 24/48

24

sizeof (short) ≤ sizeof (int) ≤ sizeof (long) ≤ sizeof (long long) 

Note

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 25/48

25

Typical Integer Sizes and Values for Signed Integers

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 26/48

26

Floating-point Types

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 27/48

27

sizeof (float) ≤ sizeof (double) ≤ sizeof (long double) 

Note

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 28/48

28

Type Summary

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 29/48

29

Variables

Variables are named memory locations that have a type,

such as integer or character, which is inher ited from

their type. The type determines the values that a var iable

may contain and the operations that may be used withits values.

Variable Declaration

Variable Initialization

Topics discussed in this section:

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 30/48

30

Variables

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 31/48

31

Examples of Variable Declarations and Definitions

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 32/48

32

Variable Initialization

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 33/48

33

When a variable is defined, it is not initialized.

We must initialize any variable requiring

prescribed data when the function starts.

Note

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 34/48

34

PROGRAM 2 Print Sum of Three Numbers

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 35/48

35

PROGRAM 2 Print Sum of Three Numbers (continued)

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 36/48

36

PROGRAM 2 Print Sum of Three Numbers (continued)

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 37/48

37

Constants

Constants are data values that cannot be changed

during the execution of a program. Like variables,

constants have a type. I n this section, we discuss

Boolean, character, integer, real, complex, and str ingconstants.

Constant Representation

Coding Constants

Topics discussed in this section:

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 38/48

38

A character constant is enclosed in single quotes.

Note

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 39/48

39

Symbolic Names for Control Characters

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 40/48

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 41/48

41

Examples of Real Constants

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 42/48

42

The two components of a complex constant must be of the

same precision, that is, if the real part is type double,then the imaginary part must also be type double.

Note

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 43/48

43

Examples of Complex Constants

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 44/48

44

Some Strings

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 45/48

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 46/48

46

Use single quotes for character constants.

Use double quotes for string constants.

Note

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 47/48

47

PROGRAM 3 Memory Constants

8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur

http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 48/48

PROGRAM 3 Memory Constants (continued)

top related