Download presentation
Presentation is loading. Please wait.
1
The C Programming Language
Hunan Institute of Science and Technology
2
Course Arrangement Class hours : Total : 72 hours 5rd ~ 18th week
12/9/2018 Course Arrangement Class hours : Total : 72 hours 5rd ~ 18th week Teaching : 48 hours 5rd ~ 16th week Programming : 24 hours th ~ 18th week 2
3
Examination of The Course
12/9/2018 Examination of The Course The exam contains 3 parts : Written test : 60% Homework and Programming experiments : 30% Programming test : 10% 3
4
Chapter 1 Overview of C PROGRAMMING IN ANSI C
5
Chapter 1 Overview of C Characteristics History
12/9/2018 Chapter 1 Overview of C Characteristics Structured High-level Machine Independent History UNIX C language use the way closed to the human nature language to express the order statements. These statements are easily to be memorized and be understood. The Compiler translates a C program to the machine codes composed by 0 and 1, and then let computer execute these machine codes. However, programmers don’t need to consider it. The function is the basic unit of C language. A C program performs its task by calling various functions, and it has a good control structure. C program can be executed in different computers, and you need not consider the difference between various computers. C language was developed along with UNIX. In fact, the goal of the birth of C is to program UNIX. Both UNIX and the most of the programs which run on UNIX are written in C. 5
6
A Simple C Program 1 – Printing a Message
12/9/2018 A Simple C Program 1 – Printing a Message The forms of the main statement: main() int main() void main() main (void) void main(void) int main(void) main() { /* printing begins */ printf("I see, I remember!"); /* printing ends */ } 1) Every program must have only one main function comment Every program must have only one main function! 2) The comments cannot be nested! 3) Every statement should end with “;” I see, I remember! I see, I remember! 4) C makes a distinction between uppercase and lowercase letters /*This is a C program /*output*/, */ printf("I see, "); printf("I remember!"); printf("I see, \nI remember!"); printf("I see, \n"); printf("I remember!"); The comments cannot be nested in C !!! 5) newline character: “\n” 6
7
A Simple C Program 2 – Adding Two Numbers
12/9/2018 A Simple C Program 2 – Adding Two Numbers main() { int number; float amount; number = 100; amount = number ; printf("number=%d\n", number); printf("amount=%6.2f", amount); } All variables must be declared before they are used. number amount 100 175.35 number=100 amount=175.35 number=100 _ 7
8
A Simple C Program 3 – Interest Calculation
12/9/2018 A Simple C Program 3 – Interest Calculation #define PRINCIPAL main() { float amount, inrate=0.11; amount = PRINCIPAL; amount = amount + inrate * amount; printf("Amount = $%.2f", amount); } Amount = $ = inrate * ; PRINCIPAL = PRINCIPAL + inrate * PRINCIPAL ; Error … 6: Lvalue reqired in function main #define instruction is a preprocessor compiler directive and not a statement, so it should not end with a “;” 8
9
A Simple C Program 4 – Use of Subfunctions
12/9/2018 A Simple C Program 4 – Use of Subfunctions int mul (int x, int y) { int p; p = x * y; return(p); } main() { int a=5, b=10, c; c = mul (a, b); printf("%d * %d = %d", a, b, c); 5 * 10 = 50 mul(5, 10) 50 9
10
A Simple C Program 5 – Use of Math Functions
12/9/2018 A Simple C Program 5 – Use of Math Functions #include <math.h> #define PI main() { int angle=30; float c; c = sin((PI/180) * angle); printf("sin(%d) = %.2f", angle, c); } sin(30) = 0.50 10
11
Basic Structure of C Programs
12/9/2018 Basic Structure of C Programs Documentation Section Link Section Definition Section Global Declaration Section main() Function Section { Declaration part Executable part } User-defined functions function 1 …… function n 11
12
Programming Style C allows the free-form programming.
12/9/2018 Programming Style C allows the free-form programming. Symbolic constants are written in uppercase letters, and others, such as variables or function names, in lowercases. Each statement is placed in a line by itself. Don’t write one statement in two or more lines. Don’t write two or more statements in one line. Properly write blank line, such as between declarations and executable statements. Use comments properly. Align a pair of braces. Align the statements in the same layer. And indent those statements in lower layer. 12
13
12/9/2018 Executing a C Program Create the source program. (the file extension is “.c”) Compile the source program. It generates the object code file. (the file extension is “.obj”) Link the program with other program files and functions that are required by the program. It generates executable file (the file extension is “.exe”). Execute the executable file. 13
14
Homework Review Questions P19
12/9/2018 Homework Review Questions P19 1.1~1.4 & 1.8~1.10 (Write down in your exercise book) 1.5~1.7 (Think about them, and then check your thought through programming experiments) 1.11~1.20 (Think whether you can understand all of them) Programming Exercises 14
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.