Introduction
Lecturer: Asst. Prof. Dr. Tansu FİLİK• Office: EEM-214• Office Hour: Wednesday 14:00 – 15:00Grading:Mid 1 15 %Mid 2 15 %Lab. 40 % Final 30 %
Laboratory: (there are 4 groups)Lab. office hours will be announced later.
Introduction to Programming Languages
Introduction: Suggested Material
• INTERNET• Problem Solving and Program Design in C
Jeri R. HanlyElliot B. Koffman
• C How to Program ( Deitel )• Additional Material
Complete Reference: C (Herbert Schildt)Also available in Turkish (Alfa Yayınevi)
Introduction to Programming Languages
Programs
• Computer programs are instructions to the computer.
• Computers do not understand human languages, so you need to use computer languages to communicate with them.
• Programs are written using programming languages.
Introduction to Programming Languages
Programming Languages
Introduction to Programming Languages
High-Level Language
Assembly Language
Machine Language
Machine Language is a set of primitive instructions built into every computer. The instructions are in the form of binary code, so you have to enter binary codes for various instructions.
Ex: 1101101010011010
Programming Languages
Assembly languages were developed to make programming easy. Since the computer cannot understand assembly language, however, a program called assembler is used to convert assembly language programs into machine code.
For example, to add two numbers, you might write an instruction in assembly code like this:
ADD A, B
Introduction to Programming Languages
…ADD A, B…
Assembly Source File
Assembler
…1101101010011010
…
Machine Code File
Programming Languages
The high-level languages are English-like and easy to learn and program.
For example, the following is a high-level language statement that computes the area of a circle with radius 5:
area = 5 * 5 * 3.1415;
Introduction to Programming Languages
Compiling Source Code
A program written in a high-level language is called a source program.
Program called a compiler is used to translate the source program into a machine language program called an object program.
The object program is often then linked with other supporting library code before the object can be executed on the machine.
Introduction to Programming Languages
CompilerSource File Object File Linker Executable File
The Software Development (in C)
From Algorithm to Program
Introduction to Programming Languages
Problem Algorithm: Commands
to finish a task.
C Program
Components of an Algorithm
• Variables and values
• Commands
• List
• Procedures (functions)
• Conditions
• Loops
• Documentation
Introduction to Programming Languages
Process of Software Development
1. Clearly identify the problem.
2. Analyze the problem completely and in detail.
3. Carefully design the algorithm considering every possible situation.
4. Code the algorithm without skipping any algorithmic detail. (The C Program)
5. Test your program.
6. Make documentation. This does not have to be the last stage.
7. ….
Introduction to Programming Languages
Syntax rules
• Computer languages have rules, just like the natural languages.
• Starting from a command or a variable, the whole program must be written in accordance with the syntax rules.
• Just like English or Turkish.
Introduction to Programming Languages
List of programming languages
The aim of this list of programming languages is to include all notable programming languages in existence, both those in current use and historical ones, in alphabetical order, except for dialects of BASIC and esoteric programming languages
Bil200 – Computer Programming
Why C?
• Structural.
• Has standard and portable library.
• C compilers are available for a wide range of platforms: Windows, UNIX, super-computers, military computers, embedded systems.
• Low level tasks can be realized easily.
• The most common known language in use.
Bil200 – Computer Programming
History of C
• C is evolved in the following way:– CPL Combined Programming Language (Barron, 1963)
– BCPL Basic CPL (Richards, 1969)
– B (Thompson, 1970)
– C K&R C (Ritchie, 1972)
– ANSI C American National Standards Institute C (X3J11, 1989)
Bil200 – Computer Programming
History of C
• Developed by Dennis Ritchie in 1972 at AT & T Bell Laboratories for system programming.
• In 1973, Ritchie and Kernighan wrote the famous UNIX operating system using C.
– Richie writes the book: “The C Reference Manual”.
– In 1977, Richie and Thompson, publishes the classic book "The C Programming Language“.
– American National Standards Institute standardizes the (ANSI) C language.
Bil200 – Computer Programming
History of C
• In 1989, ANSI C is known as “X3.159-1089” .
• In 1990, International Standards Institute (ISO) establishes ISO C (ISO/IEC 9899:1990 standard. This is identical to the ANSI standard.
• Same source code should be used in every compiler: Visual Studio or Borland Turbo C.
Bil200 – Computer Programming
What is C used for?
Systems programming:
• OSes, like Linux
• microcontrollers: automobiles and airplanes
• embedded processors: phones, portable electronics, etc.
• DSP processors: digital audio and TV systems
• . .
Bil200 – Computer Programming
C Compilers
Compilers
• Windows: · Microsoft Visual C++ .NET with Integrated Development Environment (IDE)
• · GNU C++ compiler (g++) as part of Cygwin or MinGW, without IDE
• · Dev-C++ – free compiler/IDE that is GNU compatible
• Linux: · GNU C++ compiler (g++) – part of any distribution
• · Intel C++ compiler (icc) – free for students
• Mac: · Xcode – free compiler/IDE that is GNU compatible
• Windows/Linux/Mac: · Code::Blocks – free compiler/IDE that is GNU compatible
• · NetBeans – free compiler/IDE that is GNU compatible
Bil200 – Computer Programming
Our Environment
• We will exercise on Microsoft Visual C++ v.6 (IDE)
• because that is what we have in the univ.
– See lab instructions.
• but there are many free compilers, too.
http://www.thefreecountry.com/compilers/cpp.shtml
Bil200 – Computer Programming
C Programming Language
C keywords
(may not be used as variables or commands)
Bil200 – Computer Programming
Keywords
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
C Programming Language
#include <stdio.h>
int my_function(int a, int b)
{
int c;
c=a*b;
return c;
}
void main(void)
{
int p,q,r;
scanf(“%d %d”,&p,&q);
r=my_function(p,q);
printf(“%d\n”,r);
}
Bil200 – Computer Programming
header (pre-processor)
function
Main (starting) function
C Programming Language: Example
#include <stdio.h>
/* Write numbers between 0-9 */
void main(void)
{
int num;
num = 0;
while ( num < 10 )
{
printf("%d\n", num)
}
}
Bil200 – Computer Programming
Formatted printing is done by printf(). It is defined inside stdio.h
num 0
while(num less than 10)
{
print num
increment num
}
Write explanatory comments in your program
Variable declaration: must defined before using
printf (write info on screen)
int num;
num = 0;
while ( num < 10 )
{
printf("%d\n", num)
}
Bil200 – Computer Programming
%i, %d Integer (int)
%c Character (char)
%s String
scanf (read info from keyboard)
scanf ("%d", &num);
Bil200 – Computer Programming
%i, %d Integer (int)
%c Character (char)
%s String
Headers
The pre-processor appends the included headers
to the source file. :
stdio.hmath.h
string.h
Globaldeclarations
Functions
Main
Structure of a C program
#include <stdio.h>
void main()
{
printf(“Hello World!”);
}
C Program:
Load the standard library
to manage console
input/output interface: #include <library>
Structure of a C program
#include <stdio.h>
void main()
{
printf(“Hello World!”);
}
C Program:
Curly parantheses {} determines the
beginning and end
of a command block.
Structure of a C program
#include <stdio.h>
void main()
{
printf(“Hello World!”);
}
C Program:
Command line
(function call)
prints
“Hello World!"
on the screen
Structure of a C program
#include <stdio.h>
void main()
{
printf(“Hello World!”);
}
C Program:
Each command line
is terminated with a
semicolon “ ; ”.
Structure of a C program
#include <stdio.h>
void main()
{
printf(“Hello World!”);
}
C Program:Each program has a main() to run as
the basic starter.
Structure of a C program
#include <stdio.h>
int main()
{
printf(“Hello World!”);
return 0;
}
C Program:Each program has a main() to run as
the basic starter.
main() function
“returns” a value
with “type” int.
num 0
Example - 1
Write numbers
starting from
0 up to 9
#include <stdio.h>
void main()
{
}
Problem C Program
Each C program has a main(). It starts with
{ and ends with }.
Algorithm
print num
increment num
while(num less than 10)
{
}
Example - 1#include <stdio.h>
/* Write numbers between 0-9
*/
void main()
{
}
Write explanatory
comments in your
program.
Example - 1#include <stdio.h>
/* Write numbers between 0-9
*/
void main()
{
int num;
}
Variable declaration:
They must be defined
before using them.
Example - 1#include <stdio.h>
/* Write numbers between 0-9
*/
void main()
{
int num;
num = 0;
}
Assignment of a value to
the variable. The left
side is the variable, right
side is the value.
Example - 1#include <stdio.h>
/* Write numbers between 0-9
*/
void main()
{
int num;
num = 0;
while ( num < 10)
{
}
}
The command does
not end here, so
don’t put a ; here.
Example - 1#include <stdio.h>
/* Write numbers between 0-9
*/
void main()
{
int num;
num = 0;
while ( num < 10)
{
printf("%d\n",num);
}
}
Formatted printing is done by printf(). It is
defined inside stdio.h.
Format specifier. "%d"
means write an integer
number. “\n" means skip to
the next line.
printf (write info on screen)
int num;
num = 0;
while ( num < 10)
{
printf("%d\n",num);
}
%i, %d Integer (int)
%c Character (char)
%s String
Example - 1#include <stdio.h>
/* Write numbers between 0-9
*/
void main()
{
int num;
num = 0;
while ( num < 10)
{
printf("%d\n",num);
}
}
Formatted printing is done by printf(). It is
defined inside stdio.h.
Format specifier. "%d"
means write an integer
number. “\n" means skip to
the next line.
Example – 1: Final#include <stdio.h>
/* Write numbers between 0-9
*/
void main()
{
int num;
num = 0;
while ( num < 10)
{
printf("%d\n",num);
}
}
num = num + 1;
Example – 2Problem
determine sign of number
#include <stdio.h>
/* sayının işaretini bulur */
void main()
{
}
Comment explains
purpose of the
program.
Program scope.
Algorithm
Print "Bir sayı gir"
Read sayı
if (sayı less than 0)
then
{
Print sayı " negative"
}
else
{
Print sayı " pozitive"
}
Example - 2#include <stdio.h>
/* sayının işaretini bulur */
void main()
{
int sayi;
}
Variables. Be careful
not to use Turkish
specific characters such
as ı, ö, ç, ğ, etc.
Problem
determine sign of number
Algorithm
Print "Bir sayı gir"
Read sayı
if (sayı less than 0)
then
{
Print sayı " negative"
}
else
{
Print sayı " pozitive"
}
Example - 2#include <stdio.h>
/* sayının işaretini bulur */
void main()
{
int sayi;
printf("Bir Sayı Giriniz");
scanf("%d", &sayi);
}
Use scanf() to enter
inputs from the
keyboard. Put & before
the numerical variable.
Problem
determine sign of number
Algorithm
Print "Bir sayı gir"
Read sayı
if (sayı less than 0)
then
{
Print sayı " negative"
}
else
{
Print sayı " pozitive"
}
scanf (read info from keyboard)
scanf("%d", &sayi);
%i, %d Integer (int)
%c Character (char)
%s String
Example - 2#include <stdio.h>
/* sayının işaretini bulur */
void main()
{
int sayi;
printf("Bir Sayı Giriniz");
scanf("%d", &sayi);
}
Use scanf() to enter
inputs from the
keyboard. Put & before
the numerical variable.
Problem
determine sign of number
Algorithm
Print "Bir sayı gir"
Read sayı
if (sayı less than 0)
then
{
Print sayı " negative"
}
else
{
Print sayı " pozitive"
}
Example – 2 : Final Situation#include <stdio.h>
/* sayının işaretini bulur */
void main()
{
int sayi;
printf("Bir Sayı Giriniz");
scanf("%d", &sayi);
if ( sayi > 0 )
{
printf("%d pozitif\n",sayi);
}
else
{
printf("%d negatif\n",sayi);
}
}
Problem
determine sign of number
Algorithm
Print "Bir sayı gir"
Read sayı
if (sayı less than 0)
then
{
Print sayı " negative"
}
else
{
Print sayı " pozitive"
}
Variables and Values
• Basic data types:
– Integer numbers
– Floating point type numbers
– Characters (letters)
– Character arrays (strings)
Bil200 – Computer Programming
Variables and Values
int and float
• Integers (int)
0 1 1000 -1 -10 666
• Floating point (real) numbers
1.0 .1 1.0e-1 -1e1
Bil200 – Computer Programming