Você está na página 1de 35

Pointers

A pointer is a variable it holds the value and address of variable, i.e., direct address of the
memory location. Like any variable or constant, you must declare a pointer before you can use it
Syntax:Datatype *pointername;
to store any variable value and address.
int *a,x;
x=10;
a=&x;
x,*a values and addresses are same. Because if you remove the * pointer holds the value and
address of the variable.
&x means it sends the value and address also.
int *b,y;
y=20;
*b=y;
y,*b values are same. Because if you use * for pointer it holds the value but not the address.
y means it sends the value it willnot sends the address.
For printing the address we use %u.
#include<stdio.h>
main()
{
int *a,*b,x,y;
x=10;
y=20;
clrscr();
a=&x;

*b=y;
printf("\nThe *a value is %d and address is %u",*a,a);
printf("\nThe x value is %d and address is %u",x,&x);
printf("\nThe *b value is %d and address is %u",*b,b);
printf("\nThe y value is %d and address is %u",y,&y);
getch();
}
Every pointer occupies 2 bytes.
#include<stdio.h>
main()
{
int *a;
char *b;
float *c;
double *d;
clrscr();
printf("\nThe Integer pointer occupies %d bytes",sizeof(a));
printf("\nThe Character pointer occupies %d bytes",sizeof(b));
printf("\nThe float pointer occupies %d bytes",sizeof(c));
printf("\nThe double pointer occupies %d bytes",sizeof(d));
getch();
}
WAP findout hcf of two numbers with using pointers.
#include<stdio.h>
main()

{
int *num1,*num2,i,*hcf;
clrscr();
printf("Enter two numbers");
scanf("%d%d",num1,num2);
for(i=1;i<=*num1||i<=*num2;i++)
{
if(*num1%i==0 && *num2%i==0)
*hcf=i;
}
printf("H.C.F of %d and %d is %d",*num1,*num2,*hcf);
getch();
}
WAP findout lcm of two numbers with using pointers.
#include<stdio.h>
main()
{
int *num1,*num2,i,*hcf,*lcm;
clrscr();
printf("Enter two numbers");
scanf("%d%d",num1,num2);
for(i=1;i<=*num1||i<=*num2;i++)
{
if(*num1%i==0 && *num2%i==0)
*hcf=i;

}
*lcm=(((*num1)*(*num2))/(*hcf));
printf("L.C.M of %d and %d is %d",*num1,*num2,*lcm);
getch();
}
Pointer Arithmetic:There are four arithmetic operators that can be used on pointers: ++,--, +, #include<stdio.h>
main()
{
int *p,a,b,*q;
clrscr();
printf("Enter a,b values");
scanf("%d%d",&a,&b);
*p=a;
q=&b;
printf("\nThe a value is %d address is %u",a,&a);
printf("\nThe *P value is %d address is %u",*p,p);
printf("\nThe b value is %d address is %u",b,&b);
printf("\nThe *q value is %d address is %u",*q,q);
a++;
b++;
printf("\nThe a value is %d address is %u",a,&a);
printf("\nThe *P value is %d address is %u",*p,p);
printf("\nThe b value is %d address is %u",b,&b);
printf("\nThe *q value is %d address is %u",*q,q);

p++;
q++;
printf("\nThe a value is %d address is %u",a,&a);
printf("\nThe *P value is %d address is %u",*p,p);
printf("\nThe b value is %d address is %u",b,&b);
printf("\nThe *q value is %d address is %u",*q,q);
getch();
}
If you increment a variable it increments the value if you increment a pointer it increments the
address.
Pointers using arithmetic operators
#include<stdio.h>
main()
{
int *a,*b,p,q;
clrscr();
printf("Enter two numbers");
scanf("%d%d",&p,&q);
a=&p;
*b=q;
printf("\nThe addition of two nubmers is %d",*a+*b);
printf("\nThe difference of two numbers is %d",*a-q);
printf("\nThe product of two numbers is %d",p**b);
printf("\nThe divisiob of two numbers is %d",(*a)/(*b));
getch();

}
Pointers with Arrays:- Array itself is converted to a pointer.
a[i] is represented as *(a+i)
&a[i] is represented as (a+i)
Array reading and printing with pointer style
#include<stdio.h>
main()
{
int a[10],i;
clrscr();
printf("Enter an array values:");
for(i=0;i<5;i++)
scanf("%d",(a+i));
printf("\nThe array elements are:");
for(i=0;i<5;i++)
printf("\n%d\t%u",*(a+i),(a+i));
}
#include <stdio.h>
const int MAX = 3;
main ()
{
int var[] = {10, 100, 200};
int i, *ptr;
/* let us have array address in pointer */
ptr = var;
for ( i = 0; i < MAX; i++)
{
printf("Address of var[%d] = %u\n", i, ptr );
printf("Value of var[%d] = %d\n", i, *ptr );
/* move to the next location */
ptr++;
}

}
wap to findout the smallest number using pointers.
#include<stdio.h>
main()
{
int a[10],i,*p;
clrscr();
printf("Enter an array values:");
for(i=0;i<5;i++)
scanf("%d",(a+i));
*p=*(a+0);
for(i=1;i<5;i++)
{
if(*(a+i)<*p)
*p=*(a+i);
}
printf("\nThe smallest element in the array list is %d",*p);
getch();
}
NULL Pointers in C:This is done at the time of variable declaration. A pointer that is assigned NULL is called a null
pointer. The NULL pointer is a constant with a value of zero defined in several standard
libraries.
#include <stdio.h>
void main ()
{
int *ptr = NULL;
printf("The value of ptr is : %d\n", *ptr );
}

Pointers with strings:String itself is converted to a pointer.


a[i] is represented as *(a+i)
&a[i] is represented as (a+i)
Wap to count the no.of characters in the string without counting spaces.
#include<stdio.h>
main()
{
char a[20];
int i,c=0;
clrscr();
printf("Enter a string:");
gets(a);
for(i=0;*(a+i)!='\0';i++)
{
if(*(a+i)!=32)
c++;
}
printf("\nThe no.of characters in the given string without spaces is %d",c);
getch();
}
Wap to print the given string in vertical.
#include<stdio.h>
main()
{

char a[20];
int i;
clrscr();
puts("Enter a string");
gets(a);
printf("\nThe string in vertical is:");
for(i=0;*(a+i)!='\0';i++)
printf("\n%c",*(a+i));
}
WAP read a string print the no.of characters, no.of capitals, no.of smalls, no.of digits, no.of
vowels, no.of consonents, no.of words.
#include<stdio.h>
main()
{
char a[50];
int i,nc,ns,nw,nd,tc,nv,nco;
clrscr();
nc=ns=nd=tc=nw=nv=nco=0;
puts("Enter a string");
gets(a);
for(i=0;*(a+i)!='\0';i++)
{
if(*(a+i)=='a'||*(a+i)=='e'||*(a+i)=='i'||*(a+i)=='o'||*(a+i)=='u')
nv++;
else

nco++;
if(*(a+i)>=65 && *(a+i)<=90)
{
nc++;
tc++;
}
else if(*(a+i)>=97 && *(a+i)<=122)
{
ns++;
tc++;
}
else if(*(a+i)>=48 && *(a+i)<=57)
{
nd++;
tc++;
}
else if(*(a+i)==' ')
{
nw++;
tc++;
}
}
printf("\nThe no.of characters in the string is %d",tc);
printf("\nThe no.of capitals in the string is %d",nc);
printf("\nThe no.of smalls in the string is %d",ns);

printf("\nThe no.of vowels in the string is %d",nv);


printf("\nThe no.of consonents in the string is %d",nco);
printf("\nThe no.of digits in the string is %d",nd);
printf("\nThe no.of words in the string is %d",nw+1);
getch();
}
Call by value:- When formal parameters are modified actual parameters will not get any effect.
That is nothing but call by value. Called function parameters are actual parameters.
Implementation function parameters are formal parameters.
Wap to perform addition by adding 10.
#include<stdio.h>
void add(int x)
{
x=x+10;
}
main()
{
int p;
printf("Enter one value");
scanf("%d",&p);
printf(Before calling p=%d,p);
add(p);
printf(After calling p=%d,p);
getch();
}
Pointer with Functions:-

Call by reference:- When formal parameters are modified actual parameters will get any effect.
That is nothing but call by Reference. Called function parameters are actual parameters.
Implementation function parameters are formal parameters.
Wap to perform addition by adding 10.
#include<stdio.h>
void add(int *x)
{
*x=*x+10;
}
main()
{
int p;
printf("Enter one value");
scanf("%d",&p);
printf(Before calling p=%d,p);
add(&p);
printf(After calling p=%d,p);
getch();
}
WAP to perform swapping
#include<stdio.h>
void swap(int *x,int *y)
{
*x+=*y;
*y=*x-*y;
*x-=*y;

}
main()
{
int p,q;
printf("Enter two values");
scanf("%d%d",&p,&q);
printf("\nBefore swapping p=%d and q=%d",p,q);
swap(&p,&q);
printf("\nAfter swapping p=%d and q=%d",p,q);
getch();
}
Pointer with pointer:- variable value and address can hold by pointer. Pointer value and address
can hold by pointer with pointer.
#include<stdio.h>
main()
{
int x,*p,**px;
clrscr();
x=10;
p=&x;
px=&p;
printf("\nx value is %d and address is %u",x,&x);
printf("\n*p value is %d and address is %u",*p,p);
printf("\n**px value is %d and address is %u",**px,*px);
getch();

}
UNIONS
Union is a keyword. Union is a user defined data type. A union is a collection of
variables that is referenced under a single name. A union contains a number of data types
grouped together. These data types may or may not be of the same type. Union occupies
max. no.of bytes. To know the size of the union and accessing the union variables we
need union object. we use union keyword for declaring a union. Union always ends with
semicolon. We can declare a union before a main() or within the main().
Declaring a union:Syntax:union <union-name>
{
union variable 1;
union variable 2;
--union variable n;
}union Objects;
we can create no.of objects for a union. We can create a union object before union ends
with semicolon or in main().
With in the main() if you want to create a union object use the following syntax.
<union> <union-name> <union-object>;
#include<stdio.h>
union swarna
{
int eno;
char a;
float sal;

};
main()
{
union swarna s;
clrscr();
printf("\nUnion occupies %d bytes",sizeof(s));
printf("Enter employee number,name,salary");
scanf("%d",&s.eno);
printf("The Emp No is: %d",s.eno);
s.a=getch();
printf("\nName is :%c",s.a);
scanf("%f",&s.sal);
printf("\nSalary is :%f",s.sal);
getch();
}
STRUCTURES
Structure is a user defined data type. A structure is a collection of variables that is
referenced under a single name. A structure contains a number of data types grouped
together. These data types may or may not be of the same type. Structure occupies total
no.of bytes. To know the size of the structure and accessing the structure variables we
need structure object. we use struct keyword for declaring a structure. Structure always
ends with semicolon. We can declare a structure before a main() or within the main().
Declaring a structure:Syntax:struct <structure name>
{
structure element 1;

structure element 2;
--structure element n;
};
structure Objects;
we can create no.of objects for a structure. We can create a structure object before
structure ends with semicolon or in main().
With in the main() if you want to create a structure object use the following syntax.
<struct> <structure-name> <structure-object>;
Wap read and print the employee information with using structures.
#include<stdio.h>
struct sai
{
int eno,sal;
char nm[20];
}s;
main()
{
clrscr();
printf("\nStructure occupies %d bytes",sizeof(s));
printf("\nEnter eno,name,salary:");
scanf("%d%s%d",&s.eno,&s.nm,&s.sal);
printf("\nEmployee details");
printf("\nEmployee Number is %d",s.eno);
printf("\nEmployee Name is %s",s.nm);

printf("\nEmployee Salary is %d",s.sal);


getch();
}
Structures with Array:- In the structure object if you specify the size is nothing but structure with
array.
Wap read and print the 3 students information and print with using structures using arrays.
#include<stdio.h>
main()
{
struct student
{
int sno,s1,s2,s3,tot;
char nm[10];
};
struct student s[3];
int i;
clrscr();
printf("\nThis Structure occupies %d bytes",sizeof(s));
for(i=0;i<=2;i++)
{
printf("Enter student no,name,subject1,subject2,subject3 marks:");
scanf("%d%s%d%d%d",&s[i].sno,&s[i].nm,&s[i].s1,&s[i].s2,&s[i].s3);
}
for(i=0;i<=2;i++)
{

printf("\n--------------------");
printf("\nSTUDENT INFORMATION");
printf("\n---------------------");
printf("\nStudent No=%d",s[i].sno);
printf("\nStudent Name=%s",s[i].nm);
printf("\nStudent Subject1=%d",s[i].s1);
printf("\nStudent Subject2=%d",s[i].s2);
printf("\nStudent Subject3=%d",s[i].s3);
s[i].tot=s[i].s1+s[i].s2+s[i].s3;
printf("\nStudent Total=%d",s[i].tot);
if(s[i].s1>34 && s[i].s2>34 && s[i].s3>34)
printf("\nPass");
else
printf("\nFail");
printf("\n----------------------");
}
getch();
}
structure with Pointers:- In the structure object if you specify the pointer that is nothing but
structure with pointer.
WAP to calculate the student total with using structure with pointer.
#include<stdio.h>
main()
{
struct samp

{
int sno,sub1,sub2,sub3,tot;
char n[10];
}s,*p;
p=&s;
printf("Enter sno,name,sub1,sub2,sub3 marks:");
scanf("%d%s%d%d%d",&p->sno,&p->n,&p->sub1,&p->sub2,&p->sub3);
p->tot=p->sub1+p->sub2+p->sub3;
printf("\nSTUDENT INFORMATION");
printf("\nSNO = %d",p->sno);
printf("\nSNAME = %s",p->n);
printf("\nSubject 1=%d",p->sub1);
printf("\nSubject 2=%d",p->sub2);
printf("\nSubject 3=%d",p->sub3);
printf("\nTotal =%d",p->tot);
}
Note:- * and . is replaced with -> operator.
Structure with functions:-In the function if you pass the structure object as the parameter that is
nothing but structure with functions. For working with structure with functions structure is
before the main() is compulsary. Calling function doesnt required. Return type doesnt required.
Wap read 4 numbers and check those are equal or not.
#include<stdio.h>
struct samp
{
int m,n,o,p;
}s,s1;

main()
{
printf("Enter 4 numbers:");
scanf("%d%d%d%d",&s.m,&s.n,&s1.o,&s1.p);
fun(s,s1);
}
fun(struct samp u,struct samp u1)
{
if(u.m==u1.o && u.n==u1.p)
printf("\nBoth values are Equal.");
else
printf("\nBoth values are not Equal.");
}
Structure with structure:- In the structure if u create another structure is called structure with
structure.
Or
In one structure if you create object for another structure is called structure with structure.
#include<stdio.h>
main()
{
struct emp
{
int eno;
char n[10];
struct employee

{
int sal;
}s;
}e;
clrscr();
printf("Enter eno,ename,salary:");
scanf("%d%s%d",&e.eno,&e.n,&e.s.sal);
printf("\nEMPLOYEE INFORMATION");
printf("\nENO = %d",e.eno);
printf("\nENAME = %s",e.n);
printf("\nSalary =%d",e.s.sal);
getch();
}
Wap to findout the biggest number among two numbers.
#include<stdio.h>
struct samp
{
int n;
};
struct sample
{
int m;
struct samp s;
}t;
main()

{
clrscr();
printf("Enter two numbers:");
scanf("%d%d",&t.s.n,&t.m);
if(t.s.n>t.m)
printf("\n%d is a Big.",t.s.n);
else
printf("\n%d is a Big.",t.m);
}
FILES
A file is a collection of information stored on a storage device with a particular file name.
Uses of a file:1) Stores the data in the form of a file and we can retrieve it whenever we require.
2) Using the data from the file in differentprograms.
Opening a file:- we can open a file by using
fopen().
Syntax:- fopen(file name, opening mode);
Ex: - FILE *fp;
fp=fopen(sssit.txt,w);
File opening modes:1)w :- Writing the data into a file. If the file already exists its contents will be over
written.
2)r:- Reading data from a file.
3)a:- Adds data into an existing file.(Appending)
closing a file:When the file processing is over, the file must be closed. We can close
a file by using fclose( ).

syntax:- fclose(file pointer);


ex:- fclose(fp);
File functions:1)fputc()
This function is used to write a character into a file.
syntax:-

fputc(character, file pointer);

ex:- fputc(ch,fp);
Write a program write a character to the file.
#include<stdio.h>
main()
{
FILE *fp;
char a;
clrscr();
fp=fopen("1.txt","w");
printf("Enter a character:");
scanf("%c",&a);
fputc(a,fp);
fclose(fp);
printf("Character copied to the File.");
getch();
}
Write a program add a character to the existing file.
#include<stdio.h>
main()

{
FILE *fp;
char a;
clrscr();
fp=fopen("1.txt","a");
printf("Enter a character:");
scanf("%c",&a);
fputc(a,fp);
fclose(fp);
printf("Character copied to the File.");
getch();
}

2)fgetc()
This function is used to read a character from a file.
syntax :- getc(file pointer);
ex:- fgetc(fp);
Wap read a character from the file.
#include<stdio.h>
main()
{
FILE *fp;
char c;
clrscr();
fp=fopen("1.txt","r");

if(fp==NULL)
{
printf("Unable to open the file.");
exit(0);
}
else
c=fgetc(fp);
printf("\nThe character is %c",c);
fclose(fp);
getch();
}
WAP write a line of data to the file.
#include<stdio.h>
main()
{
FILE *fp;
char c;
clrscr();
fp=fopen("ss.txt","a");
printf("Enter a line of Data:");
while(1)
{
scanf("%c",&c);
if(c!='\n')
fputc(c,fp);

else
break;
}
printf("Data Copied to the file");
getch();
}
WAP read line of data to the file.
#include<stdio.h>
main()
{
FILE *fp;
char c;
clrscr();
fp=fopen("ss.txt","r");
printf("The line of Data is :");
while(1)
{
c=fgetc(fp);
if(c!=EOF)
printf(%c,c);
else
break;
}
getch();
}

3)fprintf():- This function writes formatted data into a file.


Syntax:Ex:-

fprintf(file pointer, formatted string, list of variables)

fp=fopen(student.dat,w);

fprintf (fp , %d %s %d,sno ,name, marks);


WAP write employee information(empno,name,sal) to the file.
#include<stdio.h>
main()
{
int eno,sal;
char nm[20];
FILE *fp;
fp=fopen("sam.txt","a");
printf("Enter eno,name,sal");
scanf("%d%s%d",&eno,&nm,&sal);
fprintf(fp,"\n%d\t%s\t%d",eno,nm,sal);
printf("File Written.");
getch();
}
WAP write employee information (empno,name,sal,bonus) to the file.
#include<stdio.h>
main()
{
int sal,bo,ns;
char nm[20];
FILE *fp;

fp=fopen("sssit.txt","a");
printf("Enter name,sal");
scanf("%s%d",&nm,&sal);
if(sal>10000)
{
bo=1000;
ns=sal+bo;
}
else
{
bo=500;
ns=sal+bo;
}
fprintf(fp,"\n%s\t%d\t%d\t%d",nm,sal,bo,ns);
printf("File Written.");
getch();
}
4)fscanf():- This function reads formatted data from a file.
Syntax:-

fscanf(file pointer, formatted string, list of variables)

Ex:- fp=fopen(student.dat,r);
fscanf(fp , %d %s %d,&sno ,name,&marks);
Wap to read the employee information with their net salaries from the file.
#include<stdio.h>
main()
{

int sal,bo,ns;
char nm[20];
FILE *fp;
clrscr();
fp=fopen("sssit.txt","r");
if(fp==NULL)
printf("Unable to open the file");
else
printf("\nEmployee Information\n");
while(fscanf(fp,"%s%d%d",&nm,&sal,&bo)!=EOF)
{
ns=sal+bo;
printf("\n%s\t%d\t%d\t%d",nm,sal,bo,ns);
}
getch();
}
C - Storage Classes
A variable is nothing but space to store the data.
Every variable will occupy some space in the main memory that is RAM.
In the System we have 3 temporary memory areas.
1. RAM
2. Registers
3. Cache Memory
The registers are less capacity in the memory which is used for storing the file related to the
system resources.

The files which are registered in the registries which are executed very fast.
If we declare a variable in the memory, usually the variable will stores in the RAM
memory. The RAM is having 4 organizations.
1. Code Area 2. Global Area or Static Area 3. Stack Area 4. Heap Area
The memory Management is a process of allocating & de allocating the memory. It will be
done in 2 ways.
1. Static Memory Management
2. Dynamic Memory Management
1. Static Memory Management:- The Static Memory Management is the process of
allocating the memory for the variable by the time of compilation Implicitly.
2. Dynamic Memory Management:- The Dynamic Memory Management is a process
of allocating the memory for the variables by the time of execution explicitly. IT will
taken place in the Heap Organization.
Based on our programming requirements we can select any of the
organization in the memory for the variables for different reasons. In order to select
various organizations in the RAM memory we have to use the following storage
classes.
A storage class defines the scope (visibility) and life time of variables and/or functions within a
C Program.
There are following storage classes which can be used in a C Program

auto

register

static

extern

auto - Storage Class


auto is the default storage class for all local variables. The keyword for the automatic storage
class is auto. Memory locations for these variables are code area. The default value is garbage
value.

#include<stdio.h>
main()
{
auto int i;
auto char c;
auto float f;
printf("%d %c %f",i,c,f);
}
#include<stdio.h>
main(){
int i=0;
{
auto int a=20;
XYZ:;
printf("%d",a);
a++;
i++;
}
if (i<3)
goto xyz;
}
register - Storage Class
is used to define local variables that should be stored in a register instead of RAM. This means
that the variable has a maximum size equal to the register size (usually one word) and cant have
the unary '&' operator applied to it.
#include<stdio.h>

int main(){
register int a,b;
scanf("%d%d",&a,&b);
printf("%d %d",a,b);
}
static - Storage Class
static is the default storage class for global variables. The two variables below (count and road)
both have a static storage class. The keyword for the static class is Static. For these variable
memory is allocated in the static area. Default value for the variables is zero. The life of the
variable is persistent between the function call. For the static storage class variables, the memory
is reutilized but not reinitialized.
#include <stdio.h>
static char c;
static int i;
static float f;
static char *str;
int main(){
printf("%d %d %f %s",c,i,f,str);
return 0;
}
extern - Storage Class
Thekeyword for the external storage class is extern. For these variables memory is
allocated in the Global area.Default value for variables is zero. Life of the variable is
throughout program.
#include<stdio.h>
extern int i=2;
samp()

{
printf("The i value %d",i);
}
In a new file
#include<stdio.h>
#include "E:\ext.c"
main()
{
clrscr();
samp();
printf("\nThe square of a no is %d",i*i);
getch();
}
Typedef:- If you want to apply the keyword functionality to any name we use typedef keyword.
Syntax:- typedef keyword variable;
That keyword functionality is assigned to the variable.
#include<stdio.h>
main()
{
typedef int a;
a x,y,z;
clrscr();

x=10;
y=56;
z=x+y;
printf("\nSum = %d",z);
getch();
}
Enum:-enum is a keyword. Collection of strings are identifying with integer constants.
Bydefault integer constant starts with 0. We can set any integer constant to integer constants.
#include<stdio.h>
enum colors
{
red=10,
green,
blue,
cyan
};
main()
{
int i;
printf("\nGreen color code is %d",green);
printf("\nColor codes are:");
for(i=red;i<=cyan;i++)

printf("\n%d",i);
}
Const:- Const is a keyword default value is garbage value. We can initialize the value at the time
of declaration. Other location initializations are not permitted.
#include<stdio.h>
main()
{
const int i=76;
clrscr();
printf("\nThe constant value is %d",i);
/*i=56;*/
getch();
}

Você também pode gostar