Programming C: Posts 1 – Hello World

Updated on 09/01/2018

1. Introduction to Programming

Surely you have been exposed to computers, more or less the phone and use apps or games on it such as Word, Office Excel to work or use Chrome, Firefox to Facebook, Google, Facebook and Google are major software.

You imagine your programming is the process of creating software, Such applications. You create applications for others to use, work; you create the game for others to play, make a website for others to access. Nghĩ đến đây đã thấy hứng thú rồi 🙂

Để làm ra được phần mềm (hoặc game, website) then we must teach the computer how to work or otherwise put the rules that we wish to computer to perform as you press up on the character to jump up, press the button to the left, then the character will go to the left.

Like teaching someone to do what you have to say to them or write letters to give them read and from which they can understand what you say. Putting these rules into the computer so that it understands what you want to do that follow, too, you should write requirements that follow a specific syntax that's called programming language.

Programming language like Vietnamese to Vietnamese people communicate and understand, for British English, America and many other countries, … programming language it is the language to speakers for computers to understand. Now there are many different programming languages ​​such as C, C , Java, C #, PHP, python,… each language has its own advantages and disadvantages, and has applications in certain areas.

2. Programming C

C programming you simply use C programming language to create software.

Each software or program called (Computer Programs) will be written by C language with a natural or files with certain rules of language that we will gradually learn.

3. Installation tool

3.1 Settings

To programmable C, you need to install the gcc or g ++ shifter. However, to ease the current multiple integrated software (or called development environment – HERE) we can easily.

  • For you Windows users can download Dev-C++ or Code::Blocks installation and we can use immediately to write simple programs.

  • For you to use Linux (ubuntu, Linux Mint,…) can be installed by opening a terminal and run the following command to install:

    sudo apt-get install build-essential

    After running the above command, you can open any text editor to code (as gedit, vim, hay sublime-text,…) then translated, program run by the terminal.
    Or you can completely download and install Code::Blocks Linux to use very convenient, or use the command set:

    sudo apt-get install codeblocks

    You can view more details Programming C / C on Ubuntu (Linux) in his blog

For ease and consistency in process, he would use the Dev-C ++ nhé.

3.2 Basic introduction about Dev-C ++

For easy visualization, you open up Dev-C ++ will see the interface as shown below (Current version you are currently using is 5.11, you can use the other copy will be a bit different, but will not significantly).

Dev-C++

Dev-C ++ interface 5.11

In the image above, some parts himself marked by the meaning behind:
1. Button to create a new file
2. Button to open a file has
3. Button to compile and run programs
4. Area code editor (program code)
5. Sorry if the display area.

4. Created the first program

Now you can write the first software program to test.
B1: Create 1 new file.
B2: Type or copy the following code into the editor.

#include <stdio.h>

int main()
{
    printf("Welcome");
    return 0;
}

B3: Saved with the file name Test.c or Test.cpp
B4: Click the button to run the program and you will see a black screen pops up as follows:

Dev-C++

The first C program running

In which you can see the word Welcome showing up, it is the words are printed out using command printf. The lower part is horizontal line by Dev-C self-generated.

Mình giải thích một chút về code:

  • Current 1: Chứa phát biểu tiền xử lý #include <stdio.h> . Vì trong chương trình này ta sử dụng các lệnh trong thư viện của C là printf, so you need to declare the function library to tell the C compiler know. If you do not declare the program will error. Library stdio.h stands for standard input output (std – in – the) là thư viện nhập xuất chuẩn).

  • Current 3: ** int main() is the main component of any program C. Mọi chương trình C đều bắt đầu thi hành từ hàm **main. Brackets () said this is the function block (function). Hàm main() có từ khóa int said first function returns integer values (int).

  • Current 4 and 7: pair of braces {} limit of a function body. Body functions start with { and ends with }.

  • Current 5: printf ("Welcome");, instructs the printer to the string in quotes "". This line is called a statement, end a command in C is the semicolon( ; ).

  • Current 6: return 0; Returns the integer value is 0 according to the initial right to declare int main().

Noted:

  1. In his program not use conio.h library as in the standard C library does not have this, and therefore also not be used getch() to stop the screen they were replaced by command system("pause"); in the library stdlib.h
  2. when used return to return the value of the function, then you can skip this command program is still running, but the standard is wrong, returns 1 also wrong, summary is returned 0. If returns 1 others do not, the computer system will understand that your program after running generates need that error.

Now open the folder containing the file Test.c you just created, you will see a file named Test.exe (called exe), this is the executable program. Now if you want you can click on the .exe file is run the program or you can copy the exe file to another computer, you could still run, it's like you to copy games from one computer to another so.

From here we have:

  • File Test.c the files we create and write commands to the computer understands, This file called source files.
  • File Test.exe file is generated when we press the button Run, This file called executables.

Dev-C++

Source files and executables

Noted: The following programs we do will primarily run on a black screen as above (called the console – Read the 'worm'), but no nice interface but this is the core of programming, good learning this section we can do the program with a graphical interface and other languages ​​and most rely on this core.

Exercise: Write a program that prints out your name, your parents or lovers if. Do not forget to show off to friends again.