C-Language (yes)
C-Language (yes)
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
29. Void 30. Union 31. Short 32. Double.
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.
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
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.
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”.
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;
}
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);
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.
11
While(condition) do{ for(initialization; condition;
{ ………… increment/decrement)
………… ……….. {
……….. ……….. …………
……….. if(condition) ………..
if(condition) continue; ………..
continue; ………… if(condition)
………… ……….. continue;
……….. ……….. …………
……….. } ………..
} 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.
12
All arrays consist of contiguous memory locations. The lowest address
corresponds to the first element and the highest address to the last element.
Declaring Arrays
double balance[10];
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 −
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
−
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 ()
{
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
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 −
Initialization of 2D Array
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};
void main()
{
int arr[3][3], i, j, sum=0;
Output:
16
Enter the value for A[1][1]: 5
More dimensions in an array means more data be held, but also means
greater difficulty in managing and understanding arrays.
type array_name[d1][d2][d3][d4]………[dn];
Examples:
1. int table[5][5][20];
2. float arr[5][6][5][6][5];
In Example 1:
In Example 2:
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
It may sound a bit confusing, but don't worry. As you practice working with
multidimensional arrays, you start to grasp the logic.
#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();
}
Strings
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
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.
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.
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.
(or)
struct dob
{
int dd;
int mm;
int yy;
};
struct student
{
int rno, tot;
char name[10];
struct dob x;
} s1;
26
27