C_programming_Chapter01_SEN2201 (1)
C_programming_Chapter01_SEN2201 (1)
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
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
5
Warning: low-level language!
Inherently unsafe:
• No range checking
• Limited type safety at compile time
• No type checking at runtime
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
• gcc (included with most Linux distributions): compiler
• .o extension
• omitted for common programs like gcc
8
Debugging
10
Using gdb
11
Memory debugging
12
The IDE – all-in-one solution
• Popular IDEs: Eclipse (CDT), Microsoft Visual C++
(Express Edition), KDevelop, Xcode, . . .
• Integrated editor with compiler, debugger
• Very convenient for larger programs
13
Lecture 1
Introduction to C
Writing C Programs
15
Hello, students
15
Structure of a .c file
/* Begin with comments about file contents */
16
Comments
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
20
Initializing variables
21
Arithmetic expressions
22
Order of operations
• Order of operations:
23
Order of operations
24
Order of operations
24
Order of operations
24
Order of operations
24
Order of operations
24
Order of operations
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)
• variable names don’t have to match
• 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 ∗ /
puts(hello, 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
Summary
Topics covered:
• How to edit, compile, and debug C programs
• C programming fundamentals:
• comments
• preprocessor macros, including #include
• the main() function
• declaring and initializing variables, scope
• using puts() – calling a function and passing an argument
• returning from a function
39