0% found this document useful (0 votes)
3 views27 pages

C Programming For Problem Solving Module 5

Chapter 5 discusses structures in C, which are user-defined data types that can group different types into a single type using the 'struct' keyword. It covers structure declaration, definition, member access, initialization methods, and the use of typedef for simplifying code. Additionally, the chapter touches on nested structures, pointers to structures, self-referential structures, unions, and basic file handling in C.

Uploaded by

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

C Programming For Problem Solving Module 5

Chapter 5 discusses structures in C, which are user-defined data types that can group different types into a single type using the 'struct' keyword. It covers structure declaration, definition, member access, initialization methods, and the use of typedef for simplifying code. Additionally, the chapter touches on nested structures, pointers to structures, self-referential structures, unions, and basic file handling in C.

Uploaded by

nehanikita17
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter 5

Structure in C

Structure:

The structure in C is a user-defined data type that can group items of potentially different types into a single type. The struct keyword is used to
define the structure in the C programming language. The items within the structure are called its members, and they can be of any valid data type.
Furthermore, the values of a structure are stored in contiguous memory locations.

5.1 C Structure Declaration

Structures must be declared in C before being used in a program. In a structure declaration, member variables and their data types are specified. The
struct keyword is used to declare a structure using the following syntax:

struct structure_name
{
data_type member_name1;
data_type member_name2;
...
};

This syntax is known as a structure template or structure prototype. Note that no memory is allocated to the structure at the point of declaration. For
practical applications and a deeper understanding of how structures build complex data structures, the C Programming Course Online with Data
Structures is recommended.

5.2 C Structure Definition

To use a structure in a program, an instance of it must be defined by creating variables of the structure type. There are two ways to define structure
variables:

1. Structure Variable Declaration with Structure Template


struct structure_name
{
data_type member_name1;
data_type member_name2;

150
CHAPTER 5. STRUCTURE IN C 151

...
} variable1, variable2, ...;

2. Structure Variable Declaration after Structure Template

struct structure_name variable1, variable2, ...;

5.2.1 Accessing Structure Members

Structure members can be accessed using the dot operator (.).

structure_name.member1;
structure_name.member2;

If a pointer to the structure is used, the arrow operator (->) can be employed to access the members.

5.3 Initialize Structure Members

Structure members cannot be initialized at the time of declaration. For instance, the following code will result in a compiler error:

struct Point {
int x = 0; // COMPILER ERROR
int y = 0; // COMPILER ERROR
};

The error arises because no memory is allocated when a datatype is declared, only when variables are created.

5.3.1 Default Initialization

By default, structure members are not automatically initialized to zero or NULL. Uninitialized members will contain garbage values. However, when a
structure variable is declared with an initializer, uninitialized members are zero-initialized:

struct Point {
int x;
int y;
};
struct Point p = {0}; // Both x and y are initialized to 0

5.3.2 Methods of Initialization

Structure members can be initialized in the following ways:

1. Using Assignment Operator

struct structure_name str;


str.member1 = value1;
str.member2 = value2;

2. Using Initializer List

struct structure_name str = value1, value2 ;

3. Using Designated Initializer List (C99 Standard)

struct structure_name str = { .member1 = value1, .member2 = value2 };


CHAPTER 5. STRUCTURE IN C 152

5.4 typedef for Structures

The typedef keyword allows defining an alias for an existing datatype. This can simplify the code by reducing the need to repeatedly use the struct
keyword:

typedef struct {
int a;
} str1;

typedef struct {
int x;
} str2;

5.4.1 Example

#include <stdio.h>

typedef struct {
int a;
} str1;

typedef struct {
int x;
} str2;

int main() {
str1 var1 = { 20 };
str2 var2 = { 314 };

printf("var1.a = %d\n", var1.a);


printf("var2.x = %d\n", var2.x);

return 0;
}

Output:

var1.a = 20
var2.x = 314

5.5 Nested Structures

C allows structures to be nested within other structures, called nested structures. There are two types of nested structures:

1. Embedded Structure Nesting

struct parent {
int member1;
struct {
int member_str1;
char member_str2;
} member2;
};

2. Separate Structure Nesting


CHAPTER 5. STRUCTURE IN C 153

struct member_str {
int member_str1;
char member_str2;
};

struct parent {
int member1;
struct member_str member2;
};

5.5.1 Accessing Nested Members

Nested members can be accessed using the dot operator twice:

str_parent.str_child.member;

5.6 Structure Pointer in C

A pointer to a structure can be defined and used to access structure members using the arrow operator (->):

#include <stdio.h>

struct Point {
int x, y;
};

int main() {
struct Point str = { 1, 2 };
struct Point* ptr = &str;

printf("%d %d", ptr->x, ptr->y);

return 0;
}

Output:

1 2

5.7 Self-Referential Structures

Self-referential structures contain a pointer to the same structure type. These are used in linked lists, trees, etc.:

struct structure_name {
data_type member1;
struct structure_name* str;
};

5.8 Dot (.) Operator in C

In C, the dot (.) operator is used to access members of user-defined data types such as a structure or union. Also known as the direct member access
operator, it allows us to access the data of the struct and union via the name of variables.

Let’s take a look at an example:

#include <stdio.h>
CHAPTER 5. STRUCTURE IN C 154

struct A {
int x;
};

int main() {
struct A a = {30};

// Access structure members using the dot operator


printf("%d", a.x);

return 0;
}

Output
30

Explanation: In the above code, a.x accesses the x member of the a structure. The dot operator is used to retrieve or modify values stored in these
members of the structure.

Syntax of Dot Operator

name . member;

where,

• name: An instance of a structure or a union.

• member: Member associated with the created structure or union.

dot(.) operator with Nested Types

The dot operator can also be used to access the members of nested structures and unions. It can be done in the same way as done for the normal
structure.

name . member1 . member2;

Precedence of dot (.) Operator

The dot (.) operator has the highest operator precedence in C Language and its associativity is from left to right.

Note: dot (.) operator can only be used with structures or unions in C language.

Examples of dot (.) Operator

Accessing Members of Union

#include <stdio.h>

union A {
int x;
char c;
};

int main() {
union A a;

// Accessing and updating x member of union


a.x = 10;
printf("%d\n", a.x);
CHAPTER 5. STRUCTURE IN C 155

// Accessing and updating c member of union


a.c = ’Z’;
printf("%c", a.c);

return 0;
}

Output
10
Z

Access Nested Member Structure

#include <stdio.h>

struct Base {
struct Child {
int i;
} child;
};

int main() {
struct Base base = { 12 };

// Accessing nested structure member using dot operator


printf("%d", [Link].i);
return 0;
}

Output
12

5.9 typedef

The typedef is a keyword that is used to provide existing data types with a new name. The C typedef keyword is used to redefine the name of already
existing data types. When names of datatypes become difficult to use in programs, typedef is used with user-defined datatypes, which behave similarly
to defining an alias for commands.

Example

#include <stdio.h>

typedef int Integer;

int main() {
// n is of type int, but we are using alias Integer
Integer n = 10;
printf("%d", n);
return 0;
}

Output:
10

Explanation: Here, the typedef creates an alias Integer for the int type. This allows us to use Integer instead of int throughout the program,
making the code potentially more readable.
CHAPTER 5. STRUCTURE IN C 156

Syntax of typedef

typedef existing_type new_type;

• existing_type: The type that we want to alias (e.g., int, float, struct, etc.).

• new_type: The new alias or name for the existing type.

After this declaration, we can use the alias as if it were the real existing type in our C program.

Examples of typedef in C

5.9.1 Define typedef for Built-in Data Type

#include <stdio.h>

// Defining an alias using typedef


typedef long long ll;

int main() {
// Using typedef alias name to declare variable
ll a = 20;
printf("%lld", a);
return 0;
}

Output:
20

5.9.2 Define typedef for a Structure

#include <stdio.h>
#include <string.h>

// Using typedef to define an alias for structure


typedef struct Students {
char name[50];
char branch[50];
int ID_no;
} stu;

int main() {
// Using alias to define structure
stu s;
strcpy([Link], "Geeks");
strcpy([Link], "CSE");
s.ID_no = 108;

printf("%s\n", [Link]);
printf("%s\n", [Link]);
printf("%d", s.ID_no);
return 0;
}

Output:
Geeks
CSE
108
CHAPTER 5. STRUCTURE IN C 157

5.9.3 Define typedef for Pointer Type

#include <stdio.h>

// Creating alias for pointer


typedef int* ip;

int main() {
int a = 10;
ip ptr = &a;
printf("%d", *ptr);
return 0;
}

Output:
10

5.9.4 Define typedef for Array

#include <stdio.h>

// Here ’arr’ is an alias


typedef int arr[4];

int main() {
arr a = { 10, 20, 30, 40 };
for (int i = 0; i < 4; i++)
printf("%d ", a[i]);
return 0;
}

Output:
10 20 30 40

typedef vs #define

The #define preprocessor can also be used to create an alias but there are some primary differences between the typedef and #define in C:

• #define is capable of defining aliases for values as well, for instance, you can define 1 as ONE, 3.14 as PI, etc. typedef is limited to giving symbolic
names to types only.

• Preprocessors interpret #define statements, while the compiler interprets typedef statements.

• There should be no semicolon at the end of #define, but a semicolon at the end of typedef.

• In contrast with #define, typedef will actually define a new type by copying and pasting the definition values.

5.10 union

In C, a union is a user-defined data type that can store different data types in the same memory location. Unlike structures, all members of a union
share the same memory, allowing only one member to hold a value at any time.

Example

#include <stdio.h>
#include <string.h>

// Union definition
CHAPTER 5. STRUCTURE IN C 158

union A {
int i;
float f;
char s[20];
};

int main() {
union A a;

// Storing an integer
a.i = 10;
printf("data.i = %d\n", a.i);

// Storing a float
a.f = 220.5;
printf("data.f = %.2f\n", a.f);

// Storing a string
strcpy(a.s, "GfG");
printf("data.s = %s\n", a.s);

return 0;
}

Output

data.i = 10
data.f = 220.50
data.s = GfG

Syntax of Union in C

C Union Declaration

union name {
type1 member1;
type2 member2;
...
};

Ensure the declaration ends with a semicolon. Detailed explanations and examples can be found in the C Programming Course Online with Data
Structures.

Create a Union Variable

Two methods to define a union variable:

// With Declaration
union name {
type member1;
type member2;
...
} var1, var2, ...;

// After Declaration
union name var1, var2, var3, ...;
CHAPTER 5. STRUCTURE IN C 159

Access Union Members

Use the dot operator:

var1.member1;

Initialize Union

Assign values directly to union members:

var1.member1 = val;

Only one member can contain a value at any time.

Size of Union

The size of a union is determined by its largest member:

#include <stdio.h>

union A {
int x;
char y;
};

union B {
int arr[10];
char y;
};

int main() {
printf("Sizeof A: %ld\n", sizeof(union A));
printf("Sizeof B: %ld\n", sizeof(union B));
return 0;
}

Nested Union

Unions can be nested within structures or other unions:

struct Employee {
char name[50];
int id;
union {
float hourlyRate;
float salary;
} payment;
};

Difference Between Structure and Union


CHAPTER 5. STRUCTURE IN C 160

Parameter Structure Union


Definition Groups different data types Shares memory for all members
Keyword struct union
Size Sum of all members Size of the largest member
Memory Allocation Unique storage for each member Shared memory for all members
Accessing Members All members can be accessed simultaneously Only one member at a time

5.11 Basics of File Handling in C

File handling in C is the process in which we create, open, read, write, and close operations on a file. C language provides different functions such as
fopen(), fwrite(), fread(), fseek(), fprintf(), etc., to perform input, output, and many different C file operations in our program.

Why do we need File Handling in C?

Operations using the C program are done on a prompt/terminal and are not stored anywhere. The output is deleted when the program is closed. In the
software industry, most programs are written to store the information fetched from the program. File handling is necessary for such purposes.

Features of Using Files

• Reusability: Data stored in the file can be accessed, updated, and deleted anytime, providing high reusability.

• Portability: Files can be transferred without losing any data, minimizing the risk of flawed coding.

• Efficiency: File handling allows accessing a part of a file using fewer instructions, saving time and reducing errors.

• Storage Capacity: Files allow storing a large amount of data without having to worry about storing everything simultaneously in a program.

5.11.1 Types of Files in C

A file can be classified into two types based on the way the file stores the data:

1. Text Files: Contains data in the form of ASCII characters and generally used to store a stream of characters. They are stored with a .txt file
extension.

2. Binary Files: Contains data in binary form (0’s and 1’s). They are more secure and are generally stored with a .bin file extension.

5.11.2 C File Operations

Different possible operations on a file in C include:

• Creating a new file – fopen() with attributes like “a", “a+", “w", “w+".

• Opening an existing file – fopen().

• Reading from a file – fscanf() or fgets().

• Writing to a file – fprintf() or fputs().

• Moving to a specific location in a file – fseek(), rewind().

• Closing a file – fclose().


CHAPTER 5. STRUCTURE IN C 161

File Pointer in C

A file pointer is a reference to a particular position in the opened file. It is used to perform all file operations such as read, write, close, etc. The FILE
macro is defined inside the <stdio.h> header file.

Syntax

FILE* pointer_name;

5.12 Open a File in C

The fopen() function is used to open a file with the filename or file path along with the required access modes.

Syntax

FILE* fopen(const char *file_name, const char *access_mode);

5.12.1 Syntax of fopen()

fopen(filename, mode);
CHAPTER 5. STRUCTURE IN C 162

Parameters

• filename: Name of the file to be opened (with extension).

• mode: The purpose for which the file is to be opened.

Return Value

• Returns a FILE pointer if the file is successfully opened.

• Returns NULL if the file cannot be opened.

5.12.2 File Opening Modes

Opening Mode Description


r Opens the file for reading.
rb Opens the file for reading in binary mode.
w Opens the file for writing. If the file exists, its contents are overwritten.
wb Opens the file for writing in binary mode.
a Opens the file for appending.
ab Opens the file for appending in binary mode.
r+ Opens the file for reading and writing.
rb+ Opens the file for reading and writing in binary mode.
w+ Opens the file for reading and writing, overwriting the existing file.
wb+ Opens the file for reading and writing in binary mode.
a+ Opens the file for reading and appending.
ab+ Opens the file for reading and appending in binary mode.

Example: Opening a File

#include <stdio.h>
#include <stdlib.h>

int main() {
FILE* fptr;
fptr = fopen("[Link]", "r");

if (fptr == NULL) {
printf("The file is not opened. The program will now exit.");
exit(0);
}
return 0;
}

In C, the fopen() function is used to open a file in the specified mode. The function returns a file pointer (FILE *) which is used to perform further
operations on the file, such as reading from or writing to it. If the file exists, fopen() opens the particular file; otherwise, in some cases, a new file is
created.

Example Usage

Let’s take a look at an example:

#include <stdio.h>
#include <stdlib.h>

int main() {
FILE* fptr;

// Creates a file "demo_file"


CHAPTER 5. STRUCTURE IN C 163

// with file access as write mode


fptr = fopen("demo_file.txt", "w+");

// Writes content to the file


fprintf(fptr, "%s", "MUSE");

// Close the file


fclose(fptr);

return 0;
}

On running the following program, a new file will be created by the name demo_file.txt with the following content:

demo_file.txt

MUSE

Opening a File for Reading

#include <stdio.h>

int main() {
// Open file in read mode
FILE *file = fopen("[Link]", "r");

if (file == NULL) {
printf("Error!\n");
return 1;
}

printf("Successfull\n");

// Close the file


fclose(file);
return 0;
}

Explanation: The file [Link] is opened in read mode ("r"). If the file exists, it opens successfully. If the file doesn’t exist, fopen() returns
NULL, and the program outputs an error message.

Opening a File for Writing

#include <stdio.h>

int main() {
// Open file in write mode
FILE *file = fopen("[Link]", "w");

if (file == NULL) {
printf("Error!\n");
return 1;
}

fprintf(file, "Hello, this is a test MUSE file.\n");


printf("Data written successfully.\n");

// Close the file


CHAPTER 5. STRUCTURE IN C 164

fclose(file);
return 0;
}

Explanation: The file [Link] is opened in write mode ("w"). If the file does not exist, it is created. The program writes a string to the file using
fprintf(). After writing, the file is closed using fclose().

Opening a File for Appending

[language=C]
#include <stdio.h>

int main() {
// Open file in append mode
FILE *file = fopen("append_example.txt", "a");

if (file == NULL) {
printf("Error opening file!\n");
return 1;
}

fprintf(file, "Appending text\n");


printf("Data appended\n");

// Close the file


fclose(file);
return 0;
}

Explanation: The file append_example.txt is opened in append mode ("a"). If the file exists, the new content is appended to the end of the file. If
the file does not exist, it is created.

Handling File Opening Failures

#include <stdio.h>

int main() {
// Try to open a non-existing file
FILE *file = fopen("non_existent_file.txt", "r");

if (file == NULL) {
perror("Error");
return 1;
}

// Close the file


fclose(file);
return 0;
}

Explanation: The program attempts to open a file that does not exist, and perror() displays the system error message.

5.13 Create a File in C

The fopen() function can also create a file if it does not exist. Use modes like w, w+, wb, etc.
CHAPTER 5. STRUCTURE IN C 165

5.13.1 Example: Creating a File

#include <stdio.h>
#include <stdlib.h>

int main() {
FILE* fptr;
fptr = fopen("[Link]", "w");

if (fptr == NULL) {
printf("The file is not opened. The program will exit now.");
exit(0);
} else {
printf("The file is created Successfully.");
}
return 0;
}

Reading From a File

The file read operation can be performed using functions like fscanf(), fgets(), fgetc(), etc.

Example: Reading From a File

FILE *fptr;
fptr = fopen("[Link]", "r");
fscanf(fptr, "%s %s %s %d", str1, str2, str3, &year);
char c = fgetc(fptr);

Write to a File

File write operations can be performed by functions like fprintf(), fputs(), fputc(), etc.

Example: Writing to a File

FILE *fptr;
fptr = fopen("[Link]", "w");
fprintf(fptr, "%s %s %s %d", "We", "are", "writing", 2025);

5.14 Reading from a File

The file read operation in C can be performed using functions fscanf() or fgets(). Both functions perform similar operations to scanf and gets(),
but with an additional parameter: the file pointer. Other functions can also be used to read from a file:

Function Description
fscanf() Use formatted string and variable arguments list to take input from a file.
fgets() Input the whole line from the file.
fgetc() Reads a single character from the file.
fgetw() Reads a number from a file.
fread() Reads the specified bytes of data from a binary file.

Example:

FILE *fptr;
fptr = fopen("[Link]", "r");
fscanf(fptr, "%s %s %s %d", str1, str2, str3, &year);
char c = fgetc(fptr);
CHAPTER 5. STRUCTURE IN C 166

getc() and other file reading functions return EOF (End Of File) when they reach the end of the file. EOF indicates the end of the file, and its value is
implementation-defined.

5.15 Writing to a File

File write operations can be performed using the functions fprintf() and fputs() with similarities to read operations. Other functions that can be
used to write data to a file include:

Function Description
fprintf() Similar to printf(), it uses a formatted string and variable arguments list to print output to the file.
fputs() Prints the whole line in the file, adding a newline at the end.
fputc() Prints a single character into the file.
fputw() Prints a number to the file.
fwrite() Writes the specified number of bytes to a binary file.

Example:

FILE *fptr;
fptr = fopen("[Link]", "w");
fprintf(fptr, "%s %s %s %d", "We", "are", "in", 2012);
fputc(’a’, fptr);

5.16 Closing a File

The fclose() function is used to close a file. Always close a file after performing file operations to free up resources.

FILE *fptr;
fptr = fopen("[Link]", "w");
// Some file operations
fclose(fptr);

5.17 Seeking and Rewinding File Pointers

5.17.1 fseek()

The fseek() function moves the file pointer to a specified location. Its syntax is:

fseek(FILE *ptr, long int offset, int pos);

Example of fseek():

#include <stdio.h>

int main() {
FILE* fp;
fp = fopen("[Link]", "r");

fseek(fp, 0, SEEK_END);
printf("%ld", ftell(fp));

fclose(fp);
return 0;
}

5.18 EOF, getc() and feof() in C

In this article, we will discuss the EOF, getc() function, and feof() function in C.
CHAPTER 5. STRUCTURE IN C 167

What is EOF?

In C, EOF is a constant macro defined in the <stdlib.h> header file that is used to denote the end of the file in C file handling. It is used by various file
reading functions such as fread(), gets(), getc(), etc.

The value of EOF is implementation defined but generally is -1.

getc() Function

The getc() function is used to read a single character from the given file stream. It is implemented as a macro in <stdio.h> header file.

Syntax

getc(fptr);

Parameters

• fptr: It is a pointer to a file stream to read the data from.

Return Value

It returns the character read from the file stream. If some error occurs or the End-Of-File is reached, it returns EOF.

feof() Function

The feof() function is used to check whether the file pointer to a stream is pointing to the end of the file or not. It returns a non-zero value if the end
is reached, otherwise, it returns 0.

5.18.1 Syntax

feof(fptr);

5.18.2 Parameters

• fptr: Pointer to a file stream to read the data from.

Return Value

Returns a non-zero value (usually 1) if the end of the file is reached. Otherwise, it returns 0.

5.18.3 Why feof() is needed?

getc() returns the End of File (EOF) when the end of the file is reached. getc() also returns EOF when it fails. So, only comparing the value returned
by getc() with EOF is not sufficient to check for the actual end of the file. To solve this problem, C provides feof().

Example

#include <stdio.h>

int main() {
FILE *fptr = fopen("[Link]", "w");
char ch;

// Try to read a character using getc()


ch = getc(fptr);

// Handle the EOF return value


CHAPTER 5. STRUCTURE IN C 168

if (ch == EOF)
printf("End of File or Unable to Read");
else
printf("Read Character: %c", ch);

fclose(fptr);
return 0;
}

Output: End of File or Unable to Read

In the above program, the getc() function should be unable to read as the file is opened in the write mode only. But it still returns EOF, which makes
it difficult to find the source of error. Here, the feof() function can be specifically used to check for End of File.

5.18.4 fgets() and gets() in C

fgets()

The fgets() function reads a line from the specified stream and stores it into the string pointed to by str. It stops when either (n-1) characters are
read, the newline character is read, or the end-of-file is reached, whichever comes first.

Syntax

char *fgets (char *str, int n, FILE *stream);

Parameters

• str: Pointer to an array of chars where the string read is copied.

• n: Maximum number of characters to be copied into str (including the terminating null character).

• stream: Pointer to a FILE object that identifies an input stream.

Return Value

The fgets() function returns a pointer to the string where the input is stored.

Example of fgets()

#include <stdio.h>
#define MAX 15

int main()
{
// defining buffer
char buf[MAX];

// using fgets to take input from stdin


fgets(buf, MAX, stdin);
printf("string is: %s\n", buf);

return 0;
}

Input: Hello and welcome to GeeksforGeeks

Output: string is: Hello and welc


CHAPTER 5. STRUCTURE IN C 169

gets()

The gets() function reads characters from the standard input (stdin) and stores them as a C string into str until a newline character or the end-of-file
is reached.

It is not safe to use because it does not check the array bound. It is used to read strings from the user until a newline character is encountered.

Syntax

char *gets( char *str );

Example of gets()

#include <stdio.h>
#define MAX 15

int main()
{
// defining buffer
char buf[MAX];

printf("Enter a string: ");

// using gets to take string from stdin


gets(buf);
printf("string is: %s\n", buf);

return 0;
}

Input: Hello and welcome to GeeksforGeeks

Output: Hello and welcome to GeeksforGeeks

This document covers the usage of some standard C functions related to file handling and formatted input/output operations, including fprintf(),
scanf(), fscanf(), and fread().

5.19 The fprintf() Function

fprintf() is used to print content to a file instead of the standard output console.

Syntax:
int fprintf(FILE *fptr, const char *str, ...);

Example:

#include<stdio.h>
int main()
{
int i, n=2;
char str[50];

// Open file [Link] in write mode


FILE *fptr = fopen("[Link]", "w");
if (fptr == NULL)
{
printf("Could not open file");
return 0;
}

for (i = 0; i < n; i++)


CHAPTER 5. STRUCTURE IN C 170

{
puts("Enter a name");
scanf("%[^\n]%*c", str);
fprintf(fptr, "%d.%s\n", i, str);
}
fclose(fptr);

return 0;
}

Input:

• MUSE

• AIML

Output:

[Link] file now having output as


0. MUSE
1. AIML

5.20 The scanf() and fscanf() Functions

scanf() is used to read formatted input from stdin. The fscanf() function is used to read formatted input from a file stream.

5.20.1 The scanf() Function

scanf() is used to read formatted input from the standard input.

Syntax:
int scanf(const char *characters_set);

Example:

#include <stdio.h>
int main()
{
int a;
scanf("%d", &a);
printf("a = %d", a);
return 0;
}

Input:

Output:

a = 2

5.20.2 The fscanf() Function

fscanf() is used to read formatted input from a file.

Syntax:
int fscanf(FILE *ptr, const char *format, ...);

Example: Given the file [Link] with the following contents:


CHAPTER 5. STRUCTURE IN C 171

NAME AGE CITY


abc 12 hyderabad
bef 25 delhi
cce 65 bangalore

You can read the CITY field as follows:

#include <stdio.h>
int main()
{
FILE* ptr = fopen("[Link]", "r");
if (ptr == NULL)
{
printf("no such file.");
return 0;
}

char buf[100];
while (fscanf(ptr, "%*s %*s %s ", buf) == 1)
printf("%s\n", buf);

return 0;
}

Output:

hyderabad
delhi
bangalore

5.21 The fread() Function

The fread() function in C is used to read data from a file stream into a buffer.

Syntax:
size_t fread(void *buffer, size_t size, size_t count, FILE *stream);

Example:

#include <stdio.h>
int main()
{
FILE *file;
int buffer[5];

// Open the binary file for reading


file = fopen("[Link]", "rb");
if (file == NULL)
{
perror("Error opening file");
return 1;
}

// Read the integers from the file into the buffer


fread(buffer, sizeof(int), 5, file);

// Print the integers that were read


for (int i = 0; i < 5; i++)
{
CHAPTER 5. STRUCTURE IN C 172

printf("Element %d: %d\n", i + 1, buffer[i]);


}

// Close the file


fclose(file);
return 0;
}

Output:

Element 1: 10
Element 2: 20
Element 3: 30
Element 4: 40
Element 5: 50

Comparison: scanf() vs. fscanf()

The differences between scanf() and fscanf() are summarized below:


scanf() fscanf()
Usage Reads standard input Reads input from a file
Syntax scanf(const char *format, ...) fscanf(FILE *stream, const char *format, ...)
Functionality Requires format specifiers for input Reads data byte by byte from the file stream

5.22 fseek() Function

fseek() and rewind() are functions used for file positioning in C, defined in the <stdio.h> header file. This document discusses the differences in the
usage and behavior of both functions. fseek() sets the file position indicator to a specified offset from the specified origin.

Syntax:

int fseek(FILE *stream, long offset, int origin);

Parameters:

• stream: A pointer to the FILE stream.

• offset: Number of bytes to offset from the origin.

• origin: Position from where the offset is calculated. It can be:

– SEEK_SET: Beginning of the file.

– SEEK_CUR: Current position of the file pointer.

– SEEK_END: End of the file.

Return Value:

• Returns 0 if successful, and a non-zero value if an error occurs.

5.23 rewind() Function

rewind() moves the file pointer to the beginning of the file stream.

Syntax:

void rewind(FILE *stream);

Parameters:

• stream: A pointer to the file stream.

Return Value:

• It does not return any value.


CHAPTER 5. STRUCTURE IN C 173

Difference between fseek() and rewind() in C

The following table summarizes the differences between fseek() and rewind():

fseek rewind
Used to move the file pointer to a specific position. Used to move the file pointer to the beginning of the file stream.
Syntax: int fseek(FILE *stream, long offset, int origin); Syntax: void rewind(FILE *stream);
Returns 0 if successful, and a non-zero value if an error occurs. Does not return any value.
The stream error indicator is not cleared. The stream error indicator is cleared.
Example: fseek(file, 10, SEEK_SET); Example: rewind(file);
Moves the file pointer 10 bytes from the beginning of the file. Moves the file pointer to the beginning of the file.

5.24 Which Should Be Preferred?

In C, fseek() should be preferred over rewind().

Note from the C99 Standard: The rewind function sets the file position indicator for the stream pointed to by the stream at the beginning of the
file. It is equivalent to:

(void)fseek(stream, 0L, SEEK_SET);

except that the error indicator for the stream is also cleared.

The following code example sets the file position indicator of an input stream back to the beginning using rewind(). However, there is no way to check
whether the rewind() was successful.

Example:

int main()
{
FILE* fp = fopen("[Link]", "r");

if (fp == NULL) {
/* Handle open error */
}

/* Do some processing with file */

/* no way to check if rewind is successful */


rewind(fp);

/* Do some more processing with file */

return 0;
}

In the above code, fseek() can be used instead of rewind() to check if the operation succeeded.

Replacing rewind() with fseek():

if (fseek(fp, 0L, SEEK_SET) != 0) {


/* Handle repositioning error */
}

5.25 Command Line Arguments in C

The most important function in C is the main() function. It is mostly defined with a return type of int and without parameters:

int main() {
// ...
}
CHAPTER 5. STRUCTURE IN C 174

We can also give command-line arguments in C. Command-line arguments are the values given after the name of the program in the command-line shell
of Operating Systems. These arguments are handled by the main() function of a C program.

To pass command-line arguments, we typically define main() with two arguments: the first argument is the number of command-line arguments, and
the second is a list of command-line arguments.

Syntax

int main(int argc, char *argv[]) { /* ... */ }


or
int main(int argc, char **argv) { /* ... */ }

Here,

• argc (Argument Count) is an integer variable that stores the number of command-line arguments passed by the user, including the name of the
program. For example, if we pass one argument to a program, the value of argc would be 2 (one for the argument and one for the program
name). The value of argc should be non-negative.

• argv (Argument Vector) is an array of character pointers listing all the arguments. If argc is greater than zero, the array elements from argv[0]
to argv[argc-1] will contain pointers to strings. argv[0] is the name of the program. After that, each element up to argv[argc-1] is a
command-line argument.

Note: For better understanding, run this code on your Linux machine.

Example

The following example illustrates the printing of command-line arguments:

#include <stdio.h>

// defining main with arguments


int main(int argc, char* argv[]) {
printf("You have entered %d arguments:\n", argc);

for (int i = 0; i < argc; i++) {


printf("%s\n", argv[i]);
}

return 0;
}

Output

You have entered 4 arguments:

./main
geeks
for
geeks

Properties of Command Line Arguments in C

• They are passed to the main() function.

• They are parameters/arguments supplied to the program when it is invoked.

• They are used to control programs from outside instead of hardcoding those values inside the code.

• argv[argc] is a NULL pointer.


CHAPTER 5. STRUCTURE IN C 175

• argv[0] holds the name of the program.

• argv[1] points to the first command-line argument, and argv[argc-1] points to the last argument.

Note: You pass all command-line arguments separated by a space. If an argument itself has a space, you can pass such arguments by putting them
inside double quotes "" or single quotes ”.

Example

The following program demonstrates the working of command-line arguments:

#include <stdio.h>

int main(int argc, char* argv[]) {


printf("Program name is: %s", argv[0]);

if (argc == 1) {
printf("\nNo Extra Command Line Argument Passed Other Than Program Name");
}

if (argc >= 2) {
printf("\nNumber Of Arguments Passed: %d", argc);
printf("\n----Following Are The Command Line Arguments Passed----");
for (int i = 0; i < argc; i++)
printf("\nargv[%d]: %s", i, argv[i]);
}

return 0;
}

Output in different scenarios

1. Without argument:

When the above code is compiled and executed without passing any argument, it produces the following output:

$ ./[Link]
Program Name Is: ./[Link]
No Extra Command Line Argument Passed Other Than Program Name

2. Three arguments:

When the above code is compiled and executed with three arguments, it produces the following output:

$ ./[Link] First Second Third


Program Name Is: ./[Link]
Number Of Arguments Passed: 4
----Following Are The Command Line Arguments Passed----
argv[0]: ./[Link]
argv[1]: First
argv[2]: Second
argv[3]: Third

3. Single Argument:

When the above code is compiled and executed with a single argument separated by space but inside double quotes, it produces the following output:
CHAPTER 5. STRUCTURE IN C 176

$ ./[Link] "First Second Third"


Program Name Is: ./[Link]
Number Of Arguments Passed: 2
----Following Are The Command Line Arguments Passed----
argv[0]: ./[Link]
argv[1]: First Second Third

4. Single argument in quotes separated by space:

When the above code is compiled and executed with a single argument separated by space but inside single quotes, it produces the following output:

$ ./[Link] ’First Second Third’


Program Name Is: ./[Link]
Number Of Arguments Passed: 2
----Following Are The Command Line Arguments Passed----
argv[0]: ./[Link]
argv[1]: First Second Third

You might also like