Leture 1
Leture 1
Dr Khaleda Ali
[email protected]
Computer Languages
Machine Language
binary number codes understood by a specific CPU
Not standard
Different machine language for each type of CPU
Assembly Language
mnemonic codes that correspond to machine language instructions
Computer Languages
High-Level Language
machine-independent programming language that combines
algebraic expressions and English symbols
Compiler software
that translates a high-level language program into machine language
Source file
File containing a program written in a high-level language; the input
for a compiler
Syntax
Grammar rules of a programming language
Computer Languages
Object file
file of machine language instructions that is the output of a compiler
Linker
Software that combines object files and resolves cross-references to
create an executable machine language Program
Entering, Translating, and Running a High-Level
Language Program
Flow of Information During Program Execution
What is C?
Executable code
produces
Compiler Welcome.exe Cannot
compile?
Output
produces Incorrect
Execute Hello, welcome
to CSE 1201!
result?
eg: welcome.exe
int main(void) {
float miles, // input – distance in miles
kms; // output – distance in kilometres
return 0;
} punctuations
Our First Program (3/4)
General form of a C program:
preprocessor directives
main function heading
{
declarations
executable statements
}
Preprocessor directives
a C program line begins with # provides an instruction to the C
preprocessor
It is executed before the actual compilation is done.
Two most common directives :
#include
#define
In our example (#include<stdio.h>) identifies the header file for
standard input and output needed by the printf().
The C Preprocessor is not a part of the compiler, but is a separate
step in the compilation process. In simple terms, a C Preprocessor is
just a text substitution tool and it instructs the compiler to do
required pre-processing before the actual compilation.
Function main
Identify the start of the program
Every C program has a main ( )
'main' is a C keyword. We must not use it for any other variable.
4 common ways of main declaration
The line int indicates that the main function returns an integer value ( 0 ) to the
operating system when it finishes normal execution. The symbols ( void ) indicate
that the main function receives no data from the operating system before it begins
execution.
int void main(void) main( )
main(void) main(void) { {
{ {
return 0; } }
} }
Program Structure: Declaration
Declaration Statements
Identifier: name of a variable or function
Reserved words (or keywords)
◻ e.g. int, void, double, return
Standard identifiers
◻ e.g. printf, scanf
User-defined identifiers
◻ Avoid reserved words and standard identifiers
◻ Consist only of letters, digit characters and underscores, and must not begin with
a digit character
◻ Case-sensitive
◻ e.g. invalid: 1Letter, double, int, TWO*FOUR, joe’s
valid: maxEntries, _X1234, this_IS_a_long_name
The curly braces { }
} End of segment
Identifiers
Binary number
Basic Data Types cont…
float
fractional parts, positive and negative
keyword: float
float height;
height = 1.72;
Basic Data Types
Slide 29
Basic Data Types cont…
char
equivalent to ‘letters’ in English language
Example of characters:
Numeric digits: 0 - 9
Lowercase/uppercase letters: a - z and A - Z
Space (blank)
Special characters: , . ; ? “ / ( ) [ ] { } * & % ^ < > etc
single character
keyword: char
char my_letter;
The declared character must be
my_letter = 'U'; enclosed within a single quote!
ASCII Code
int main(void)
{
double height, radius, base, volume;
Input operation
an instruction that copies data from an input device into memory
Output operation
an instruction that displays information stored in memory to the
output devices (such as the monitor screen)
Input/Output Functions
Example:
printf(“Thank you”);
printf (“Total sum is: %d\n”, sum);
%d is a placeholder (conversion specifier)
◻ marks the display position for a type integer variable
\n is an escape sequence
◻ moves the cursor to the new line
Program Structure: Executable
e.g. sum = sum + item;
Another version:
int age;
double cap; // cumulative average point
printf("What are your age and CAP? ");
scanf("%d %lf", &age, &cap);
printf("You are %d years old, and your CAP is %lf\n", age, cap);
Escape Sequence
Escape Sequence Effect
\a Beep sound
\b Backspace
\f Formfeed (for printing)
\n New line
\r Carriage return
\t Tab
\v Vertical tab
\\ Backslash
\” “ sign
\o Octal decimal
\x Hexadecimal
\O NULL
The scanf function
Reserved Words
Keywords that identify language entities such as statements, data
types, language attributes, etc.
Have special meaning to the compiler, cannot be used as identifiers
(variable, function name) in our program.
Should be typed in lowercase.
Example: const, double, int, main, void,printf, while, for, else (etc..)
Few notes on C program cont…
Punctuators (separators)
Symbols used to separate different parts of the C program.
These punctuators include:
[ ] ( ) { } , ; “: * #
Usage example:
int main void()
{
int num = 10;
printf(“%d”, num);
return 0;
}
Few notes on C program cont…
Operators
Tokens that result in some kind of computation or action when
applied to variables or other elements in an expression.
Example of operators:
*+=-/
Usage example:
result = total1 + total2;
Input/Output
%d and %lf are examples of format specifiers; they are placeholders for values to
be displayed or read
Placeholder Variable Type Function Use
%c char printf / scanf
%d int printf / scanf
%f float or double printf
%f float scanf
%lf double scanf
%e float or double printf (for scientific notation)
■ Examples of format specifiers used in printf():
■ %5d: to display an integer in a width of 5, right justified
■ %8.3f: to display a real number (float or double) in a width of 8, with 3
decimal places, right justified
■ Note: For scanf(), just use the format specifier without indicating width, decimal
places, etc.
Input/Output
\n is an example of escape sequence
Escape sequences are used in printf() function for certain special effects or to
display certain characters properly
These are the more commonly used escape sequences:
Run-time errors
An attempt to perform an invalid operation, detected
during program execution.
Occurs when the program directs the computer to
perform an illegal operation, such as dividing a number by
zero.
The computer will stop executing the program, and
displays a diagnostic message indicates the line where the
error was detected
Common Programming Errors cont…