You are on page 1of 48

Introduction to the C Language

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.

C History

Developed between 1969 and 1973 along with Unix Due mostly to Dennis Ritchie Designed for systems programming

Operating systems Utility programs Compilers Filters

Evolved from B, which evolved from BCPL

Background
C is a structured programming language. It 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.
3

C History

Original machine (DEC PDP-11) was very small

24K bytes of memory, 12K used for operating system

Written when computers were big, capital equipment

Group would get one, develop new language, OS

Taxonomy of the C Language


5

C Programs
It's time to write your first C program! This section will take you through all the basic parts of a C program so that you will be able to write it.
Topics discussed here :

Structure of a C Program Your First C Program Comments The Greeting Program


6

Structure of a C Program
7

The Greeting Program


8

Examples of Block Comments


9

Examples of Line Comments


10

Nested Block Comments Are Invalid


11

PROGRAM 2-1

The Greeting Program

Computer Science: A Structured Programming Approach Using C

12

2-3 Identifiers
One feature present in all computer languages is the identifier. Identifiers allow us to name data and other objects in the program. Each identified object in the computer is stored at a unique address. If we didnt have identifiers that we could use to symbolically represent data locations, we would have to know and use objects addresses. Instead, we simply give data identifiers and let the compiler keep track of where they are physically located.
13

Table 2-1

Rules for Identifiers

14

Note
An identifier must start with a letter or underscore: it may not have a space or a hyphen.

15

Note
C is a case-sensitive language.

16

C Type Examples
int i; int *j, k; unsigned char *ch; float f[10];

Integer j: pointer to integer, int k ch: pointer to unsigned char Array of 10 floats Array of three arrays of five function returning int * pointer to function returning int

char nextChar(int, char*);2-Arguments function int a[3][5][10]; int *func1(float); int (*func2)(void);

C Expression Classes

arithmetic: + * / % comparison: == != < <= > >= bitwise logical: & | ^ ~ shifting: << >> lazy logical: && || ! conditional: ? : assignment: = += -= increment/decrement: ++ -sequencing: , pointer: * -> & []

Examples of Valid and Invalid Names

19

Types
A type defines a set of values and a set of operations that can be applied on those values. For example, a light switch can be compared to a computer type. It has a set of two values, on and off. Only two operations can be applied to a light switch: turn-on and turn-off.
Topics discussed in this section:
Void Type Integral Type Floating-Point Types
20

Data Types
21

Character Types
22

Integer Types
23

Note
sizeof (short) sizeof (int) sizeof (long) sizeof (long long)

24

Typical Integer Sizes and Values for Signed Integers

25

Floating-point Types
26

Note
sizeof (float) sizeof (double) sizeof (long double)

27

Type Summary

28

Variables
Variables are named memory locations that have a type, such as integer or character, which is inherited from their type. The type determines the values that a variable may contain and the operations that may be used with its values.

Topics discussed in this section:


Variable Declaration Variable Initialization
29

Variables
30

Examples of Variable Declarations and Definitions

31

Variable Initialization
32

Note
When a variable is defined, it is not initialized. We must initialize any variable requiring prescribed data when the function starts.

33

PROGRAM 2

Print Sum of Three Numbers

34

PROGRAM 2

Print Sum of Three Numbers (continued)

35

PROGRAM 2

Print Sum of Three Numbers (continued)

36

Constants
Constants are data values during the execution of a constants have a type. In Boolean, character, integer, constants. that cannot be changed program. Like variables, this section, we discuss real, complex, and string

Topics discussed in this section:


Constant Representation Coding Constants
37

Note
A character constant is enclosed in single quotes.

38

Symbolic Names for Control Characters


39

Examples of Integer Constants

40

Examples of Real Constants

41

Note
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.

42

Examples of Complex Constants

43

Some Strings
44

Null Characters and Null Strings


45

Note
Use single quotes for character constants. Use double quotes for string constants.

46

PROGRAM 3

Memory Constants

47

PROGRAM 3

Memory Constants (continued)

48

You might also like