Before I start, I want to say "Hello World!" in English first. This is the first post for this blog and my purpose is to introduce readers to the C programming language. I don't claim to be the best teacher or I don't claim that you'll learn everything about C in this blog but I'll try and do my best, after all, I'm a first year software engineering student.
First of all, you'll need a compiler (if you don't know what a compiler is, click here). I use Net Beans IDE for C programming, it can also be used with several other languages. You can download it here. Choose the 30mb one that has C/C++ or choose the 288mb one that has all supported languages if you like. The latter is good to have if you're planning on learning other languages in the future.
After installing Net Beans, open it and go to "File" and click on "New Project" then from categories, choose "C/C++" and from projects, choose "C/C++ Application", click on "Next", name your project, choose a location for it and from the drop-down box, choose "C" and click on "Finish". Then, from the solution explorer box on the left of Net Beans IDE, click on your project's "Source Files" node, then double click on "main.c". Click on the image below to see the steps.

Everything in C programming language is case-sensitive, so be careful about that. A and a are not the same.
// Two slashes next to each other mean a comment line
// and this line will be ignored by the compiler
/*
These mean that anything in between is a comment and will be ignored by the compiler
*/
/* Apparently, I can't use pointy brackets in a post so I'll use screenshots of them. */
#include
/* # sign is a directive for the compiler and this is basically telling the compiler to include the stdio.h (standard input/output) header file to our code. We include header files that contain useful functions for us. Click here to see what function means in detail. */#include
/* stlib means standard library, the .h extension means header file. */int main() /* Every executable program must have a main function, this is where a program begins to execute. int means an integer and in this context, it means the return type for this function "main" is an integer */
{ /* Curly braces indicate beginning and ending points of a scope. Click here to see what a scope is. They basically show where something's domain or area begins and ends. In this case, the opening curly brace means the domain of function "main" has begun. Every line in between these curly braces under the function "main" will be executed when the function "main" is called and the function "main" is going to be called by the OS (operating system) when we run this program */
return 0; /* When you run a program, it is executed line by line. If all the code in the scope of function "main" is executed then the function "main" returns the value 0 to the caller which is the OS. This means the program ran successfully. Don't think that every function needs to return a value for running successfully or not. For example, you can write a function to calculate sum of two numbers and then return the sum. We will discuss this in the coming posts. And, oh, in C and C based languages ";" means end of line, without the quotation marks though. */
}
So this was the explanation for the code already created by the compiler for us. Remember the #include directives which include header files that contain functions? In the stdio.h header file, there is a function called "printf" which prints strings of text. So we will use a printf function to print "Hello World" on our screens. Let's see how it is used.

#include

int main()
{
printf("Hello World!\n"); /* "\n" here is a special character which means a new line. */
return 0;
}
After writing your printf, hit F6 if you're using Net Beans IDE, this will compile and run your program. It may be a different key in other compilers but you get the idea.
Aaaand... Tadaaaa! You've written your first program in the C language, congratulations!

0 comments:
Post a Comment