Variables, Functions and Pointers

Requirements:

  1. Linux Distribution (obviously)
  2. A text editor :
    Vim, Emacs, gedit, kate, atom
  3. A Compiler :
    gcc or clang
  4. A Shell:
    bash, xterm, sh

My setup for this tutorial:

  1. Debian GNU/Linux 9.4
  2. Atom (Text editor)
  3. gcc version 6.3.0-18+deb9u1 (Debian’s Release version for Debian 9.1)
    bash

 

In this tutorial, i will be going over Variables, Functions and Pointers in
that order.

Variables:

Variables are data items that can take in a value, the main data types are:

  1. char, a single character (eg ‘a’ , ‘d’ , etc). This is 1 byte in size
  2. short, an integer that has the size of 2 bytes.
  3. int, an integer that has the size of 4 bytes.
  4. float, a floating-point integer
  5. double, a floating-point integer with double precision
  6. long long, an integer whose size is 8 byte

 Along with these, you can declare them unsigned (except float or double).
This means their values start at 0 , rather then their signed versions which
vary. When a variable is signed, the processor has to use the first most bit
to determine whether or not the variable is a negative number. For example, the
char’s values can range from -127 to 127, where as its unsigned counterpart (
delared as “unsigned char”) can range from 0-255. Lets start up our editor and
go back to our first project. We will create some new variables to print.

#include <stdio.h>

int main(int argc,char * argv[]){
     int foo = 42;
     float pi = 3.1415
     long long bigNum = 53339847763;
     char letter = 'B';

     printf("Hello World!\n");
     printf("Printing foo: %d\n",foo);
     printf("Printing pi: %f\n",pi);
     printf("Printing bigNum: %lld\n", bigNum);
     printf("Printing letter: %c\n", letter);
     return 0;
}

 Now save that, recompile, and re-run. Notice how i have the percent symbols along with a character? That represents a data type the printf format can output. ‘%d’ represents a decimal value, usually an integer. ‘%f’ is for float and ‘%lld’ is for long long (as it represents a long long int). I wont get into the reasoning why printf allows you to add variables to the parameters yet, but its known as variable arguments.

Functions:

 Functions are sub-routines that programmers write to prevent any type of
duplication. They are declared with a type , name, and then parenthesis followed
by parameters. For example:

int multiply(int a, int b);

 You can either implement it here or at the bottom of the file. I will give you both examples within our main.c . You can either declare it like this:

#include <stdio.h>

int multiply(int a , int b){ // Declare and implement here
     return a * b;
}

int main(int argc,char * argv[]){
     int foo = 42;
     int bar = 3;

     printf("Hello World!\n");
     printf("Printing foo: %d\n",foo);
     printf("Printing bar: %d\n",bar);
     printf("Multiplying the two: %d",multiply(foo,bar));
     return 0;
}

or you can do this :

#include <stdio.h>

int multiply(int a , int b); // declare it here

int main(int argc,char * argv[]){
     int foo = 42;
     int bar = 3;

     printf("Hello World!\n");
     printf("Printing foo: %d\n",foo);
     printf("Printing bar: %d\n",bar);
     printf("Multiplying the two: %d",multiply(foo,bar));
     return 0;
}

 

int multiply(int a , int b){ //implement it here
     return a * b;
}

 Notice how I used the function as a variable? This is because the function
returns an integer.

Introduction to Pointers:

 Pointers can be a bit of a hard concept to first understand, so I will touch on them today. They are represented by the asterisk character. Pointers are variables that point to another in memory. C passes all its variables by value. This means that the arguments of the the function are passed in by temporary variables. When you pass in foo and bar in the multiply(int a, int b) function, it is creating temporary variables with the value of foo and bar. So if we want to swap two variables, we cant just pass in two integers, because the values will stay the same.

So we do the following:

void swap(int * a , int * b){
     int temp = *a;
     *a = *b;
     *b = temp;
}

 Note: the void type tells the compiler that it will not return anything, it is just used to manipulate data. In this function, I pass in two pointers to integers. After, i create a temporary variable, and dereference the pointer in temp. This means i am storing the value at the address the pointer is holding. I then set the value the address of a is holding to the value the address of b is holding, and then set the value the address b is holding to the value of temp.

So lets use this function in our program:

#include <stdio.h>

int multiply(int a , int b);
void swap(int * a, int * b);

int main(int argc,char * argv[]){
     int foo = 42;
     int bar = 3;

     printf("Hello World!\n");
     printf("Printing foo: %d\n",foo);
     printf("Printing bar: %d\n",bar);
     printf("Multiplying the two: %d",multiply(foo,bar));
     swap(&foo,&bar);
     printf("Printing foo: %d\nPrinting bar: %d\n",foo,bar);
     return 0;

}

int multiply(int a , int b){
     return a * b;
}

void swap(int * a , int * b){
     int temp = *a;
     *a = *b;
     *b = temp;
}

 Recompile and run with ./test1 like normal. Notice foo and bar is switched? Why? Because when we call the swap function, we are using the ampersand ‘&’ symbol to tell the function to take in the address of foo and the address of bar. To declare pointers, you must use the ampersand sign. For example:

int x = 6;
int * y = &x;

 

 In the next tutorial, i will teach about conditional statements and loops

Leave a Reply