c history and basic

7
Chp 4. History of C & C primitives History of C o Developing a common programming language which can be used to solve different types of problems on various hardware platforms has always been a computer professional’s goal. o Languages developed before C turned out to be not sufficient in general. o In 1972, Dennis Ritche at AT & T’s Bell Laboratories, USA , developed C language incorporating the best features. o C is considered to be a middle-level language since it has the features of both low-level and high-level languages. o High-level language (Problem-oriented language ) has been designed to give faster program development. o Low-level language (Machine-oriented language ) has been designed to give faster program execution. o Programs written in C language gives relatively high machine efficiency as compared to high-level languages and provides relatively high programming efficiency as compared to low-level languages. Components of C language: 1. Character set 2. Data types 3. Constants 4. Variables 5. Keywords 1. Character Set: A character denotes any alphabet, digit or special symbol used to represent information. Alphabets : A, B, ……, Z or a, b, ……., z Digits : 0, 1, ………, 9 Special Symbols : ~ ! @ # $ % ^ & * ( ) _ - + = | \ { } [ ] : ” < > ? ; ’ , . / White Space : blank space, horizontal tabs (equal to 5 spaces), new line 2. Data types: To communicate with the computer, values of different types are given to the computer. Each of these requires memory (storage) of different size. Such types for the values are called as data types. Data types can be divided into: 1. Primary data types : void, character (char), integer (int), float, double 2. Secondary data types : array, pointer, structure, union, enum Primary data types: 1. char : It is used to store a single character or symbol.( Eg. ‘a’, ‘A’, ‘f’, ‘*’,’#’, ‘+’) 2. int : It is used to store positive or negative integers. (Eg: 0,1,…….,- 4, - 10 ) 3. float : It is used to store real numbers with single precision (precision of six digits after decimal points) (Eg: 10.234, 657.123456) 1

description

c history and basic

Transcript of c history and basic

Page 1: c history and basic

Chp 4. History of C & C primitives

History of C o Developing a common programming language which can be used to solve different types of problems on

various hardware platforms has always been a computer professional’s goal.o Languages developed before C turned out to be not sufficient in general.o In 1972, Dennis Ritche at AT & T’s Bell Laboratories, USA, developed C language incorporating the best

features.o C is considered to be a middle-level language since it has the features of both low-level and high-level

languages.o High-level language (Problem-oriented language) has been designed to give faster program development.o Low-level language (Machine-oriented language) has been designed to give faster program execution.o Programs written in C language gives relatively high machine efficiency as compared to high-level

languages and provides relatively high programming efficiency as compared to low-level languages.

Components of C language: 1. Character set2. Data types3. Constants4. Variables5. Keywords

1. Character Set:A character denotes any alphabet, digit or special symbol used to represent information.Alphabets : A, B, ……, Z or a, b, ……., zDigits : 0, 1, ………, 9Special Symbols : ~ ! @ # $ % ^ & * ( ) _ - + = | \ { } [ ] : ” < > ? ; ’ , . /White Space : blank space, horizontal tabs (equal to 5 spaces), new line

2. Data types: To communicate with the computer, values of different types are given to the computer. Each of these requires memory (storage) of different size. Such types for the values are called as data types.Data types can be divided into:

1. Primary data types : void, character (char), integer (int), float, double2. Secondary data types : array, pointer, structure, union, enum

Primary data types:1. char : It is used to store a single character or symbol.( Eg. ‘a’, ‘A’, ‘f’, ‘*’,’#’, ‘+’)2. int : It is used to store positive or negative integers. (Eg: 0,1,…….,- 4, -10 )3. float : It is used to store real numbers with single precision (precision of six digits after decimal points)

(Eg: 10.234, 657.123456)4. double : It is used to store real numbers with double precision i.e. twice the storage space required by

float. (Eg: 12.12345678, 345.9873458)5. void: It is used to specify empty set containing no values.

Data types Storage spacechar 1 byteint 2 bytefloat 4 bytedouble 8 bytevoid 0 byte

3. Constants:Constants are fixed values that remain unchanged during the program execution. Constants can be divided into two major categories:

1. Primary constants : Integer, Real, Character2. Secondary constants : Array, Pointer, Structure, Union, Enum

1

Page 2: c history and basic

Rules for constructing Integer constants:1. An Integer constant must have at least one digit.2. It must not have a decimal point.3. It can be positive or negative.4. If no sign precedes, it is assumed to be positive.5. No commas or blanks are allowed within an integer constant.6. The allowable range for integer constants is -32,768 to 32,767 or 0 to 65,535(16 bit / 2 byte)Ex: +26, 17, -9

Rules for constructing Real constants:1. A Real constant must have at least one digit.2. It must have a decimal point.3. It can be positive or negative.4. Default sign is positive.5. No commas or blanks are allowed within a real constant.Ex: +32.5567, 0.23, -9213.5

Rules for constructing Character constants:1. It is either a single alphabet, a single digit or a single special symbol written within single inverted

commas.2. The maximum length can be 1 character.Ex: ‘A’, ‘b’, ‘3’, ‘=’

4. Variables:A value which may vary (change) during program execution is called a variable. A variable can be considered as a name given to the memory location where a constant is stored. The type of variables that it can support depends on the type of value it stores.

Rules for constructing variable names:1. A variable name is any combination of alphabets, digits or underscores only. The length of a

variable name can be up to 31 characters. Some compilers allow variable names whose length could be up to 247 characters.

2. The first character must be an alphabet or underscore.3. No commas or blanks are allowed within a variable name.4. No special symbol other than an underscore can be used in a variable name.Ex: net_salary, name1, name2, employeeID

Variable declaration and assignment of values:All the variables must be declared before their use. Declaration does two things:1. It tells the compiler what the variable name is,2. It specifies the type of data, the variable will store.

A variable is declared as follows:

Data_type variable_name;

Ex: int salary, tax; or char name;

To assign values to the variable i.e. define a variable, assignment operator (=) is used.

variable_name = value;

Ex: int i;

i=2;

It is possible to assign a value to the variable at the time of declaration.

Data_type variable_name = value;

Ex: int i=2;

2

Page 3: c history and basic

5. Keywords:Keywords are words whose meaning has already been explained to C compiler. Keywords cannot be used as variable names. They are also called as “Reserved words”. There are only 32 keywords in C.

auto double int struct break elselong switch case enum register typedefchar extern return union const floatshort unsigned continue signed for voiddefault goto sizeof volatile do ifstatic while

Identifiers: Identifiers are the names that are supplied for variables, types, functions, and labels in your program. Keywords cannot be used as identifiers; they are reserved for special use. Once declared, the identifier can be used later in program statements to refer to the associated value.

C Instructions: There are basically three types of instructions in C:1. Type Declaration Instruction : To declare the type of variables used in a C program.

Ex: int studentID; char ans;

2. Arithmetic Instruction : To perform arithmetic operations between constants & variables.Ex: a = b + c; i = j * 3;

3. Control instruction : To control the sequence of execution of various statements in a C program.

Operators: An operator is a symbol that tells the compiler to perform certain mathematical or logical operation on data stored in variables. The variables that are operated are termed as operands.1. Arithmetic operators :

+ : Addition - : Subtraction * : Multiplication/ : Division % : Remainder (Modulus)

2. Relational operators : Used to compare two operands.== : equality != : not equal to < : greater than > : less than<= : greater than or equal to >= : less than or equal to

3. Assignment operator : (=) Used to assign a value to a variable.

4. Increment & Decrement operators :a. Increment operator (++) adds one to the operand.b. Decrement operator (--) subtracts one from the operand.c. These operators are called unary operators.d. These operators may be used before or after the operand.

Ex: int i=5; i++; : postfix ++i; : prefix i--;

--i;

o Postfix operators has the effect of “use then change”.o Prefix operators has the effect of “change then use”.

Comments: o Comment about the program should be enclosed within /* */. Any number of comments can be written at

any place in the program. It can be written before the statement, after the statement or within the statement as shown below:Ex: /* formula */ si = p*n*r/100;

si = p*n*r/100; /* formula */

3

Page 4: c history and basic

si = p*n*r/ /* formula */100;

o Comments cannot be nested. Ex: /*Program for /*simple interest*/ is as follows*/

o A comment ca be split over more than one line, as in,/* This isa jazzycomment*/Such a comment is called a multi-line comment.

Format specifiers: Format specifiers are used to provide the type and size of the data. Each format specifier must begin with %. The general form of a format specifier is:

%fwsfxwhere fws = field width specifier

fx = format specifierThe field width specifier tells how many columns on the screen should be used while printing a value.Ex: %7d tells to print the variable as a integer in the field of seven columns.If a minus sign is included (%-7d), this means left justification is desired and the value will be padded with blank spaces on the right.

Format specifiers Type%d int%c char%f float%lf double

Ex: printf(“%d”, 12345); 1 2 3 4 5

printf(“%10d”, 12345); 1 2 3 4 5

printf(“%010d”, 12345); 0 0 0 0 0 1 2 3 4 5

printf(“%-10d”, 12345); 1 2 3 4 5

printf(“%f”, 98.7654); 9 8 . 7 6 5 4

printf(“%7.2f”, 98.7654); 9 8 . 7 7

Input / Output functions: The standard C library consists of two functions that perform input and output, namely, scanf() and printf(). The printf() statement outputs the value to the screen. It provides certain features through which the

screen output is effectively controlled. The general form of printf() function is:

printf(“control string”, arg1,arg2,….);

The control string may contain:o Characters that are printed as they are.o Format specifications that begin with % sign.o Escape sequence that begins with \ sign.

The arguments arg1, arg2…are the variables whose values are printed.Ex: int total=60; float per=89.4; printf(“Total = %d \n Percentage = %f”, total, per);

Output: Total = 60Percentage = 89.4

4

Page 5: c history and basic

The scanf() statement allows us to read data or receive data from the keyboard. The general form of scanf() function is:

scanf(“control string”, arg1,arg2,….);

Control string specifies the field format in which data is to be entered and the arguments arg1,arg2…specify the ADDRESS OF LOCATION where the value is to be stored.

Ex: scanf(“ %d “, &a);

The ampersand (&) before the variables is a must. & is an ‘Address of’ operator. It gives the location number used by the variable in memory. It tells at which location should it store the value supplied by the user from the keyboard.

Integer and float conversion: Following are the rules for implicit conversion of floating point and integer values in C.1. An arithmetic operation between an integer and integer always give an integer result.2. An arithmetic operation between a real and real always give a real result.3. An arithmetic operation between an integer and real always give a real result.Ex: 5 / 2 = 2 5.0 / 2 = 2.5 5.0 / 2.0 = 2.5

Type conversion in Assignments: While storing the value, the expression always takes the type of the variable into which its value is being stored.Ex: int a;

a = 5 / 2; output: 2a = 2 / 5; output: 0a = 5.0 / 2; output: 2a = 5.0 / 2.0; output: 2

float f;f = 5 / 2; output: 2.000000f = 2 / 5; output: 0.000000f = 5.0 / 2; output: 2.500000f = 4 / 2; output: 2.000000

Hierarchy of operations: The priority or precedence in which the operations in an arithmetic statement are performed is called the hierarchy of operations. The hierarchy of commonly used operators are:

Priority Operators1st * / %2nd + -3rd =

Ex: Determine the hierarchy of operations and evaluate the following expression.k = 3 / 2 * 4 + 3 / 8 + 3

Stepwise evaluation of this expression is shown as:k = 3 / 2 * 4 + 3 / 8 + 3k = 1 * 4 + 3 / 8 + 3 operation : /k = 4 + 3 / 8 + 3 operation : *k = 4 + 0 + 3 operation : /k = 4 + 3 operation : +k = 7 operation : +

5