C Language chapter 1
Communicating with computers involve speaking a language that computer understands.
Computer Language has 4 aspects.
1. How it stores data.
2. How it operates on data.
3. How it handles input and output.
4. How it controls the sequence of execution of instructions in a program.
Background of C language
1. C was developed by AT&T bell laboratories of USA in 1972. It was designed and written by man named Dennis Ritchie.
2. In late 70s C language began to replace the most popular and familiar languages of that time, PL/I, ALGOL, PASCAL, APL, etc.
3. No on pushed C, it was not made official bell labs language. Thus, without any advertisement, C's reputation began to spread, and its pool of users grew.
4. C Language survived more than 3 decades.
5. To learn C++, JAVA, which has classes, objects, inheritance, polymorphism, Templates, exceptional handling and references, it is very important to learn C.
6. To organize the program C++, JAVA make use of principle called OOP - Object oriented programming.
7. Major part of the most popular operating systems like windows, UNIX, LINUX are written in C language.
8. Device driver's programs are exclusively written in C language.
9. Popular gaming frameworks are built using C language.
10. Mobile Devices like cellular phones, palmtops, microwave oven, washing machines device drivers' programs, these smart devices smartness came from a microprocessor, an operating system and a program embedded on these devices. These devices not only have to run fast but also have to work in limited amount of space. With these constraints of time and space, C is the language of choice.
Alphabets: - A to Z, a to z
Digits: - 0 to 9
Constant - It is an entity that does not change.
Variable: - It is an entity that may change.
Keywords -
1. Keywords are the words whose meaning has already been explained to the C compiler (to the computer).
2. They are also called reserved words.
3. keywords are not allowed to use while constructing variable names.
4. There are total 32 keywords available in C language.
01.void
02. main
03. int
04. char
05. float
06. if
07. else
08. do
09. while
10. for
11. switch
12. default
13. continue
14. goto
Popular compiler vendors Microsoft and Borland have provided their own keywords apart from above 32 keywords available in C language. It has been suggested by ANSI committee to vendor specific keywords should be preceded by two underscores. __asm , __near , __far. Not every vendor follows this.
Constants: - It is categorized into 2 parts.
- Primary Constants.
- Secondary Constants.
Primary Constants: -
- integer constant.
- real constant
- character constant.
Secondary Constants: -
- Array
- pointers
- structures
- union
- Enum
- Integer constant must have at least one digit.
- It should not have decimal point.
- It can be either positive or negative.
- Default sign of integer constant is positive.
- If no sign precedes the integer constant, it is assumed to be positive.
- The allowed range of integer constant is -32768 to 32767 for 16-bit compiler.
- No commas or blanks are allowed while constructing integer constant.
- 426, +782, -8000, -7605
Rules for constructing real constant.
- The real constant must have at least one digit.
- It should have decimal point.
- It can be either positive or negative.
- Default sign is positive.
- If no sign precedes the real constant, it is assumed to be positive.
- No commas or blanks are allowed while constructing real constant.
- Example: - +325.34, 426.0, -32.76, -48.5792
The exponential form of representation of real constant has two parts. (mantissa / Exponential)
The part appearing before e is called mantissa.
The part appearing following e is called exponential.
Example - +3.2e-5, -3.2e-5, 4.1e8, -0.2e+3
Rules for constructing exponential form of representation of real constant.
- The mantissa and exponential part must be separated by letter e.
- There should be at least 1 digit in exponential part.
- Default sign of mantissa and exponential part is positive.
- If no sign appears before mantissa part and exponential part, it is assumed to be positive.
- The allowed range of exponential form of representation of real constant is -3.4e38 to 3.4e38.
Rules for constructing character constant.
- The character constant should be either single alphabet, single digit or special symbol enclosed in single inverted commas.
- Both the inverted commas should point towards left.
- Example - 'A', 'a', '1', '2', '+', '-','/', '%'
Compilers- There are two types of compilers.
- 16-Bit compiler
- 32-Bit compiler.
Variables
- It is an entity that may change during the execution of program.
- Variable names are the names given to the location in the memory.
- The memory location may hold integer constant, real constant or character constant.
- The type of variable any language may support, depends on the type of constant it can hold.
- Specific type of variable support the specific type of constant.
- Example-
- An integer variable can hold only integer constant.
- A real variable can hold only real value.
- A character constant can hold only character value.
Rules for constructing Variable Names
- Variable name is any combination of 1 to 31 alphabets, digits or underscore.
- The first character of variable name should be either alphabet or underscore.
- Best practice is to stick with 31 characters.
- Some compilers allow to use 247 characters.
- No comma or blanks are allowed.
- No special symbol other than underscore are allowed to create variable name.
- Example - si_int, m_hra, pop_e_89;
It is always advisable to construct the meaningful variable names.
- Example-
- int si , m_hra;
- float bassal;
- char code;
- %d for integer constant.
- %f for real constant.
- %c for character constant.
- \n for new line.
- printf() is a function for displaying output.
- scanf() is a function for taking input.
- printf("%d%d%d",i,j,k);
- scanf("%d%d%d",&i,&j,&k);
Compilation and Execution
Once we have written the program, we need to type it and instruct the machine to execute it. (converting programs to 0s and 1s i.e. machine language).
To type the program, we need another program called editor.
To execute the program, or converting the program to machine language we need another program called compiler.
Famous compiler vendors like Microsoft, Borland provide IDE [Integrated development environment] which consist of editor and compiler both.
Popular compilers are: -
- Turbo C, Turbo C++, Microsoft C are the compilers work under Ms. DOS.
- Visual C++, Borland C++ are the compilers work under windows.
- gcc works under Linux.
- Microsoft C++ and Borland C++ compilers also contain C compilers bundled with them.
Type of C instructions
- Type declaration instructions.
- Arithmetic instructions
- control instructions.
Type declaration instructions- To declare the type of variables used in C programs.
Arithmetic instructions - To perform the arithmetic operations between variables and constants.
Control Instructions - To control the sequence of executions of various statements in C program.
Integer and float conversion
In order to effectively develop C programs, It will be necessary to understand the rules that are used for implicit conversion of floating point and integer values in C.
- An arithmetic operation between an integer and integer always yields an integer result.
- An arithmetic operation between a real and a real always yields a real result.
- An arithmetic operation between an integer and real always yields a real result. In this operation integer value is first promoted to real value and then operation is performed, so result is real.
Type Conversion in assignment
It may happen that type of expressions (consonants) and type of variable on the left-hand side of the assignment operator may not be same. In such cases value of the expression is promoted or demoted depending on the type of the variable on left hand side of the assignment operator.
Example:
int i;
float b;
i=3.5;
b=30;
In the first assignment sine I is an integer and value is in the float type , value is demoted to integer and assigned to i.
Int i=3.5 [Wrong] after demoted the value based on the type integer, assignment would be like ,
int i=3 [Correct].
In the second assignment since b is float and value is the integer, float b cannot store integer value, here value is promoted to float type and assigned to variable b.
float b = 30.000000 [correct]
ASSUME K IS AND INTEGER AND A IS FLOAT TYPE
k = 2 / 9 ;
k = 2.0 / 9 ;
In the first expression since 2 and 9 are integers so arithmetic operations performed and result is stored like an integer value. 2/9=0.2222 but k is an integer value so 0 is stored in K.
In the second expression , 2.0 divide by 9 here arithmetic operations cannot be performed , first 9 should be promoted to 9.0 then arithmetic operations performed. 2.0/9.0 yields results 0.4444 , again K is the integer so value should be demoted to integer and then stored in integer variable k. 0.4444-> 0 and Int k=0;
Hierarchy of Operators
While executing an arithmetic statement, which has two or more operators, we should understand how exactly it will get executed.
Eg.
2*x-3*y = (2*x)-(3*y)
A/B*C = ?
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 shown below.
(a) within parenthesis () same hierarchy mentioned above is applicable.
If there are more than one parenthesis in an arithmetic statement, the operations within the innermost parenthesis would be performed first followed by second innermost pair and so on.
We should use pair of parenthesis (). A careless imbalance of right and left parenthesis is a common error. Best way to avoid this error is to use () and then type expression inside it.
Associativity of operators.
When an expression contains two operators of equal priority, the tie between them is settled using associativity of operators.
- Left to right associativity. - Means left operand is unambiguous.
- Right to left associativity. - Means right operand is unambiguous.
Unambiguous - It must not be involved in evaluation of any other sub-expression.
Instructions in C language
- Type declaration instructions.
- Arithmetic instructions.
- Control instructions.
Control instructions in C language control the sequence of execution of various statements in a C program.
Control instructions determine the "flow of control" in a program.
There are 4 types of instructions in C language.
- Sequence control instruction.
- Selection or decision control instruction.
- Repetition or loop control instructions.
- Case Control instructions.
(1). Sequence control instructions- Ensures that the instructions are executed in the same order in which they appear in the program.
(2 and 4) Selection or Decision or Case control instructions - allows the computer to take decision as to which instruction is to be executed next.
(3)Loop Control Instructions - Loop control instructions helps the computer to execute a group of statements repeatedly.
Summary: -
- There are 3 types of primary constants and variables in C
- Integer
- float
- character.
- A variable name can be of maximum 31 characters.
- Do not use keywords (32) while constructing variable names.
- An expression may contain any combination of constant, variables and operators.
- Operators having equal precedence are evaluated using associativity.
- Left to right associativity. - Operand to the left of the operator should be unambiguous. {not open for more than one interpretation.}
- Right to left associativity. - Operand to the right of the operator should be unambiguous. {not open for more than one interpretation.}
- Input and output in C language is achieved using scanf() and printf() functions.
Comments
Post a Comment