Transcript return 0

From Algorithms to Programs
• Both are sets of instructions on how to do a task
• Algorithm:
– talking to humans, easy to understand
– in plain (English) language
• Program:
– talking to computer (compiler)
– can be regarded as a “formal expression” of an
algorithm
High-Level Language
#include <stdio.h>
int main()
{
printf(“Hello World”);
return 0;
}
Source code
10100110 01110110
00100110 00000000
11111010 11111010
01001110 10100110
11100110 10010110
11001110 00101110
10100110 01001110
11111010 01100110
01001110 10000110
etc...
Executable code
• Compilers and linkers translate a high level
program into executable machine code.
Why C?
• Flexible language:
– Structured language
– Low level activities possible
•
•
•
•
Standard library exists, allowing portability
It can produce lean and efficient code
Wide availability on a variety of computers
Widely used
History of C
• Developed in 1972 by Dennis Ritchie on a DEC PDP11 at Bell
Systems Lab as a system development language
– Derived from the language B of Ken Thompson, which itself was based
on BCPL, developed by Martin Richards
• For many years the de-facto C standard was the version provided
with Unix System V
– The C ProgramminLanguage, Brian Kernigham and Dennis Ritchie,
Prentice-Hall 1978
• In 1983 ANSI creates a group to begin the standardization of C
– ANSI C is finalized in 1989 (C89), and ISO adopts it in 1990
– ANSI updated the standard and C99 was adopted in 2000
Basic Structure of a C Program
Example: Hello World
Algorithm:
C Program:
#include <stdio.h>
output “Hello World!”
int main()
{
printf(“Hello World!”);
return 0;
}
Basic Structure of a C Program (cont)
Example: Hello world
C Program:
#include <stdio.h>
Includes declarations for
the standard input/output
library of procedures.
Read: “Hash-include”
int main()
{
printf(“Hello World!”);
return 0;
}
Basic Structure of a C Program
Example: Hello World
C Program:
#include <stdio.h>
Curly braces mark the
beginning and end of a
block of instructions.
int main()
{
printf(“Hello World”);
return 0;
}
Basic Structure of a C Program
Example: Hello World
C Program:
#include <stdio.h>
Instruction (function call)
to output “Hello World”
int main()
{
printf(“Hello World”);
return 0;
}
Basic Structure of a C Program
Example: Hello World
“Statements” (lines of
instructions) always end
C Program:
with a semi-colon (;)
#include <stdio.h>
int main()
{
printf(“Hello World”);
return 0;
}
Example -- Count to 10
Print out numbers 0 to 9
int main()
{
set count to 0
while ( count is less than 10 )
{
output count
add 1 to count
}
return 0;
}
Example -- Count to 10 (cont)
#include <stdio.h>
Print out numbers 0 to 9
int main()
{
set count to 0
while ( count is less than 10 )
{
output count
add 1 to count
}
return 0;
}
Example -- Count to 10 (cont)
#include <stdio.h>
Print out numbers 0 to 9
/* Print out numbers 0 to 9 */
int main()
{
Comment
set count to 0
while ( count is less than 10 )
{
output count
add 1 to count
}
return 0;
}
Example -- Count to 10 (cont)
#include <stdio.h>
Print out numbers 0 to 9
/* Print out numbers 0 to 9 */
int main()
{
int count;
set count to 0
while ( count is less than 10 )
{
output count
add 1 to count
}
Variable
declaration
return 0;
}
Example -- Count to 10 (cont)
#include <stdio.h>
Print out numbers 0 to 9
/* Print out numbers 0 to 9 */
int main()
{
int count;
count = 0;
while ( count < 10 )
{
printf(“%d\n”, count);
count=count+1;
}
return 0;
set count to 0
while ( count is less than 10 )
{
output count
add 1 to count
}
}
Example -- Count to 10 (cont)
#include <stdio.h>
Print out numbers 0 to 9
/* Print out numbers 0 to 9 */
int main()
{
int count;
set count to 0
while ( count is less than 10 )
{
output count
add 1 to count
}
count = 0;
while ( count < 10 )
{
printf(“%d\n”, count);
Assignment
of a value
count=count+1;
}
(right
expression) to a
return (left).
0;
variable
}
Example -- Count to 10 (cont)
#include <stdio.h>
Print out numbers 0 to 9
/* Print out numbers 0 to 9 */
int main()
{
int count;
count = 0;
while ( count < 10 )
{
printf(“%d\n”, count);
count=count+1;
}
No semireturn 0;
set count to 0
while ( count is less than 10 )
{
output count
add 1 to count
}
}
colon here!
Example -- Count to 10 (cont)
#include <stdio.h>
Print out numbers 0 to 9
/* Print out numbers 0 to 9 */
int main()
{
int count;
count = 0;
while ( count < 10 )
{
printf(“%d\n”, count);
count=count+1;
}
return 0;
set count to 0
while ( count is less than 10 )
{
output count
add 1 to count
}
}
Example -- Count to 10 (cont)
#include <stdio.h>
Print out numbers 0 to 9
/* Print out numbers 0 to 9 */
int main()
{
int count;
count = 0;
while ( count < 10 )
{
printf(“%d\n”, count);
count=count+1;
}
return 0;
set count to 0
while ( count is less than 10 )
{
output count
add 1 to count
}
}
Format string
Example -- Count to 10 (cont)
#include <stdio.h>
Print out numbers 0 to 9
/* Print out numbers 0 to 9 */
int main()
{
int count;
count = 0;
while ( count < 10 )
{
printf(“%d\n”, count);
count=count+1;
}
return 0;
set count to 0
while ( count is less than 10 )
{
output count
add 1 to count
}
}
Example -- What’s your sign?
Find the sign of a number
output “Enter a number”
input num
#include <stdio.h>
/* Find the sign of a number */
int main()
{
float num;
printf(“Enter a number: “);
scanf(“%f”, &num);
if (num is less than 0)
then
{
output num “ is -’ve”
}
else
{
output num “ is +’ve”
}
if ( num < 0 )
{
printf(“%f is -’ve\n”, num);
}
else
{
printf(“%f is +’ve\n”, num);
}
return 0;
}
Example -- What’s your sign? (cont)
Find the sign of a number
output “Enter a number”
input num
#include <stdio.h>
/* Find the sign of a number */
int main()
{
float num;
printf(“Enter a number: “);
scanf(“%f”, &num);
if (num is less than 0)
then
{
output num “ is -’ve”
}
else
{
output num “ is +’ve”
}
if ( num < 0 )
{
printf(“%f is -’ve\n”, num);
}
else
{
printf(“%f is +’ve\n”, num);
}
return 0;
}
Example -- What’s your sign? (cont)
Find the sign of a number
output “Enter a number”
input num
#include <stdio.h>
/* Find the sign of a number */
int main()
{
float num;
printf(“Enter a number: “);
scanf(“%f”, &num);
if (num is less than 0)
then
{
output num “ is -’ve”
}
else
{
output num “ is +’ve”
}
if ( number < 0 )
{
printf(“%f is -’ve\n”, num);
}
else
{
printf(“%f is +’ve\n”, num);
}
return 0;
}
Example -- What’s your sign? (cont)
Find the sign of a number
output “Enter a number”
input num
#include <stdio.h>
/* Find the sign of a number */
int main()
{
float num;
printf(“Enter a number: “);
scanf(“%f”, &num);
if (num is less than 0)
then
{
output num “ is -’ve”
}
else
{
output num “ is +’ve”
}
if ( num < 0 )
{
printf(“%f is -’ve\n”, num);
}
else
{
printf(“%f is +’ve\n”, num);
}
return 0;
}
Example -- What’s your sign? (cont)
Find the sign of a number
output “Enter a number”
input num
#include <stdio.h>
/* Find the sign of a number */
int main()
{
float num;
printf(“Enter a number: “);
scanf(“%f”, &num);
if (num is less than 0)
then
{
output num “ is -’ve”
}
else
{
output num “ is +’ve”
}
if ( num < 0 )
{
printf(“%f is -’ve\n”, num);
}
else
{
printf(“%f is +’ve\n”, num);
}
return 0;
}
C Values and Variables
• Basic Types:
– Integers
– Floating point numbers
– Characters
– Character Strings
Basic Types: int and float
• Integers (int)
0
1
1000
-1
-10
• Floating point numbers (float)
1.0
.1
1.0e-1
1e1
666
Basic Types: char
• Characters (char)
’a’ ’z’ ’A’ ’Z’ ’?’ ’@’ ’0’ ’9’
- Special Characters: preceded by \
’\n’ ’\t’ ’\0’ ’\’’ ’\\’ etc.
Basic Types: character string
• Character Strings (a string of char-s)
• Examples:
– ”Hi there!”
– ”Line 1\nLine 2\nLine 3”
– ””
– ”\”\””
Arithmetic Expressions
• take arithmetic (numerical) values and
• return an arithmetic (numerical) value
• Are composed using the following operators:
+
+
*
/
%
(unary plus)
(unary minus)
(addition)
(subtraction)
(multiplication)
(division or quotient)
(modulus or remainder)
Precedence in Expressions
• Defines the order in which an expression is
evaluated
Precedence in Expressions -- Example
1 + 2 * 3 - 4 / 5 =
1 + (2 * 3) - (4 / 5)
B.O.D.M.A.S.
B stands for brackets,
O for Order (exponents),
D for division,
M for multiplication,
A for addition, and
S for subtraction.
More on precedence
• *, /, % are at the same level of precedence
• +, - are at the same level of precedence
• For operators at the same “level”, left-to-right
ordering is applied.
2 + 3 – 1 = (2 + 3) – 1 = 4
2 – 3 + 1 = (2 – 3) + 1 = 0
2 * 3 / 4 = (2 * 3) / 4 = 6 / 4
2 / 3 * 4 = (2 / 3) * 4 = 0 * 4
Precedence in Expressions –
Example (cont)
1 + 2 * 3 - 4 / 5 =
1 + (2 * 3) - (4 / 5)
6.2
Precedence in Expressions –
Example (cont)
1 + 2 * 3 - 4 / 5 =
1 + (2 * 3) - (4 / 5)
6.2
Precedence in Expressions –
Example (cont)
1 + 2 * 3 - 4 / 5 =
1 + (2 * 3) - (4 / 5)
Integer division
results in integer
quotient
Precedence in Expressions –
Example (cont)
1 + 2 * 3 - 4 / 5 =
1 + (2 * 3) - (4 / 5)
= 0
D’oh
Precedence in Expressions –
Example (cont)
1 + 2 * 3 - 4 / 5 =
1 + (2 * 3) - (4 / 5)
7
int-s and float-s
• float is a “communicable” type
• Example:
1 + 2 * 3 - 4.0 / 5
= 1 + (2 * 3) - (4.0 / 5)
= 1 + 6 - 0.8
= 6.2
int-s and float-s – Example 2
(1 + 2) * (3 - 4) / 5
= ((1 + 2) * (3 - 4)) / 5
= (3 * -1) / 5
= -3 / 5
= 0
int-s and float-s – Example 2
(cont)
(1 + 2.0) * (3 - 4) / 5
= ((1 + 2.0) * (3 - 4)) / 5
= (3.0 * -1) / 5
= -3.0 / 5
= -0.6
int-s and float-s – Example 3
(1 + 2.0) * ((3 - 4) / 5)
= (1 + 2.0) * (-1 / 5)
= 3.0 * 0
= 0.0
Unary operators
• Called unary because they require one operand.
• Example
i = +1;
j = -i;
/* + used as a unary operator */
/* - used as a unary operator */
• The unary + operator does nothing – just emphasis
that a numeric constant is positive.
• The unary – operator produces the negative of its
operand.
Increment and decrement
operators
• ++ is the increment operator
i++;
is equivalent to
i = i + 1;
• -- is the decrement operator
j--;
is equivalent to
j = j - 1;
(King, pp53-54)
Example -- Simple Expressions
Evaluate an expression
set result to
1+2*3-4/5
output result
#include <stdio.h>
Example -- Simple Expressions (cont)
Evaluate an expression
#include <stdio.h>
/* Evaluate an expression */
set result to
1+2*3-4/5
output result
Example -- Simple Expressions (cont)
Evaluate an
expression
#include <stdio.h>
/* Evaluate an expression */
int main()
{
set result to
1+2*3-4/5
output result
return 0;
}
Example -- Simple Expressions (cont)
Evaluate an
expression
#include <stdio.h>
/* Evaluate an expression */
int main()
{
float result;
set result to
1+2*3-4/5
output result
return 0;
}
Example -- Simple Expressions (cont)
Evaluate an
expression
#include <stdio.h>
/* Evaluate an expression */
int main()
{
float result;
set result to
1+2*3-4/5
output result
result = 1 + 2 * 3 - 4 / 5;
return 0;
}
Example -- Simple Expressions (cont)
Evaluate an
expression
#include <stdio.h>
/* Evaluate an expression */
int main()
{
float result;
set result to
1+2*3-4/5
output result
result = 1 + 2 * 3 - 4 / 5;
printf(“%f\n”, result);
return 0;
}
Example -- Simple Expressions (cont)
Evaluate an
expression
#include <stdio.h>
/* Evaluate an expression */
int main()
{
float result;
set result to
1+2*3-4/5
output result
Output:
7.000000
result = 1 + 2 * 3 - 4 / 5;
printf(“%f\n”, result);
return 0;
}