c programming unit 01

20
In this session, you will learn to: Identify the benefits and features of C language Use the data types available in C language Identify the structure of C functions Use input-output functions Use constructs Objectives

Upload: prashanth-shivakumar

Post on 17-Aug-2015

61 views

Category:

Technology


2 download

TRANSCRIPT

In this session, you will learn to:Identify the benefits and features of C language

Use the data types available in C language

Identify the structure of C functions

Use input-output functions

Use constructs

Objectives

Identifying the Benefits and Features of C Language

Ken Thompson developed a new language called B.

B language was interpreter-based, hence it was slow.

Dennis Ritchie modified B language and made it a compiler-based language.

The modified compiler-based B language is named as C.

C language:Possesses powerful low-level features of second generation languages.

Provides loops and constructs available in third generation languages.

Is very powerful and flexible.

C as a Second and Third Generation Language

C language:Offers all essentials of structured programming.

Has functions that work along with other user-developed functions and can be used as building blocks for advanced functions.

Offers only a handful of functions, which form the core of the language.

Has rest of the functions available in libraries. These functions are developed using the core functions.

Block Structured Language - An Advantage for Modular Programming

Features of the C Language

The features that make C a widely-used language are:Pointers: Allows reference to a memory location by a name.

Memory Allocation: Allows static as well as dynamic memory allocation.

Recursion: Is a process in which a functions calls itself.

Bit Manipulation: Allows manipulation of data in its lowest form of storage.

Using the Data Types Available in C language

The types of data structures provided by C can be classified under the following categories:

Fundamental data types

Derived data types

Fundamental Data Types

Fundamental Data Types:Are the data types at the lowest level.

Are used for actual data representation in the memory.

Are the base for other data types.

Have machine dependent storage requirement.

Are of the following three types:

char

int

float

The storage requirement for fundamental data types can be represented with the help of the following table.

Data Number of bytes on a 32-byte machine

Minimum Maximum

char 1 -128 127

int 4 -2^31 (2^31) - 1

float 4 6 digits of precision

6 digits of precision

Fundamental Data Types (Contd.)

Derived Data Types

Derived Data Types:Are represented in memory as fundamental data type.

Some derived data types are:short int

long int

double float

The storage requirement for derived data types can be represented with the help of the following table.

Data Number of bytes on a 32-byte

machine

Minimum Maximum

short int 2 -2^15 (2^15) - 1

long int 4 -2^31 (2^31) - 1

double float 8 12 digits of precision

6 digits of precision

Derived Data Types (Contd.)

Defining Data

The syntax for defining data is:

[data type] [variable name],...;Declaration is done in the beginning of a function.

Definition for various data types is shown in the following table.

Data definition Data type Memory defined Size (bytes) Value assigned

char a, c; char ac

11

--

char a = 'Z'; char a 1 Z

int count; int count 4 -

int a, count =10; int acount

44

-10

float fnum; float fnum 4 -

float fnum1, fnum2 = 93.63;

float fnum1fnum2

44

-93.63

Practice: 1.1

Write the appropriate definitions for defining the following variables:1. num to store integers.

2. chr to store a character and assign the character Z to it.

3. num to store a number and assign the value 8.93 to it.

4. i, j to store integers and assign the value 0 to j.

Practice: 1.1 (Contd.)

Solution:1. int num;

2. char chr=’Z’;

3. float num = 8.93;

4. int i, j=0;

Defining Strings:Syntax:

char (variable) [(number of bytes)];

Here number of bytes is one more than the number of characters to store.

To define a memory location of 10 bytes or to store 9 valid characters, the string will be defined as follows:

char string [10];

Defining Data (Contd.)

Practice: 1.2

Write the appropriate definitions for defining the following strings:1. addrs to store 30 characters.

2. head to store 14 characters.

Practice: 1.2 (Contd.)

Solution:1. char addrs[31];

2. char head[15];

Summary

In this session, you learned that:C language was developed by Ken Thompson and Dennis Ritchie.

C language combines the features of second and third generation languages.

C language is a block structured language.

C language has various features that make it a widely-used language. Some of the important features are:

Pointers

Memory Allocation

Recursion

Bit-manipulation

Summary (Contd.)

The types of data structures provided by C can be classified under the following categories:

Fundamental data types: Include the data types, which are used for actual data representation in the memory.

Derived data types: Are based on fundamental data types.

Fundamental data types:char, int, and float

Some of the derived data types are:short int, long int, and double float

Definition of memory for any data, both fundamental and derived data types, is done in the following format:[data type] [variable name],...;

Summary (Contd.)

In C language, the functions can be categorized in the following categories:

Single-level functions

Multiple-level functions

For standard input-output operations, the C environment uses stdin, stdout, and stderr as references for accessing the devices.

There are two types of constructs in C language:Conditional constructs

Loop constructs

Each Unit contains 40 to 60 slides in it.