C Programming Notes
C Programming Notes
int main()
{
struct Point p1; // The variable p1 is declared like a normal variable
}
How to initialize structure members?
Structure members cannot be initialized with declaration.
struct Point
{
int x = 0; // COMPILER ERROR: cannot initialize members here
int y = 0; // COMPILER ERROR: cannot initialize members here
};
The reason for the above error is simple: when a data type is declared, no
memory is allocated for it. Memory is allocated only when variables are
created.
Structure members can be initialized using curly braces ‘{}’. For example, the
following is a valid initialization.
struct Point
{
int x, y;
};
int main()
{
// A valid initialization. member x gets value 0 and y
// gets value 1. The order of declaration is followed.
struct Point p1 = {0, 1};
}
8.3 Accessing Structure member
Structure members are accessed using dot (.) operator.
#include<stdio.h>
struct Point
{
int x, y;
};
int main()
{
struct Point p1 = {0, 1};
return 0;
}
Limitations Of C Structure:
1. No Data Hiding: C Structures do not permit data hiding. Structure
members can be accessed by any function, anywhere in the scope of
the Structure.
2. Functions inside Structure: C structures do not permit functions
inside Structure
3. Static Members: C Structures cannot have static members inside their
body
4. Access Modifiers: C Programming language does not support access
modifiers. So they cannot be used in C Structures.
5. Construction creation in Structure: Structures in C cannot have
constructor inside Structures.
struct structure1
{
----------
----------
};
struct structure2
{
----------
----------
struct structure1 obj;
};
#include<stdio.h>
struct Address
{
char HouseNo[25];
char City[25];
char PinCode[25];
};
struct Employee
{
int Id;
char Name[25];
float Salary;
struct Address Add;
};
void main()
{
int i;
struct Employee E;
printf("\nDetails of Employees");
printf("\n\tEmployee Id : %d",E.Id);
printf("\n\tEmployee Name : %s",E.Name);
printf("\n\tEmployee Salary : %f",E.Salary);
printf("\n\tEmployee House No : %s",E.Add.HouseNo);
printf("\n\tEmployee City : %s",E.Add.City);
printf("\n\tEmployee House No : %s",E.Add.PinCode);
}
Output :
Enter Employee Id : 101
Enter Employee Name : Suresh
Enter Employee Salary : 45000
Enter Employee House No : 4598/D
Enter Employee City : Delhi
Enter Employee Pin Code : 110056
Details of Employees
Employee Id : 101
Employee Name : Suresh
Employee Salary : 45000
Employee House No : 4598/D
Employee City : Delhi
Employee Pin Code : 110056
Union is a user-defined data type, but unlike structures, they share the same memory
location.
In union, members will share the memory location. If we try to make changes in any
of the members then it will be reflected to the other member as well. Let's
understand this concept through an example.
union abc
{
int a;
char b;
}var;
int main()
{
var.a = 66;
printf("\n a = %d", var.a);
printf("\n b = %d", var.b);
In the above code, the union has two members, i.e., 'a' and 'b'. The 'var' is a variable of
union abc type. In the main() method, we assign the 66 to 'a' variable, so var.a will
print 66 on the screen. Since both 'a' and 'b' share the memory location, var.b will
print 'B' (ascii code of 66).
The size of the union is based on the size of the largest member of the union.
union abc{
int a;
char b;
float c;
double d;
};
int main()
{
printf("Size of union abc is %d", sizeof(union abc));
return 0;
}
As we know, the size of int is 4 bytes, size of char is 1 byte, size of float is 4 bytes, and the
size of double is 8 bytes. Since the double variable occupies the largest memory among all
the four variables, 8 bytes will be allocated in the memory. Therefore, the output of the above
program would be 8 bytes.
Opening a file:
Where,
FILE is the structure which is defined in the header file <stdio.h>.
Closing a file
The fclose() function is used for closing a file.
When this function is used the file pointer is disconnected from a file.
Syntax:
int fclose(FILE *fp);
Where,
fp is the file pointer that points to the file that has to be closed.
An integer value is returned which will indicate if the function was successful or not.
In addition to the fclose() function we even have the fcloseall() function which will close all
the streams which are open currently except the standard streams (stdin, stdout and stderr).
Syntax:
intfcloseall(void);
This function will flush any of the stream buffers and will return the number of streams which
are closed.
Reading a file
Following are the list of functions which are used for reading a file:
Writing a file
Following are the list of functions which are used for writing a file:
Syntax:
int ferror(FILE *stream);
This function returns 0 if there are no errors and a value if there are some errors.
Example 1: Write to a text file
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
FILE *fptr;
// use appropriate location if you are using MacOS or Linux
fptr = fopen("C:\\program.txt","w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter num: ");
scanf("%d",&num);
fprintf(fptr,"%d",num);
fclose(fptr);
return 0;
}