Java Script Notes of Class 12 Unit - 4
Java Script Notes of Class 12 Unit - 4
Programming in C
Usually, In C programs flows from top left to right bottom of main() functions. We
can create any number of functions below that main() functions. These function
are called from main() function. Requirement while creating a functions.
a) Declare a function
b) Call statement
c) Definition of function.
After the function is called from the main(), the flow of control will return to the
main() function.
Advantage:
Those function which calls itself is known as recursive function and the concept of
using recursive functions to repeat the execution of statements as per the
requirement is known as recursion. The criteria for recursive functions are:
Pointers in C are similar to as other variables that we use to hold the data in our
program but, instead of containing actual data, they contain a pointer to the
address (memory location) where the data or information can be found.
Importance of Pointer.
Pointer declaration
Data_type *variable_name
Advantages
2. It helps to reduce program length and complexity i.e. faster program execution
time.
4. It helps to return one or more than one value from the functions.
Program example:
#include
int main()
{
int n,*ptr;
printf("Enter any number");
scanf("%d",&n);
ptr =&n;
printf("Value is %d\n",*ptr);
return 0;
}
Structure Union
Memory allocation is the total sum of all Memory allocation is performed by sharing
elements. memory with highest datatype.
Can be used un complex data structure. Cannot be used un complex data structure.
Cannot be used to interact with hardware. Suitable for interacting with hardware.
Example: struct sample{ int a; float p; char Example: union sample{ int a; float p; char
n[10] } a; n[10] } a;
File Handling in C
Syntax:
FILE *fptr
Mode:
Store/write data
Syntax:
Eg; suppose if we want to store name, disease, age and bed number of a
patient then, it is written as
int a, b;
Program example
1) Create a datafile “patient.txt” and store name, disease, age and bed number of a
patient.
#include
int main()
{
char n[10], d[10];
int a, b;
FILE *fptr;
fptr = fopen(“patient.txt”,”w”);
printf("Enter name, disease, age and bed number");
scanf(“%s %s %d %d”, n, d, &a, &b);
fprintf(fptr,"%s %s %d %d\n”, n, d, a, b);
fclose(fptr);
return 0;
}
[Note: This program will store only single record to store multiple record we have
to use loop as following programs.
2) Create a datafile “student.txt” and store name, class and marks obtained in 3
different subject for few students/n-students.
#include
int main()
{
char n[10];
int c, e, ne, m, i, num;
FILE *fptr;
fptr = fopen("student.txt","w");
printf("How many record?");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
printf("Enter name class and 3 marks");
scanf("%s %d %d %d %d",n, &c, &e, &ne, &m);
fprintf(fptr,"%s %d %d %d %d \n",n, c, e, ne, m);
}
fclose(fptr);
return 0;
}
3) Create a datafile “student.txt” and store name, class and marks obtained in 3
different subject until user press “y” / as per user requirement.
#include
int main()
{
char n[10],ch[3];
int c, e, ne, m;
FILE *fptr;
fptr = fopen("student.txt","w");
do
{
printf("Enter name class and 3 marks");
scanf("%s %d %d %d %d",n, &c, &e, &ne, &m);
fprintf(fptr,"%s %d %d %d %d\n",n, c, e, ne, m);
printf("Press Y to continue");
scanf("%s",ch);
} while (strcmp(ch,"Y") == 0 || strcmp(ch,"y")==0);
fclose(fptr);
return 0;
}
Add/Append data
Syntax:
Eg; suppose if we want to display/read name, disease, age and bed number of
a patient from data file then, it is written as
int a,b;