C-Introduction-C.sc Dept, NIT, Raichur
-
Upload
anil-kandagal -
Category
Documents
-
view
218 -
download
0
Transcript of C-Introduction-C.sc Dept, NIT, Raichur
![Page 1: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/1.jpg)
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
![Page 2: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/2.jpg)
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
![Page 3: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/3.jpg)
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.
![Page 4: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/4.jpg)
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
![Page 5: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/5.jpg)
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
![Page 6: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/6.jpg)
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 :
![Page 7: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/7.jpg)
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
![Page 8: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/8.jpg)
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
![Page 9: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/9.jpg)
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
![Page 10: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/10.jpg)
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
![Page 11: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/11.jpg)
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
![Page 12: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/12.jpg)
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
![Page 13: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/13.jpg)
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.
![Page 14: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/14.jpg)
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
![Page 15: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/15.jpg)
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
![Page 16: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/16.jpg)
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
![Page 17: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/17.jpg)
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
![Page 18: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/18.jpg)
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: * -> & []
![Page 19: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/19.jpg)
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
![Page 20: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/20.jpg)
8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur
http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 20/48
![Page 21: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/21.jpg)
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
![Page 22: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/22.jpg)
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
![Page 23: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/23.jpg)
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
![Page 24: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/24.jpg)
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
![Page 25: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/25.jpg)
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
![Page 26: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/26.jpg)
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
![Page 27: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/27.jpg)
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
![Page 28: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/28.jpg)
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
![Page 29: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/29.jpg)
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:
![Page 30: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/30.jpg)
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
![Page 31: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/31.jpg)
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
![Page 32: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/32.jpg)
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
![Page 33: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/33.jpg)
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
![Page 34: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/34.jpg)
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
![Page 35: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/35.jpg)
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)
![Page 36: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/36.jpg)
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)
![Page 37: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/37.jpg)
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:
![Page 38: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/38.jpg)
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
![Page 39: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/39.jpg)
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
![Page 40: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/40.jpg)
8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur
http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 40/48
![Page 41: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/41.jpg)
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
![Page 42: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/42.jpg)
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
![Page 43: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/43.jpg)
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
![Page 44: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/44.jpg)
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
![Page 45: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/45.jpg)
8/13/2019 C-Introduction-C.sc Dept, NIT, Raichur
http://slidepdf.com/reader/full/c-introduction-csc-dept-nit-raichur 45/48
![Page 46: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/46.jpg)
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
![Page 47: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/47.jpg)
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
![Page 48: C-Introduction-C.sc Dept, NIT, Raichur](https://reader031.fdocuments.net/reader031/viewer/2022021405/577cd3441a28ab9e78970429/html5/thumbnails/48.jpg)
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)