C LANGUAGE
*) Introduction:-
C was an offspring of the ‘Basic Combined Programming Language (BCPL) called B , developed in the 1960’s at Combridge University .B language was modified by Dennis Ritchie And was implement at Bell Laboratories in 1972. The new language was named C. It was written originally for programming under an operating system UNIX which itself was later written in entirely in C.
*) Important Feature of C :-
There are only 32 keywords and its strength lies in its built-in-function. several standard function are available which can be used for developing programs.
Some of the main features of C are:-
1) Portability:-
Programs written in C Language can be used in different environments by either slight modification or without modifaction. Different environment means different computer system and different o/s.
2) Efficiency:-
Knowledge about the efficiency of a language can be taken by the time taken in execution after computation by the program written in that language and by the space covered by that program. Its efficiency is always high. These program take less space and less time in execution.
3) Versatility:-
C language is a versatile language of the different type of data type and operatars of C language.
4) Flexibility :-
C is a high level language but it also has the feature of low level language. This is the basic reason that C language Can be used for any type of job. So C is a flexible language.
5) Capacity of to extend itself:-
C language has the capability to extend it library. Library of C language is extended by adding functions developed by programmers to the library of C.
6) Divided in modules:-
In C language large program are made easier by dividing the large programs into modules debugging, testing and modification of program become easier.
7) Working with bit (1 or 0):-
C language has bitwise operators which marks easier to work with bits. We can do bitwise OR, bitwise AND etc.
8) Pointers:-
C language has the facility to work with Pointers. So to define and use of data structure become easier in C.
*) Structure of C program:-
C Program is written by using different function. Each function is a group of statements which does a special work.
Structure of the program will be as follows.
Comments
Pre- processor directives
Global Variables
Main Function
{
Local Variables
Statements
}
In C program comments are used to make documentation. Comments are ignored by the compiler comments are written between /* */.
Preprocessor directives are executed before compilation by the compiler. Preprocessor directives gives some instruction to the computer.
# include and # define are the examples of preprocessor directives. # include gives the information about the files and header files used in the program, # define is used to inform the macro name to the compiler.
In a program same variables are used in approximately every function. Such variable are called global variables. C program written by different by using function but every program has main ( ) function. It is necessary to write main ( ) function one and only one time in the program.
Other type of functions are defined by the user which are called in main ( ) function or in any user defined function. Middle brackets { } defines the limits of the function.
*) Some Important steps:-
1) To separate the statements semicolon (;) is used which is indication of end of statement.
2) C language is a case sensitive language i.e. upper case & lower case are considered different in C language.
*) Character set:-
Character set of C language contain upper and lower alphabets, number and special symbols. Alphabets and numbers in the combined form are called alphanumeric characters.
e.g. A-Z, a-z, 0-9.
*) Reserve word:-
World whose meaning and use in C language is pre decided are called reserve words. These words have particular meaning for C language. As use of these words are also fix they can’t be used by the user in other purpose. C language has total 32 reserve words.
*) Identifiers:-
At the time of working on computer datas are in main memory of computer gives separate symbolic name of area of their memory. There symbolic name are called identifiers.
There are certain rules for these identifiers.
1) Identifiers should start with an alphabet or underscore ( _ ).
2) After first character alphabet or underscore or number may be used.
3) These names can’t be the same as reverse word.
4) C is case sensitive so generally lower case is used.
*) Constants:-
Identifiers whose value remains same in whole program are called constants. Constant are of four types-
Constant are defined by using const
1) Numeric constants
2) Character constants
3) String constants
4) Enumerated constant
1) Numeric Constants:-
Numeric constant are made up digits (0 to 9) in which there may be or may not be decimal. There must be minimum one digit in numeric constant. No comma or blank space is allowed in between numeric constant. Numeric constants may be positive or negative.
These constant are of two types-
a) Integer Constant:-
Integer constant refers to the sequence of digits. There are three types of integer namely decimal, octal and hexadecimal.
1) Decimal integer consists of a set of digits preceded by an optional - or + sign.
Embedded spaces, commas and non digit characters are not allowed between digits.
2) Octal integer consists of any combination of digits from the set 0 through 7.
3) Hexadecimal constant consists of digits (0 – 9) and alphabets (A – F).
To store large integer constants on there machine by appending qualifiers such as U, L and UL to the constant.
U :- for unsigned integer.
UL:- for unsigned long integer.
L :- for long integer.
b) Real constant :-
Integer number are inadequate to represent qualities that very continuously such as distances, heights, temperatures, prices and so on. These quantities re represented by numbers are called real constant.
Ex. 0.5, -235.5, +.97, 5E8, 0.4561e6.
2)Characters constant:-
Character constant is single character which is written in the single quotes ‘ ‘.
Ex. ‘x’, ‘y’, ‘z’.
3) String constant:-
A string constant is a sequence of characters enclosed in double quotes. The characters may be letters, numbers, special characters and blank space. In C language at the time of storage of string ‘\0’ is called NULL character.
4) Enumerated constant:-
Enumerated constant are of user defined type. It has a set of fix values. Value of identifies can be any one out of this set.
Ex. Enum color (blue, red, green, yellow).
In this ex value of color can be either blue or red or green or yellow.
*) Variable:-
A variable is a data name that may be used to store a data value. Their value can be changed with in the program but at a time one variable can have only one value.
Some rules for variable name.
1) first character of a variable name can be either alphabet or underscore ( _ ) only.
2) Variable name can’t be a reserve word or underscore may be possible.
3) After first character alphabet, digit (0 to 9) or underscore may be possible.
4) No –special character or blank space is allowed within the name of variable.
5) ANSII standard recognize a length of 31 character. However the length should not be normally more than eight characters.
*) Data types:-
C language supports different type data. The data type are represented in memory in different ways. There are four data types basically in C.
1) int:- for the storage of integer value.
2) char:- one byte which store one character.
3) float:- floating point number of single precision.
4) double:- floating point number of double precision.
Data type | Size | Limits |
Char or signed char | 1 | -128 to +127 |
Unsigned char | 1 | 0 to 255 |
Int or signed int | 2 | -32768 to +32767 |
Unsigned int | 2 | 0 to 65535 |
Short int / signed short int | 1 | -128 to +127 |
Unsigned short int | 1 | 0 to 255 |
Long int / signed long int | 4 | -2147483648 to +2147483647 |
Unsigned long int | 4 | 0 to 4294967295 |
Float | 4 | 3.4E-38 to 3.4E+38 |
Double | 8 | 1.7E-308 t0 1.7E+308 |
Long double | 10 | 3.4E-4932 to 1.1E+4932 |
*) Escape sequence:-
When we press a character on the keyboard it is display on the screen but some special character are used with backslash ( \ ). This combination is used for special function and are called escape sequence. For example “\n” means new line.
Meaning | Escape sequence | ASCII value |
Bell (alert) | \a | 001 |
Back space | \b | 008 |
Horizontal tab | \t | 011 |
Vertical tab | \v | 010 |
New line | \n | 012 |
Form feed | \f | 013 |
Carriage return | \n | 014 |
Quotation mark | \” | 043 |
Apostrophe | \’ | 039 |
Question mark | \? | 063 |
Back slash | \\ | 092 |
NULL | \0 | 000 |
*)Qualifiers:-
Words which are written before type of a variable and changes the nature of the variable are called qualifiers.
Ex. Int k
Qualifiers are of two types:-
1) size qualifiers:-
It change the size of the type of data. Generally there are two size of qualifiers.
a) short , b) long.
Ex. Sort int , long int, long double.
2)Size qualifiers:-
Size qualifiers indicates the sign of the type of a data. Following are the two sign qualifiers.
a) signed , b) unsigned.
Ex. Unsigned int , unsigned float, signed int.
*) Output function printf():-
Output data or information is taken from the memory of computer system and given to the statndard output device or output file. C library has a function printf () which sends the data from computer memory to the standard output device. Printf () function is written in following ways :-
Printf(“Control string”,variable1.variable2,…………);
Variable1 and variable2 are those variable whose values are send to the output device. Control string is always written in the double quotes ( “). This is a sequence and format in which value of variable1, variable2,…..etc are written on to the output device.
Format | Type of value of the variable |
%c | Character |
%d | Decimal integer |
%e | Floating point value (Exponent form ) |
%f | Floating point |
%lf | Double floating point |
%lF | Long double floating point |
%l | Long integer |
%h | Short integer |
%o | Octal integer |
%x | Hexadecimal |
%s | String |
%u | Unsigned decimal integer |
*) Input function scanf():-
Value are taken from the standard input device and given to the different variables in the program. In the library of c language there is a function scanf() which takes the data from keyboard and allot than to the variable of program. Scanf() function is written in the following way:-
Scanf(“Control string”,&variable1,&variable2,…………”);
In scanf() function above &variable1,&variable2 are the address of the those variable where data from the keyboard in to be placed. & symbol before variable indicates that this is the address of that variable not the value of it.
*) Operators:-
An operators is a symbol that tells the compiler to perform certain mathematical or logical manipulations. Operators are used in programs to manipulate data and variable.
*) Types of operators:-
1) Arithmetic operators.
2) Relational operators.
3) Logical operators.
4) Conditional operators.
5) Increment & decrement operators.
6) Bitwise operators.
7) Assignment operators.
8) Other operators.
1) Arithmetic operator:-
Arithmetic operators are used for numerical calculation. Arithmetic operators are divided into two parts:-
a) Unary operators:-
Operators which need only one operand for the operation are called unary operators.
Ex: +a,-b
Here minus and plus are the unary operator.
b) Binary operator:-
Operators which need two operand for the operation are called binary operators.
Operators | Meaning | Object |
+ | Plus | Addition |
- | Minus | Subtraction |
* | Asterisk | Multiplication |
/ | Slash | Division |
% | Modulus | Remainder |
| | |
2) Relational Operator|:-
They are used for comparison . All these operator need two operands. If on comparison result is true then it return 1 if result is false then it return 0.
Operator | Meaning |
< | Less than |
<= | Less than equal to |
> | Greater |
>= | greater than equal to |
== | Equals to |
!= | Not equal to |
3) Logical operator:- They works on two operands . These operators may be logical or relational expressions. Result of logical operation are always either true or false.
Operator | Meaning |
&& | AND |
|| | OR |
! | NOT |
AND Operator:-
The result of logical AND will be true if both the operands are true. If any one of the operand or both the operand are false then result of AND will be false.
OR Operator :-
The result of logical operator OR will be true if anyone the operands of it are true. If both the operands are False than only OR will give the result as false.
NOT Operator:-
It is an unary operator. So NOT operator need only one operand. If operand is false than it change it to true and if operand is true than it will change it to false.
4) Conditional operator (? And :):-
This is a ternary operator. So in this operator these are three operators. This is written as following:-
Condition ? operand 1 : operand 2
In this operator firstly the condition is checked. If this condition is true operand 1 is taken and if it is False operand 2 will be taken.
5) Increment and Decrement operator ++ and -- :-
In C language two useful operator are given:-
1) increment ++
2) decrement –
Both these operator are unary operator so they need only one operand. Increment operand ++ increase the value of variable by 1. decrement operator -- decrement the value of variable by 1.
These operators are written as following :-
1) ++ variable or variable ++
2) -- variable or variable --
1) ++ variable:-
If ++ operator is placed before the variable then the value of
that variable is increased by 1 and then it is used in the
processing.
2) variable ++:-
If ++ operator is used after variable then first the value of
that variable is used in processing and then increased by 1.
3) -- variable :-
If – operator is used before the variable then first the value
of that variable is decreased by 1 and then it is used in
processing.
4) variable -- :-
If --operator is placed after variable then first the value
of variable is used in processing and then decreased by 1.
6) Bitwise operator :-
In c language data can be used in bitwise processing. Bitwise operators are used to process on bits. Bitwise operators work on integer only.
Bitwise operator | Meaning |
& | Bitwise AND |
| | Bitwise OR |
~ | One’s complement |
<< | Left shift |
>> | Right shift |
^ | Bitwise XOR |
7) Assignment operator:-
To assign a value to a variable assignment operator is used. This is represent by ‘ = ‘ symbol. This operator evaluates its right hand side and assign it to the variable written on L.H.S. The right hand side of this operator may be an expression.
Ex. x=x+4 or x+=4
x=x-4 or x-=4
8) Special operator :-
C supports some special operator of integer such as comma, sizeof operators, pointer operator and member selection operator.
Comma operators:-
This is used to link the related expression together.
Sizeof operator :-
It is a compile time operator and used with an
operand, it returns the number of byte the operand
occupies.
This is normally used to determines the length of arrays and structure when their size are not known to the programmer. It is also used to allocate memory space dynamically to variables during the execution of a program.
*) Type Conversion / Type Cast :-
To convert a type of variable to desired data
type. Two types of type casting are there
1) internal / implicit.
2) Expernal / Explicit.
In internal type casting system automatically converts the type of an identifier for example:-
x=a*2.5;
x=a/(float)b;
here b is converted or type cast to float to perform float division and a is automatically converted or type cast to float.
*) Operator Precedence and Associativity:-
Each operator in ca has a precedence associated with it. This precedence is used to determine how an expression involving more than one operator is evaluated. There are distinct levels of precedence and an operator may belong to one of the levels. The operators at the higher level of precedence are evaluated first. The operator of the same precedence are evaluated either form left to right or right to left, depending on the level. This is known as Associativity property of an operator. It is very important to not carefully the order of precedence and associativity of operators.
Precedence Group Operator Associativity Function, Array, Struct Member, Pointer ( ), [ ], . , à L à R Unary operator -, +, |, ~, ++, --, &, *, (typecast), sizeof R à L Mathematic Multiplication, Divide & Remainder *, /, % L à R Arithmetic Add & Sub +, - L à R Bitwise operator <<, >> L à R Relational operator <,<=,>,>= L à R Equality operator ==, != L à R Bitwise AND & L à R Bitwise XOR ^ L à R Bitwise OR | L à R Logical AND && L à R Logical OR || L à R Conditional operator ? : R à L Assignment operator =, +=, -=, /=, %=, >=, <=, <<=, !=,*=, &=, >>= R à L Comma operator , L à R |
*) Instruction or Expression :-
It is a combination of operator and operands. If the operator is binary operator and it evaluates the numeric value if the operators are arithmetic operator or it will evaluate a Boolean value (T or F ) / ( 1 or 0) if the operators are logical / relational operators.
In c language the Boolean values are 1 and 0, 1 for true and 0 for false. Thus in both the case an expression will evaluate a numeric values.
C is a structured language.
1) One line. ( printf, scanf, declaration of data type, function )
2) Conditional.( if else, ? :, switch )
3) Loop. ( for, do while, while )
Q). Difference between & and && ?
Ans.
& | && |
1) It means the address of variable. | It means the AND Logical operator. |
2) Unary operator. | Binary Operator and Logical Operator. |
3) Ex. scanf(“%d”,&a); | Ex. if(a>5 && b<10) |
Managing input and output operations
*) Stdio.h :-
The file name stdio.h is an abbreviation for standard input – output header files. The instruction # include <stdio.h> tolls the compiler to search for a file named stdio.h and place its contents at this point in the program.
*) Formatted input :-
Formatted input refers to an input data that has been arranged in a particular format. This can be done using scanf function. The general form of scanf is
Scanf(“ Control string “,arg 1,arg 2,……….arg n);
Here, the control string specifies the field format in which the data is to be entered and the arguments specifies the address of locations where the data is stored, control string and arguments are separated by commas.
Control string contains the field specification which directs the interpretation of input data.
· Field specification consist of the conversion character %, a data type character and an optional number, specifying the field width.
· Blanks, tabs or newline.
*) Inputting integer number
The field specification for reading an integer number is %wd.
% indicates that a conversion specification follows w is an integer number that specifies the field width of the number to be read and d known as data type character.
Ex. 1) scanf(“%2d%5d”,&a,&b);
50 31426
Ans. a=50.
b=31426.
2) scanf(“%2d%5d”,&a,&b);
31426 50
Ans. a=31
b=426
input data item must be separated by space, tab or new line.
*) Skipping
An input value may be skipped by specifying * in the place of field width.
Ex. scanf(“%d%*d%d”,&a,&b);
Data = 123 456 789
a =123
b=789
456 is skipped.
Ex. scanf(“%d%*d%d”,&a,&b,&c);
Data = 123 456 789
a=123
b=789
c= -12345 (Garbage value)
Ex. scanf(“%2d%4d”,&x,&y);
Data= 6789 432
x=67
y=89
*) Inputting number
For read number the specification %f is used.
Ex. scanf(“%f%f%f”,&a,&b,&c);
Data= 475.89 43.21E-1 678
a=475.89
b=4.32
c=678.0
if the number to read is of double type then the specification should be %lf instead of simple %f.
*) Reading Mixed Data Type
We can use one scanf statement to input a data containing mixed mode data.
Ex. scanf(“%d%f%c,&a,&b,&c);
Data= 123.86
a=123
b=0.86
c= (Enter or Blank)
Code | Meaning |
%c | For a single character |
%d | For a integer |
%e | For a floating point value |
%f | For a floating point value |
%g | For a floating point value |
%h | For a short integer |
%i | Read a decimal, hexadecimal or octal integer. |
%o | Read an octal integer. |
%s | Read a string |
%u | Read an unsigned decimal integer |
%x | Read a hexadecimal integer |
%[..] | Read a string of word (s) |
The following letters may be used as prefix certain converts
hà for long integer.
là for long double or integer.
Là for long double.
*) Formatting output
Formatted output can be done using printf function.
The printf statement provides certain features that can be effectively explited to control the alignment and spacing of print outs on the terminals. The general form of printf statement is
printf(“Control string”,arg 1, ……, arg n);
control string consists of types of items.
· Character that will be printed on the screen as they appers.
· Format specifications that define the output format for display of each item.
· Escape sequence characters such as \n, \t and \b.
The control string indicates how many arguments follow and what their types are.
A simple format specification has he following form %w.p type specifier.
Where w is an integer number that specifies the total number of column for the output value and p is another integer number that specifies the number of digits to the right of the decimal point on the number of character to b printed form a string. Both w and p are optional.
Printf never aupplies a new line automatically and therefore multiple printf statements may be used to build one of output.
*)Output of integer number
The format specifies for printing an integer number is %wd.
Where w specifies the minimum field width for the output. However
if a number is greater than the specified field width it will be printed in full overriding the minimum specification d specifies the value to be printed is an integer . the number is written right- justified in the given field width. It is possible to force the printing to be left justified by placing a minus sign directly after the % character. It is also possible to pad with zeros the leading blanks by placing a 0 before the field width specifier. We can also place + before the number by placing a + before the field width.
Format Output
9 | 8 | 7 | 6 |
printf(“%d”,9876);
| | 9 | 8 | 7 | 6 |
printf(“%6d”,9876);
9 | 8 | 7 | 6 |
printf(“%2d”,9876);
9 | 8 | 7 | 6 | | |
printf(“%-6d”,9876);
0 | 0 | 9 | 8 | 7 | 6 |
printf(“%06d”,9876);
| + | 9 | 8 | 7 | 6 |
printf(“%+6d”,9876);
+ | 9 | 8 | 7 | 6 | |
printf(“%+-6d”,9876);
*) Output of real number
The output of a real number may be displayed in decimal notation using the following format.
%w.p f .
The w indicates the minimum number of position that are to be used for the display of the value and the integer p indicates the number of digit to be displayed after the decimal point. The value when displayed is rounded to a decimal placed and printed right justified is the field of w columns. The default precision is 6 decimal places.
y=98.7654
Format Output
9 | 8 | . | 7 | 6 | 5 | 4 |
| | 9 | 8 | . | 7 | 7 |
9 | 8 | . | 7 | 7 | | |
9 | 8 | . | 7 | 6 | 5 | 4 |
Printf(“%7.4f ”,y);
Printf(“%7.2f ”,y);
Printf(“%-7.2f ”,y);
Printf(“%f ”,y);
| | 9 | . | 8 | 8 | E | + | 0 | 1 |
Printf(“%10.2e ”,y);
- | 9 | . | 8 | 7 | 6 | 5 | E | + | 0 | 1 |
Printf(“%11.4e ”,-y);
9 | . | 8 | 8 | E | + | 0 | 1 | | |
Printf(“%-10.2e ”,y);
9 | . | 8 | 7 | 6 | 5 | 4 | E | + | 0 | 1 |
Printf(“%e”,y);
Some system also support a special field specification character that lets the user define the field size at run-time.
Printf(“%*.*f ”,width, precision, number);
In this case, both the field width and the precision are gives as arguments which will supply the value of w and p. for Ex.
Printf(“%*.*f ”,7,2,number);
Is equivalent to
Printf(“%7.2f ”,number);
The advantage of the below format is that the value for width and precision may be supplied at runtime, thus making the format a dynamic one.
Ex. int width=7, precision=2
Printf(“%*.* ”,width, precision, number);
*) Printing of a single character
A single character can be displayed in the desired position using the format
%wc
The character will be displayed right justified in the field of w columns. We can make the display left justified by placing a minus sign before the integer w. The default value for w is 1.
![]() |

Formatted functions | ||
Type | Input | Output |
Char | Scanf() | Printf() |
Int | Scanf() | Printf() |
Float | Scanf() | Printf() |
String | Scanf() | Printf() |
Unformatted functions | ||
Type | Input | Output |
Char | getch(), grtche(), getchar() | putch() |
Int | - | - |
Float | - | - |
String | gets() | puts() |
*)Getchar()
This function read one character from the keyboard after the new line character is received (when we press enter key). The function getchar() can also be assigned to a variable of the type char. The function getchar() stands for “get character”.
*) Getch()
This function also reads a character but this will not echo the pressed key on the screen and it also don’t need any confirmation key like enter.
*) Getche()
This function is similar as getch() but it will echo the character pressed on the screen.
*) Putchar()
The function putchar() stands for “Put character” variable or character itself but enclosed in single quotes.
*) Putc()
The function putc() is intended for files, but it,s output can be redirected to any standard device like the screen (stdout) or printer(stdprn). The function putc() takes the following form.
Putc( a, device);
Where a in the character to be sent to the output and device is the output device.
Decision making and Branching
*) Control Statements
In c many time conditional statement are needed in program. In conditional statement if condition is true some specific statements will be executed, if condition is FALSE other statements will be executed. Many times needs the repetation of execusion of some statements in program. In a program the sequence of execution statement is called flow control. Control program finds the flow of control and defines that how control will go to the next part of the program. So control statements decide the sequences of execution of statements of program. C language have the following types of control statement.
*) Decision making statement
1) if statement
2) if else statement
3) switch statement.
4) conditional operator statement.
1) if statement
The id statement is a powerful decision making statement and is used to control the flow of execution of statements. It in basically a two- way decision statement and it is used in conjuction with an expression.
If statement are of four types:-
a) simple if statement.
b) If else statement.
c) Nested if else statement.
d) Else if ladder.
a) Simple if statement
The general form of simple if statement is
If (condition)
{
Statement group;
}
In above if statement group may be one or more statement. If there is only one statement then there is no need of { and }. First the condition written with if statement is checked if it is true then control will start the execution of first statement of statement group.
b) The if …..else statement
The if ……else statement is an extension of the simple if statemrnt. The general form is
If (condition)
{
True block statement (s)
}
Else
{
False block statement(s)
}
If the condition is true, then the true block statement (s) will be executed otherwise the false statement (s) are executed. In either case either true block or false will be executed not both.
c) Nested if …… else statement
When there are more if statements inside if else statement than it is called nested if else.
The general form is :-
If (condition 1)
{
If (condition 2)
Statement group 1;
Else
Statement group 2;
}
Else
Statement group 3;
In the above if the condition 1 is false than the statement group 3 will be executed otherwise it continues to perform the second test. If the condition 2 is true the statement 1 will be executed otherwise the statement 2 will be evaluated.
*) The else – if – ladder
These type of if statements are used for the decision of multi directional statements. This statement is used when we have to apply several conditions one after another. This statement is written as.
If (condition 1)
Statement group 1;
Else if (condition 2)
Statement group 2;
Else if (condition 3)
Statement group 3;
………………
………………
……..………..
………………
Else if (condition n)
Statement group n;

This construct is known as the else id ladder. The conditions are evaluated (from the top of the ladder) downwards. As soon as the true condition is found the statement associated with it is executed and control is transferred down / skipping the rest of the ladder. When all the n conditions become false, then the find else containing the default statement will be executed.
*) Switch Statement
This is a multidirectional condition statement. The statement also contain a condition like if else statement. In switch group are executed on the basis of the value of variable of expression.
This is written as
switch (expression)
{
Case value 1:
Statement group 1;
Break;
Case value 2:
Statement group 1;
Break;
-----------------
-----------------
-----------------
Case value n:
Statement group n;
Break;
Default:
Statement group;
In the above switch statement first the expression or variable is checked. This value may be integer or character. This value is compared with every case statement. As this value matches any case statement statement -group related to that case than the default statement-group will be executed. If default is not present then the flow of control comes out from the switch and no statement will be executed.
*) Conditional Operator ? and :
C languageh as an unusual operator useful for making two-way decision. This operator is a combination of ? and : takes three operands. This is popularly known as conditional operator.
The general form is ….
Conditional expression ? expression 1 : expression 2
The conditional expressional expression is executed first. If the result is nonzero expression 1 is evaluated and is returned as the value of the conditional expression otherwise expression 2 is evaluated and value is returned.
Decision Making & Looping
*) Looping
Some time it is needed to repeat the execution one of or more statements are executed again and again in the same sequence. This is called looping.
There are three types of looping statements in c language
1) for statement
2) while statement
3) do-while statement
In looping, a sequence of statement are executed until some condition for termination of the loop are satisfied. A perform therefore consists of two segment, one known as the control statements. The control statement tests certaining conditions and then directs the repeated execution of the statements contained in the body of the loop.
A looping include four following steps:-
1) setting and initialization of a counter .
2) execution of the statements in the loop.
3) test for a specified condition for execution of the loop.
4) increment or decrement the counter.
C language provides three loops constructs for performing loop operations. They are
1) the while statement 2) the for statement 3) the do while statement.
*) The while statement:-
The basic format of the while statement is
While (test condition)
{
Body of the loop
Increment/decrement
}
The while is an entry- controlled loop statement. The test condition is evaluated and if the condition is true, then the body of the loop is executed. After execution of the body, the variable is incremented or decremented. Now once again the test condition is checked and if the condition is true the same process is repeated again until the test condition finally becomes false and the controlled is transferred out of the loop. On exit, the program continues with the statement immediately after the while statement. The body of the loop may have two or more statements and we need braces only if there are two or more statements.
*) The do while statement:-
The basic format of the do .. while is
Do
{
Body of the loop
Increment/decrement
} while(test condition);
On reaching the do statement, the program proceed to evaluate the body of the loop first. At the end of the loop, the test condition in the while statement is evaluated. If the condition is true, the program continues to evaluate the body of the loop once again. This process continues as long as the condition is true. When the condition becomes false, the loop will be terminated after the while statement.
Since the test condition is evaluated at the bottom of the loop the do…while construct provides an exit- controlled loop that provides a more concise loop control structure. The general form of the loop is
For ( initialization ; test condition ; increment/decrement )
{
Body of the loop
}
Here initialization of the variable is done first, then the value of the control variable is tested using the test condition. The test condition is a relational expression that determines when the loop will exit. Now when the body of the loop is executed the control is transferred back to the for statement after executing the last statement in the loop. Now the control variable is incremented or decremented. The new value of the control variable is again tested to see whether it satisfies the loop condition. If the condition is satisfied, the body of the loop is again executed. This process continues till the value of the control variable fails to satisfy the test condition.
*) Difference between while and Do...while:-
1) In do…while loop body execute at least one time but in the while statement execution of the body is not necessary.
2) In do…while condition is checked after one time execution of loop body but in while statement condition is checked before execution of loop starts.
*) Nested Loop:-
When a loop executes inside another loop it is called nested loop.
For example for loop inside another for loop. The important thing in nested loop is that outer loop finishes later and inner loop finishes first.
The concept is
For ( )
{
For ( )
{
Body of loop
}
}
*) Break statement:-
Break statement is mostly used in loop on switch statement. This statement is also used to finish the loop in between. When break statement executes in loop flow of control comes out of the loop.
*) Continue statement:-
In c language break and continue both statement are used in loop to stop the execution of the loop. when break is used in loop the after executing the break statement the flow of control comes out of the loop but when continue is used in the loop execution of the loop is stopped only for the current iteration and execution of loop is started for next iteration. It tells the compiler “Skip the current iteration and continue with the next iteration”.
*) Goto statement:-
This is the control statement without any condition. When goto statement executes then flow of control leaves its normal flow and start execution from the label described by the goto statement. In c language goto is not used in general. By using the goto the program becomes easier.
This statement is written as
---------
---------
---------
Goto label name;
----------
----------
Label name:
----------
----------
Statement group
----------
----------
Here label name is an identifier after which : is necessary. Label name cannot be a number or integer. Label name may be before or after goto statement. i.e. flow of control can be transferred in forward or backward direction.
Array
*) Introduction
An array is a group of related data items that share a common name. A particular value is indicated by writing a number called index number or subscript in brackets after the array name. while the complete set of values is referred to as an array, the individual values are called elements. Arrays can be of any variable type.
*) One-Dimensional Array
A list of items can be given One variable name using only one subscript and such a variable is called a single – subscripted variable or one dimensional array.
C performs no bound checking and therefore care should be exercised to ensure that the array indices are within the declared limits.
*) Initialization of array
We can initialize the elements of arrays in the same way as the ordinary variable when they are declared. The general form of initialization of array is:-
Static type array-name [size] = {list of values};
The value in list are separated by commas.
*) Two dimensional array
It is also possible for arrays to have two or more dimensions. The two dimensional array is called a matrix.
The general form is
Type array-name [row-size] [column-size];
*) Initialization of two dimensional array
Two dimensional array can be initialized same as one dimensional array.
For e.g.
int table [2][3] = {0,0,0,1,1,1}
It initialize the first row to 0 and second row to 1.
It can also be written as
Int table[2][3] = {{0,0,0},{1,1,1}}
If the values are missing in an initialization then it automatically set to zero.
No comments:
Post a Comment