C Programming Language: Functions, Pointers, and Files

Classified in Computers

Written at on English with a size of 4.96 KB.

Functions in C

Void functions provide a way to emulate what are called procedures in other languages (e.g., PASCAL). They are used when it is not required to return a value. Here is an example that prints the squares of certain numbers:

void square ()
{
int count;

for (counter = 1; count < 10; counter++)
printf ("%d\n", counter * counter);
}

main ()
{
square ();
}

Input and Output Parameters

To use parameters in/out, the formal parameter must be preceded by the ampersand symbol (&), and the current parameter must be a variable (not an expression). Input/output parameters are used when you want a procedure to change the contents of the current variable. The fact that these parameters are explicitly defined as variables makes the programmer aware of where a procedure can modify a variable that is passed as a parameter.

The performance of input/output parameters is based on passing the procedure a reference to the current variable instead of its value. Therefore, these parameters are also called reference parameters.
#include <stdio.h>
#include <conio.h>
#include <math.h>
main () {
void roots (float a, float b, float c, float &R1, float &R2)
{
float value;
value = sqrt (b * b - 4.0 * a * c);
R1 = (-b + value) / (2.0 * a);
R2 = (-b - value) / (2.0 * a);
}

Struct in C

A struct is a way to group variables into a structure. When you create a struct, you can create multiple instances of this new type of variable.

struct Structure1 {
char c;
int i;
float f;
};

int main () {
struct Structure1 s1, s2;
s1.c = 'a';
s1.i = 1;
s1.f = 3.14;
s1.d = 0.00093;
s2.c = 'a';
s2.i = 1;
s2.f = 3.14;
s2.d = 0.00093;
}

Pointers

A pointer is a special type of variable that is responsible for storing memory addresses. A memory address specifies where a record or variable is being saved in the machine's memory.
To create a pointer to point to integers, we do something similar to declaring an int variable, except that we precede the pointer's name with an asterisk (*) to tell the compiler that this is a pointer to an int and not an int itself.

int A; // Integer variable
int *pA; // Pointer to integer

Files

C defines the data structure FILE <http://c.conclase.net/librerias/?ansitip=FILE> in the header file stdio.h <http://c.conclase.net/librerias/?ansilib=stdio> for file handling. We always use pointers to these structures.
The definition of this structure depends on the compiler, but it generally keeps a field with the current position of the read/write buffer to improve file access performance and areas for domestic use.

  • FILE *fopen (char *name, char *mode);
    This function is used to open and create files on disk. The return value is a pointer to a FILE structure <http://c.conclase.net/librerias/?ansitip=FILE>.
  • size_t fwrite (void *pointer, size_t size, size_t nregistros, FILE *file);
    This function is designed to work with records of constant length. It can write to a file one or more records of the same length stored from a particular memory address.
  • size_t fread (void *pointer, size_t size, size_t nregistros, FILE *file);
    This function is designed to work with constant length records. It can read from a file one or more records of the same length and store them from a particular memory address. The user is responsible for ensuring that there is enough space to contain the information read.
  • int fclose (FILE *file);
    It is important to close open files before leaving the application. This function is useful for that. Closing a data file stores data still in the buffer memory and updates some data in the file header that the operating system keeps. It also allows other programs to open the file for use. Very often, files cannot be shared by several programs.

Example: Output a File Twice
#include <stdio.h>
int main ()
{
FILE *file;
file = fopen ("precio.c", "r");
while (!feof (file)) fputc (fgetc (file), stdout);
rewind (file);
while (!feof (file)) fputc (fgetc (file), stdout);
fclose (file);
getchar ();
return 0;
}

Entradas relacionadas: