0% found this document useful (0 votes)
6 views2 pages

Quicknote PCD Final

The document outlines the declaration and usage of tagged and typedef structures in C programming, including file handling for binary and text files. It discusses error handling, reading and writing data, and the importance of storage classes and levels of coupling and cohesion in programming. Additionally, it covers function definitions with input parameters and return statements.

Uploaded by

amberlylbma-wm23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Quicknote PCD Final

The document outlines the declaration and usage of tagged and typedef structures in C programming, including file handling for binary and text files. It discusses error handling, reading and writing data, and the importance of storage classes and levels of coupling and cohesion in programming. Additionally, it covers function definitions with input parameters and return statements.

Uploaded by

amberlylbma-wm23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1. Declare tagged & typedef structure { 6.

Binary file
 Tagged structure: char department;  Close file: - Data are stored in the same format
struct Student{ char progTitle[10]=”DIA”; fclose (fptr); as they are stored in memory
char id[10]; int year=1; - There is no line or newline character
char class[3]; };  Repeatedly read a set of integers  Create file pointer:
int year; *The progTitle and year from keyboard (-1) to stop and print FILE *fptr;
}; should not be initialized in
to file:
structure declaration
do{  Open file for writing(wb),
 Type definitions structure: scanf(“%d”, &num); append(ab) , read(rb):
typedef struct{ 5. Text file
fprintf(fptr, ”%d\n”, fptr = fopen(“file.bin”,
char id[10]; - Data are human readable num); ”wb”);
char class[3]; - Stored in character format }while (num != -1);
int year; - Each line ends with a newline
character  Error message:
}Student;  Read 5 sets of integers:
- End-of-file (EOF) marker for the end if (fptr == NULL){
for(i=0; i<5; i++) printf(“Can’t
2. Declare variables for the above of the file
{ open the file
structure  Create file pointer:
scanf(“%d”, &num); file.bin!”);
 Tagged structure FILE *fptr;
fprintf(fptr, ”%d\n”, exit(-1);
struct Student stud; num); }
 Open file for writing(w), append(a) , }
 Typedef read(r):
 Write data to file:
Student stud; fptr =  Calculation  Write the result fwrite(&num, sizeof(int),
fopen(“Student.txt”,”w”); into output text file 1, fptr);
 Declare array of the structure Int week,temp, totalTemp=0;
 Error message: double average;
variable  Read data from file:
if (fptr == NULL){ //if (!fptr) char header[50];
Struct Student stud[4]; fread(&num, sizeof(int),
printf(“Can’t open the file fscanf(fptr1,”%[^\n]\n”,header);
Student stud[4]; 1, fptr);
student.txt!”); totalTemp=0;
exit(-1) while(fscanf(fptr1,”%d”,&week)!
3. Change the value of particular  Close file:
} =EOF){
member of the structure variable fclose (fptr);
for (int i=0;i<7;i++){
& array of structure variable  Write data to file: fscanf(fptr1,”%d”,&temp);
Note: use strcpy if need to change totalTemp+=temp; 7. Understand & identify all the 4
fprintf (fptr,”I love
string value programming”); } storage classes
strcpy(stud.id,”WAD12345”); fprintf(fptr,”%d|”,student1); average=totalTemp/7.0;  Extern(global/outside function)
strcpy(stud.class,”DIA”); fprintf(fptr2,”%d %.2f\  Auto(local &parameter)
stud.year=1;  Read from text file where delimiters n”,week,average);  Static(keyword)
exist, e.g. ‘|’ is used to separate }  Register (keyword)
4. Identify errors in structure & give each data: fclose(fptr1);
the reason fclose(fptr2)
fscanf (fptr,”%d|%d|%[^\n]\
struct Programme n”,&score1,&score2,name);
8. Identify the levels of coupling & cohesion
 Coupling  Return
void main()
{
int num;
num=cal();
}
int cal()
{
int n;
n=2+4;
return n;
}

 Cohesion

Best

9. Evaluate output based on the given program segment


which is using pointer

10. Function(s) with input parameter & output


parameters & return statement
Main function, declare variables, call the function(s) &
display output
 Pointer
void main()
{
int num;
cal(&num);
}
void cal(int *n)
{
*n=2+4;
}

You might also like