C Files

File Handling

In C, you can create, open, read, and write to files by declaring a pointer of type FILE, and use the fopen() function:

FILE *fptr;
fptr = fopen(filename, mode);

FILE is basically a data type, and we need to create a pointer variable to work with it (fptr). For now, this line is not important. It’s just something you need when working with files. Continue reading C Files

C Math Functions

Math Functions

There is also a list of math functions available, that allows you to perform mathematical tasks on numbers.

To use them, you must include the math.h header file in your program:

#include <math.h>

Continue reading C Math Functions

C Recursion

Recursion

Recursion is the technique of making a function call itself. This technique provides a way to break complicated problems down into simple problems which are easier to solve.

Recursion may be a bit difficult to understand. The best way to figure out how it works is to experiment with it.

Continue reading C Recursion

C Function Declaration and Definition

Function Declaration and Definition

You have already learned from the previous chapters that you can create and call a function in the following way:

Example

// Create a function
void myFunction() {
  printf("I just got executed!");
}

int main() {
  myFunction(); // call the function
  return 0;
}

A function consist of two parts :

  • Declaration: the function’s name, return type, and parameters (if any)
  • Definition: the body of the function (code to be executed)

Continue reading C Function Declaration and Definition

C Variable Scope

Now that you understand how functions work, it is important to learn how variables act inside and outside of functions.

In C, variables are only accessible inside the region they are created. This is called scope.


Continue reading C Variable Scope

C Function Parameters

Parameters and Arguments

Information can be passed to functions as a parameter. Parameters act as variables inside the function.

Parameters are specified after the function name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma: Continue reading C Function Parameters

C Functions

A function is a block of code which only runs when it is called.

You can pass data, known as parameters, into a function.

Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times.

Continue reading C Functions

C Pointers and Arrays

Pointers & Arrays

You can also use pointers to access arrays.

Consider the following array of integers:

Example

int myNumbers[4] = {25, 50, 75, 100};

You learned from the arrays article that you can loop through the array elements with a for loop:

Example

int myNumbers[4] = {25, 50, 75, 100};
int i;

for (i = 0; i < 4; i++) {
  printf("%d\n", myNumbers[i]);
}

Result:

25
50
75
100

Instead of printing the value of each array element, let’s print the memory address of each array element:

Example

int myNumbers[4] = {25, 50, 75, 100};
int i;

for (i = 0; i < 4; i++) {
  printf("%p\n", &myNumbers[i]);
}

Result:

0x7ffe70f9d8f0
0x7ffe70f9d8f4
0x7ffe70f9d8f8
0x7ffe70f9d8fc

Note that the last number of each of the elements’ memory address is different, with an addition of 4.

It is because the size of an int type is typically 4 bytes, remember:

Example

// Create an int variable
int myInt;

// Get the memory size of an int
printf("%lu", sizeof(myInt));

Result :

4

So from the “memory address example” above, you can see that the compiler reserves 4 bytes of memory for each array element, which means that the entire array takes up 16 bytes (4 * 4) of memory storage:

Example

int myNumbers[4] = {25, 50, 75, 100};

// Get the size of the myNumbers array
printf("%lu", sizeof(myNumbers));

Result:

16

How Are Pointers Related to Arrays

Ok, so what’s the relationship between pointers and arrays? Well, in C, the name of an array, is actually a pointer to the first element of the array.

Confused? Let’s try to understand this better, and use our “memory address example” above again.

The memory address of the first element is the same as the name of the array:

Example

int myNumbers[4] = {25, 50, 75, 100};

// Get the memory address of the myNumbers array
printf("%p\n", myNumbers);

// Get the memory address of the first array element
printf("%p\n", &myNumbers[0]);

Result:

0x7ffe70f9d8f0
0x7ffe70f9d8f0

This basically means that we can work with arrays through pointers!

How? Since myNumbers is a pointer to the first element in myNumbers, you can use the * operator to access it:

Example

int myNumbers[4] = {25, 50, 75, 100};

// Get the value of the first element in myNumbers
printf("%d", *myNumbers);

Result:

25

To access the rest of the elements in myNumbers, you can increment the pointer/array (+1, +2, etc):

Example

int myNumbers[4] = {25, 50, 75, 100};

// Get the value of the second element in myNumbers
printf("%d\n", *(myNumbers + 1));

// Get the value of the third element in myNumbers
printf("%d", *(myNumbers + 2));

// and so on..

Result:

50
75

Or loop through it:

Example

int myNumbers[4] = {25, 50, 75, 100};
int *ptr = myNumbers;
int i;

for (i = 0; i < 4; i++) {
  printf("%d\n", *(ptr + i));
}

Result:

25
50
75
100

It is also possible to change the value of array elements with pointers:

Example

int myNumbers[4] = {25, 50, 75, 100};

// Change the value of the first element to 13
*myNumbers = 13;

// Change the value of the second element to 17
*(myNumbers +1) = 17;

// Get the value of the first element
printf("%d\n", *myNumbers);

// Get the value of the second element
printf("%d\n", *(myNumbers + 1));

Result:

13
17

This way of working with arrays might seem a bit excessive. Especially with simple arrays like in the examples above. However, for large arrays, it can be much more efficient to access and manipulate arrays with pointers.

It is also considered faster and easier to access two-dimensional arrays with pointers.

And since strings are actually arrays, you can also use pointers to access strings.

For now, it’s great that you know how this works. But like we specified in the previous chapter; pointers must be handled with care, since it is possible to overwrite other data stored in memory.

C Pointers

Creating Pointers

You learned from the previous chapter, that we can get the memory address of a variable with the reference operator &:

Example

int myAge = 43; // an int variable

printf("%d", myAge);  // Outputs the value of myAge (43)
printf("%p", &myAge); // Outputs the memory address of myAge (0x7ffe5367e044)

A pointer is a variable that stores the memory address of another variable as its value.

A pointer variable points to a data type (like int) of the same type, and is created with the * operator. Continue reading C Pointers

C Memory Address

Memory Address

When a variable is created in C, a memory address is assigned to the variable.

The memory address is the location of where the variable is stored on the computer.

When we assign a value to the variable, it is stored in this memory address.

To access it, use the reference operator (&), and the result represents where the variable is stored: Continue reading C Memory Address