A small introduction to C
On my first thread i was thinking which subject would i start. Sure there are more basic concepts to research, but due the fact they could be a little abstract to a first thread, and also to a first learner, i decided to begin with basic concepts of C programming language, and explain more abstract concepts like registers, linker, stacks, heaps and such later. So lets begin!
Our first app will begin doing something very simple. Just print a line on the screen. I will be using visual studio 2015 to this demonstration, but i will also add instructions to visual studio 2013, so follow me.
When you start, the visual studio IDE will contain only the basic instructions.
This is what we got in visual studio 2015:
#include "stdafx.h"
int main()
{
return 0;
}
And this is what we got in visual studio 2013:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
As it is, this code runs absolutelly fine. However this code is well suited to run within microsoft systems. In visual Studio 2015, you have to change very little code for it to be portable, but in visual studio 2013, i believe it would be benefitial to learn how to make this code portable. If you dont mind for the sake of learning and demonstration, or already know how to port this code to ANSI C, you can jump the STEPS 1 to 5 steps with no problem. Just keep in mind, as it is, the library "stdafx.h" is a requirement, and in case of visual studio 2013, even a little amount of non portable code is.
STEP 1: The first thing we gotta do is, click on the project tab, then click on your project settings, or hit alt + F7
STEP 2: In the window you just opened, click on Configuration Properties -> C/C++, Preprocessor. Click thePreprocessor Definitions and the the Edit option
STEP 3: Add this line in the box: _CRT_SECURE_NO_WARNINGS
STEP 4: Now lets jump into the headers option, so we dont have to bring always microsoft pre-compiled headers. Choose the option "Not Using Precompiled Headers"
STEP 5: Now, one last step, which is optional. Lets make a default type of project depending on the type of code we want to write. In this case ill choose C code instead of C++
Excellent! Now we can use only native C/C++ libraries in our code and write code which is compatible with ANSI C. Of course you can transfer the code with little modifications, but for the sake of better explanations and for complete begginers understading, i believed it would be a good practice to add these optional steps!
At this point we all should have these lines of code in the IDE:
int main()
{
return 0;
}
Those should run fine when you hit F5 (debug), or Ctrl+F5(Run without debugging). So lets add 2 more lines then explain how everything works.
#include <stdio.h>
int main()
{
printf("Hello Reader!\n");
return 0;
}
Now just press Ctrl+F5 and Voilá. You got your first program running
Now explaining each line:
#include <stdio.h> - First we include a library, which contains functions, in this case, relationed to I/O(Input/Output data), like the printf() function below.
int main() - this is starting point of every C program. Many other languages like Java or C# have this starting point, which can receive arguments, like the function printf() below. Those arguments can be, an int and an array, containing the number of arguments and the arguments themselves, that can be passed to the main function, which represents in a short, your program. Then instead of int main() we would have int main()( int argc, char *argv[] ) . But do not worry about these datatypes lilke int or char yet :]
{ - this bracket represents the beggining of multiple instructions. Of course it is optional for you to insert just one instruction. In this case, it is the beggining of the instructions of the main function, which itself, is the starting point of your program.
printf("Hello Reader!\n"); - printf is a function, which comes with the stdio.h header. It prints character strings at your screen. You can create your own header libraries with your own functions and code, so you dont have to write this code everytime you are starting new code or app. How convenient is that. The "\n" is a escape sequence, which adds a new line to the runnning app. Lets say it wasnt there, what would happen if u had inserted another printf() function with strings as arguments? They would start right by the side of "Hello Reader" message. Nasty. You can try it yourself as homework. The last character is the semi-colon ";" which in many programming languages, and also C, represents the end of each instruction.
return 0; - return is an argument which will put and end to your function (the main function in this case) whenever it is reached. But it will also send the value returned to whoever calls it. In this case we will return 0, and 0 means nothing, but we can return a great amount of datatypes, as long as the returned argument is compatible with the function signature, in this case int main(), means the returned value must be an int, or integer(not fractional number, within a range).
} - this bracket represents the end of multiple instructions. So it ends the multiple instructions contained within the main function. Any instruction writen bellow it, will not be part of the main function, which means it will not run in your app, since the main function represents the starting point of your program. In this case, we could choose not finish it with the bracket, since it reached the return 0; instruction before the bracket, for for the sake of good practice, and not driving the IDE or your coleagues nuts, please dont go that way.
Hunf! We've come a long way for such an small program. But if you've read and got everything so far, thats a great acomplishment. Tap yourself on the back! You already know about includes, function escopes, returning arguments and printing lines on the screen. Thats the start of your knowledge, but with a good foundation, you can get anywhere!