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

C Programming Language

0.1 INTRODUCTION
1. INTRODUCTION C is a programming language developed at AT & Ts Bell Laboratories of USA in 1972. It was designed and written by a man named Dennis Ritchie. Its popular is because it is reliable, simple and easy to use. SPECIAL FEATURES OF C Middle Level Language Structured Programming Language Modularity Portability Powerful Data Structures The Efficiency STEPS LEARNING IN C Instead of straight-away learning how to write programs. We must first know what alphabets, numbers and special symbols are used in C, Then how using those constants, variables and keywords are constructed. Finally how are these combined to form an instruction? A group of instructions would be combined later on to form a program. This is illustrated in the Figure Alphabets Digits Special symbols Constants Variables Keywords Instructions Programs

THE C CHARACTER SET A character denotes any alphabet, digit or special symbol used to represent information. Table shows the valid alphabets, numbers and special symbols allowed in C. Alphabets Digits Special symbols A, B Y, Z, or a, b, , y, z 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ~!@#%^&*()_-+=|\{}[]:;"'<>,.?/

2. KEYWORDS The keywords are also called Reserved Words. There are only 32 Key words available in c They are as follows The keywords cannot be used as variable name, if we do so we are trying to assign a new meaning to the keyword which is not allowed. Vivekanand. M. Bonal 1

C Programming Language
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

3. CONSTANTS A Constant is an Entity (Quantity) that doesnt change. The Entity can be stored in the memory of the Computer. TYPES OF C CONSTANTS C constants can be divided into two major categories: 1. Primary Constants 2. Secondary Constants These constants are further categorized as shown in Figure 1. PRIMARY CONSTANTS a. Integer Constants b. Real Constants c. Character Constants 2. SECONDRY CONSTANTS a. Array b. Pointer c. Structure d. Union e. Enum RULES FOR CONSTRUCTING INTEGER CONSTANTS An integer constant must have at least one digit. It must not have a decimal point. It can be either positive or negative. If no sign precedes an integer constant it is assumed to be positive. No commas or blanks are allowed within an integer constant. The allowable range for integer constants is -32768 to 32767. RULES FOR CONSTRUCTING REAL CONSTANTS Real constants are often called Floating Point constants. The real constants could be written in two formsFractional form and Exponential form. Following rules must be observed while constructing real constants expressed in fractional form: A real constant must have at least one digit. Vivekanand. M. Bonal 2

C Programming Language
It must have a decimal point. It could be either positive or negative. Default sign is positive. No commas or blanks are allowed within a real constant. Ex.: +325.343 426.089 -32.764 RULES FOR CONSTRUCTING CHARACTER CONSTANTS A character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas. Both the inverted commas should point to the left. For example, A is a valid character constant whereas A is not. The maximum length of a character constant can be 1 character. Example: 'A' 'I' '5' '=' 4. VARIABLES A Variable corresponds to an area of memory which can state a value of given type. The value of contained can be modified during the program execution. An entity that may vary during program execution is called a variable. Variable names are names given to locations in memory. These locations can contain integer, real or character constants. In any language, the types of variables that it can support depend on the types of constants that it can handle This is because a particular type of variable can hold only the same type of constant. EXAMPLES - An integer variable can hold only an integer constant. A real variable can hold only a real constant A character variable can hold only a character constant.

RULES FOR CONSTRUCTING VARIABLE NAMES A variable name is any combination of 1 to 31 alphabets, digits or underscores. Some compilers allow variable names whose length could be up to 247 characters. Still, it would be safer to stick to the rule of 31 characters. Do not create unnecessarily long variable names as it adds to your typing effort. The first character in the variable name must be an alphabet or underscore. No commas or blanks are allowed within a variable name.

Vivekanand. M. Bonal

C Programming Language
No special symbol other than an underscore (as in gross_sal) can be used in a variable name. Ex.: si_int m_hra pop_e_89 These rules remain same for all the types of primary and secondary variables. 5. C INSTRUCTIONS There are basically THREE types of instructions in C: 1. Type Declaration Instruction 2. Arithmetic Instruction 3. Control Instruction 1. TYPE DECLARATION INSTRUCTION To declare the type of variables used in a C program. Any variable used in the program must be declared before using it in any statement. The type declaration statement is written at the beginning of main( ) function. There are several subtle variations of the type declaration instruction. While declaring the type of variable we can also initialize it as shown below. int i = 10, j = 25 ; float a = 1.5, b = 1.99 + 2.4 * 1.44 ; The order in which we define the variables is sometimes important sometimes not. For example, int i = 10, j = 25 ; is same as int j = 25, j = 10 ; 2. ARITHMETIC INSTRUCTION To perform arithmetic operations between constants and variables. A C arithmetic instruction consists of a variable name on the left hand side of = and variable names & constants on the right hand side of =. The variables and constants appearing on the right hand side of = are connected by arithmetic operators like +, -, *, and /. 3. CONTROL INSTRUCTION To control the sequence of execution of various statements in a C program As the name suggests the Control Instructions enable us to specify the order in which the various instructions in a program are to be executed by the computer. In other words the control instructions determine the flow of control in a program. There are FOUR types of control instructions in C. They are: a. Sequence Control Instruction b. Selection or Decision Control Instruction Vivekanand. M. Bonal 4

C Programming Language
c. Repetition or Loop Control Instruction d. Case Control Instruction The Sequence control instruction ensures that the instructions are executed in the same order in which they appear in the program. Decision and Case control instructions allow the computer to take a decision as to which instruction is to be executed next. The Loop control instruction helps computer to execute a group of statements repeatedly. 6. RECEIVING INPUT To make the program general the program it should ask the user to supply the values of p, n and r through the keyboard during execution. This can be achieved using a function called scanf( ). This function is a counter-part of the printf( ) function. printf( ) outputs the values to the screen whereas scanf( ) receives them from the keyboard. This is illustrated in the program shown below. EXAMPLE /* Calculation of simple interest */ /* Author gekay Date 25/05/2004 */ main( ) { int p, n ; float r, si ; printf ( "Enter values of p, n, r" ) ; scanf ( "%d %d %f", &p, &n, &r ) ; si = p * n * r / 100 ; printf ( "%f" , si ) ; } The first printf( ) outputs the message Enter values of p, n, r on the screen. Here we have not used any expression in printf( ) which means that using expressions in printf( ) is optional. Note that the ampersand (&) before the variables in the scanf( ) function is a must. & is an Address of operator. It gives the location number used by the variable in memory. When we say &a, we are telling scanf( ) at which memory location should it store the value supplied by the user from the keyboard. Note that a blank, a tab or a new line must separate the values supplied to scanf( ). Note that a blank is creating using a spacebar, tab using the Tab key and new line using the Enter key. This is shown below: Ex.: The three values separated by blank Vivekanand. M. Bonal 1000 5 15.5 5

C Programming Language
Ex.: The three values separated by tab. Ex.: The three values separated by newline. 1000 5 15.5 1000 5 15.5 7. RULES FOR C PROGRAM Each instruction in a c program is written as a separate statement therefore a complete c program world complies a series of statement. All statement is entered in small case letter. C has no specific rules for the position which a statement is to be written thats why it is often called a free from language. Every C statement must end with a ; Semicolon. It acts as a statement terminator. 8. COMPILATION AND EXECUTION Assuming that you are using a Turbo C or Turbo C++ compiler here are the steps that you need to follow to compile and execute your first C program 1. Start the compiler at C> prompt. The compiler (TC.EXE is usually present in C:\TC\BIN directory). 2. Select New from the File menu. 3. Type the program. 4. Save the program using F2 under a proper name (say Program1.c). 5. Use Ctrl + F9 to compile and execute the program. 6. Use Alt + F5 to view the output.

*************************

Vivekanand. M. Bonal

C Programming Language

0.2 C OPERATORS
Operators can be classified on the case of operation that they perform or based on the number of operands it operators on. There are three types of Operands Unary, Binary & Ternary. CLASSIFICATIONS OF C OPERATORS 1. Arithmetic Operators 2. Relational Operators 3. Logical Operators 4. Assignment Operators 5. Increment/ Decrement Operators 6. Bit wise Operators 1. ARITHMETIC OPERATORS Addition Subtraction Multiplication Division Modular division or Modulas EXAMPLE:

+ * / %

C can handle any complex expression with ease. Some of the examples of C
C EXPRESSION

expressions are shown below.


ALGEBRAIC EXPRESSION

axbcxd (m + n) (a + b) 3x2 + 2x + 5 INTEGER AND FLOAT CONVERSIONS

a*bc*d (m + n) * (a + b) 3*x*x+2*x+5

An arithmetic operation between an integer and integer always yields an integer result. An operation between a real and real always yields a real result. An operation between an integer and real always yields a real result. In this operation the integer is first promoted to a real and then the operation is performed. Hence the result is real.

I think a few practical examples shown in the below would put the issue beyond doubt.
C OPERATION C RESULT C OPERATION C RESULT

5/2 5.0 / 2 5 / 2.0 5.0 / 2.0 Vivekanand. M. Bonal

2 2.5 2.5 2.5

2/5 2.0 / 5 2 / 5.0 2.0 / 5.0

0 0.4 0.4 0.4 7

C Programming Language
RELATIONAL OPERATORS It is used to check the relation between two operands. The relational operators take two operands compare them and returns a value of 1 or 0 depending on whether the tested relation was true or false. Less than Less than or Equal Greater than

< <= >

Greater than or Equal Equal to Not Equal

>= == !=

Equal to & not equal to be more specifically called as Equality operators. LOGICAL OPERATORS Logical AND && Logical OR

||

Logical NOT

Not takes one operand (unary) && and || are used to combine the conditions. ASSIGNMENT OPERATORS The assignment operator is used to assign the value to a variable.

Variable = Expression;
Expression always should be in right hand side. In C the equal sign is used as the assignment operator. INCREMENT/ DECREMENT OPERATORS Prefix form ++i increments i & returns the incremented value. ++i i++ ADD ADD 1 to i 1 to I if i=1 then result i=2 if i=1 then result i=1

Postfix form i++ increments i but returns prior, non incremented Value -- i Decrements i & returns the decremented value. BIT WISE OPERATORS Bitwise AND Bitwise OR Bitwise XOR Bitwise NOT Shift Left Shift Right

& | ^ ~ << >>

Used for clearing the Bit. Its for setting the Bit. Used for security propose & Encryption & Decryption. its for complimenting It shifts left by multiply by 2 for the position It shifts right by Dividing by 2 for the position

*********************** Vivekanand. M. Bonal 8

C Programming Language

0.3 Decision control structures

There are THREE types in decision control Structures. 1. If Statement 2. If else Statement 3. Conditional Operator 1. IF STATEMENT The General form of If Statement if( Condition is True) { Execute this statement; } The keyword if tells the compiler that what follows is a decision control instruction. The condition following the keyword if is always enclosed within a pair of parentheses. If the condition, whatever it is, is true, then the statement is executed. If the condition is not true then the statement is not executed; instead the program skips past it. 2. IF ELSE STATEMENT The General form of If else Statement if( Condition is True) { Execute this statement; } else { Execute this Statement; } if the expression evaluates to true and another group of statements if the expression evaluates to false? Of course! This is what is the purpose of the else statement 3. CONDITIONAL OPERATOR The conditional operator ? & | are sometimes called Ternary Operator Since they take 3 arguments Conditional operation General form is

? T: F;

Exp1? Exp2:Exp3;

What this expression says is: if expression 1 is true (that is, if its value is non-zero), then the value returned will be expression 2, otherwise the value returned will be expression 3.

************************* Vivekanand. M. Bonal 9

C Programming Language

0.4 LOOP control structures


There are THREE types of loop control structures 1. For loop Statement 2. While loop Statement 3. Do-While loop Statement 1. FOR LOOP STATEMENT It allows specifying the 3 things in a single line. i. ii. iii. Initialization Expression/ Condition Update

General form of for loop is for (initialization ; condition ; Update) These 3 things are separated by semicolon.
INITIALIZATION: is an assignment statement that is used to set the loop control. EXPRESSION: Testing the loop counter to determine whether value is has reached the

number of repetitions desired.


UPDATE: Increasing the value of loop counter each time the program segment within the

loop has been executed.


EXAMPLE: for ( i=1; i<=10; i++).

Above Ex: ( i=1 is initialize counter ; i<= is test counter ; i++ increment counter )

2. WHILE LOOP STATEMENT The general form of while is as shown below: while ( test loop counter using a condition ) { Statement 1; Statement 2; Update; } The statements within the while loop would keep on getting executed till the condition being tested remains true. When the condition becomes false, the control passes to the first statement that follows the body of the while loop. In place of the condition there can be any other valid expression. So long as the expression evaluates to a non-zero value the statements within the loop would get executed. Vivekanand. M. Bonal 10 / Increment or Decrement Operation

C Programming Language
3. DO WHILE LOOP STATEMENT The general form of do while is as shown below: do { Statement 1; Statement 2; } while ( test loop counter using a condition ); This means that do-while would execute its statements at least once, even if the condition fails for the first time. Its same as while but with the test condition at the bottom of the loop. DIFFERENCE BETWEEN THE WORKING OF WHILE AND DO- WHILE LOOPS. This difference is the place where the condition is tested. The while tests the condition before executing any of the statements within the while loop. As against this, the do-while tests the condition after having executed the statements within the loop. do-while would execute its statements at least once, even if the condition fails for the first time. The while, on the other hand will not execute its statements if the condition fails for the first time
NOTE: BREAK STATEMENT

The break statement will helps to come out of the loop or switch statement. The break statement doesnt work with if statement. When using Break statement its nice to write the enclosing loop to iterate in the straightest forward.

***********************

Vivekanand. M. Bonal

11

C Programming Language

0.5 ARRAYS
C Programming provides a capability that enables a user to define a set of similar data types called Array. An Array is a fixed sized sequenced collection of elements of the same data types. An Array is a collection of similar elements. Its elements of array are stored in the contiguous memory locations. Array is classified into THREE types. 1. One Dimensional Array 2. Two Dimensional Array 3. Three Dimensional Array 1. ONE DIMENSIONAL ARRAY A list of names can be given on variable using one subscript & such a variable is called One Dimensional Array or Single Subscripted Variable.
EXAMPLE: int Number [5];

Number [0] = 1; Number [1] = 2; Number [2] = 3; Number [3] = 4; Number [4] = 5;

N[0] N[1] N[2] N[3] N[4]

1 2 3 4 5

A. INITIALIZATION/ DECLARATION OF AN ARRAY OR To begin with, like other variables an array needs to be declared so that the compiler will know what kind of an array and how large an array we want. Declaration of an array in General Form: int marks[30]={2,4,3,5,6}; Here, int specifies the type of the variable, just as it does with ordinary variables and the word marks specifies the name of the variable. N [5] this 5 number is often called the dimension of the array. The bracket [ ] tells the compiler that we are dealing with an array. B. ENTERING DATA INTO AN ARRAY Here is the General format that places data into an array: for ( i = 0 ; i <= 29 ; i++ ) { printf ( "\nEnter marks " ) ; scanf ( "%d", &marks[i] ) ; }

Vivekanand. M. Bonal

12

C Programming Language
C. READING DATA FROM AN ARRAY Here is the General format that Reading data from an array: for ( i = 0 ; i <= 29 ; i++ ) { printf ( "%d, a[i]); } 2. TWO DIMENSIONAL ARRAY The two dimensional array is also called as Matrix. The two dimensional array contain Rows & Columns begins with Zero only. A. INITIALIZATION/ DECLARATION OF AN ARRAY OR Declaration of an array in General Form: int a[row][column];
EXAMPLE: int a [r][c];

C[0] C[1] 2 3 4 5

R [0] = 1; R [1] = 2; R [2] = 3; R [3] = 4; B. ENTERING DATA INTO AN ARRAY

Here is the General format that places data into an array: for ( r = 0 ; r <= R ; r++ ) { for ( c=0; c<= C; c++) scanf ( "%d", &a[r][c] ) ; } C. READING DATA FROM AN ARRAY Here is the General format that Reading data from an array: for ( r= 0 ; r <= R ; r++ ) { for ( c= 0 ; c <= C ; c++ ) { printf ( "%d, a[r][c]); } }

************************* Vivekanand. M. Bonal 13

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