Lecture 1
Lecture 1
Lecture Slides
University of Messina
Computer Science (Data Analysis)
Programming
Lecture 1: Outline
• Introduction
• Course Logistics and Syllabus
– Learning Objectives
– Textbooks
– Labs
– Grading
• Some Fundamentals
• Compiling and running your first C
program
Course organization
• Francesco La Rosa, PhD
• Lessons (classroom A-S-1):
– Monday (16-18),
– Tuesday (14-16),
– Wednesday (14-16)
• LAB Session: SBA-2-1 (II Floor SBA)
• https://moodle2.unime.it/course/view.php?id=49094
– Course info, Lecture slides
• Email: [email protected]
Learning Objectives
•First course in Computer Science
– No previous knowledge is assumed!
•By the end of the course, students will:
– Understand fundamental concepts of
computer programming/imperative structured
programming languages
– Design algorithms to solve (simple) problems
– Use the C programming language
Textbooks
• Al Kelley, Ira Pohl, A Book on C: Programming in C, 4th
Edition, Pearson Education Limited, 1998
– Main (first) textbook for this course
– Teaches you how to program (in C)
– Follows an approach suited for a first programming language
CPU
Input Device ALU CU Output Device
DEFVAR a,b,c;
BEGIN
READ a
READ b
READ c
c := a+b
IF (c <10) THEN c:=c+10
PRINT c
END …
Higher level languages
• High level languages
– Writing portable programs, using more abstract
instructions
– A high level instruction (statement) is translated into
many machine instructions
– Translation of high level language into machine
instructions: done by special computer programs –
compilers or interpreters
Compilers/Interpreters
Source
Source Machine Code
Compiler
Code Code
Interpreter
Input Output
Input Executable Output data data
data Program data
main: a special name that indicates where the program must begin execution. It is
a special function.
first statement: calls a routine named printf, with argument the string of characters
“Programming is fun \n”
last statement: finishes execution of main and returns to the system a status
value of 0 (conventional value for OK)
The format in C
• Statements are terminated with semicolons
• Indentation is nice to be used for increased readability.
• Free format: white spaces and indentation is ignored by
compiler
• C is case sensitive – pay attention to lower and upper
case letters when typing!
– All C keywords and standard functions are lower case
– Typing INT, Int, etc instead of int is a compiler error
• Strings are placed in double quotes
• New line is represented by \n (Escape sequence)
Compiling and running C programs
Editor
Source code
file.c
Compiler
Object code
file.obj
Libraries Linker
Executable code
file.exe
IDE (Integrated
Development
Environment)
C Compilers and IDE’s
• One can:
– use a text editor to edit source code, and then use independent
command-line compilers and linkers
– use an IDE: everything together + facilities to debug, develop
and organize large projects
• There are several C compilers and IDE’s that support
various C compilers
• Lab: Free Software (under the GNU General Public License)
– Works with gcc (GNU C Compiler)
• supports the C99 standard
• available on Windows and Unix
– The GNU Project (http://www.gnu.org/): launched in 1984 in
order to develop a complete Unix-like operating system which is
free software - the GNU system.
Debugging program errors
Syntactic Editor
Source code
Errors file.c
Compiler
Object code
file.obj
Libraries Linker
Executable code
file.exe
Semantic
Errors
Syntax and Semantics
• Syntax errors: violation of programming
language rules (grammar)
– "Me speak English good."
– Use valid C symbols in wrong places
– Detected by the compiler
• Semantics errors (errors in meaning):
– "This sentence is excellent Italian."
– Programs are syntactically correct but don’t produce
the expected output
– User observes output of running program
Second program
#include <stdio.h>
int main (void)
{
printf ("Programming is fun.\n");
printf ("And programming in C is even more fun.\n");
return 0;
}
Displaying multiple lines of text
#include <stdio.h>
int main (void)
{
printf ("Testing...\n..1\n...2\n....3\n");
return 0;
}
Output:
It is not necessary
to make a separate Testing...
call to printf for ..1
each line of output ! ...2
....3
Variables
• Programs can use symbolic names for
storing computation data and results
• Variable: a symbolic name for a memory
location
– programmer doesn’t has to worry about
specifying (or even knowing) the value of the
location’s address
• In C, variables have to be declared before
they are used
Using and Displaying Variables
#include <stdio.h>
int main (void)
{
int sum;
sum = 50 + 25;
printf ("The sum of 50 and 25 is %i\n", sum);
return 0;
}
The printf routine call has now 2 arguments: first argument a string containing also a
format specifier (%i), that holds place for an integer value to be inserted here
Displaying multiple values
#include <stdio.h>
int main (void)
{
int value1, value2, sum;
value1 = 50;
value2 = 25;
sum = value1 + value2;
printf ("The sum of %i and %i is %i\n",value1, value2, sum);
return 0;
}
#include <stdio.h>
int main(void)
{
// Declare variables
int value1, value2, sum;
// Assign values and calculate their sum
value1 = 50;
value2 = 25;
sum = value1 + value2;
// Display the result
printf ("The sum of %i and %i is %i\n",
value1, value2, sum);
return 0;
}