0% found this document useful (0 votes)
4 views4 pages

C Programs: Preprocessor, File I/O, CLI

The document outlines several C programming tasks including using pre-processor directives, reading from a file, writing to a file, and handling command line arguments. Each section provides an aim, algorithm, program code, and a result confirming successful execution. The programs demonstrate basic file operations and command line argument handling in C.
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)
4 views4 pages

C Programs: Preprocessor, File I/O, CLI

The document outlines several C programming tasks including using pre-processor directives, reading from a file, writing to a file, and handling command line arguments. Each section provides an aim, algorithm, program code, and a result confirming successful execution. The programs demonstrate basic file operations and command line argument handling in C.
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

Preprocessor directive

Aim:
To write a C program using pre-processor directives

Algorithm:
[Link] the program.
[Link] necessary header files.
[Link] the macros.
4. Use conditional predictive to check whether the macro exists.
5. If exists, read the input from the user.
6. Otherwise use the default value
7. Display the result.
8. Stop the program.

Program:
#include <stdio.h>
#include <conio.h>
#define INPUT
void main()
{
int a=0;
#ifdef INPUT
printf("Enter a:");
scanf("%d", &a);
#else
a=2;
#endif
printf("Value of a: %d\n", a);
getch();
}

Result:
Thus the C program using pre-processor directives has been executed and output was verified
successfully.

File Operation -Read


AIM:
To write a C program to read the content of the file.

ALGORITHM:

1. Start the Program


2. Include necessary header files.
3. Declare necessary variables.
4. Open the file using fopen() function
5. If the file does not exist display message, otherwise read the content of the file.
[Link] the contents of the file.
[Link] the file.
[Link] the program.

PROGRAM:

# include <stdio.h>
int main( )
{
FILE *fp ;
char data[50] ;
printf( "Opening the file test.c in read mode" ) ;
fp = fopen( "test.c", "r" ) ;
if ( fp == NULL )
{
printf( "Could not open file test.c" ) ;
return 1;
}
printf( "Reading the file test.c" ) ;
while( fgets ( data, 50, fp ) != NULL )
printf( "%s" , data ) ;
printf("Closing the file test.c") ;
fclose(fp) ;
return 0;
}

RESULT:

Thus the C program to read the content of the file has been executed and the output was
verified successfully.

File Operation-Write
AIM:
To write a C Program to write the contents to a fie.

ALGORITHM:

[Link] the Program


2. Include necessary header files.
3. Declare necessary variables.
4. Open the file using fopen() function in write mode.
5. If the file does not exist display message, otherwise read the content of the file.
6. Read input from user using gets() function
7. Write the contents into the file using fputs() function
8. Close the file using fclose() function
[Link] the program.

PROGRAM:

# include <stdio.h>
# include <string.h>
int main( )
{
FILE *fp ;
char data[50];
printf( "Opening the file test.c in write mode" ) ;
fp = fopen("test.c", "w") ;
if ( fp == NULL )
{
printf( "Could not open file test.c" ) ;
return 1;
}
printf( "\n Enter some text from keyboard” \
“ to write in the file test.c" ) ;
while ( strlen ( gets( data ) ) > 0 )
{
fputs(data, fp) ;
fputs("\n", fp) ;
}
printf("Closing the file test.c") ;
fclose(fp) ;
return 0;
}

RESULT:
Thus the C program to write the contents into the file has been executed and the output
was verified successfully.
COMMAND LINE ARGUMENTS
AIM:
To write a C Program to print the arguments passed using command line argument.

ALGORIHTM:

1. Start the Program


2. Include necessary header files.
[Link] the arguments argc ,argv[] inside the main function
4. Check the number of arguments passed
5. If the argc >=2, display the arguents passed
[Link] arguments list is empty
[Link] the program.
PROGRAM:

#include <stdio.h>
#include <conio.h>
int main(int argc, char *argv[])
{
int i;
if( argc >= 2 )
{
printf("The arguments supplied are:\n");
for(i = 1; i < argc; i++)
{
printf("%s\t", argv[i]);
}
}
else
{
printf("argument list is empty.\n");
}
return 0;
}

RESULT:
Thus the C program to print the arguments passed using command line arguments
executed successfully.

You might also like