by 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 codeThis 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 codeThis 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-editingFor 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 applicationHere 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 SyntaxConsists 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!
[size=200][center]Introduction to C Language[/center][/size]
Before saying what would C be, we have to know what a programming language actually is:
A [b][i]programming language[/b][/i] is a sequence of instructions which are translated by the compiler or interpreter into [i]machine code[/i].
There are three types of programming languages:
[list]
[*] 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: [i]assembly[/i] (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: [i]C[/i] - 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: [i]Java, C#, Python, Ruby etc..[/i][/list]
[size=150][b]Ok, thanks, now what's C?[/b][/size]
[b][i]C[/i][/b] is a [b]mid-level[/b] 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 [i]structured programming[/i] language and was created to be compiled using a relatively basic compiler.
Let's now get to the steps of creating a C program:
[list]
[*] 1) Writing the source code
[*] 2) Compiling it (+ preprocessing)
[*] 3) Link-editing
[*] 4) Running the application.[/list]
[size=150][b]1) Writing the source code[/b][/size]
This step is where you use the text editor (an IDE or even Notepad) to write the source code and save it to a [b][i]file.c[/b][/i]. The file extension for a C source code is .C (extensions redundant in Linux OSs).
[size=150][b]2) Compiling the code[/b][/size]
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).
[size=150][b]3) Link-editing[/b][/size]
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.
[size=150][b]4) Running the application[/b][/size]
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:
[list]
[*] [i]wrong answer[/i] - usually has to do with a logical mistake in your code
[*] [i]exceptions[/i] - 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[/list]
[size=150][b]C Syntax[/b][/size]
Consists of typical symbols and keywords:
[list]
[*] english alphabet
[*] arabic digits
[*] special symbols: .;,:?'"()[]{}<>!|\/~_#%&^+-*=
[*] keywords: [/list]
[code]
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
[/code]
Writing a C program means writing "sentences" called [b]statements[/b]. To make up those statements there are used different types of "words":
[list]
[*][b]keywords[/b] (see above) - language predefined
[*][b]variables[/b] - these are user chosen words which have to be defined before usage. To define a variable, you use the following format:[/list]
[code]<DATA_TYPE> <variable name> (= <initialization value>);
example:
int integerNumber = 5; //this defines the variable integerNumber that takes the value of 5.[/code]
[list]
[*][b]constants[/b] - same like the variables, but defined using the [b]const[/b] keyword before the DATA_TYPE. As their name say, constants [b]cannot[/b] be modified in code.[/list]
[code]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
[/code]
[b]!![/b] [i]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.[/i]
[b]Comments[/b] are plain text which is ignored by the compiler. You can write a comment like this:
[code]
...
// this is a oneline comment
/* <- this lets
me
type until it meets this -> */
...
[/code]
Let's look over the most basic program we can write in C:
[code]
#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)
[/code]
Every C program out is composed of multiple entities called [b]functions[/b]. 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]
Hello world!
[/code]
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!