Unit - 5
Unit - 5
Example:
Below is a simple C/C++ program to demonstrate functions.
/* An example function that takes two parameters 'x' and 'y‘ as input
and returns max of two input numbers*/
int max(int x, int y)
{
if (x > y) return x; else
return y;
}
/* main function that doesn't receive any parameter and
returns integer */
int main(void)
{
int a = 10, b = 20;
// Calling above function to find max of 'a' and 'b' int m = max(a, b);
#include <stdio.h>
// Declaring global variable
int a = 23;
void function1()
{
// Function using global variable a printf("The number is %d \n", a);
}
void function2()
{
// Function using global variable a
printf("The number is %d \n", a);
}
int main() {
// Calling functions function1();
function2();
return 0; }
Output:
The number is 23
The number is 23
Static Variable
⚫The static variable is defined using the static keyword.
⚫Its scope depends on the area of its declaration.
⚫If a static variable is defined within a function, it is a local variable.
If declared outside the function, its scope is global.
⚫A static variable statically allocated memory to the variable and its
lifetime is throughout the program. It holds its value whenever
a program is called.
⚫The default value of the static variable is 0.
#include <stdio.h>
void value()
{
int a = 10; // Local variable
static int b = 20; // Static variable
a = a + 10;
b = b + 10;
printf("The value of local variable: %d \n", a);
printf("The value of Static variable: %d \n", b);
}
int main() { value();
printf("Calling function 2nd time \n");
value();
printf("Calling function 3rd time \n"); value();
return 0; }
Output:
The value of local variable: 20 The value of Static variable: 30
Calling function 2nd time
The value of local variable: 20 The value of Static variable: 40
Calling function 3rd time
The value of local variable: 20 The value of Static variable: 50 Auto
Variable
⚫All variables declared in C programming are automatic by default.
⚫You can use the auto keyword to declare the automatic variable.
⚫ An automatic variable is a local variable whose lifetime is within
the code only.
⚫ Its default value is garbage.
Example:
#include <stdio.h> int main() {
FILE *fp;
fp= fopen ("data.txt", "w");
}
Output:
⚫ File is created in the same folder where you have saved your code
⚫ You can specify the path where you want to create your
file.
#include <stdio.h> int main() {
FILE *fp;
fp= fopen ("D://data.txt", "w");
}
Closing a File:
⚫The file (both text and binary) should be closed
after reading/writing.
⚫Closing a file is performed using the fclose() function. fclose(fptr);
⚫Here, fptr is a file pointer associated with the file to be closed.
Example:
FILE *fp;
fp = fopen ("data.txt", "r");
fclose (fp);
⚫The fclose function takes a file pointer as an argument. The file
associated with the file pointer is then closed with the help of fclose
function.
⚫ It returns 0 if close was successful and EOF (end of file) if there is
an error has occurred while file closing.
⚫ After closing the file, the same file pointer can also be used with
other files.
⚫ In ‘C’ programming, files are automatically close when the
program is terminated. Closing a file manually by writing
fclose function is a good programming practice.
Writing to a File
⚫ In C, when you write to a file, newline characters ‘\n’ must be
explicitly added.
⚫ The stdio library offers the necessary functions to write to a file:
⚫ fputc(char, file_pointer): It writes a character to the file pointed to
by file_pointer.
⚫ fputs(str, file_pointer): It writes a string to the file pointed to by
file_pointer.
⚫ fprintf(file_pointer, str, variable_lists): It prints a string to the file
pointed to by file_pointer. The string can optionally include
format specifiers and a list of variables variable_lists.
⚫ The program below shows how to perform writing to a file:
fputc() Function:
#include <stdio.h> int main()
{
int i;
FILE * fptr;
char str[] = "Guru99 Rocks\n";
fptr = fopen("fputc_test.txt", "w"); // "w" defines "writing
mode"
for (i = 0; str[i] != '\n'; i++) {
/* write to file using fputc() function */
fputc(str[i], fptr);
}
fclose(fptr); return 0;
}
Output:
⚫ The above program writes a single character into the
fputc_test.txt file until it reaches the next line symbol “\n” which
indicates that the sentence was successfully written. The process
is to take each character of the array and write it into the file.
⚫ In the above program, we have created and opened a file called
fputc_test.txt in a write mode and declare our string which will be
written into the file.
⚫ We do a character by character write operation using for loop and
put each character in our file until the “\n” character is encountered
then the file is closed using the fclose function.
fputs () Function:
#include <stdio.h>
int main() {
FILE * fp;
fp = fopen("fputs_test.txt", "w+");
fputs("This is Guru99 Tutorial on fputs,", fp);
fputs("We don't need to use for loop\n", fp);
fputs("Easier than fputc function\n",
fp);
fclose(fp); return (0);
}
Output:
⚫ In theabove program, we have created and opened a file
called
fputs_test.txt in a write mode.
⚫ After that we do a write operation using fputs() function by writing
three different strings
⚫ Then the file is closed using the fclose function.
Reading data from a File
⚫There are three different functions dedicated to reading data from a
file
⚫fgetc(file_pointer): It returns the next character from the file
pointed to by the file pointer. When the end of the file has
been reached, the EOF is sent back.
⚫fgets(buffer, n, file_pointer): It reads n-1 characters from the file
and stores the string in a buffer in which the NULL character
‘\0’ is appended as the last character.
⚫fscanf(file_pointer, conversion_specifiers, variable_adresses): It
is used to parse and analyze data. It reads characters from the
file and assigns the input to a list of variable pointers
variable_adresses using conversion specifiers. Keep in mind that
as with scanf, fscanf stops reading a string when space or newline is
encountered.
Interactive File Read and Write with getc and putc
⚫These are the simplest file operations. getc stands for get character,
and putc stands for put character. These two functions are
used to handle only a single character at a time.
⚫Following program demonstrates the file handling functions in ‘C’
programming:
#include <stdio.h> int main() {
FILE * fp;
char c;
printf("File Handling\n");
//open a file
fp = fopen("demo.txt", "w");
//writing operation
while ((c = getchar()) != ‘\n’) {
fputc(c, fp); }
//close file
fclose(fp);
printf("Data Entered:\n");
//reading
fp = fopen("demo.txt", "r");
while ((c = getc(fp)) != EOF) {
printf("%c", c);
}
fclose(fp);
return 0;
}
Output:
⚫ In the above program we have created and opened a file called
demo in a write mode.
⚫ After a write operation is performed, then the file is closed using
the fclose function.
⚫ We have again opened a file which now contains data in a reading
mode. A while loop will execute until the eof is found. Once the
end of file is found the operation will be terminated and data will be
displayed using printf function.
⚫ After performing a reading operation file is again closed using the
fclose function.
Summary
⚫A file is a space in a memory where data is stored.
⚫‘C’ programming provides various functions to deal with a file.
⚫A mechanism of manipulating with the files is
called as file
management.
⚫A file must be opened before performing operations on it.
⚫A file can be opened in a read, write or an append mode.
⚫getc and putc functions are used to read
and write a single character.
⚫The function fscanf() permits to read and parse data from a file
⚫We can read (using the getc function) an entire file by looping to
cover all the file until the EOF is encountered
⚫We can write to a file after creating its name, by using the
function fprintf() and it must have the newline character at
THANK
YOU