Placeholder Image

字幕列表 影片播放

  • Hi, my name is Alex and this is the first in a series of videos on becoming a better computer programmer.

    我是Alex,這是如何成為更好的程序設計師影片系列的首部曲

  • For this particular video we're going to be focusing on the C programming language,

    在此我們把重心放在C程式語言

  • which is commonly used at universities as a teaching tool and has much in common with most languages used in commercial development.

    這是大學裏很普遍的教學工具,和大多數商用開發語言之間有許多共通點

  • At this point I am assuming zero knowledge of programming and C in general,

    我假設大家沒有任何C語言的基礎

  • so obviously feel free to move to a more advanced video as your needs require.

    如果有需要的話,你可以自行去看其他進階的影片

  • By way of introduction, let me say I've been programming as a hobbyist and a professional for the best part of 13 years now,

    這13年來,我一直是個專業的程式設計愛好者

  • having intially started with Basic before teaching myself C.

    在自修C語言之前,我學的是BASIC語言

  • I now work for the market leader in financial analytics software,

    我現在為財務分析軟體的銷售領導者工作

  • still using the C family of languages on a day-to-day basis.

    每天還是在使用和C相關的語言

  • Whenever you encounter a new programming language you're invariably shown what's called a 'Hello World' program,

    每當你遇到一個新的程式語言,你就會想寫一個可以印出'Hello World'的程式

  • which is generally the simplest meaningful program that can be written in a language.

    這通常是以程式語言寫出來的程式之中,最簡單且算得上有意義的那一個

  • On screen now is the 'Hello World' program in C which we're going to pick apart over the next few minutes.

    現在在螢幕上就是'Hello World'程式,我們將在接下來的幾分鐘好好地把它剖析一遍

  • You can take this program and compile it in any C or C++ compiler,

    你可以用任何C或C++的編譯器來編譯

  • such as Microsoft Visual Studio on Windows, which I'm using here.

    就像是我現在用的,微軟視窗作業系統下的視覺開發工具( Visual Studio)

  • The first thing to note about C is it's a procedural programming language.

    首先要注意的是:C是程序式的程式語言

  • This means the program is arranged as functions while control and execution flows from one function to another.

    程式由好幾個程序組成,這些程序會依照一定的流程執行

  • Your program is read and executed line-by-line by the computer.

    電腦將指令逐行讀出然後執行

  • You may be wondering how the computer knows where to start this execution,

    你可能很想知道,電腦是如何得知從程式的哪個部份開始執行

  • and the answer is that when you compile your program it looks for a function named main, as this one is here.

    答案就在你程式中名字叫作'main'的函式,程式就是從這裏開始執行的

  • It takes arguments, signified by the words in brackets, which are inputs from the command line

    main函式可以從命令列傳入參數,這些參數夾在一對小括號裏

  • and returns an integer - signified by int. Don't worry if you don't understand these terms yet.

    main函式會傳回一個整數(在C語言中,int表示整數型別)。現在你還不必為了不知道這些術語是什麼意思而擔心

  • Note that you can have only one main function in a program or you won't be able to compile it.

    請注意,每一個程式裏只能有一個main函式,否則編譯器是不會放過你的

  • Each function, including the main function, must be included in what's called a block, signified by these curly braces.

    每個函式,包含main函式,都由完整的程式碼區塊所組成,我們會將這個區塊寫在一對大括號裏

  • The code between these outer braces constitutes what's called the scope of the function.

    這一對大括號形成函式的視野範圍

  • This has implications later on when you declare variables

    視野範圍對我們往後宣告變數會有影響

  • which only exist within the scope of a function, or a subsection of a function.

    變數只能存在函式的視野範圍之內、或視野範圍的裏層

  • Again, don't worry about this too much now, and instead remember

    不必太擔心,而是請記得:

  • that you can identify the scope of a block of code with these curly braces.

    成對的大括號設定了程式區塊的視野範圍

  • Functions also end in a return statement. If you're not returning anything specific

    函式由return指令來結束。如果不打算傳回具體的資訊

  • - that is, the function returns type void - you can skip the return statement altogether,

    這表示該函式的回傳型別為void,這時候return指令是可以省略掉的

  • but it will just be implied by the compiler. When learning C,

    如果你把這樣的return指令省略掉,編譯器會自動幫你加上去。在學習C語言的時候

  • you should use the return statement every time just to keep track of what you're doing in your program.

    在須要return指令的地方,你就自己寫上去,這樣你會很清楚程式的每一個步驟

  • In this case, our function returns a 0; returning a zero from a main function of a program signifies

    在這種情況下,main函式傳回一個0。這是我們的程式用來

  • to the operating system that the program has terminated without errors,

    通知作業系統:程式正常結束。

  • whereas any other number would be returned as an error code. These codes enable you to have multiple programs working together,

    其他非0的數值則代表main函式傳回錯誤代碼給作業系統。這些代碼讓數個程式得以協同工作

  • reading and processing each others' output codes.

    接收和處理彼此所傳回的代碼

  • This is relatively common practice in industry.

    這是業界常見的作法

  • The last part of this program, and the most important part, is the printf() statement.

    printf()指令是程式的最後、也是最重要的一部份

  • Printf is another function which is defined by the C language. It displays a message to the standard output device.

    printf是由C語言所定義的函式,我們用printf將訊息顯示在標準輸出設備上(標準輸出設備指的是螢幕或命令提示列視窗)

  • Thus if you run this program in a command line, it will print to that.

    如果這個程式在命令提示列下執行,相關的訊息就會顯示在命令提示列視窗裏

  • There's a couple of key things to note about the way printf is used.

    在使用printf函式時,有幾件事要特別注意

  • Firstly, we have to tell the compiler where to find Printf. This is done by the include statement at the top of the program

    首先,我們得讓程式開頭部份的include指令負責告訴編譯器:printf函式在那裏

  • stdio (standard Input/output) is part of the standard C library

    stdio(標準輸入/輸出)是C語言的標準函式庫的一部份

  • and contains the necessary information about this function.

    在其中包含了printf的資訊

  • Secondly, the section included in the brackets after printf is what's known as an argument.

    緊接在printf之後,那一對小括號裏的一小段文字就是參數

  • Functions can take any number of arguments, or no arguments like our main function, signified by the word void.

    函式可以完全不接收、或者接收任意數量的參數

  • For the printf function, the argument is, as you've probably guessed,

    你大概猜到了,printf函式的參數

  • what you want printed to the screen. Make sure when you pass a string of characters like this

    就是那些你想要印到螢幕上的字串。當你想要傳遞字串時

  • to use double quotation marks, not single, for reasons we'll cover in the next video.

    請用雙引號,而不是單引號。下一部的影片會解釋為什麼

  • The backslash-n is what's known as a special 'escape character' and tells the printf function

    倒斜線n就是所謂的「脫逸字元」,它告訴printf函式

  • we want a new line after this point.

    我們想要印出一個跳行符號到標準輸出設備上

  • As a quick note, this system("PAUSE") statement simply tells the computer to wait for the user

    這裏有個小提醒,指令system("PAUSE")要電腦等停下來等待使用者

  • to press a key before continuing in the program. I've inserted it here so that when

    在程式繼續執行下前必須按下任意鍵。我將這條指令放在這裏的目的是

  • when we run our program, the output window will stay open until we hit a key,

    當程式執行時,用來顯示執行結果的視窗會一直留在螢幕上,直到我們按下任意鍵才消失

  • allowing us to see what our program is doing.

    如此一來我們就可以從容觀察我們的程式到底做了些什麼

  • Finally, notice that the end of this line, and the end of the return statement, have a semicolon.

    最後要注意的,這一行結尾的地方,在return指令結束的地方要有一個分號

  • Forgetting this is a key mistake you'll find happens a lot when learning C,

    忘了在指令的結尾加上分號,這是在學習C語言時常犯的致命錯誤

  • and it causes problems because the compiler won't know where your lines end.

    因為編譯器不知道你的指令到底在哪裏結束,這是導致你的程式無法被編譯的原因之一

  • If you find a program isn't compiling, always double check you put semicolons at the end of all your lines,

    如果你的程式無法正確被編譯,請確認你是否在每條指令的結尾加上分號

  • notwithstanding a few exceptions we'll cover later.

    之後我們會交待少數的例外

  • And that's all there is to it. We can now compile and run this program, and see the output here.

    就是這麼簡單。我們現在編譯並執行這個程式,看看得到了什麼結果

  • For now, try creating this program yourself, and try putting different message in the program

    現在請試著編寫自己的程式,嘗試在程式裏使用和'Hello world'不同的訊息

  • and see how the output changes. Of course, feel free to post any questions afterwards.

    看看程式輸出的結果有什麼變化。如果有任何問題的話,請貼文發問

  • Thank you.

    謝謝大家

Hi, my name is Alex and this is the first in a series of videos on becoming a better computer programmer.

我是Alex,這是如何成為更好的程序設計師影片系列的首部曲

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋