I am learning C in 2020 – Beginner – Data types, Headers and Makefile

Operation System : Windows 10

IDE : https://cplayground.com/

Requirements: gcc compiler

Book : Head first C chapter 4

Goal

Learn more about C

What I learned

The different data types

Keep in mind that there are other data types that are untreated here.

Char

I sincerely thought each char was stored as it is, but apparently they are stored as numbers not as A,B,C .Each alphabet is assigned a number, for example a’s value is 97 while it’s uppercase A is 65. 1 byte is used to store a char regardless of operating system.

Int

Unlike a char, an int value is stored either in 2 bytes or 4 bytes depending on the platform. This means int’s min and max values are platform dependent. You can get the min/max values from the limits header’s INT_MAX and INT_MIN.

Short

If it is unnecessary to store monstrously large int values or you only need to store values within the -32,768 to 32,767 range, this data type will do the job. It’s storage size is 2 bytes.

Long

With the storage size being 8 bytes, one can store really large numbers in this data type. Values ranging from -9223372036854775808 to 9223372036854775807. I feel one is most likely not going to use this as often except if you are working for NASA. Please do not quote me on that.

Float

Whole numbers aren’t the only numbers right ? Decimals should not be left out. If you are developing a management system that involves money, you are going to be working with a lot of floats. 4 bytes are used to store a floated value.

Double

This is also a float value but for longer float values. So it is a long to an int. Similar to a pointer and a long data type, 8 bytes are used to store such a value.

What is a preprocessor ?

I assumed this was simply another word for compiler, but it is nahht. You know those words that come with a hashtag e.g. #include, yep that is a preprocessor. These are commands to the compiler before the compilation occurs. One can define or replace constants ( unmodifiable) or include header files or even conditionally define a function with these directives.

Why do I need a gcc compiler ?

According to a reputable source, wikipedia, the GCC stands for GNU Compiler Collection. It is a compiler system produced by the GNU project. Compiler means a program that translates a source code to a much lower-level language e.g. machine code.

Type-Casting

Nothing other than forcing a change of one type to another. So if you have an int variable but you need its float value, simply cast it.

#include <stdio.h>
#include <stdlib.h>

int main() {


    int x  = 13;
    float y = (float) x;
    printf("The int value is %d , while its float value is %f\n", x, y);
    return 0;
}
Run.... 
The int value is 13 , while its float value is 13.000000

Function declaration or function signature

Through this, you can tell the compiler everything it needs to know about a function without defining the body of the function. People usually declare a function, when the function definition is not in the source code file it is called or when its definition comes after it gets called.

#include <stdio.h>
#include <stdlib.h>

void callfriend(char * friend);

int main() {

    
    callfriend("comfort");
    return 0;
}


void callfriend(char * friend){
    
    printf("Hello %s\n", friend);
}

Creating a header file

I swear naming conventions in programming continues to amaze me. I really thought the “header” file was a very extravagant file type. But it is simply a file with .h extension containing function signatures, commands or preprocessors and constants. There are two ways by which you can use an external header file with the #include preprocessor.

Double quotes

If you surround your header file with double quotes, you are essentially telling the preprocessor “The header file I want lives in the same directory / folder as this file here”.

#include "local.h"

Angle brackets

On the other hand, when surrounded by angle brackets, the preprocessor knows to find it in the standard folder for header files.

#include <stdio.h>

If you want to override the standard stdio header file, how would you go about it ?

What is the object code file

According to this 2011 stack overflow response, it is the machine code result after compilation. One can create an object file by running the following command :

gcc <filename>.c -c

This will generate : <filename>.o

There are several steps involved that I sincerely do not fully grasp.

Linking object code files

How could one combine several source files into one major file without copy and paste? Well, you see the gcc compiler does not only compile the source code into object files, but also link them. The result of linking leads to an executable. This can be done as follows :

gcc file1.c file2.c file3.c -o onegiantexecutable

Author Notes

This is most likely not the cleanest implementation, just a heads up.

Links

  1. C – Data Types
  2. ASCII Table
  3. C – Preprocessors
  4. GNU Compiler Collection
  5. Type Casting – C Programming
  6. Function declarations
  7. How to write your own header file in C?
  8. Where Does GCC Look to Find its Header Files?