Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

C-Language (yes)

C is a high-level, case-sensitive programming language developed by Dennis Ritchie in 1972, known for its function-oriented approach and ability to manage memory directly. It includes a rich set of operators, a defined character set, and various data types, including primary, derived, user-defined, and empty types. The document also covers C tokens, input/output functions, program structure, and control statements for decision-making and looping.

Uploaded by

madhukr.mca10
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C-Language (yes)

C is a high-level, case-sensitive programming language developed by Dennis Ritchie in 1972, known for its function-oriented approach and ability to manage memory directly. It includes a rich set of operators, a defined character set, and various data types, including primary, derived, user-defined, and empty types. The document also covers C tokens, input/output functions, program structure, and control statements for decision-making and looping.

Uploaded by

madhukr.mca10
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 27

C- Language

 “C” is a one of the important High Level Language it is also called as


middle level language.
 It is a case sensitive language.
 It was developed by Dennis Ritchie in 1972.
 It is a collection of features of both languages is called as “BCPL” and
“B”.
 It is a function oriented Language.
 It is also used to Develop the system software.
 The maximum software of a “UNIX” operating system is written in these
language only.
 It has the rich set of operators..
 It is the only language which is the capable of touch the memory (or)
manage the memory directly.

Character set
“C” character set can be classified in to the following ways.
1. Alphabets:- A to Z (or) a to z
2. Numerical:- 0 to 9
3. Special symbols:- @,$,#,{,},(),[ ], ., -, *, :, ;, _, etc

C – Tokens
 The individual small unit of a “C” programme is called as “C”
token. The flowing of the various C-Tokens.
1. Key words.
2. Identifiers
3. Operators.
4. Strings
5. Special symbols.

1) Keywords: These are also called as Reserved words. These words are
already having the Pre-defined meaning i.e already
defined and placed in a system “C” has 32 key words,
those are

1. Auto 2. Break 3. Case 4. Const


5. Char 6. Continue 7. Default 8. Do
9. Enum 10. Extern 11. Else 12. Float
13. for 14. Goto 15. If 16. Int
17. Long 18. Register 19. Return 20. Static
21. Struct 22. Size of 23. Switch 24. typedef
25. Volatile 26.while 27.unsigned 28. Signed

1
29. Void 30. Union 31. Short 32. Double.

2) Identifiers: An Identifier is a user defined word. it is a name of the


memory location. Where the Actual data is stored.
Identifiers are used to represent the data.
Ex: a, b, c, Sum, Count………..
Rules to Identifiers Name:
 An Identifier name should starts with an alphabet only.
 Blank spaces are not allowed with in a identifier name.
 Reserve words can’t be used as identifiers.
 Special symbol are not allowed Except “_“.
Variable: A variable is an identifier, It’s value/Changes during the
Execution of a programme.
Ex: h, t, m, Sum……….
Constants: A constant is an identifier it’s value is fixed during the
Execution of a programme. Such concept is called constant.
Ex: Pi, half, k………

Operator: An operator can be treated as a computational unit, that takes


operands and perform some operation and return a value.
Ex: +, -, *, %, /……………
String: A string is a sequence of characters (or) It is character Array.
Ex:- Priyadharshini Degree College.

Data Types: A data type is used to specify the type of data in “C” there
are 4 data types. Those are
1. Primary data
2. Derived data type
3. User data type
4. Empty data type
1) Primary Data type: These are also called as Built
in Data types. The flowing are the various primary
data types.
A) Int B) Character C) Float D) Double.

Declaring a variable: To declare a variable in “C” Programme, it has


flowing syntax.
Data type variable name or variable
list;
Where data type is a valid data type in “C” and variable list may represent
single variable (or) multiple variables. If multiple variables are there, those

2
are separated by “,” (Comma) every variable Declaration statement must
be ended with “ ; ” (Semi colon).

3
Int: It is used to declare the integer type of variables (or) integer type of
data.
Ex: int a;
i.e “a” is a integer type of variable in which we can store the
integer type of data.
 Every integer variable occupies 2 bytes in memory.
 The range of an integer variable is -32768 to +32767.
Long int: It is used to store the (or) Declare the integer the type of variable
in which we can store the value which is not in the range of
ordinary integer.
 A long integer occupies 4 bytes in memory.
 It’s range is -2147483648 to +2147483647.
Char: It is used to declare the character type of data.
Ex: Char a; i.e a is a character variable in which we can store the
character type of data.
 Every character variable occupies one byte in a memory.
 It’s range is from -128 to +127.
Float: It is used to declare the Float (Real) type of data.
EX: Float b; i.e b is a float type of data in which we can store the
float type of data.
 A float variable occupies 4 bytes in a memory.
 It’s range is from 3.4E-38 to 3.4 E 38 that means 3.4*10 38

Double: It is also used to Declare the float type of data when it


Exceeds the range of float.
Ex: double a;
Every double variable occupies 8 bytes in memory
It’s range is form 1.7E-308 to 1.7E 308 that means
1.7*10-308 to 1.7* 10308

Formatted I/O functions (or) I/O Statements


“C’ provided the flowing input output statements.
1. Scanf ()
2. Printf ()
scanf(): It is a input statements it is used to read tha data from key board.
Syntax:
Scanf(“controalstring”, &var1, &var2……..);
Where control string represents type of entered data for these, it contains
“%” followed by one (or) two Alphabets.
It should be placed with a double Quote “ “
Ex: %d for integer type of data
4
%f for float type of data
% s for String type of data
%c for Character type of data
% ld for long integer type of data
%lf for long float type of data
%u for Unsigned type of data
%x for hexadecimal type of data
&var1 , & var 2 are the variables and which are preceded by a symbol called
as ambercent (&) is called as address operator it gives an address of a
specified variable.
Ex: &a: it gives the address of a variable a
Ex: scanf (“%d%d”, &a,&b);

Printf( ): It is a output statement it is used to Display the Information on


the screen.
Syntax:
printf(“controlstring”, var1,var2……….);
Where control string a specifies a type of data which is to be displayed on
the screen
Var1, var2……… is called as variables.
Ex: printf(“%d”,c);
printf(“sum=”%d”,c);
printf(“ NAB COMPUTER EDUCATION”);

Structure of “C” Program


It contains the following sections .these are shown in the following figure.
Documentation section
Linkage section
Definition section
Global declaration section
Main()
{
Declaration part;
Statement part;
}
Sub program section
{
Declaration part;
Statement part;
}
Documentation section: It is used to define the comments regarding a
“C” program. To write the comments the
following rules are followed by the “C”
programmer.

5
1. They comments should be placed between/*………*/
2. Nexted comments are not allowed.
3. Comments may be splited in to severallines.
Ex: /* program for finding sum of two numbers*/
Note: It is an optional, it is ignored by the compiler.

Linkage Section: it is used to link header files of built in functions from


system library to “C” program.
For these, it is used a pre-processor directive called as #include
#include <header file>
Example:- #include < stdio.h>
#include < math.h>
#include < conio.h>
#include < string.h>
Defination Section: It is used to Define the symbolic constants in “C”
Program. To Defined a symbolic constant this section
uses a pre-processor firective called as # defire.
Syntax:- # define symbolic constant constant value (Or) constant
name
Where symbolic constant name is represents name of the
symbolic constant and constant value is the value of constant. When ever a
symbolic constant name is an encountered by the compiler, It is
immeadiately Replaced by its defined constant value.
Ex: # define Pi 3.1415
# define P printf
Global declaration section: It is used to declare the Global variables.
These variables are accessed by any
function with in a “C” program a long
with main function (or) main ()

Main(): Generally “C” program is a collection of functions. Every “C”


program must contains at least one function called as main(). It is followed
by a pair of empty parenthesis the execution of a “C” program must begins
with main function only. It is a entry control of the program.
Main function should starts with { and ends with } it contains two parts
1. declaration part 2. statement part.
Declaration part is used to declare the variables which are use in a
program.
Statement part contains one or more statements.
declaration part and statement part should be ended with ; (semi colon).
Sub-program section: it is used to define the user defined functions. It
also starts with { and ended with } it also contains declaration part and
statement part. Generally, user defined functions are placed after the main
function.
Program compiling for Alt+F9
Program run (or) execution for Ctrl+F9
6
Program output for Alt+F5
For save F2

Decision making control statements or Conditional statements


These statements allow the computer to evaluate a condition. A condition
returns either true or false value. It is used to define (or) develop the
conditional statements. It is available in the following ways
1) simple if statements
2) if-else statements
3) nested if statements
4) else if statement
Simple if statement or if statement: It is a one way decision making
control statement.
Syntax:
if(condition) if(condition)
Statement1; {
Statement y; statement1;
Statement2;
}
Statement y;
Here condition executed first, if it is true statements or compound
statements will be executed and the control is transferred to “statement y”.
if-else statement: It is a two way decision making control statement.
Syntax:
if(condition) if(condition)
true statement; {
false statement; true block statements;
statement y; }
else
{
false statements;
}
statement y;
Hear condition is executed first, if it is true then true block statement or
compound block statements will be executed and the control is transfer to
statement y otherwise false block statement will be executed.

Nested if statement: A if statement set to be a nested if statement, when


it contains one or more if statements in it.

7
Syntax:
if(condition1)
{
if(condition2)
{
statement1;
}
else
{
statement2;
}
}
else
{
statement3;
}
statement y;
Hear condition1 is executed first, if it is false, statement3 will be executed
otherwise condition1 become true, control enter in to condition2, if it is true
statement1 will be executed otherwise statemnt2 will be executed and the
control is transferred “statement y”.

Else if ladder statement (or) if-else-if statement: It is a multi decision


making control
statement.
Syntax:
if(condition1)
{
Staement1;
}
else if(condition2)
{
Statement2;
}
else if(condition3)
{
Statement3;
}
else
{
default statement;
}
.
.
Statement y;

Hear conditions are executed from top to downwards. If all conditions become
fail then default statements will be executed and the control is transfer to
statement y, if any one of the condition among “n” conditions becomes true
then its corresponding statements are executed and the control is transfer to
statement y.
Switch statement:
8
This statement causes particular group of statements to be choosen from
several available groups. The selection is based up on the current value of an
expression, which is included with in the switch statement.
Syntax:
Switch (expression)
{
Case value1: block1;
break;
Case value2: block2;
break;
Case value3: block3;
break;
.
.
default : defaultblock;
}

 Where an expression may be integer or character, value1,


value2…….valuen are called as case labels or case constants. These are
unique with in the switch structure.
 Each case label should be followed by column (:) block1,
block2……..block n may be represents single or multiple statements and
ends with semi column(;) switch, case, break and default are the
keywords.
 When switch statement is executed, the value of an expression in
successively compared with the values of case labels.
 If match is found then it’s associated block of statements will be
executed and the control exists from the switch statement by executing
break statement.
 If match is not found then default block will be executed. The default
block is an optional. The switch statements should be starts with {and
ends with }.

Looping control statements:


These control statements are used to specify the repeated type of execution.
In which some set of statements are executed in repeated manner until some
condition has been satisfied. This control statements are classified in to 3
types, those are
1) while statement 2) do-while statement 3) for statement.

1) while statement: It is a one of the looping control statement.


Syntax:
While(condition)
{
Body of the loop;
……………………….
……………………….
}
 When it is executed, the condition is evaluated first. If it is true, body
of the loop will be executed.

9
 After executing the body of the loop, the control will be entered in to
condition once again and it is true body of the loop will be executed.
This process is continue as long as the condition is true.
 If the condition becomes fails, the control will be transferred to
statement, which is place after the while loop.
 It is called as entry controlled loop or pre-conditional looping
statement because, hear the condition is evaluated first then only
statements are executed.
2) do-while statement: sometimes body of the loop will be executed
atleast one time this behavior is available in do-
while statement.
Syntax:
do
{
Body of the loop;
……………………….
……………………….
}
While (condition);

 When it is executed, if the body of the loop will be executed first


and control goes to evaluate the condition. If it is true body of the
loop will be executed, once again and control goes to evaluate the
condition, this process continue until the condition become fail.
 It is called as Exit control loop or Post conditional statement
because the condition is evaluated at the bottom of the loop.
3) For loop statement: It is also called as Entry control
looping statement and simplest looping control
statement.

Syntax:
for(initialization; condition; increment or decrement)
{
Body of the loop;
……………………….
……………………….
}
 Where initialization part is used to initialize the control variable line i=1,
j=1, s=0…….it consist of one or more initialization statement and which
are separated by comma (,) this part should end with semi column (;).
 Condition part also allows one or more conditions and combined by
logical operators like i<=n, j<=m……
 Increment or decrement part is used to increment or decrement the
value one by one and control the variable like i++ ,j++...
 When it is executed initialization is done first then condition is
evaluated, if it is true body of the loop will be executed and control will
be entered in to for loop for incrementing the counter variable and once
again condition is evaluated. This process continues until condition
become fail.

Branching statements or jumping statements


10
goto statement: it is used to break the normal execution flow of a program
by transferring control from one part to some other part of the program.
Syntax: goto labelname;
Where label name is an identifier and used to label the statements
to which the control has to be transferred. It is must followed by
coloumn(:) the label is placed immediately before the statement
where the control is to be forward or transferred.
Example: goto xyz; or goto tv9;
The goto is forward or backward, if the label name is found before the goto
statement then such goto is called backward goto otherwise forward goto.
These are shown in the following figure.

Example of goto statement:


Labelname:……….. goto labelname;
………… …………
……….. ………..
goto labelname; Labelname:………..
……….. ………..
…....... ………..
Break statement: It is used to change the normal execution of a program. It
causes termination from a specified loop.
Syntax: break;
Example: break;
The behavior of a break statement in various loops is shown in the following
figures.
Syntax of while loop Syntax of do-while loop Syntax of for loop
While(condition) do{ for(initialization; condition;
{ ………… increment/decrement)
………… ……….. {
……….. ……….. …………
……….. if(condition) ………..
if(condition) break; ………..
break; ………… if(condition)
………… ……….. break;
……….. ……….. …………
……….. } ………..
} While(condition); ………..
}

Continue statement: It is used to continue the loop without executing the


some of set of statements in that loop.
Syntax: continue;
Example: continue;
The behavior of continue statement in various loops is shown in the following
figures.

Syntax of while loop Syntax of do-while loop Syntax of for loop

11
While(condition) do{ for(initialization; condition;
{ ………… increment/decrement)
………… ……….. {
……….. ……….. …………
……….. if(condition) ………..
if(condition) continue; ………..
continue; ………… if(condition)
………… ……….. continue;
……….. ……….. …………
……….. } ………..
} While(condition); ………..
}

Exit(): It causes termination from a entire program.


Syntax: exit();

Syntax of while loop Syntax of do-while loop Syntax of for loop


While(condition) do{ for(initialization; condition;
{ ………… increment/decrement)
………… ……….. {
……….. ……….. …………
……….. if(condition) ………..
if(condition) exit(); ………..
exit(); ………… if(condition)
………… ……….. exit();
……….. ……….. …………
……….. } ………..
} While(condition); ………..
}

ARRAY’S

Arrays a kind of data structure that can store a fixed-size sequential collection
of elements of the same type. An array is used to store a collection of data,
but it is often more useful to think of an array as a collection of variables of
the same type.

Instead of declaring individual variables, such as number0, number1, ..., and


number99, you declare one array variable such as numbers and use
numbers[0], numbers[1], and ..., numbers[99] to represent individual
variables. A specific element in an array is accessed by an index.

12
All arrays consist of contiguous memory locations. The lowest address
corresponds to the first element and the highest address to the last element.

Arrays are divided into three types.

1) Single or one dimensional array


2) Two-dimensional array
3) Multi dimensional array

1) Single or one dimensional array: an array is a list of elements or data


types, which are stored on contiguous (side by side) memory locations.

Declaring Arrays

To declare an array in C, a programmer specifies the type of the elements and


the number of elements required by an array as follows

Syntax: data type arrayName [ arraySize ];

This is called a single-dimensional array. The arraySize must be an integer


constant greater than zero and data type can be any valid C data type. For
example, to declare a 10-element array called balance of type double, use
this statement −

double balance[10];

Here balance is a variable array which is sufficient to hold up to 10 double


numbers.

Initializing Arrays

You can initialize an array in C either one by one or using a single statement
as follows double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};

The number of values between braces { } cannot be larger than the number
of elements that we declare for the array between square brackets [ ].

If you omit the size of the array, an array just big enough to hold the
initialization is created. Therefore, if you write −

double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};

13
Following is an example to assign a single element of the array − balance[4] = 50.0;

The above statement assigns the 5th element in the array with a value of 50.0.
All arrays have 0 as the index of their first element which is also called the
base index and the last index of an array will be total size of the array minus
1. Shown below is the pictorial representation of the array we discussed above

Accessing Array Elements

An element is accessed by indexing the array name. This is done by placing


the index of the element within square brackets after the name of the array.
For example − double salary = balance[9];

The above statement will take the 10th element from the array and assign the
value to salary variable. The following example shows how to use all the three
above mentioned concepts viz. declaration, assignment, and accessing arrays

#include <stdio.h>

void main ()
{

int n[ 10 ]; /* n is an array of 10 integers */


int i,j;

/* initialize elements of array n to 0 */


for ( i = 0; i < 10; i++ )
{
n[ i ] = i + 100; /* set element at location i to i + 100 */
}

/* output each array element's value */


for (j = 0; j < 10; j++ )
{
printf("Element[%d] = %d\n", j, n[j] );
}
}

When the above code is compiled and executed, it produces the following result −

Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106

14
Element[7] = 107
Element[8] = 108
Element[9] = 109

2) Two-dimensional array: A two dimensional array can be imagined as a


matrix or table of rows and columns or as an array of one dimensional
arrays. Two dimensional arrays are used to represent the data in tabular
form. 2D arrays are generally known as matrix. Two-dimensional Arrays

The simplest form of multidimensional array is the two-dimensional array. A


two-dimensional array is, in core, a list of one-dimensional arrays. To declare
a two-dimensional integer array of size [x][y], you would write something as
follows − data type arrayName [ x ][ y ];

Where data type can be any valid C data type and arrayName will be a
valid C identifier. A two-dimensional array can be considered as a table which
will have x number of rows and y number of columns. A two-dimensional array
a, which contains three rows and four columns can be shown as follows −

Thus, every element in the array a is identified by an element name of the


form a[ i ][ j ], where 'a' is the name of the array, and 'i' and 'j' are the
subscripts that uniquely identify each element in 'a'.

Syntax of Declaring two-dimensional Array

To declare an array in C, a programmer specifies the type of the elements and


the number of elements required by an array as follows −

data type arrayName [ arraySize ];

Initialization of 2D Array

There are many ways to initialize two Dimensional arrays –

int disp[2][4] = {
{10, 11, 12, 13},
{14, 15, 16, 17}
};

OR
15
int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};

Things which you must consider while initializing 2D array


You must remember that when we give values during one dimensional array
declaration, we don’t need to mention dimension. But that’s not the case with 2D
array; you must specify the second dimension even if you are giving values during
the declaration. Let’s understand this with the help of few examples –

/* Valid declaration*/ /* Valid declaration*/


int abc[2][2] = {1, 2, 3 ,4 } int abc[][2] = {1, 2, 3 ,4 }

/* Invalid declaration – you must specify second dimension*/


int abc[][] = {1, 2, 3 ,4 }
/* Invalid because of the same reason mentioned above*/
int abc[2][] = {1, 2, 3 ,4 }

Store Data into 2D array

void main()
{
int arr[3][3], i, j, sum=0;

/*Accepts input from the user and stores it in 2-D array*/


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“\nEnter the value for A[%d][%d]:“,i,j);
scanf(“%d”,&arr[i][j]);
}
}

/*Calculate sum of elements in 2-D array*/


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=sum+arr[i][j];
}
}
printf(“\nThe sum of the elements of 2-D array is %d”, sum);
}

Output:

Enter the value for A[0][0]: 1

Enter the value for A[0][1]: 2

Enter the value for A[0][2]: 3

Enter the value for A[1][0]: 4

16
Enter the value for A[1][1]: 5

Enter the value for A[1][2]: 6

Enter the value for A[2][0]: 7

Enter the value for A[2][1]: 8

Enter the value for A[2][2]: 9

The sum of the elements of 2-D array is 45


3) Multi-dimensional array: C allows for arrays of two or more dimensions.
A two-dimensional (2D) array is an array of arrays. A three-dimensional
(3D) array is an array of arrays of arrays.

In C programming an array can have two, three, or even ten or more


dimensions. The maximum dimensions a C program can have depend on
which compiler is being used.

More dimensions in an array means more data be held, but also means
greater difficulty in managing and understanding arrays.

How to Declare a Multidimensional Array in C

A multidimensional array is declared using the following syntax:

type array_name[d1][d2][d3][d4]………[dn];

Where each d is a dimension, and dn is the size of final dimension.

Examples:

1. int table[5][5][20];
2. float arr[5][6][5][6][5];

In Example 1:

 int designates the array type integer.


 table is the name of our 3D array.
 Our array can hold 500 integer-type elements. This number is reached
by multiplying the value of each dimension. In this case: 5x5x20=500.

In Example 2:

 Array arr is a five-dimensional array.


 It can hold 4500 floating-point elements (5x6x5x6x5=4500).

Can you see the power of declaring an array over variables? When it comes to
holding multiple values in C programming, we would need to declare several
variables. But a single array can hold thousands of values.

17
Note: For the sake of simplicity, this tutorial discusses 3D arrays only. Once
you grab the logic of how the 3D array works then you can handle 4D arrays
and larger.

Explanation of a 3D Array

Let's take a closer look at a 3D array. A 3D array is essentially an array of


arrays of arrays: it's an array or collection of 2D arrays, and a 2D array is an
array of 1D array.

It may sound a bit confusing, but don't worry. As you practice working with
multidimensional arrays, you start to grasp the logic.

The diagram below may help you understand:

Let’s see a complete example on how to initialize a 3D array:

#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k;
int arr[3][3][3]=
{
{
{11, 12, 13},
{14, 15, 16},
{17, 18, 19}
},
{
{21, 22, 23},
{24, 25, 26},
{27, 28, 29}
},
{
{31, 32, 33},
{34, 35, 36},
{37, 38, 39}
},

18
};
clrscr();
printf(":::3D Array Elements:::\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
printf("%d\t",arr[i][j][k]);
}
printf("\n");
}
printf("\n");
}
getch();
}

In the code above we have declared a multidimensional integer array named


“arr” which can hold 3x3x3 (or 27) elements.

Strings

String: A string is a character array


Syntax: data type array-name[size];
Where Data type should be char, array name is a name of the string variable
and size represents maximum length of a string.
For example
Char a[10];

String handling functions: These functions are used to perform the


operations on strings like
1. Finding the length of a string
2. Copying a string
3. Concatenating given strings
4. Comparing given two strings
5. Reverse of a given string
6. Uppercase of a given string
7. Lowercase of a given string

19
Those are 1. strcpy( ) string copying
2. strcat ( ) strings containing(adding)
3. strcmp( ) string comparison
4. strlen( ) string length
5. strrev( ) string reverse
6. strupr( ) string upper case
7. strlwr( ) string lower case
All these functions are stored in <string.h> header file name
1) strcpy( ): It is used to copy the contents of one string to anotherstring.
Syntax: strcpy(string1,string2);
Where string1 is a character array and string2 maybe a character array
Or
String constant when it is executed, the contents of string2 will be copied
on string1
Note: Must and should be maintain string1 is a Empty and string2 is a
enter the data
The data will be transfered in to string2 to string1
Example:
1. char (a,b); ‘a’ is a empty
‘b’ is a entered data
2. char (a, “Degree”);
2) strcat ( ): It is used to concatenating the given two strings
Syntax: strcat (string1, string2);
Where, string1 is a character array and string2 may be a
character array (or) string constrant.
When it is executed, the contents of string2 will be appended to
string1 by removing the NULL character at the end of string one.
The resultant string is stored in string1.
Example:
1. strcat (a,b);
2. strcat(a, “couege”);
3. strcat ( a, star cat(b,c));
3) strcmp( ): It is used to compaire the contents of two strings based on their
respective ASCII values
ASCII values in A=65 a=97
B=66 b=98
Syntax: strcmp (string1, string2);

20
Where string1 and string2 maybe character array’s (or) string constants when
it is executed. It compares the contents of given two strings and written the
following values.
O : when given two strings are identical
+ve : when string is alphabetically above string1.
- ve : when string2 is alphabetically below string1.
Example:
strcmp(a,b);
strcmp(“INDIA”, “INDIA”);
4) strlen( ): It is used to find the length of a given string syntax.
Identifier=strlen (string1);
Where identifier is a integer type of variable and string1 maybe character
array (or) string constants. Identifier is an optional.
Example:
1. l=strlen (“INDIA”)
2. printf (“ length = %d”, strlen (“INDIA”));
3. strlen (a);
5) strRev( ): It is used to display the reverse of a given string.
Syntax: strrev(string1); Where, string1 is a character array.
Example: printf (“Reverse of a string is =%S”, strrev (a)); Or strrev (a);
Two-Dimentional character array’s (or) string constrant
This array’s are used to represent the strings in tabular form (or) this array’s
are used to store the “n” strings in a single variable.
Syntax: data type array name [size1][size2];
Where, data type should be character array name is a name of the two-
dimetional character array, size1 Indicates No. of rows and size2 indicates
No.of columns.
Example:
Char a[10][15];
Where a[10] Indicates for No. of names (or) rows
[15] Indicates for string length (or) columns
i.e. a is a two-dimensional array . In which we can stores ten strings and
maximum length of a string is 15 characters. Here only used one Indexing
variable i.e a[10][15];

21
FUNCTIONS

A function is a self-contained programme when ever we have largetask, it can


be divided in to number of groups (or) modules. Each and every module is
called as sub-programme (or) function.
A function is a set of well defined instructions to solve which are used to solve
a problem. A group of instruction in a program can be organized as a function.
Basically functions are classified in to two types.
1. User defined functions.
2. Pre-defined/syntax/library /built in function.
User defined functions are difined by user only
Ex:- Main( )
Add ( )
System defined functions are already execute then if required
Ex:- sqrt ( )
cos ( )
strlen ( )
In “C” language a user defined functions has the following syntax.
Returntype function-name (argument list);
Declaration of arguments
{
local variable declaration;
statement1;
statement2;
----------
----------
----------
----------
return (expression);
}
Where returntype represents type of data which is return by sub-program
Function-name is a name of the function, argument list is a list of
arguments which are used to develop the data communication between main(
) and sub-programmes. arguments declaration represents (type) declaration
of arguments, local variable declaration is used to declare the variables inside
a function, statement 1 and statement 2……………….. statement n is used to
return the data from sub-programme to main( )
Note:- All elements of a function are not mandatory.
“C” user defined functions are classified in to the following type.
1. Function with no arguments and no return values
2. Function with arguments and no return values
3. Function with arguments and return values
4. Function with no arguments and return values.

22
FUNCTION WITH NO ARGUMENTS AND NO RETURN VALUES
In these case, tere is data communication between main() and sub-
programme (or) viceversa this is a shown in the flowing figure.
FUNCTION WITH ARGUMENTS AND NO RETURN VALUES
In this case, there is a one-way data communication between calling function
(main () and (or) sub called function before sending arguments (or)
parameters those must be declared and defined inside the main ()
This is shown in the flowing figure.
Note:- This type actual parameters should match the type of formula
parameters.
Note no2:- The cant of actual parameters should match the count of formula
parameters.
FUNTION WITH ARGUMENTS AND RETURN VALUES
Here there is a two way data communication between calling and called
functions this is shown in the following signature.
Two-way data communication.

STRUCTURES
A Structure is a collection of different data items which are stored in
contiguous memory location and these data items are collectively organized
by a single name
Syntax:-
Struct Struct name
{
data type 1 mEMBER 1;
data type 2 mEMBER 2;
: :
: :
: :
data type n member;
};
Where Struct is a keyword struct name is a name of structure. It is also called
as tag name. It is an optional Datatype1 member1, Datatype2 member2……
Data type-n member-n are called as members (Or) fields of different data
types like datatype 1, datatype2, datatype-n respectively. A structure
declaration should be terminated with semicolon (;)

Example:-
struct student
{
int rno, tot;
char Name [10]
float avg;
};

23
Note:-
2) A Structure Members be initialized at the time of structure
declaration.
3) A Structure name can not be used to refer the member of a
structure.
4) Compiler can not allocate the memory for structure Member at the
time of structure declaration.

 To refer the data (Or) structure members, we have to use


structure variable.
 A Structure variable can be declared in the following two ways.

2) Use the tag name (Or) structure name


3) Without using tag name (Or) Structure name.

Note:-
1) A structure members be initialized at the time of structure declaration.
2) A structure name cannot be used to refer the members of a structure.
3) Compiler cannot allocate the memory for structure members at the time
of structure declaration.
4) To refer the data (or) structure members. We have to use structure
variable.
5) A structure variable can be declared in the following two ways.
6) Use the tag name (or) structure name
7) With out using tag name (or) structure name.
Note:- when a structure variable is declared then only compiler allocates
memory for structure members.

STRUCTURE VARIABLE DECLARATON USING TAG -NAME


Syntax:- struct structure name variable list;
Where structure is a key word and struct name is a tag name and struct
variable list is a list of structure variable.
Ex:- struct student s1, s2, s3;

24
STRUCTURE DECLARATION WITHOUT USING TAG – NAME
Syntax:-
struct struct name
{
datatype1 member1;
datatype2 member2;
datatype3 membern;
} struct variable list;

MEMBER OPERATOR(.)
It is also called as dot operator. It is used to establish the connection between
structure variable and its member.
Ex:- s1.no, s1.name, s1.total, s1.avg e.t.c
INITILIZATION OF A STRUCTURE
A structure variable can also be initialized with required data items at the time
of It’s declaration.
Syntax:- Struct struct-name structure = {list of values};
Ex:- Struct student s[10];
“S” is a array type of structure variable in which we can store the “10”
students data.
ARRAYS WITH IN A STRUCTURE
When we declare an array is a member of structure then such a concept is
called array with in a structure.
Ex:- struct student
{
int rno, tot;
char name[10];
int sub[10]; (*arraynumber*)
float avg;
};
STRUCTURE AND FUNCTIONS
It means how to pass the structure members (data) as arguments. Is called
function.
 The following are two types through which we can pass the structure
data as argument to the called functions.
 By passing individual elements of a structure
 By passing entire structures as a argument.
EX1:- display (s1.rno, s1.name, s1.tot, s1.avg); (individual)
EX2:- display (s1);/ *entire structure as a argument*/
Note:- when we should the entire structure as a argument then the structure
must be declared as global.

25
STRUCTURES WITH IN A STRUCTURE
It is also called as nested structures a structure is called a nested structure
when it contains one (or) more structure (or) when we declare a structure
variable as a member of another structure then such a structure is also called
nested structure.

Ex:- struct student


{
int rno, tot;
char name[10];
struct dob;
{
int dd;
int mm;
int xx;
}x;
}s1;

(or)

struct dob
{
int dd;
int mm;
int yy;
};
struct student
{
int rno, tot;
char name[10];
struct dob x;
} s1;

Above structure, members are refered in the following way


s1.rno, s1.name, s1.tot, s1.avg, s1.x.dd, s1.x.mm, s1.x.x

26
27

You might also like