Review of C Programming
Review of C Programming
INTRODUCTION
In 1972, the Dennis Ritchie at Bell Labs developed C language incorporating the best feature of both BCPL and B languages. WHY IS C LANGUAGE POPULAR? 1. C is a machine independent and highly portable language. 2. It is easy to learn. 3. C language allows manipulation of BITS, BYTES and ADDRESSES. 4. It has a rich set of operators to solve scientific and business applications. 5. User can create their own functions and add to C library. 6. It has a large library of functions.
C BASICS
From this section onwards we discuss Review of C programming, that you have learnt in your previous year. This chapter gives you an overview of C language and tells about various components of a C program, such as character set, keywords, constants, variables, data types, basic input-output statements, decision statements etc. The review of C programming section will definitely help you to brush up your C knowledge acquired so far.
C LANGUAGE COMPONENTS
The four main components of C language are: 1. The character set 2. Tokens a. Keywords b. Identifiers c. Constants d. Operators 3. Variables 4. Data types
Operators
An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation on data stored in variables. The variables that are operated are termed as operands. C operators can be classified into the following types
1. 2. 3. 4. 5. 6. 7. 8. 9. Arithmetic operators Relational operators Logical operators Conditional operators (Ternary) Increment and decrement (Unary) Bitwise operators Shorthand operators Assignment operators Special operator +.-.*,/,% <,<=,>,>=,==,!= !, &&, || ?: ++,-!, &,|,~,^,<<,>> +=,-+,*=,/=,%= = sizeof, , (comma)
Variables A variable is an object or element that may take on any value of a specified type. Variables are nothing but identifiers, which are used to identify various programming elements. Each variable in reality represents the name of a memory location in which a value can be stored. They are used to denote constants, arrays, functions, structures, unions, files and may other such element used in a program.
Data types Data types indicate the type of data a variable can have. A data type requires us to know. a. Its representation in the program. b. Memory allocated for it. c. The range of values that can be used. d. The possible operations that can be performed. Data types are classified as i. Simple or Fundamental data types (int, float,double, char, long int , short int and unsigned) ii. Derived data types (Derived data include arrays, functions, pointers, references, constants, structures, unions and enumerations)
printf (Enter the radius of a circle); scanf (%d, &r); a = PI * r * r; c = 2 * PI * r; printf ( Area = %f, a); printf (Circumference = %f,c); }
i. ii.
Example
i. ii. iii.
TYPES OF STATEMENTS
They are, i. ii. iii. iv. v. vi Null statement Expression statement Compound statement Selection statement Looping statement Jumping statement
nested-if statement Format: if (expression1) if (expression 2) statement else statement else if (expression 3) statement else statement
1; 2; 3; 4;
switch(control expression) {
case case-label-1 :
statement-list-1; break; case case-label-2 : statement-list-2; break; ...... default : default block;
Looping statement
The concept of loops is fundamental to structured programming. A loop is a program construct that causes a statement to be executed again and again. The process of repeating the execution of a certain set of statements again and again is termed as looping. C has several such statements that can be used to form loops they are
i. ii. iii.
THE while STATEMENT while (test condition) { Statement 1; Statement 2; .. } Statement n+1;
THE do-while STATEMENT do { Statements 1; Statements 2; .. } while (test condition); Statements n+1;
THE
for
STATEMENT
for(Expression 1;Expression 2;Expression 3) { Statements 1; Statements 2; .. } Statements n+1; Where 1. Expression 1 represents the initialization expression. 2. Expression 2 represents the expression for the final condition. 3. Expression 3 represents the increment or decrement expression.