c notes 2-1

Share Embed Donate


Short Description

c language notes...

Description

Tokens: In a passage of text, individual words and punctuation marks are called tokens or lexical units. Similarly, the smallest individual unit in a c program is known as a token or a lexical unit. C tokens can be classified as follows: 1. Keywords 2. Identifiers 3. Constants 4. Strings 5. Special Symbols 6. Operators

Variables: A variable is a name given to the memory location. EX: For-is a valid variable for-is a invalid variable.

Data Type: C has a concept of 'data types' which are used to define a variable before its use. The definition of a variable will assign storage for the variable and define the type of data that will be held in the location. A programming language is proposed to help programmer to process certain kinds of data and to

provide useful output. The task of data processing is accomplished by executing series of commands called program. A program usually contains different types of data types (integer, float, character etc.) and need to store the values being used in the program. C language is rich of data types. A programmer has to employ proper data type as per his requirements. C has different data types for different types of data and can be broadly classified as: 1. Primary Data Types(Primitive) 2. Secondary Data Types(non Primitive) 3. User defined data types

Integer Data Types: Integers are whole numbers with a range of values, range of values are machine dependent. Generally an integer occupies 2 bytes memory space and its value range limited to -32768 to +32767 (that is, -215 to +215-1). A signed integer use one bit for storing sign and rest 15 bits for number. To control the range of numbers and storage space, C has three classes of integer storage namely short int, int and long int. All three data types have signed and unsigned forms. A short int requires half the amount of storage than normal integer. Unlike signed integer, unsigned integers are always positive and use all the bits for the magnitude of the number. Therefore, the range of an unsigned integer will be from 0 to 65535. The long integers are used to declare a longer range of values and it occupies 4 bytes of storage space.

Syntax: int ; int num1; short int num2; long int num3; Example: 5, 6, 100, 2500. Integer Data Type Memory Allocation:

Floating Point Data Types: The float data type is used to store fractional numbers (real numbers) with 6 digits of precision. Floating point numbers are denoted by the keyword float. When the accuracy of the floating point number is insufficient, we can use the double to define the number. The double is same as float but with longer precision and takes double space (8 bytes) than float. To extend the precision further we can use long double which occupies 10 bytes of memory space. Syntax:

float ; float num1; double num2; long double num3; Example: 9.125, 3.1254.

Floating Point Data Type Memory Allocation:

Character Data Type: Character type variable can hold a single character and are declared by using the keyword char. As there are singed and unsigned int (either short or long), in the same way there are signed and unsigned chars; both occupy 1 byte each, but having different ranges. Unsigned characters have values between 0 and 255, signed characters have values from –128 to 127. Syntax: char ; char ch = ‘a’; Example: a, b, g, S, j.

Void Type: The void type has no values therefore we cannot declare it as variable as we did in case of integer and float. The void data type is usually used with function to specify its type.

User defined Data Type User can create his own data type for handling data that does not fit in one of the existing data types. Programmer can create an identifier that denotes an already existing data type with the help of a user defined data type. The programmer defined data type identifier is then used in a program to declare variables. In short its purpose is to redefine the name of an existing data type. Syntax: typedef; Enumerated Data Types Enumerated data types are a user defined ordinal data type. The main purpose of the enumerated data type is to allow numbers to be replaced by words. This is intended to improve the readability of programs.

Enumerated Data Type (enum) : An Enumerated data type consists of an ordered set of distinct constant values defined in a data type in a program. The format of en-um is:enum name { value-l,value-2,value-3, .......... ,value-4; }; where., name is the name of the enumerated data type, also known as tag and value-l,value2,value-3,……,value-n are values that variable of type name can take. enum fruit { a pple,orange,mango, pineapple; }; This declaration states that a variable of type fruit can have any of the four values namely apple, orange, mango and pineapple, but no other value. Once the type fruit has been defined then variable of that type can be defined in the following manner :-fruit a,b; Now, the variable a and b are of type fruit and can assume value from the list : (apple, orange, mango, pineapple).

Example (2): Part-1

enum days

{ mon,tue,wed,thu,fri,sat,sun;

}; Part-2

days holiday,wdays;

The first part defines an enumeration named days. The second part declares the variables holiday and wdays to be enumeration variable of type days. Thus each variable can be assigned any one of the constants mon,tue,wed,thu,fri,sat,sun. The two parts in example-2 can be combined if desired, resulting in enum days { mon,tue,wed,thu,fri,sat,sun; } holiday,wdays; or without the name(tag), simply enum { mon,tue,wed,thu,fri,sat,sun; } holiday,wdays; Enumeration constants are automatically assigned equivalent integer values, beginning with 0 for the first constant, with each successive constant increasing by 1. Therefore in example-1, the enumeration constants will represent the following integer values: Mon 0 Tue 1 Wed 2

Thu Fri Sat Sun

3 4 5 6

These automatic assignments can be overridden by assigning explicit integer values which differ from the default values. Those constants are not assigned explicit values will automatically be assigned values which increase successively by 1 from the last explicit assignment. enum days { mon=10; tue=ll; wed=12; thu=13 fri=20; sat=21; sun=50; };

Operations on Enumerated Data Types • Assignment example: - holiday=sun; 2. Comparing using Relational Operators Example: Consider the enumerated data types defined in previous example: Expression sun „A‟ (65) */ The strcmp function is required because relational operations are not supported with strings. int strlen ( string_value ); /* String Length */ The strlen function returns the length of the string value. The strlen function returns the number of individual characters contained in the string value. The length is indicated by the position that the NULL character is stored in.

Basic structure of C program C language is very popular language among all the languages. The basic structure of a Clanguage. The structure of a C program is a protocol (rules) to the programmer, while writing a C program. The general basic structure of C program is shown in the figure below. The whole program is controlled within main ( ) along with left brace denoted by “{” and right braces denoted by “}”. If you need to declare local variables and executable program structures are enclosed within “{” and “}” is called the body of the main function. The main ( ) function can be preceded by documentation, preprocessor statements and global declarations.

Documentations The documentation section consist of a set of comment lines giving the name of the program, the another name and other details, which the programmer would like to use later.

Preprocessor Statements The preprocessor statement begin with # symbol and are also called the preprocessor directive. These statements instruct the compiler to include C preprocessors such as header files and symbolic constants before compiling the C program. Some of the preprocessor statements are listed below

Preprocessor statements are processed before the source program is handed over to the compiler. They are placed in a source program before the function main. The various preprocessor directories are: 1) Symbolic Names: An identifier is replaced by sequence of characters which may represent a constant or string. These identifiers (also called symbolic names) are associated with constants and hence they are called symbolic constant. The syntax of macro definition for symbolic name is #define Name String Ex: #define PI 3.142 This instructs the compiler that whenever PI is used in the program, it is replaced by the constant 3.142. 2) Macro Substitution: The symbolic names can also be used to define the macros. Ex: consider the macro definition as shown below #define square(x) (x*x) Whenever square appears in the program with the x as a parameter it is replaced by (x) (x*x)

#include #include Void main() { int m=10,n=5; printf(“5 square is =%d\n”,square(n)); printf(“10 square is=%d\n”,square(m)); printf(“10+5 square is =%d\n”,square(m+n)); getch(); } Output: 5 square is=25 10 square is=100 10+5 square is =65 The two answers are correct and the square of 10+5 is 65 which is wrong. The correct answer is 225. Note that when macro SUARE (m+n) is called the actual replacement will be (10+5*10+5). To get the correct square the actual definition can be modified by placing the brackets as shown below #define square((x)*(x)) By doing these changes to the macro definition if we execute the above program the output is 5 square is=25 10 square is=100 10+5 square is =225 3) File inclusion: It is a process using which the external files containing functions and definitions are included in C programming. This avoids the rewriting of functions and macros. Any external file can be include #include directory. This syntax is #include file name EX: #includeb?a:b; i.e if a>b, then big=a else big=b. Based on the functions, operators can be classified as given below

Arithmetic Operators: Arithmetic operators are used to perform numerical operators in C. They are divided into two classes namely, Unary and Binary arithmetic operators. Unary operators: An operator which adds on only one operand to produce the result is called unary operator. EX: *a, -9, &c are all expressions with unary operators. Binary operator: An operator which adds on two operands to produce the result is called binary operator.

EX: c+d, d%c, x/y are all expressions with binary operators.

Arithmetic Operators: C provides all the basic arithmetic operators, they are +, -, *, /, % Integer division truncates any fractional part. The modulo division produces the remainder of an integer division. Eg: a+b a–b a*b -a * b a / b a % b Here „a‟ and „b‟ are variables and are known as operands. % cannot be used for floating point data. C does not have an operator for exponentiation. Integer Arithmetic: When the operands in an expression are integers then the expression is an integer expression and the operation is called integer arithmetic. This always yields an integer value. For Eg. a = 14 and n = 4 then a - b = 10 Note : During modulo division,the a + b = 18 sign of the result is always the sign a * b = 56 of the first operand (the dividend ) a/b=3 - 14 % 3 = -2 a%b=2 -14 % - 3 = 2 14 % -3 = 2 Write a program to illustrate the use of all Arithmetic operator main ( ) { int sum, prod , sub, div, mod, a, b ; printf(“Enter values of a, b :”) ; scanf(“ /.d %d”, & a, & b) ; sum = a+b ; printf(“sum = %d”, sum); sub = a-b; printf(“sub = %d”, sub); prod = a * b ; printf(“prod = %d”, a* b); div = a/b; printf(“ Div = %d”, div); mod = a % b ; printf(“ mod = %d”,a % b); } Real Arithmetic / Floating Pont Arithmetic: Floating Point Arithmetic involves only real operands of decimal or exponential notation. If x, y & z are floats, then x = 6.0/7.0 = 0.857143

y = -1.0/3.0 = 0.333333 z = 3.0/2.0 = 1.500000 % cannot be used with real operands Mixed mode Arithmetic: When one of the operands is real and the other is integer the expression is a mixed mode arithmetic expression. Eg: 15/10.0 = 1.500000 15/10 = 1 10/15 = 0 -10.0/15 = -0.666667

Type conversion: In C language we can convert the data from one data type to another data type. Sometimes the compiler itself converts the data from one data type to another data type. This type of processing one data type to another data type is called type conversion. It is done using 2 methods:  Implicit conversion  Explicit conversion The process of conversion of lower data type to higher data type automatically by the compiler is called promotion or implicit conversion. The rules followed by compiler during implicit conversion as shown in below table

Lower data type char int long int float double Higher data type

Char

Char int long int float double

Operand 2 int long int

int int long int float double

long int long int long int float double

float

double

float float float float double

double double double double double

Higher data type

Lower data type

If operand 1 is =int and operand 2=double observe from table that the operand is promoted to double. Lower data type is converted to higher data type.

Modes of expressions:   

Integer expression Floating point expression Mixed mode expression

Arithmetic Operator Procedure: BODMAS: Bracket Of Division Multiplication Addition Subtraction. Ex: 2*(a%5)*(4+ (b-3)/(c+2)) Where a=8 b=15 & c=4 2*(8%5)*(4+ (15-3)/ (4+2)) 2*3*4+ (12/6) 2*3*4+2 2*3*6 36

Relational Operators: Relational operators < >= = !=

Priority Associativity 1 1 1 1 2 2

LR LR LR LR LR LR

Precedence of operators: 1. 2. 3. 4.

Evaluate the expressions within the brackets. Evaluate unary operator. Evaluate arithmetic expressions. Evaluate relational expression.

Ex: 100/20=1! =20 5=1! =20 5=1! =20 5=1! =20 0=5>=1! =20 0=1! =20 0! =20 1

Logical operators: Logical operators &&

Priority 2

Associtaivity LR

|| !

3 1

LR LR

Relational and logical expressions: 1. 2. 3. 4. 5.

Evaluate the expression within brackets. Evaluate unary operators. Evaluate arithmetic expressions. Evaluate relational expressions. Evaluate logical expressions.

Ex: Evaluate a+2>b&&! c||a! =d&&a-2b&&!c||a!=d&&a-26&&! 0||11! =7&&11-26&&! 0||11! =7&&9 Expression is an expression evaluated to true or false. Value 1: value to be assigned when the result of expression is true. Value 2: value to be assigned when the result of expression is false. Ex: max= (a>b)? a:b; Whenever „a‟ is greater then „b‟ the value of „a‟ is assigned to max. Otherwise the value of b is assigned to max. Bitwise operators: Ex: AND, OR & XOR Comma operator: Ex: int a=10, b=20, c=40;

Hierarchy of operators: To evaluate any expression we should know the hierarchy or precedence of all types of operators. In C language each operator is associated with priority value. Based on the priority of each operator is predefined in C language. The predefined priority or precedence order given to each of the operator is called precedence of the operator. The operations that are carried out based on the precedence of operators are called hierarchy of operators. The below table shows precedence and associativity of the operators. SL.NO Operator Operators precedence Associativity (highest to lowest) 1 () Inner most brackets/function calls LR 2 [] Array element reference LR 3 Unary operators ++,--,size of(),+,-,&,* RL 4 Member access  or * LR 5 Arithmetic operators *,/,%,+,LR 6 Shift operators > LR 7 Relational operators < , , >=,=, != LR 8 Bitwise operators AND, OR, XOR LR 9 Logical operators &, | LR 10 Conditional ?: RL operators 11 Assignment =,+=,-=,*=,/=,%=,
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF