Professional Documents
Culture Documents
PREPROCESSOR
3.1 Preprocessor
Before a C program is compiled in a compiler, source code is processed by a program called
preprocessor. This process is called preprocessing.
1. #include
#include is used to insert specific header file into C program.
Syntax:
#include filename
The content that is included in the filename will be replaced at the point where the directive is
written.
By using the file inclusive directive, we can include the header files in the programs.
Macros, function declarations, declaration of the external variables can all be combined in the
header file instead of repeating them in each of the program.
The stdio.h header file contains the function declarations and all the information regarding the input
and output.
If the first way is used, the file and then the filename in the current working directory and the
specified list of directories would be searched.
If the second way, is used the file and then the filename in the specified list of directories would be
searched.
2. #define
#define is used to create symbolic constants (known as macros) in C programming language.
This preprocessor command can also be used with parameterized macros.
Syntax:
#define name replacement text
Where,
name – it is known as the micro template.
replacement text – it is known as the macro expansion.
#define LOWER 30
void main()
{
int i;
for (i=1;i<=LOWER; i++)
{
printf("\n%d", i);
}
}
3. #undef:
The #undef directive tells the preprocessor to remove all definitions for the specified macro. A
macro can be redefined after it has been removed by the #undef directive.
Syntax:
#undef macro_definition
Example:
#include <stdio.h>
#define PI 3.14
#undef PI
void main()
{
printf("%f",PI);
}
4. #if
The #if preprocessor directive evaluates the expression or condition. If condition is true, it
executes the code.
Syntax:
#if expression
//code
#endif
Example:
#include <stdio.h>
#include <conio.h>
#define NUMBER 0
void matin() {
#if (NUMBER==0)
printf("Value of Number is: %d",NUMBER);
#endif
getch();
}
Output: Value of Number is: 0
5. #ifdef:
The #ifdef preprocessor directive checks if macro is defined by #define. If yes, it executes the
code otherwise #else code is executed, if present.
Syntax:
#ifdef MACRO
//code
#endif
Example:
#include <stdio.h>
#include <conio.h>
#define NOINPUT
void main() {
int a=0;
#ifdef NOINPUT
a=2;
#else
printf("Enter a:");
scanf("%d", &a);
#endif
printf("Value of a: %d\n", a);
getch();
}
Output: Value of a: 2
6. #ifndef:
The #ifndef preprocessor directive checks if macro is not defined by #define. If yes, it
executes the code otherwise #else code is executed, if present.
Syntax:
Y #ifndef MACRO
//code
#endif
Example:
#include <stdio.h>
#include <conio.h>
#define INPUT
void main() {
int a=0;
#ifndef INPUT
a=2;
#else
printf("Enter a:");
scanf("%d", &a);
#endif
printf("Value of a: %d\n", a);
getch();
}
Output:
Enter a:5
Value of a: 5
3.2 FILES
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,
Text Files
2. The precise definition of the .txt format is not specified, but typically matches the
reads text and, for that reason, are considered universal (or platform
independent).
Binary Files
3.3 Creating and Reading and writing text and binary files
File Operations
In C, you can perform four major operations on the file, either text or binary:
When working with files, you need to declare a pointer of type file. This declaration is needed
for communication between the file and program.
FILE *fptr;
Opening a file is performed using the library function in the "stdio.h" header file: fopen().
ptr=fopen(“fileopen”,”mode”);
For Example:
fopen("E:\\cprogram\\newprogram.txt","w");
fopen("E:\\cprogram\\oldprogram.bin","rb");
R Open for reading. If the file does not exist, fopen() returns NULL.
Rb Open for reading in binary mode. If the file does not exist, fopen() returns NULL.
W Open for writing. If the file exists, its contents are overwritten. If the file
does not exist, it will be created.
Wb Open for writing in binary mode. If the file exists, its contents are overwritten. If the file
does not exist, it will be created.
A Open for append. i.e, Data is added If the file does not exists, it will be created.
to end of file.
Ab Open for append in binary mode. If the file does not exists, it will be created.
i.e, Data is added to end of file.
r+ Open for both reading and writing. If the file does not exist, fopen() returns NULL.
rb+ Open for both reading and writing If the file does not exist, fopen() returns NULL.
in binary mode.
w+ Open for both reading and writing. If the file exists, its contents are overwritten. If the file
does not exist, it will be created.
wb+ Open for both reading and writing If the file exists, its contents are overwritten. If the file
in binary mode. does not exist, it will be created.
a+ Open for both reading and If the file does not exists, it will be created.
appending.
ab+ Open for both reading and If the file does not exists, it will be created.
appending in binary mode.
Closing a File
The file (both text and binary) should be closed after reading/writing.
fclose(fptr);
#include <stdio.h>
int main()
{
int num;
FILE *fptr;
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;
}
#include <stdio.h>
int main()
{
int num;
FILE *fptr;
if ((fptr = fopen("C:\\program.txt","r")) == NULL){
printf("Error! opening file"); // Program exits if the file pointer returns NULL.
exit(1);
}
fscanf(fptr,"%d", &num);
printf("Value of n=%d", num);
fclose(fptr);
return 0;
}
To write into a binary file, you need to use the function fwrite(). The functions takes four
arguments: Address of data to be written in disk, Size of data to be written in disk, number of
such type of data and pointer to the file where you want to write.
fwrite(address_data,size_data,numbers_data,pointer_to_file);
Example:
#include <stdio.h>
struct threeNum
{
int n1, n2, n3;
};
int main()
{
int n;
struct threeNum num;
FILE *fptr;
if ((fptr = fopen("C:\\program.bin","wb")) == NULL)
{
printf("Error! opening file"); // Program exits if the file pointer returns NULL.
exit(1);
}
for(n = 1; n < 5; ++n)
{
num.n1 = n;
num.n2 = 5n;
num.n3 = 5n + 1;
fwrite(&num, sizeof(struct threeNum), 1, fptr);
}
fclose(fptr);
return 0;
}
fread(address_data,size_data,numbers_data,pointer_to_file);
Example:
#include <stdio.h>
struct threeNum
{
int n1, n2, n3;
};
int main()
{
int n;
struct threeNum num;
FILE *fptr;
C programming supports different file open mode to perform different operations on file. To
append data into a file you can use a file open mode.
Input file path from user to append data, store it in some variable say filePath.
Declare a FILE type pointer variable say, fPtr.
Open file in a (append file) mode and store reference to fPtr using fPtr = fopen(filePath, "a");.
Input data to append to file from user, store it to some variable say dataToAppend.
Write data to append into file using fputs(dataToAppend, fPtr);.
Finally close file to save all changes. Use fclose(fPtr);.
Example
#include <stdio.h>
#include <stdlib.h>
#define BUFFER_SIZE 1000
void readFile(FILE * fPtr);
int main()
{
FILE *fPtr;
char filePath[100];
char dataToAppend[BUFFER_SIZE];
printf("Enter file path: ");
scanf("%s", filePath);
fPtr = fopen(filePath, "a");
if (fPtr == NULL)
{
printf("\nUnable to open '%s' file.\n", filePath);
printf("Please check whether file exists and you have write privilege.\n");
exit(EXIT_FAILURE);
}
printf("\nEnter data to append: ");
fflush(stdin); // To clear extra white space characters in stdin
fgets(dataToAppend, BUFFER_SIZE, stdin);
fputs(dataToAppend, fPtr);
fPtr = freopen(filePath, "r", fPtr);
printf("\nSuccessfully appended data to file. \n");
printf("Changed file contents:\n\n");
readFile(fPtr);
fclose(fPtr);
return 0;
}
void readFile(FILE * fPtr)
{
char ch;
do
{
ch = fgetc(fPtr);
putchar(ch);
} while (ch != EOF);
}
Example:
#include<stdio.h>
#include<conio.h>
struct person
{
int id;
char fname[20];
char lname[20];
};
int main ()
{
FILE *outfile;
outfile = fopen ("person.dat", "w");
if (outfile == NULL)
{
fprintf(stderr, "\nError opend file\n");
exit (1);
}
Example:
#include<stdio.h>
#include<conio.h>
struct person
{
int id;
char fname[20];
char lname[20];
};
int main ()
{
FILE *infile;
struct person input;
infile = fopen ("person.dat", "r");
if (infile == NULL)
{
fprintf(stderr, "\nError opening file\n");
exit (1);
}
while(fread(&input, sizeof(struct person), 1, infile))
printf ("id = %d name = %s %s\n", input.id,
input.fname, input.lname);
return 0;
}
1. fseek()
2. ftell()
3. rewind()
Following are the list of operations we can perform using the fseek() function.
Operation Description
fseek(fptr, N, 1) This will take us N bytes forward from the current position in the
file.
fseek(fptr, -N, 1) This will take us N bytes backward from the current position in the
file.
fseek(fptr, -N, 2) This will take us N bytes backward from the end position in the file.
Example:
#include <stdio.h>
int main ()
{
FILE *fp;
fp = fopen("file.txt","w+");
fputs("This is tutorialspoint.com", fp);
fseek( fp, 7, SEEK_SET );
fputs(" C Programming Language", fp);
fclose(fp);
return(0);
}
#include<stdio.h>
#include<conio.h>
void main(){
FILE *fp;
char c;
clrscr();
fp=fopen("file.txt","r");
while((c=fgetc(fp))!=EOF)
{
printf("%c",c);
}
rewind(fp);//moves the file pointer at beginning of the file
while((c=fgetc(fp))!=EOF)
{
printf("%c",c);
}
fclose(fp);
getch();
}
The contents in file.txt is “ this is a simple text”
The output can be displayed as “this is a simple textthis is a simple text”.
TUTORIAL QUESTIONS
1. #include <stdio.h>
int main(void) {
FILE f;
f = fopen("file","wb");
printf("%d",f != NULL);
fclose(f);
return 0;
}
2. #include <stdio.h>
int main(void) {
int i;
i = fprintf(stdin,"Hello!");
printf("%d",i == EOF);
return 0;
}
3. #include <stdio.h>
int main(void) {
FILE *f = fopen("file","w");
char c;
fputs("12A",f);
fclose(f);
f = fopen("file","r");
fscanf(f,"%c",&c);
fclose(f);
printf("%c",c);
return 0;
}
4. Implement a c program to read the content of five students in a file.
ASSIGNMENT QUESTIONS
SET-1
SET-3
SET-4
SET-5
1. Write a program to merge two given files and store in a target file.
2. How to handle errors in file management?
SET-6
SET-7
IMPORTANT QUESTIONS
1. Define file. Expalin how to open and read a file.
2. Discuss about the different modes available for opening a file.
3. Explain about fseek( ).
4. Write a ‘C’ program to count the number of characters in a file.
5. Explain various standard library functions for handling files.
6. Write a ‘C’ program to create a file contains a series of integer numbers and then reads
all numbers of this file and write all odd numbers to other file called odd and write all
even numbers to a file called even.
7. Discuss file positioning functions in C.
8. What are file streams? Discuss about state of file, opening and closing file with a sample
C program.
9. Write a program to merge two given files and store in a target file.
10. How to handle errors in file management?
11. Expalin about fprintf and fscanf functions.
12. Implement a C program to display the content of file.
13. Define preprocessor commands ? Explain its advantages?
14. #define is used for?
15. What Are Types Of Preprocessor In C
OBJECTIVE QUESTIONS
8. #include<stdio.h>i
int main()
{
FILE *fp;
fp=fopen("trial", "r");
return 0;
}
What does fp point to in the program ?
10 #include<stdio.h>
int main()
{
FILE *fp1, *fp2;
fp1=fopen("file.c", "w");
fp2=fopen("file.c", "w");
fputc('A', fp1);
fputc('B', fp2);
fclose(fp1);
fclose(fp2);
return 0;
}