Вы находитесь на странице: 1из 12

CCP Notes Unit 3

C language is a general purpose and structured pragramming langauge developed by 'Dennis Ritchie' at AT &T's Bell Laboratories in the 1972s in USA. It is also called as 'Procedure oriented programming language.' C is not specially designed for specific applications areas like COBOL (Common BusinessOriented Language) or FORTRAN (Formula Translation). It is well suited for business and scientific applications. It has some various features like control structures, looping statements, arrays, macros required for these applications. The C language has following numerous features as: Features of C Language : 1. It is robust language because of rich set of binary in function 2. It is efficient and fast because of its variant data-types and powerful operation. 3. It is highly Portable i.e., programs written in one computer can be run on another 4. It is well suited for structure program, thus allows the user to think about the problem in the terms of functional blocks. 5. Debugging, testing and maintenance is easy 6. ability to extend itself, we can continuously add our own functions to the program. Execution of C Program : C program executes in following 4 (four steps).

Creating a program : An editor like notepad or wordpad is used to create a C program. This file contains a source code which consists of executable code. The file should be saved as '*.c' extension only. 2. Compiling the program : The next step is to compile the program. The code is compiled by using compiler. Compiler converts C code to binary code i.e. object code. 3. Linking a program to library : The object code of a program is linked with libraries that are needed for execution of a program. The linker is used to link the program with libraries. It creates a file with '*.exe' extension. 4. Execution of program : The final executable file is then run by loading the instruction into the main memory.
1.

History of C :

Year of Establishment 1960 1963

Language Name ALGOL-60 CPL (Combined Programming

Developed By Cambridge University Cambridge University

Perpared by :SivaBalan , Sr.Asst Professor , NHCE, Bangalore

Language) 1967 1970 1972 BCPL (Basic Combined Programming Language) B C Martin Richard at Cambridge University Ken Thompson at AT & T's Bell Laboratories. Dennis Ritchie at AT & T' Bell Laboratory.

The development of C was a cause of evolution of programming languages like Algol 60, CPL (Combined Programming Langauge), BCPL (Basic Combined Programming Language) and B.

Algol-60 : (1963) :

ALGOL is an acronym for Algorithmic Language. It was the first structured procedural programming language, developed in the late 1950s and once widely used in Europe. But it was too abstract and too general structured langauage.

CPL : (1963) :

CPL is an acronym for Combined Programming Language. It was developed at Cambridge University.

BCPL : (1967) :

BCPL is an acronym for Basic Combined Programming Language. It was developed by Martin Richards at Cambridge University in 1967. BCPL was not so powerful. So, it was failed.

B : (1970) :

B language was developed by Ken Thompson at AT & T Bell Laboratories in 1970. It was machine dependent. So, it leads to specific problems.

C : (1972) :

'C' Programming Langauage was developed by Dennis Ritchie at AT & T Bell Laboratories in 1972. This is general purpose, compiled, structured programming langauage. Dennis Ritchie studied the BCPL, then improved and named it as 'C' which is the second letter of BCPL Note : C is a middle level language because it was due to have both a relatively good programming efficiency and relativity good machine effecience.

Structure of C Program
The basic structure of C program is as follow: Document Section Links Section (File) Definition Section Global variable declaration Section

CCP Notes Unit 3 void main() { Variable declaration section Function declaration section executable statements; } Function definition 1 ----------------------------------------Function definition n where, Document Section : It consists of set of comment lines which include name of a program, author name, creation date and other information. Links Section (File) : It is used to link the required system libraries or header files to execute a program. Definition Section : It is used to define or set values to variables. Global variable declaration Section : It is used to declare global or public variable. void main() : Used to start of actual C program. It includes two parts as declaration part and executable part. Variable declaration section : Used to declare private variable. Function declaration section : Used to declare functions of program from which we get required output. Then, executable statements are placed for execution. Function definition section : Used to define functions which are to be called from main().

Introduction
To learn any language we learn the following in order English Language Alphabets Words C Character set Tokens C Language

Perpared by :SivaBalan , Sr.Asst Professor , NHCE, Bangalore

Sentence Paragraph

Statements or Instructions Programs

C Character set
Like alphabets of English language , C has its own , rich character set. It consists of basic symbols of the language used to construct the Tokens of C language. The following are the c character set Alphabets a,b,c,dz A,B,C,DZ Digits Special Characters 0,1,2,3,4,5,6,7,8,9 : , ~, !, @,#,$,%,^,&,*,(,),_,-, etc. Lower case letter Upper case letter All decimal digits

Tokens
In a passage of text , individual words and punctuation marks are called tokens. Similarly in a C program the smallest individual units are known as C tokens. C has 6 types of tokens.
1. Keywords or

Reserved words
2. Identifiers 3. Constants 4. Strings 5. Operators 6. Special symbols

1. Keywords

Keywords are called Reserved words.

CCP Notes Unit 3

Keywords are the words whose meaning is already defined and explained to the C language compiler. Therefore keywords cannot be used as variable names. They should only be used to carry the pre-defined or pre-assigned meaning of the language syntax and not to store values in it. All keywords must be written in lowercase. The following are some sample keywords available in C Language

ANSI C Keywords auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while

2. Identifiers

Identifiers refer to the names of variables, functions and arrays. These are user defined names and consist of a sequence of letters and digits, with a letter as a first character Both uppercase and lowercase letters are permitted, although lowercase letters are commonly used. The underscore character is also permitted in identifiers. It is usually used as a link between two word in long identifiers. No other special characters are used in naming a identifier Identifiers are case sensitive

3. Constants A constant can be defined as a value or quantity which does not change during the execution of a program .

Perpared by :SivaBalan , Sr.Asst Professor , NHCE, Bangalore

C constant can be classified as follows

Constants in C

Numeric Constants Character Constants Special constant

Floating point constants

CCP Notes Unit 3

Integer Constants

String Constants Single character constants

Integer Constant: An integer constant refers to a sequence of digits. There are three types of integers, namely

Decimal octal hexadecimal Decimal integers consist of a set of digits 0 through 9, preceded by an optional or + sign. Some examples of decimal integer constants are 123 , -431 , 0 , 34567 ,+678 Spaces, commas, and non-digit characters are not permitted between digits. For example 15 750 , 20,000 , Rs 1000 are illegal numbers. An octal integer constant consists of any combination of digits from the set 0 through 7 with a leading 0. Some examples are: 037 , 0 , 0435 , 0567

Perpared by :SivaBalan , Sr.Asst Professor , NHCE, Bangalore

Hexadecimal integer constant : A sequence of digits preceded by 0x is considered as hexadecimal integer. They may also include alphabets A through F or a through F. The letters A through F represent the numbers 10 through 15. The examples for hexadecimal integers are: 0x2 , 0x9F , 0xbcd , 0x

Floating-point constants:
Decimal :These quantities are represented by numbers containing fractional parts like 23.78. Such numbers are called floating-point constants or real constants. Examples for floating-point constants are given below. 213. , .95 , -.71 , +.5 Exponential A real number may also be expressed in exponential(or specific notation). For example the value 213.45 may be written as 2.1345e2 in exponential notation. e2 means multiply by 102. The general form is mantissa e exponent

Character Constant
Single Character Constants: o

A character constant contains a single character enclosed within a pair of single quote marks. Examples of character constants are:

5 X ;

Note that the character constant 5 is not the same as the number 5. The last constant is a blank space.

Character constants have integer values known as ASCII values. For Example, the statement printf (%d, A); would print the number 65,the ASCII value of the letter a. Similarly, the statement printf(%c, 65) would give the output as letter A

String Constant
A string constant in C denotes a sequence of one or more characters enclosed with

CCP Notes Unit 3

in a pair of double quotes Examples : Hi , NHCE BANGALORE , 898 , RA01

Special Character Constants:


C supports some special backslash character constants that are used in output functions. For example, the symbol \n stands for new line character. The below table gives you a list of backslash constants. Backslash Character Constants Constant \a \b \f \n \r \t \v \ \ \? \\ \0 single quote double quote question mark backslash mark null character Meaning audible alert(bell) back space form feed new line character carriage return horizontal tab vertical tab

Note that each one of them represents one character, although they consist of two characters. These character combinations are called escape sequences.

Variable :
It is a data name which is used to store data and may change during program execution. It is opposite to constant. Variable name is a name given to memory cells location of a computer where data is stored.

* Rules for varibales:


First character should be letter or alphabet. Keywords are not allowed to use as a variable name.

Perpared by :SivaBalan , Sr.Asst Professor , NHCE, Bangalore

White space is not allowed. C is case sensitive i.e. UPPER and lower case are significant. Only underscore, special symbol is allowed between two characters. The length of indentifier may be upto 31 characters but only only the first 8 characters are significant by compiler. Some examples for valid variable names: Abhi Value I_rate

Mumbai Rate

s1 sum1

ph_value distance

The examples given below are invalid varaiable names: 345 $sum (rate) sum.result

DATA TYPES
C offers a standard, minimal set of basic data types. Sometimes these are called "primitive" types. A lot of complex data structures can be developed from these basic data types. The C language defines 5 fundamental data types: 1. Integer (int) 2. Character (char) 3. floating point (float) 4. double-precession (double) 5. void Range of data types : Data type char int float double void Bytes in Ram 1 bytes 2 bytes 4 bytes 8 bytes nothing Range of data type -128 to 127 -32, 768 to 32,767 3.4c-38 to 3.4 c+ 38 1.7C 308 to 1.7c +308

Integer Types : Integers are whole numbers with a range of variables supported by a particular machine.

CCP Notes Unit 3

In a signed integer uses one bit for sign and 15 bits for magnitude

C has three classes of integer storage


short int int long int It has a set of qualifiers i.e.,

sign qualifier unsigned qualifier short int uses half the range of storage amount of data, unsigned int use all the bits for the magnitude of the number and are positive. short int int long int Datatype char or signed unsigned char int or signed int unsigned int Size char 1 byte 1 byte 2 byte 2 bytes Range -128 to 127 0 to 255 -32,768 to 32, 767 0 to 65535

Floating Point Datatype:


Floating Point numbers are stored with 6 digits of precision. Those are defined with keyword float. When the accuracy is not sufficient then the datatype double can be used. double gives a precesion of 14 digits these known as double precesion numbers. Still for a better process we can use long double which uses 80 bits float double long double

Character Datatype : Character are usually stored in 8 bits


Perpared by :SivaBalan , Sr.Asst Professor , NHCE, Bangalore

Void datatype : A void type has no value this is usually used to specify the return
type of function , this function does not return any value to calling function.

Example : void main() {


}

Declaration of Variable :
Variables are declared using data types. The variables get its meaning once it is associated to a datatype.It tells the complier what the variable name is used, what type of data is stored in it Syntax: datetype v1,v2,.vn; Eg : int a, b; float sum; double ratio; Program for variable declaration void main( ) { float x,p; x=10.1; p=5.2; printf (x = %f\n, x); printf (p = %f\n, p); } O/P : x= 10.10000 p = 5.2

Вам также может понравиться