C Language Tutorials by Ghulam Murtaza Dahar - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

Output

Enter name

Hillary

Enter salary

1234.23

Displaying

Name: f%Bary

Salary: 1234.2

Note: You may get different garbage value of name.

Why this output?

Initially, Hillary will be stored in u.name and other members of union will contain garbage value. But when user enters value of salary, 1234.23 will be stored in u.salary and other members will contain garbage value. Thus in output, salary is printed accurately but, name displays some random string.

Passing Union To a Function

Union can be passed in similar manner as structures in C programming. Visit this page to learn more about: How structure can be passed to function in C programming?

C Programming Structure Examples

This page contains examples and source code on structures in C programming language. To understand all examples in this page, you should have knowledge of following structure topics.

1. Structure Introduction

2. Structure and Pointers

3. Passing Structure to Function

Example of Structures in C Programming

C Programming Examples And Source Code

C Program to Store Information(name, roll and marks) of a Student Using Structure

C Program to Add Two Distances (in inch-feet) System Using Structures

C Program to Add Two Complex Numbers by Passing Structure to a Function

C Program to Calculate Difference Between Two Time Period

C Program to Store Information of 10 Students Using Structure

C Program to Store Information Using Structures for n Elements Dynamically

C Programming Files

In C programming, file is a place on disk where a group of related data is stored.

Why files are needed?

When the program is terminated, the entire data is lost in C programming. If you want to keep large volume of data, it is time consuming to enter the entire data. But, if file is created, these information can be accessed using few commands.

There are large numbers of functions to handle file I/O in C language. In this tutorial, you will learn to handle standard I/O(High level file I/O functions) in C.

High level file I/O functions can be categorized as:

1. Text file

2. Binary file

File Operations

1. Creating a new file

2. Opening an existing file

3. Reading from and writing information to a file

4. Closing a file

Working with file

While working with file, you need to declare a pointer of type file. This declaration is needed for communication between file and program.

FILE *ptr;

Opening a file

Opening a file is performed using library function fopen(). The syntax for opening a file in standard I/O is:

ptr=fopen("fileopen","mode")

For Example:

fopen("E:\\cprogram\program.txt","w");

/* --------------------------------------------------------- */

E:\\cprogram\program.txt is the location to create file.

"w" represents the mode for writing.

/* --------------------------------------------------------- */

Here, the program.txt file is opened for writing mode.

Opening Modes in Standard I/O

File Mode Meaning Of Mode