Title: Types of Programming Language (1)
1Types of Programming Language (1)
- Three types of programming languages
- Machine languages
- Strings of numbers giving machine specific
instructions - Example
- 1300042774
- 1400593419
- 1200274027
- Assembly languages
- English-like abbreviations representing
elementary computer operations (translated via
assemblers) - Example
- LOAD BASEPAY
- ADD OVERPAY
- STORE GROSSPAY
2Types of Programming Language (2)
- High-level languages
- Codes similar to everyday English
- Use mathematical notations (translated via
compilers) - Example
- grossPay basePay overTimePay
3High Level Languages
- FORTRAN
- Used for scientific and engineering applications
- COBOL
- Used to manipulate large amounts of data still
used in established business-software - Pascal
- Designed for the teaching of structured
programming - C
- Evolved by Ritchie from two previous programming
languages, BCPL and B (early 1970s) - Used to develop UNIX and other modern operating
systems - Standardized in 1989 and 1999 into an
"unambiguous, machine-independent" definition - Hardware independent (portable)
4The C Program Environment
- Edit
- Preprocess
- Compile
- Link
- Load
- Execute
5A First C Program (1)
- Comments
- Text surrounded by / and / is ignored by
computer - Used to describe program
- include ltstdio.hgt
- Preprocessor directive
- Tells computer to load contents of a certain file
- ltstdio.hgt allows standard input/output operations
6A First C Program (2)
- int main()
- The keyword main indicates where the program
starts executing - Parenthesis ( ) always follow the word main
- int means that main "returns" an integer value
(more on this later!) - Braces ( and ) indicate a block of code
- The body of main must be contained in braces
7A First C Program (3)
- printf( "Welcome to C!\n" )
- Instructs computer to perform an action
- Specifically, prints the string of characters
within quotes (" ") - Entire line called a statement
- All statements must end with a semicolon ()
- Escape character (\)
- Indicates that printf should do something out of
the ordinary - \n is the newline character
- return 0
- Indicates the end of the main function
8Compiling and Executing (5)
9An Alternative First C Program
- Produces the same output as the previous program
- Another variation printf(Welcome\nto\nC!\n)
would produce three lines of output, with
newlines inserted after the words welcome and to,
as well as at the end.
10Adding two Numbers (Program Listing)
11Adding two Numbers (New Concepts)
- Lines 13 and 16 read input from the keyboard
- scanf( "d", integer1 )
- scanf( "d", integer2 )
- Lines 8 - 10 declare the programs variables
- int integer1
- int integer2
- int sum
- Line 18 performs a mathematical calculation on
the numbers input from the keyboard - sum integer1 integer2
- Line 20 prints to the screen the result of this
calculation - printf( "Sum is d\n", sum )
12Adding two Numbers (Discussion)
- As before
- Comments, include ltstdio.hgt and main
- int integer1, integer2, sum
- Definition of variables
- Variables locations in memory where a value can
be stored - int means the variables can hold integers (e.g.
-1, 3, 0, 47)
13Adding two Numbers (Discussion)
- Variable names (identifiers)
- integer1, integer2, sum
- Identifiers consist of letters, digits (cannot
begin with a digit) and underscores( _ ) - Case sensitive
- Definitions appear before executable statements
- If an executable statement references and
undeclared variable it will produce a syntax
(compiler) error
14Adding two Numbers (Discussion)
- scanf( "d", integer1 )
- Obtains a value from the user
- scanf uses standard input (usually keyboard)
- This scanf statement has two arguments
- d - indicates data should be a decimal integer
- is confusing in beginning for now, just
remember to include it with the variable name in
scanf statements - When executing the program the user responds to
the scanf statement by typing in a number, then
pressing the enter (return) key
15Adding two Numbers (Discussion)
- (assignment operator)
- Assigns a value to a variable
- Is a binary operator (has two operands)
- sum integer1 integer2
- sum gets the result of integer1 integer2
- Variable receiving value on left
- printf( "Sum is d\n", sum )
- Similar to scanf
- d means decimal integer will be printed
- sum specifies what integer will be printed
- Calculations can be performed inside printf
statements - printf( "Sum is d\n", integer1 integer2 )
- Write down some sample screen output that you
would expect to see when the program is run
16Adding two Numbers (Output)
- This shows an example of what you see when you
run this program
Enter first integer45 Enter second
integer72 Sum is 117