Unit 3. Introduction To Programming in C
Unit 3. Introduction To Programming in C
3
Introduc-on to programming in C
Programming
Grade in Industrial Technology Engineering
2016-2017
Outline
2
Outline
3
1. Introduc:on to the C programming language
History of C
1989
C becomes standard (ISO/IEC 9899-1990)
New languages have been developed from C: Objec:ve C, C++, C#, etc.
4
1. Introduc:on to the C programming language
ANSI C
C Programming Language
6
1. Introduc:on to the C programming language
Compila:on + Linking process
Compiler
Object
Source code code
Linker
Object
code Executable
Object
code
High-level Low-level
Low-level Machine
languages Languages
languages language
7
1. Introduc:on to the C programming language
First C program
Development environments
Dev C/C++ (integrated MinGW 3.4.2 compiler)
hJp://www.bloodshed.net/dev/devcpp.html
(Download)
9
1. Introduc:on to the C programming language
Programming languages
10
1. Introduc:on to the C programming language
C Alphabet
C alphabet
Symbols that can appear in a C program
LeJers
All but and accents (only in comments!)
Numbers
Special characters
C is case sensi:ve: uppercase and lowercase leJers are
dierent
Keywords are wriJen in lowercase
11
1. Introduc:on to the C programming language
C lexicon
Delimiters
Blank spaces, tabs, line breaks
Operators
Represent opera:ons: arithme:c, logic, assignment, etc. (+, -, *, etc.)
Iden-ers
Keywords cannot be used as iden:ers
Variable names (user_age) cannot start with a number
Func:on names (printf, scanf)
Literals
Values that do not change:
Numbers: 2, 3.14159
Strings: "Hello world"
Characters: 'a'
12
1. Introduc:on to the C programming language
C syntax
Data
Values processed by the program
Expressions
Combina:on of operands and operators with a single value as a result
May include func:on calls, even though they do not return a value
user_age >= 18
3.14159*radius*radius
Statements/Instruc-ons/Statements
Complete ac+on
area=3.14159*radius*radius;
printf("Hello world");
int a;
Blocks or compound statements
Group of statements
Braces { }
The statements of the main func:on are enclosed in a block
13
1. Introduc-on to the C programming language
Data
3.14159, radius, area
Expressions
3.14159*radius*radius
Instruc-ons
area = 3.14159*radius*radius;
printf(%f \n, area);
Blocks
14
1. Introduc:on to the C programming language
Example
15
1. Introduc:on to the C programming language
Example
Lesson 4. Control
flow and loops
16
Outline
17
2. Basic program structure
Program elements
File inclusion
Main func-on
Output instruc-on
18
2. Basic program structure
main func:on
return 0;
}
20
2. Basic program structure
File inclusion
22
2. Basic program structure
Comments
23
Outline
24
3. Variables and constants
Storing and using values
25
3. Variables and constants
Storing and using values
26
3. Variables and constants
Program data
Data
Informa:on processed by the program
Read, used in calcula:ons, wriJen
Types of data
Variables
Symbols whose value change during the program execu:on
radius, area
Constants
Symbols whose value do not change during the program
execu:on
PI
27
3. Variables and constants
Characteris:cs of variables and constants
28
3. Variables and constants
Deni:on of variable
0 0 0 0 1 1 0 1 0 26
265
1 2.00 radius
2 0000 256 0 1 0 0 0 0 0 1 A
3 12.5 257 0 1 1 0 1 1 1 0 n
4 6636 area
258 0 1 1 0 0 0 0 1 a
259 0 1 1 0 0 0 0 1 97
29
3. Variables and constants
Variable declara:on
30
3. Variables and constants
Variable declara:on
a = 10;
printf("%i", a);
}
31
3. Variables and constants
Data types (see later)
32
3. Variables and constants
Assignment
compa:ble types
oat <--- int adds .0 to the int
int <--- char assigns the ASCII code of the char to the int
char <--- int if the value of the int is out of range, it is truncated
int <--- oat the decimal part of the oat is truncated
int a=5, b;
char c='Z';
float x, y=3.1;
b=a;
x=a;
b=c;
c=a;
b=y;
34
3. Variables and constants
Ini:aliza:on
a = 8;
Mul:ple declara:on/ini:aliza:on is allowed
int a, b, c;
int a=5, b=4, c=8;
int a=1, b, c=a;
35
3. Variables and constants
Constants
37
3. Variables and constants
Constants
38
3. Variables and constants
#define and const
Constant deni-on
Variable declara-on
Read value
Print value
40
Outline
41
4. Simple data types
Data types
Data can be structured or unstructured
Simple data types
Symbols with a single element and a single value
Numbers: integer numbers, real numbers,
Characters: single leJers
42
4. Simple data types
C simple data types
43
4. Simple data types
int type
44
4. Simple data types
float and double types
float and double data types are used to represent real values
double more precision, but also larger memory size
The decimal separator for literals is .
Scien:c nota:on can be used
Regular: 82.3473
Without leading 0: .34
Scien:c nota:on: 2.4E-4
printf("number: %f \n", 82.3473); // 82.34730
printf("number: %f \n", 2.4E-4); // 0.000240
45
4. Simple data types
char type
printf("%c", letter);
46
4. Simple data types
void type
3. Generic pointers
void *p;
void variables are not allowed
47
4. Simple data types
Character strings
48
Outline
49
5. Expressions and instruc:ons
Deni:on of expression
50
5. Expressions and instruc:ons
Examples of expressions
Examples
a + b
x == y
x <= y
51
5. Expressions and instruc:ons
Operator types
Number of operands
Unary
-: nega:ve number
++: variable increment
--: variable decrement
!: logic nega:on
Binary
Opera-on type
Arithme-c Rela-onal
+ : Addi:on or posi:ve sign == : Equal
- : Subtrac:on or nega:ve sign < : Less than
*: Product <= : Less or equal than
/: Division > : Larger than
%: Module >= : Larger or equal than
!= : Dierent from
Assignment
= : Assign Logical
<op>= : Opera:on and assignment ! : NOT (nega:on)
&, &&: AND (conjunc:on)
|, ||: OR (disjunc:on)
52
5. Expressions and instruc:ons
Deni:on of instruc:on
Instruc-ons or sentences
Orders of the program to accomplish a task
Keywords: short terms interpreted as a command by the computer
Are applied on operators and expressions
Types
According to the func:on
Declara:on
Assignment
Input and output
Control
According to the overall structure of the program
Data process
Input
Output
53
5. Expressions and instruc:ons
Example
Variable declara-on
Read value
Print value
54
Outline
55
6. Operators
Arithme:c operators
Operator Opera-on
+ Addi:on
- Substrac:on
* Mul:plica:on
/ Division
% Remainder or Module
57
6. Operators
Arithme:c operators pow and sqrt
58
6. Operators
Arithme:c operators
Unary operators
++ --
Increase / decrease a variable
They can be used in prex or sux mode:
++x : increment x in 1 and then proceed with the expression evalua:on
x++ : evaluate the expression and then increment x in 1
59
6. Operators
Rela:onal operators
Operator Operation
== Equals
!= Dierent from
60
6. Operators
Logic operators
AND
AND, OR, NOT
Operand values
They are applied on boolean T F
expressions which may be the
T T F
result of rela:onal opera:ons or
other logic opera:ons F F F
Examples: Result of the
expression
To pass the lecture, exam and
exercises must be passed OR
Pass =
Pass_Exer AND Pass_Exam T F NOT
T T T T F
To pass the lecture, at least one
of the parts needs to be passed F T F F T
Pass =
Pass_Exer OR Pass_Exam
61
6. Operators
Logic operators
Opera-on Operator
and &&
or ||
not !
c == 'w' True 1
c == "w" False 0
62
6. Operators
Assignment
Basic assignment
= opera:on for seng the value of a variable
The previous value, if any, is replaced
63
6. Operators
Precedence
64
6. Operators
Category
Unary ! NOT (negacin lgica) !a
++ Increment ++a
-- Decrement --a
- Sign change -b
* Indirec:on *p
& Address &a
Mul-plica-on * Mul:plica:on a*b
/ Division a/b
% Module a%b
Addi-on + Addi:on a+b
- Substrac:on a-b
Rela-onal < Less than a<b
<= Less or equal than a<=b
> Larger than a>b
>= Larger or equal than a>=b
Equality == Equals to a == b
!= Dierent to a != b
Logic && AND a && b
|| OR a || b
Assignment = assignment a = b
Outline
66
Outline
67
8. Basic input and output
I/O func:ons
printf()
Prints informa:on on the standard output device
Usually, on the screen
Syntax
printf("argument format", arguments)
#include <stdio.h>
int main ( ) {
int n=10;
printf ( "%i", n);
return 0;
}
69
8. Basic input and output
printf
Placeholders
%[flags][width][.precision][length]<type>
Flags
+ : prints number sign
space: prexes non-nega:ve values with a blank space
- : lew-aligns the output
# : trailing numbers and decimal values are always printed
0: uses 0 instead of spaces for padding
Width
Minimum number of characters to output (pads if necessary)
Precision
Maximum limit of characters to output (rounds if necessary)
70
8. Basic input and output
printf
%d, %i Integer
%u Integer, unsigned
%x Hexadecimal
%f Float
%lf Double
%s Character string
%p Pointer
71
8. Basic input and output
Special characters
Special characters
\n : Line break
\t : Tabula:on
\b : backspace
Escape characters
\' : to print the ' character
\" : to print the " character
\\ : to print the \ character
72
8. Basic input and output
scanf
scanf()
Reads informa:on from the standard input device
Usually, the keyboard
Syntax
scanf("argument format", &variable)
The & operator means that the variable in the arguments is passed by reference
Pass by reference: the address of the variable is passed; the value is changed in the func:on
More than one variable can be read in the same scanf instruc:on
#include <stdio.h>
/* Global declarations */
Function prototypes
/* Main function */
int main (void)
{
Local variable and constant declaration
Instructions
}
75
Bibliography
Recommended lectures
Basic
Ivor Horton. Beginning C: From Novice to Professional. Apress,
2006 (4th Edi:on) Chapters 1, 2
Stephen G. Kochan. Programming in C. Sams, 2004 (3rd
Edi:on), Programming in C Chapters 3, 4
Addi5onal informa5on
Stephen Prata. C Primer Plus. Sams, 2004 (5th Edi:on)
Chapters 1-4
76