Website Hosting for Just 20 ForumCoin ~ Advertise on ForumCoin
ForumCoin is an online community where you earn ForumCoin for making posts and for referring other members. You can use the ForumCoin virtual currency to buy and sell goods and services and to exchange for traditional currencies. Find out more!
Get paid up to 150 ForumCoin to submit your article.

Introduction to C Language

Postby JusApee » 14 Dec 2016, 16:56

Introduction to C Language


Before saying what would C be, we have to know what a programming language actually is:
A programming language is a sequence of instructions which are translated by the compiler or interpreter into machine code.
There are three types of programming languages:
  • Low-level P.L. - these are close related to the computer's Kernel (the software that manages interactions between hardware and software) and therefore are the fastest
    example: assembly (assembly code is usually hard to read and it does not require a compiler, but an assembler which directly codes the instructions to the binary sequence.)
  • Mid-level P.L. - these languages combine the advantages of high-level P.Ls with the high level functionality of assembler languages (these are basically fast languages made to replace the hard readability of assembly)
    example: C - it can pretty much replace assembly code, having good control of hardware. It was initially used to code operating systems, compilers, interpreters, etc.
  • High-level P.L. - these languages are the "farthest" away from the computer's Kernel. They have a lot of features and are made especially to ease the programmer's life. It may use a lot of natural language elements or even automatically deal with memory management (feature which mid-level languages like C do not have).
    examples: Java, C#, Python, Ruby etc..

Ok, thanks, now what's C?
C is a mid-level programming language mostly (currently) used in embedded systems. It first appeared in 1973 and was used to reimplement the Unix operating systems. It soon after became the most used language in the world. C is still used in creating operating systems (MS Windows 10 still runs on C, alongside C++ and Assembly).
It is a structured programming language and was created to be compiled using a relatively basic compiler.

Let's now get to the steps of creating a C program:
  • 1) Writing the source code
  • 2) Compiling it (+ preprocessing)
  • 3) Link-editing
  • 4) Running the application.

1) Writing the source code
This step is where you use the text editor (an IDE or even Notepad) to write the source code and save it to a file.c. The file extension for a C source code is .C (extensions redundant in Linux OSs).

2) Compiling the code
This is where the compiler translates the code into machine code, but before looking at your main() function (see below), it first checks for preprocessor directives(not our subject for now).
Errors during the compiling are usually syntax errors (forgetting a ";" semicolon at the end of a statement) or mistyping a variable or a function.
The file resulted after the compilation is called object file and it contains unreadable machine code (try opening a .o file with notepad, but careful not to alter it).

3) Link-editing
For this step, a link-editor comes in. What a link editor does is, simply put, takes one or more object files and creates an executable file based on those.
Errors appearing during link-editing may be errors in function definitions, forgetting a required library or a required file missing.

4) Running the application
Here is where the user comes in place. In this step you actually execute the program to check your logic.
All errors from here on will be logical, meaning that there's something wrong with the way you solved the problem. Errors can consist in:
  • wrong answer - usually has to do with a logical mistake in your code
  • exceptions - the more grave problems that actually crash your application, these are also caused by some mistakes in your code, but crashes occur when there usually are memory flaws or infinite loops

C Syntax
Consists of typical symbols and keywords:
  • english alphabet
  • arabic digits
  • special symbols: .;,:?'"()[]{}<>!|\/~_#%&^+-*=
  • keywords:
Code: Select all
asm       continue     extern     long        static      void
auto      default      float      register    struct      volatile
break     do           for        return      switch      while
case      double       goto       short       typedef      
char      else         if         signed      union
const     enum         int        sizeof      unsigned


Writing a C program means writing "sentences" called statements. To make up those statements there are used different types of "words":
  • keywords (see above) - language predefined
  • variables - these are user chosen words which have to be defined before usage. To define a variable, you use the following format:
Code: Select all
<DATA_TYPE> <variable name> (= <initialization value>);
example:
int integerNumber = 5; //this defines the variable integerNumber that takes the value of 5.

  • constants - same like the variables, but defined using the const keyword before the DATA_TYPE. As their name say, constants cannot be modified in code.
Code: Select all
const int constInteger = 5; // this defines a constant integer named constInteger equal to 5

// trying to assign our const a new value
constInteger = 10;      // the compiler will throw an error here


!! The compiler does not take space into consideration. You can use as much space (includes space, tabs and new lines) in your code as you desire.
Comments are plain text which is ignored by the compiler. You can write a comment like this:
Code: Select all
   ...
      // this is a oneline comment
      /* <- this lets
           me
           type until it meets this -> */
   ...


Let's look over the most basic program we can write in C:
Code: Select all
#include <stdio.h>            // the include directive - this includes the stdio (Standard Input/Output)
                                          // which handles input and output of our program
int main() {               // the main function of the program - this is what gets executed when the program is ran
   printf("Hello world!");      // prints "Hello World!" in the console
   return 0;               // since main() is an int function, that means it should be
}                        //            returning an int answer (more about this later)


Every C program out is composed of multiple entities called functions. main() is the function that gets called when the program is executed. Running a function means executing every instruction found in that function, therefore, building our program from the above code will produce the following output:
Code: Select all
Hello world!


This is the introduction, the "boring" stuff. This is barely the top of the hill. The more we go down, the more we have to cover.
I'll keep posting these C Guides to help you learn it. Thanks for reading, if you got here and expect the next one!
  • 2

JusApee
 
Posts: 46
Reputation: 16
ForumCoin: 341

Re: Introduction to C Language

Postby Fergal » 14 Dec 2016, 20:12

Thanks for adding such a detailed article JusApee. Do you program in C yourself? If so, do you do it full time and what type of work do you do with it?
  • 0

Author of: 52 Life Tips
User avatar
Fergal
Site Admin
 
Posts: 14,735
Location: Ireland
Reputation: 2980
ForumCoin: 15,400

Re: Introduction to C Language

Postby JusApee » 14 Dec 2016, 20:25

No problem, Fergal. Yes, I did program in C myself, but I'm not working full time. I'm currently a Computer Science student and a lot of people told me I should teach others how to program in C, because I can explain it pretty well. I have to admit that I did a little research as well before writing this article. I wouldn't have known such details like C's history if I weren't to research beforehand.
I'll keep on adding these "guides".
Have a nice evening!
  • 1

JusApee
 
Posts: 46
Reputation: 16
ForumCoin: 341



Your Ad Here.

Return to Articles & Tutorials



Who is online

Users browsing this forum: Apple [Bot], Google [Bot] and 9 guests

Reputation System ©'