C Notes III-UNIT
C Notes III-UNIT
C Notes III-UNIT
C Preprocessor directives:
Before a C program is compiled in a compiler, source code is processed
by a program called preprocessor. This process is called preprocessing.
Commands used in preprocessor are called preprocessor directives and
they begin with “#” symbol.
Below is the list of preprocessor directives in C.
Syntax/Description
Preprocessor
Syntax:#define
Macro This macro defines constant value and can be any of the
basic data types.
Syntax:#include<file_name>
Header file
The source code of the file “file_name” is included in the main
inclusion
program at the specified place.
Syntax: #ifdef, #endif, #if, #else, #ifndef
Conditional
Set of commands are included or excluded in source program
compilation
before compilation with respect to the condition.
Syntax:#undef
Other directives
#undef is used to undefine a defined macro variable.
#include <stdio.h>
#define height 100
#define number 3.14
#define letter 'A'
#define letter_sequence "ABC"
#define backslash_char '\?'
void main()
{
printf("value of height : %d \n", height );
printf("value of number : %f \n", number );
printf("value of letter : %c \n", letter );
printf("value of letter_sequence : %s \n", letter_sequence);
printf("value of backslash_char : %c \n", backslash_char);
}
EXAMPLE PROGRAM FOR CONDITIONAL COMPILATION DIRECTIVES:
A) EXAMPLE PROGRAM FOR #IFDEF, #ELSE AND #ENDIF IN C:
“#ifdef” directive checks whether particular macro is defined or not. If it is
defined, “If” clause statements are included in source file.
Otherwise, “else” clause statements are included in source file for compilation
and execution.
#include <stdio.h>
#define RAJU 100
int main()
{
#ifdef RAJU
printf("RAJU is defined. So, this line will be added in " \
"this C file\n");
#else
printf("RAJU is not defined\n");
#endif
return 0;
}
FILES
What is file?
File is a collection of bytes that is stored on secondary storage devices like disk.
There are two kinds of files in a system. They are,
1. Text files (ASCII)
2. Binary files
Text files contain ASCII codes of digits, alphabetic and symbols.
Binary file contains collection of bytes (0’s and 1’s). Binary files are
compiled version of text files.
Difference between Text and binary Files
int main()
{
FILE *fp;
char ch;
fp = fopen("one.txt", "w");
printf("Enter data...");
while( (ch = getchar()) != EOF) {
putc(ch, fp);
}
fclose(fp);
fp = fopen("one.txt", "r");
return 0;
}
struct emp
{
char name[10];
int age;
};
void main()
{
struct emp e;
FILE *p,*q;
p = fopen("one.txt", "a");
q = fopen("one.txt", "r");
printf("Enter Name and Age:");
scanf("%s %d", e.name, &e.age);
fprintf(p,"%s %d", e.name, e.age);
fclose(p);
do
{
fscanf(q,"%s %d", e.name, e.age);
printf("%s %d", e.name, e.age);
}
while(!feof(q));
}
Difference between Append and Write Mode
Write (w) mode and Append (a) mode, while opening a file are almost the same.
Both are used to write in a file. In both the modes, new file is created if it
doesn't exists already.
The only difference they have is, when you open a file in the write mode, the
file is reset, resulting in deletion of any data already present in the file. While in
append mode this will not happen. Append mode is used to append or add
data to the existing data of file(if any). Hence, when you open a file in
Append(a) mode, the cursor is positioned at the end of the present data in the
file.
Reading and Writing in a Binary File
A Binary file is similar to a text file, but it contains only large numerical data.
The Opening modes are mentioned in the table for opening modes above.
fread() and fwrite() functions are used to read and write is a binary file.
fwrite(data-element-to-be-written, size_of_elements, number_of_elements,
pointer-to-file);
fread() is also used in the same way, with the same arguments like fwrite()
function. Below mentioned is a simple example of writing into a binary file
const char *mytext = "The quick brown fox jumps over the lazy dog";
FILE *bfp= fopen("test.txt", "wb");
if (bfp)
{
fwrite(mytext, sizeof(char), strlen(mytext), bfp);
fclose(bfp);
}
#include<stdio.h>
#include<stdlib.h>
struct employee
{
char name[50];
char designation[50];
int age;
float salary
} employee;
int main()
{
int n, i, chars;
FILE *fp;
fp = fopen("employee.txt", "wb");
if(fp == NULL)
{
printf("Error opening file\n");
exit(1);
}
printf("Testing fwrite() function: \n\n");
printf("Enter the number of records you want to enter: ");
scanf("%d", &n);
for(i = 0; i < n; i++)
{
printf("\nEnter details of employee %d \n", i + 1);
fflush(stdin);
printf("Name: ");
gets(employee.name);
printf("Designation: ");
gets(employee.designation);
printf("Age: ");
scanf("%d", &employee.age);
printf("Salary: ");
scanf("%f", &employee.salary);
chars = fwrite(&employee, sizeof(employee), 1, fp);
printf("Number of items written to the file: %d\n", chars);
}
fclose(fp);
return 0;
}
Expected Output:
Testing fwrite() function:
struct employee
{
char name[50];
char designation[50];
int age;
float salary
} emp;
int main()
{
FILE *fp;
fp = fopen("employee.txt", "rb");
if(fp == NULL)
{
printf("Error opening file\n");
exit(1);
}
fclose(fp);
return 0;
}
Expected Output:
Testing fread() function:
Name: Bob
Designation: Manager
Age: 29
Salary: 34000.00
Name: Jake
Designation: Developer
Age: 34
Salary: 56000.00
fprintf() function #
Syntax: int fprintf(FILE *fp, const char *format [, argument, ...] );
fprintf() function is same as printf() but instead of writing data to the console, it
writes formatted data into the file. Almost all the arguments of fprintf() function
is same as printf() function except it has an additional argument which is a file
pointer to the file where the formatted output will be written. On success, it
returns the total number of characters written to the file. On error, it returns
EOF.
The following program demonstrates how to use fprintf() function.
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
char name[50];
int roll_no, chars, i, n;
float marks;
fp = fopen("records.txt", "w");
if(fp == NULL)
{
printf("Error opening file\n");
exit(1);
}
fclose(fp);
return 0;
}
Expected Output:
Testing fprintf() function:
int main()
{
FILE *fp;
char name[50];
int roll_no, chars;
float marks;
fp = fopen("records.txt", "r");
if(fp == NULL)
{
printf("Error opening file\n");
exit(1);
}
fclose(fp);
return 0;
}
Expected Output:
Name: Tina Roll no: 1 Marks: 45.00
Name: Nina Roll no: 5 Marks: 89.00
Name: Tim Roll no: 2 Marks: 49.00
Name: Jim Roll no: 8 Marks: 41.00
Name: King Roll no: 9 Marks: 59.00
fseek():
This function is used for seeking the pointer position in the file at the specified
byte.
Syntax: fseek( file pointer, displacement, pointer position);
Where
file pointer ---- It is the pointer which points to the file.
displacement ---- It is positive or negative.This is the number of bytes which
are skipped backward (if negative) or forward( if positive) from the current
position.This is attached with L because this is a long integer.
Pointer position:
This sets the pointer position in the file.