Professional Documents
Culture Documents
MIT6 087IAP10 Lec01 PDF
MIT6 087IAP10 Lec01 PDF
Introduction to C
Writing C Programs
1
What is C?
1
Features of C
C features:
• Few keywords
• Structures, unions – compound data types
• Pointers – memory, arrays
• External standard library – I/O, other facilities
• Compiles to native code
• Macro preprocessor
2
Versions of C
• mostly backward-compatible
• not completely implemented in many compilers
• 2007 – work on new C standard C1X announced
In this course: ANSI/ISO C (C89/C90)
3
What is C used for?
Systems programming:
• OSes, like Linux
• microcontrollers: automobiles and airplanes
• embedded processors: phones, portable electronics, etc.
• DSP processors: digital audio and TV systems
• . . .
4
C vs. related languages
• object-oriented programming
• polymorphism
• . . .
5
Warning: low-level language!
Inherently unsafe:
• No range checking
• Limited type safety at compile time
• No type checking at runtime
1 Athena is MIT's UNIX-based computing environment. OCW does not provide access to it.
6
6.087 Lecture 1 – January 11, 2010
Introduction to C
Writing C Programs
7
Editing C code
• .c extension
• Editable directly
• More later. . .
7
Compiling a program
8
More about gcc
• Run gcc:
athena%1 gcc -Wall infilename.c -o
outfilename.o
• optimization, linking
1 Athena is MIT's UNIX-based computing environment. OCW does not provide access to it.
9
Debugging
10
Using gdb
• c – continue execution
11
Memory debugging
12
The IDE – all-in-one solution
13
Using Eclipse
14
6.087 Lecture 1 – January 11, 2010
Introduction to C
Writing C Programs
15
Hello, 6.087 students
15
Structure of a .c file
16
Comments
spans
m u l t i p l e l i n e s ∗ /
/ ∗ h e l l o . c −− our f i r s t C program
17
The #include macro
/ ∗ h e l l o . c −− our f i r s t C program
18
More about header files
19
Declaring variables
float phi;
20
Initializing variables
21
Arithmetic expressions
y = x+3∗x/(y−4);
22
Order of operations
• Order of operations:
23
Order of operations
24
Order of operations
24
Order of operations
24
Order of operations
Now, z = 5.0.
24
Order of operations
Now, z = 5.0.
24
Order of operations
Now, z = 5.0.
24
Function prototypes
25
Function prototypes
• General form:
return_type function_name(arg1,arg2,...);
• Arguments: local variables, values passed from caller
• Return value: single value returned to caller when function
exits
• void – signifies no return value/arguments
int rand(void);
26
The main() function
27
Function definitions
Function declaration
{
declare variables;
program statements;
}
• Must match prototype (if there is one)
• no semicolon at end
• Curly braces define a block – region of code
• Variables declared in a block exist only in that block
• Variable declarations before any other statements
28
Our main() function
/ ∗ The main ( ) f u n c t i o n ∗ /
i n t main ( void ) / ∗ e n t r y p o i n t ∗ /
{
/ ∗ w r i t e message t o console ∗ /
p u t s ( "hello, 6.087 students" ) ;
r e t u r n 0 ; / ∗ e x i t ( 0 => success ) ∗ /
}
29
Alternative main() function
/ ∗ w r i t e message t o console ∗ /
p u t s ( msg ) ;
30
More about strings
31
Console I/O
32
Preprocessor macros
33
Defining expression macros
34
Conditional preprocessor macros
35
Conditional preprocessor macros
• #pragma
preprocessor directive
• #error, #warning
trigger a custom compiler error/warning
• #undef msg
remove the definition of msg at compile time
36
Compiling our code
1 Athena is MIT's UNIX-based computing environment. OCW does not provide access to it.
37
Running our code
Or, in gdb,
1
athena% gdb hello.o
.
.
Reading symbols from hello.o...done.
(gdb) run
Starting program: hello.o
hello, 6.087 students
(gdb) quit
athena%
1
Athena is MIT's UNIX-based computing environment. OCW does not provide access to it.
38
Summary
Topics covered:
• How to edit, compile, and debug C programs
• C programming fundamentals:
• comments
39
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use,visit: http://ocw.mit.edu/terms.