C Language Note Full Docx
C Language Note Full Docx
MATERIAL
C Language
UNIT 1- UNIT 4
Introduction:
• C is one of the foundations for information technology and computer science.
• We use system in Computer architecture, Operating system, Networking communication,
Database, graphics ...... etc.,
• C is most commonly use programming language in industries.
• Most of the web browsers and word process are written in c language.
• C language is a base for almost all programming language.
Ex: C++,JAVA,Python,Perl,PHP,Ruby ..... etc.,
Generations:
According to c….
• In 1960 ALGOL (Algorithmic language) it is developed by international committer.
• In 1963 CPL(Combined programming language) it is developed by Cambridge university.
• In 1967 BCPL(Basic combined programming language) it is developed by Martin Richard at
Cambridge university.
• In 1970 B language Ken Thomson at ATST Bell Labs. (American Telephone and Telegraph
Company).
• In 1972 C language was introduced by Dennis Ritche at AT&T Bell Labs .
Applications of C:
1. OS (operating Systems): These are like UNIX, Linux, window XP……etc…. In this operating
systems are used in c coding.
2. ES (Embedded System): These are nothing but the combination of software and hardware
is known as c in embedded systems.
Ex: TV remote controls, ticket printing machines, .... etc.,
3. DD (Device Drivers): Software we can use for devices. In this device driver it stores
audios/videos configuration etc….,
Ex: printer, scanner, camera, Bluetooth etc.,
4. Compiler: These are nothing but the apps to check the errors in program
Ex: C language, C++, java…compilers
5. Text editors: These are the nothing but the ribbons in MS office and in other software.
Ex: Notepad, Word document,
Levels of language:
They are 3 types of levels of language.
C Tokens:
Character sets: These are nothing but the set of alphabets; letters and some special
characters that are valid in c language.
Alphabets:
Upper Case: A to Z
Lower Case: a to z
Specials Characters: These are the special characters are ~,!,@,#,$,%,^,S,*,(,),_, ,+,=,|,\,{,},
[,],:,;,”,’,<,>,,,.,?,/.
Key Words:
These are the reserved words used in programing, each key word has a fixed meaning that
cannot be changed.
1. Auto
2. Break
3. Case
4. Char
5. Const
6. Continue
7. Default
8. Do
9. Double
10. Else
11. Enum
12. Extern
13. Float
14. for
15. Goto
16. If
17. Int
18. Long
19. Register
20. Return
21. Short
22. Signed
23. Size of
24. Static
25. Struct
26. Switch
27. Typedef
28. Union
29. Unsigned
30. Void
31. Volatile
32. While
1. Create
2. Compile
3. Execute or Run
4. Get the Output
▪ # Preprocessor directive.
▪ include- It is a folder.
▪ stdio.h – Standard input output.header. It can store the printf S scanf functions.
▪ conio.h --- console input output.header. It can store clrscr and getch commands.
▪ main()- Heart of the program.
▪ Getch ---- get the character., clrscr -----clear the screen
C – Data Types
1. C data types are defined as the data storage format that a variable can store a data to
perform a specific operation.
2. Data types are used to define a variable before to use in a program.
3. Size of variable, constant and array are determined by data types.
Types Data Types
Basic data types int, char, float, double
Enumeration data type enum
Derived data type pointer, array, structure, union
Void data type void
Data types: There are 4 types and their formats are:
➢ Int (0 to 9) %d
➢ Float (decimal value) %f
➢ Char (A to Z or a to z) %c
➢ Double (big decimal values) %lf
Identifiers:
Variables and Constants:
Variables are 2 types.
1. Local variables: In this int a is written in the main function and always checks the main
function itself.
2. Global variables: But in this global variable int a will be declared outside of the main
function.
Examples:
#include<stdio.h>
#include<conio.h>
int a=25; Global variable
main()
{
int a=25; Local variable
printf(“a=%d”,a);
getch( );
}
Constants:
Constants are 2 types.
1. Numerical
2. Character
Numerical: Character:
Int (0-9) Single ‘A’
Float 0.1, 0.2------- String “Name”
Escape sequences:
1. \n new line
2. \t tab space
3. // double slash (to block a single line)
4. /* ......... */ to block some more lines
Sample program of escape sequences:
#include<stdio.h>
#include<conio.h>
main()
{
printf("My name is ISAN \n");
printf("\t\t ISAN \n");
//printf("\t\t ISAN \n");
/*printf("\t\t I am in kakinada\n");*/
getch();
}
Sample programs of without scanf in Data types:
Int: Float:
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
main() main()
{ {
int a=20; float a=2.25;
printf("the value of a is %d",a); printf("the value of a is %f",a);
} }
Char: Double:
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
main() main()
{ {
char a='X'; double a=24536996546978678;
printf("the value of a is %c",a); printf("the value of a is %lf",a);
} }
Write all data types in a single program without scanf
Sample programs of with scanf in Data types:
Int: Float:
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
main() main()
{ {
int a; float a;
printf("Enter the value of a \n"); printf("Enter the value of a \n");
scanf("%d",Sa); scanf("%f",Sa);
Operators
An operator is a symbol that else the compiler to perform specific mathematical, logical
operations and C language is which inbuilt in the operation and provides the following types of
operators. C language offers many types of operators. These are
• Arithmetic Operators
• Assignment Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Conditional operator (ternary operator)
• Increment/ decrement operator
• Special operator
Arithmetic operators:
+ addition a+b , - subtraction a-b , * multiplication a*b ,
/ division a/b , % modulus a%b
printf("%d-%d=%d",a,b,a-b);
Model 3: (Without using extra variables) }
#include<stdio.h>
#include<conio.h>
main()
{
int a=15,b=20;
{
Model 4:(Without extra variable with int r,h;
scanf) printf("enter the value of r and h is \n");
#include<stdio.h> scanf("%d %d",Sr,Sh);
#include<conio.h> printf("%d - %d = %d",r,h,r-h);
main() }
Write a program of using multiplication operator:
Model 3: (Without using extra variables) Model 4:(Without extra variable with
#include<stdio.h> scanf)
#include<conio.h> #include<stdio.h>
main() #include<conio.h>
{ main()
int a=15,b=20; {
printf("%d*%d=%d",a,b,a*b); int r,h;
} printf("enter the value of r and h is \n");
scanf("%d %d",Sr,Sh);
printf("%d * %d = %d",r,h,r*h);
}
Write a program of using division operator:
Model 3: (Without using extra variables) Model 4:(Without extra variable with
#include<stdio.h> scanf)
#include<conio.h> #include<stdio.h>
main() #include<conio.h>
{ main()
float a=15,b=20; {
printf("%f/%f=%f",a,b,a/b); int r,h;
} printf("enter the value of r and h is \n");
scanf("%d %d",Sr,Sh);
printf("%d / %d = %d",r,h,r/h);
}
Write a program of using Modulus operator:
Model 3: (Without using extra variables) Model 4:(Without extra variable with
#include<stdio.h> scanf)
#include<conio.h> #include<stdio.h>
main() #include<conio.h>
{ main()
int a=15,b=10; {
printf("%d %% %d=%d",a,b,a%b); int r,h;
} printf("enter the value of r and h is \n");
scanf("%d %d",Sr,Sh);
printf("%d %% %d = %d",r,h,r%h);
}
#include<stdio.h>
#include<conio.h>
main()
{
int a=10,b;
b=a;
printf("b=%d\n",b);
b+=a;
printf("b=%d\n",b);
b-=a;
printf("b=%d\n",b);
b*=a;
printf("b=%d\n",b);
b/=a;
printf("b=%d\n",b);
b%=a;
printf("b=%f\n",b);
}
Conditional operators:
It returns one value if condition is true and return another value if condition is false.
Syntax: (condition)?T:F
For practice:
Write a sample program of conditional operators with scanf
Relational operators:
Operator Example Description
“>” X>Y X is greater than Y
“<” X<Y X is less than Y
“>=” X>=Y X is greater than or equal to Y
“<=” X<=Y X is less than or equal to Y
“==” X==Y X is equal to Y
“!=” X!=Y X is not equal to Y
#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
scanf("%d%d",Sa,Sb);
printf("%d>%d=%d\n",a,b,(a>b));
printf("%d<%d=%d\n",a,b,(a<b));
printf("%d>=%d=%d\n",a,b,(a>=b));
printf("%d<=%d=%d\n",a,b,(a<=b));
printf("%d==%d=%d\n",a,b,(a==b));
printf("%d!=%d=%d\n",a,b,(a!=b));
getch();
}
Example 1: Example 2:
#include<stdio.h> #include<conio.h> #include<stdio.h> #include<conio.h>
main() main()
{ {
int a=5; int a=5;
printf("a is %d \n",++a); printf("a is %d \n",--a);
} printf("the value of a is %d",a);
}
Logical operator:
Operator Name Example Description
SS Logical AND (x>5)S(y<5) It returns true when both condition are true.
|| Logical OR (x>5)|(y<5) It returns true when at least condition is true.
! Logical NOT !((x>5)S(y<5)) It reverse the state of the operand
If “!((x>5)SS(y<5))” is true, logical NOT
operator makes it false.
X Y XSY X|Y
Write a sample program of logical operator:
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 1
For practice:
Write a sample program of logical operators with output values
Special operator:
This operators are ,,*,S, ,.,sizeof();............... ,
Sizeof(); :
Data types 16 bit 32 bit 64 bit
Int 2 4 8
Float 4 4 4
Char 1 1 1
Double 8 8 8
Bit wise operators in C are S-AND, |-OR, ^- XOR, ~- NOT, <<- left shift, >>- 0 shift.
Operator symbols Operator name
X Y XSY X|Y X^Y
S Bit wise AND
0 0 0 0 0
| Bit wise OR
0 1 0 1 1
^ Bit wise XOR
1 0 0 1 1
~ Bit wise NOT
1 1 1 1 0
>> Right shift
<< Left shift
64 32 16 8 4 2 1
1 1 0 0 0 1 0
For practice:
Convert 25 , 40 into binary values and write all bitwise values
Write a program of bit wise operators:
Expressions:
Sample program:
#include<stdio.h>
#include<conio.h>
main()
{
int a=2, b=3,c;
c=a+b*5;
printf(“the value of c is %d \n “, c);
}
Type casting and type conversion:
If it is type casting in program is used to modifies a variables from new data types should
be mentioned before a variable name and values in “()” it known as type conversion.
#include<stdio.h>
#include<conio.h>
main()
int a;
a=(int)10.12/4.52;
printf("a is %d",a);
}
LOOPS
A loop in programming comes into use when we need to repeatedly execute a block of
statements.
Loops are three types. They are
Example 1:
#include<stdio.h>
#include<conio.h>
main()
{
int a=10,b=20;
if(a==b)
{
printf("condition is satisfied");
}
getch();
}
For practice:
Write a sample program of if using different conditions and see the output
If_else statements:
In this statements when the if condition becomes false then the else part is printed. When the
if condition is true then the else part is not printed.
Syntax:
if(condition)
{
Stmts;
}
Else
{
Stmts;
}
Example 1:
#include<stdio.h>
#include<conio.h>
main()
{
int a=10,b=20;
if(a==b)
{
printf("condition is satisfied");
}
else
{
printf("not satisfied");
}
}
For practice:
Write a sample program of if else using different conditions and see the output
In this statements if condition one is false then the condition two is checked and statements are
executed if it is true. When condition two is also false then else part will be executed.
Syntax:
if(condition)
{
Stmts;
}
else if(condition)
{
Stmts;
}
else
{
Stmts;
}
Example 1:
#include<stdio.h>
#include<conio.h>
main()
{
int a;
for(a=20;a>10;a--)
{
printf("%d \n",a);
}
}
For practice:
Write a sample program with for loop using different operator
Write a sample program to print squares of numbers from 1 to 10
While loop : In this statements loop is executed until condition becomes false.
Syntax:
while(condition)
{
Stmts;
Increments or decrements;
}
Write a sample program of while loop program:
Example 1:
#include<stdio.h>
Example 2(even numbers)
#include<conio.h>
#include<stdio.h>
main()
#include<conio.h>
{
main()
int i=1;
{
while(i<10)
int i=1;
{
while(i<=10)
printf("%d \n",i);
{
i++;
printf("%d \n",i*2);
}
i++;
}
}
}
Do while loop:
In this loop is executed for the first time it ignores the condition after execution of the while
loop for the first time the condition will be checked.
Syntax:
do
{
Stmts;
Increment or decrement;
}
while(condition)
Switch case: In these statements which are used to execute only specific block of statements is
a series of block are called case control statements.
Syntax:
Switch(condition)
{
Case 1:stmts;
Case 2:stmts;
Case 3:stmts;
default: stmts;
}
Break case:
It is mainly used in 2 ways in c programming. The first one is used to terminate the loop, the
second one is used for true condition in the switch.
Syntax: break();
Write a sample program using of switch case and break case:
Example 1:
#include<stdio.h>
#include<conio.h>
main()
{
int num;
scanf("%d",Snum);
switch(num)
{
case 1:
printf("hello");
break;
case 2:
printf("hai");
break;
case 3:
printf("happy");
break;
case 4:
printf("isan");
break;
default:
printf("none of the above");
}
}
Example 2:
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c,j;
printf("enter two values \n");
scanf("%d %d",Sa,Sb);
printf("case 1:Add\n case 2:sub\n case 3:mul\n case 4:div\n case 5:mod\n");
printf("enter the case user want \n");
scanf("%d",Sj);
switch(j)
{
case 1:c=a+b;
printf("the value of c is %d \n",c);
break;
case 2:c=a-b;
printf("the value of c is %d \n",c);
break;
case 3:c=a*b;
printf("the value of c is %d \n",c);
break;
case 4:c=a/b;
printf("the value of c is %f \n",c);
break;
case 5:c=a%b;
printf("the value of c is %d \n",c);
break;
default:
printf("there is no values \n");
}
}
Example 3: vowels program
#include<stdio.h>
#include<conio.h>
main()
{
char i;
printf("Enter the charcter \n");
scanf("%c",Si);
switch(i)
{
case 'a':
printf("It is vowel");
break;
case 'e':
printf("It is vowel");
break;
case 'i':
printf("It is vowel");
break;
case 'o':
printf("It is vowel");
break;
case 'u':
printf("It is vowel");
break;
default:
printf("It is not a vowel");
}
}
Continue:
In continue, statements are used to continue the next iteration for loop, while loop and
so that the remaining statements are striked with in the loop for the particle interaction.
Syntax: Continue;
Write a sample program of continue:
#include<stdio.h>
#include<conio.h>
main()
{
int a=15;
do
{
if(a==20)
{
a++;
continue;
}
printf("value of a: %d \n", a);
a++;
}
while(a<25);
}
Goto statements:
In this statements is used to transform the a normal flow a program to specific table program.
Syntax:
goto label
stmts;
stmts;
label
Write a sample program of goto program:
#include<stdio.h>
#include<conio.h>
main()
{
printf("www.");
goto x;
y:
printf("expert");
goto z;
x:
printf("c programming");
goto y;
z:
printf(".com");
}
Arrays
1. An array is a data structure containing no. of data values which have the same type. These
values are knows as elements. It can be individuals sections by their position within the
array.
2. An array is a variable which is capable of holding many values where as that are older
variables can hold a single value at a time.
3. An array is a data structure in c, that can store a fixed size and sequential collection of
elements of same data type.
4. For example if u want to store 10 members then instead of defining 10 variables its easy to
define an array of 10 length.
10 15 19 20 17
#include<conio.h>
main()
int a[2][2],b[2][2],c[2][2],i,j;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",Sa[i][j]);
}
printf("\nEnter Array B elements\n");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",Sb[i][j]);
for(i=0;i<2;i++)
for(j=0;j<2;j++)
c[i][j]=a[i][j]+b[i][j];
for(i=0;i<2;i++)
for(j=0;j<2;j++)
printf("%3d",c[i][j]);
printf("\n");
}
Multiplication of matrices:
#include <stdio.h>
#include<conio.h>
int main()
{
int a[2][2]={1,2,3,4}, b[2][2]={1,2,3,4}, c[2][2]={0,0,0,0}, i, j, k;
for(i=0; i<2; ++i)
{
for(j=0; j<2; ++j)
{
for(k=0; k<2; ++k)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
printf("Output Matrix\n");
for(i=0; i<2; ++i)
{
for(j=0; j<2; ++j)
{
printf("%3d", c[i][j]);
}
printf("\n");
}
getch();
}
For practice :
Write a sample program of multi dimensional array without using loops
Strings
1. Strings are nothing but array of characters and entered with null characters [‘\0’]. This null
character indicates the end of the string.
2. String are always enclosed by (“---”) double equations were as character is enclosed by
string single quotation (‘ --- ‘).
Declaration:
Data type string name [string size];
Ex: char s[10];
Initialization of string:
char s[10]={‘H’,’E’,’L’,’L’,’O’};
char s[6]=”hello”;
char s[]=”hai”;
Difference between above Declarations is when we declare char s[10] 10 bytes memory space
is allocated for holding the string value.
When we declare as s[ ]memory space will be allocated as per the required during for the
program.
Example 1: Example 2:
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
#include<string.h> #include<string.h>
main() main()
{ {
char s1[10]=”hello”; char s1[10]={'h','e','l','l','o'};
printf(“s1=%s \n”,s1); printf("s1=%c \n",s1[0]);
} }
String functions:
➢ strcat ➢ strlen
➢ strncat ➢ strupr
➢ strcpy ➢ strlwr
➢ strncpy ➢ strrev
➢ strcmp ➢ strset
String concatenation:
In this function concatenation the position of one string is placed at the end of the other string.
Syntax: strcat(s1,s2);
Write a sample program of String concatenation:
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char s1[10]="hello";
char s2[10]="everyone";
strcat(s1,s2);
printf("s1=%s \n",s1);
}
String unconcatenation:
It is also same as String concatenation but it will declare the length of the s1 or s2. So it will
print the length position.
Syntax: strncat(s1,s2,5);
Write a sample program of String unconcatenation:
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char s1[10]="hello";
char s2[10]="everyone";
strncat(s1,s2,5);
printf("s1=%s \n",s1);
}
String copy function:
It copies the s1 content to the s2 content. In this s1 content has 30 bytes of memory and s2 has
declared 20 bytes of memory only means. It will only print 20 characters.
Syntax: strcpy(s1,s2);
Write a sample program of String copy function:
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char s1[10]="hello";
char s2[10]="lucky";
strcpy(s1,s2);
printf("s1=%s \n",s1);
}
String uncopy function:
In this it will copy the lengthened position which will be mentioned in the function.
Syntax: strncpy(s2,s3,n);
Write a sample program of String uncopy function:
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char s1[10]="hello";
char s2[10]="lucky";
strncpy(s1,s2,3);
printf("s1=%s \n",s1);
}
String length:
It will show the length of your given string
Syntax: len=strlen(s);
Write a sample program of string length:
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char s1[10]="hello";
int len;
len=strlen(s1);
printf("len=%d \n",len);
}
String reverse:
It reverses the word which is given in a string.
Syntax: strrev(s);
Write a sample program of String reverse:
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char s1[10]="apple";
strrev(s1);
printf("s1=%s \n",s1);
}
String lower:
In this string lower it will print the higher level language to the lower level language.
Syntax: strlwr(s);
Write a sample program of String lower:
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char s1[10]="HELLO";
strlwr(s1);
printf("s1=%s \n",s1);
}
String upper:
In this function it will change lower case to upper case. Syntax: strupr(s)
Write a sample program of String upper:
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char s1[10]="HELLO";
strupr(s1);
printf("s1=%s \n",s1);
}
String compare:
In this function s1 and s2 are same It will print 0. If s1 is less than s2 it will print -1. If s1
is greater than s2 it will print 1.
Write a sample program of String compare:
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char s1[10]="apple";
char s2[10]="app";
int x,y,z;
x=strcmp(s1,s2);
y=strcmp(s2,"app");
z=strcmp(s1,"app");
printf("x=%d \n",x);
printf("y=%d \n",y);
printf("z=%d \n",z);
}
String set:
In this program of string “ hello” is said to ‘S’ using string set function and output will be
display like ‘SSSSS’ .
Write a sample program of String set:
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char s1[7]="orange";
strset(s1,'*');
printf("s1=%s \n",s1);
}
Write a program with all string functions and write the output
Functions
A function is a self contained block or a program of one or more statements that perform a
special task. In this function we have 2 types:
In built function
User defined function
1. In built function: This function is also called as library function. This function cannot be
changed by a user.
Example: Printf(); , Scanf(); ,main()……………
2. User defined function: In this function user can give the unique name to a function and to
create a function by using functional elements.
Example: variables , constants………
3. Uses of function: If we want to perform a task continuously then it is not necessary to
rewrite the program again and again. If we shift the particular statements in a user define
function. So it can be used many no of times.
4. Elements of user define function: They are 3 types:
Declaration function.
Called function.
Define function.
5. Declaration of a function: They are 4 types:
Function type
Function name
Parameter list
Semicolon(;)
6. Function type: They are 2 types:
Return type
Non return type
7. Return type: Int, float, char, double in this it only return one value at a time is known as
return type.
8. Non return type: Void is a non return type. It does not return a any value.
9. Function name: In this function name user can give a name to the function. Ex: int isan( );
10. Parameter list: It means how many inputs user giving or receiving is known as parameter
list. This parameter list we also called as arguments.
11. They are 2 types of parameter lists:
Actual parameter list
Formal parameter list
12. Actual parameter list: The arguments of calling function are known as Actual arguments.
Example: actual(10,20);
13. Formal parameter list: The arguments are called function is known as formal parameter
list but it copies the actual arguments values is also known as formal arguments.
Example: formal(int a, int b)
14. Semicolon: The terminating is used to terminate the function.
15. Function declaration:
int hello( );
int hello(int a,int b);
void hello( );
void hello (int a) ; semicolon
Syntax:
void name()
{
variables;
stmts;
return type
}
Example:
#include<stdio.h>
#include<conio.h>
void hello()
{
int a=10;
printf(“a=%d \n”, a);
}
main()
{
hello();
hello();
}
Recursion: A function that calls itself is known as a recursive function. And, this technique is known as
recursion.
void recurse( )
{
... .. ...
recurse( );
... .. ...
}
int main( )
{
... .. ...
recurse( );
... .. ...
}
#include <stdio.h>
int sum(int num);
int main()
{
int num, result;
printf("Enter a positive integer: ");
scanf("%d", &num);
result = sum(num);
printf("sum = %d", result);
return 0;
}
int sum(int num)
{
if (num!=0)
return num + sum(num-1); // sum() function calls itself
else
return 0;
}
Call by value and Call by reference in C
There are two methods to pass the data into the function in C language, i.e., call by value and call by
reference.
#include <stdio.h>
int main()
int a = 10;
int b = 20;
swap(a,b);
int temp;
temp = a;
a=b;
b=temp;
printf("a = %d, b = %d\n",a,b);
#include <stdio.h>
int main()
int a = 10;
int b = 20;
swap(&a,&b);
int temp;
temp = *a;
*a=*b;
*b=temp;
}
Storage classes
A function can access a variable when it declared in a specific block other function
outside cannot be accessed. The space of a variable depends upon its storage classes.
They are 4 types:
Auto (Automatic)
External variable
Static variable
Register variable
Register variable:
1. It is also local variable but stored in the memory were as auto variable are stored in the
main memory.
2. Register variable will be accessed very faster the normal variable. Since they are stored in
register memory rather than main memory.
3. But only limited variable can be used as a register memory since it can used register size is
very low.
Pointers
About the pointer:
1. A pointer is a variable that stores the address of another variable. C pointer is used to
allocate memory dynamically that is run time.
2. The pointer variable might be belonging to the data type such as int, float, char, double.
3. Normal variable store the value where as pointer variable stores the address of the variable
4. The content of the ‘C’ pointer always be a whole number that is address.
5. Always c pointer is initialized to null that is “int *p= null”. The null pointer is zero ‘0’.
Addresses of values:
%u (unsigned)
%X (HEXA DECIMAL)
%x (hexa decimal)
a means value of a
Sa means address of a
p means pointer value (address)
*p means value of a
}
Example 2:
#include<stdio.h>
#include<conio.h>
main()
{
int a[4]={10,20,30,40},*p;
p=Sa[4];
// the following gives values in a (pointer format)
printf("a[0]=%d \n",*(a+0));
printf("a[1]=%d \n",*(a+1));
printf("a[2]=%d \n",*(a+2));
printf("a[3]=%d \n",*(a+3));
// the following gives address of a (pointer format)
printf("a[0]=%u \n",(a+0));
printf("a[1]=%u \n",(a+1));
printf("a[2]=%u \n",(a+2));
printf("a[3]=%u \n",(a+3));
}
Structure
1. Structure is a collection of different data type which are grouped together and each in a
structure is called member. Structure is a user defined data type.
2. If you want access a structure member in C. Structure variable should be declared.
3. Many structure variables can be declared for some structure and memory with the allocated
for each separately.
4. A structure is a collection of one or more variable of different data type grouped together
under a single name.
Syntax:
struct member
{
Datatype variable;
‘’’’’’’’’’’’’’’’’’’’’’’’’’’’
};
Declaration of structure:
struct student
{
char name[20];
int roll no;
float avg;
};
All the members of a structure are the related to the structure variable members are s1.name.
The dot(.) sign is used to access the structure member.
Write a sample program of structure:
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
char name[20];
int rollno;
float avg;
};
main()
{
struct student s1={"asdf",521,92.3};
struct student s2={"hjkl",522,95.3};
printf("name=%s\nrollno=%d\navg=%f\n",s1.name,s1.rollno,s1.avg);
printf("name=%s\nrollno=%d\navg=%f\n",s2.name,s2.rollno,s2.avg);
getch();
}
Structure with pointer:
In c language pointer can also be applied to structure that is we can have pointers pointing to
the structure in such the same way we have pointer pointing to int, float, char, double .......... , in
such that pointer are known as structure pointer. Structure elements can be accessed by
structure variable and a dot operator are by using structure in arrow operator is used.
Nested structure:
A structure with another structure is called nested structure.
Unions
A union is a special data type available in C that allows to store different data types in the same
memory location. You can define a union with many members, but only one member can
contain a value at any given time. Unions provide an efficient way of using the same memory
location for multiple-purpose.
Defining a Union
To define a union, you must use the union statement in the same way as you did while defining
a structure. The union statement defines a new data type with more than one member for your
program.
Syntax :
union [union tag]
{
member definition;
member definition;
...
member definition;
} [one or more union variables];
Example :
union Data
{
int i;
float f;
char str[20];
} data;
The memory occupied by a union will be large enough to hold the largest member of the union.
For example, in the above example, Data type will occupy 20 bytes of memory space because
this is the maximum space which can be occupied by a character string. The following example
displays the total memory size occupied by the above union −
#include <stdio.h>
#include<conio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
main( )
{
union Data data;
printf( "Memory size occupied by data : %d\n", sizeof(data));
}
When the above code is compiled and executed, it produces the following result −
Memory size occupied by data: 20
Here, we can see that the values of i and f members of union got corrupted because the final
value assigned to the variable has occupied the memory location and this is the reason that the
value of str member is getting printed very well.
Same example once again where we will use one variable at a time which is the main purpose of
having unions −
#include <stdio.h>
#include<conio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
data.i = 10;
printf( "data.i : %d\n", data.i);
data.f = 220.5;
printf( "data.f : %f\n", data.f);
strcpy( data.str, "C Programming");
printf( "data.str : %s\n", data.str);
}
When the above code is compiled and executed, it produces the following result −
data.i : 10
data.f : 220.500000
data.str : C Programming