Coupon Accepted Successfully!


C - Structured Datatypes

A structure in C is a collection of items of different types.

Following is the example how to define a structure.

struct student {

char firstName[20];

char lastName[20];

char SSN[9];

float gpa;


your variables of student type:

struct student student_a, student_b;

Pointers to Structs:

Declaring pointers to structures is basically the same as declaring a normal pointer:

struct student *student_a;

To dereference, you can use the infix operator: ->.

printf(“%s\n”, student_a->SSN);

Unions Datatype

Unions are declared in the same fashion as structs, but have a fundamental difference. Only one item within the union can be used at any time, because the memory allocated for each item inside the union is in a shared memory location.

Here is how we define a Union

union Shape {

int circle;

int triangle;

int ovel;


C - Working with Files

When accessing files through C, the first necessity is to have a way to access the files. For C File I/O you need to use a FILE pointer, which will let the program keep track of the file being accessed. For Example:

FILE *fp;

To open a file you need to use the fopen function, which returns a FILE pointer. Once you’ve opened a file, you can use the FILE pointer to let the compiler perform input and output functions on the file.

FILE *fopen(const char *filename, const char *mode);

Here filename is string literal which you will use to name your file and mode can have one of the following values

w - open for writing (file need not exist)

a - open for appending (file need not exist)

r+ - open for reading and writing, start at beginning

w+ - open for reading and writing (overwrite file)

a+ - open for reading and writing (append if file exists)

Note that it’s possible for fopen to fail even if your program is perfectly correct: you might try to open a file specified by the user, and that file might not exist (or it might be write-protected). In those cases, fopen will return 0, the NULL pointer.

Here’s a simple example of using fopen:

FILE *fp;


fp=fopen(“/home/tutorialspoint/test.txt”, “r”);

This code will open test.txt for reading in text mode. To open a file in a binary mode you must add a b to the end of the mode string; for example, “rb” (for the reading and writing modes, you can add the b either after the plus sign - “r+b” - or before - “rb+”)

To close a function you can use the function:

int fclose(FILE *a_file);

fclose returns zero if the file is closed successfully.

An example of fclose is:


To work with text input and output, you use fprintf and fscanf, both of which are similar to their friends printf and scanf except that you must pass the FILE pointer as first argument.

Binary I/O

There are following two functions which will be used for binary input and output:

size_t fread(void *ptr, size_t size_of_elements,

size_t number_of_elements, FILE *a_file);


size_t fwrite(const void *ptr, size_t size_of_elements,

size_t number_of_elements, FILE *a_file);

Both of these functions deal with blocks of memories - usually arrays. Because they accept pointers, you can also use these functions with other data structures; you can even write structs to a file or a read struct into memory.

Test Your Skills Now!
Take a Quiz now
Reviewer Name