Вы находитесь на странице: 1из 27

Fundamentals

of

High-Level
Programming
Instructor: Miss A. Mark
Introduction
Programming

Programming can be considered as the
production effort in coming up with a set of
instructions that describe how a problem can
be solved

The definition of a particular language
consists of both syntax (how the various
symbols of the language may be combined)
and semantics (the meaning of the language
constructs)

There are many languages in which these
instructions can be written. E.g. "C" , C++,
C#, Pascal, Oracle PL/SQL, Java, Perl, Basic
etc. 2
Procedural Programming Languages

Verbal oriented languages
– Decomposition around verbs(Operations)
– Dividing operations in the series of smaller
operations

Example of these languages are:
– C, Pascal, Basic etc.

Procedural programming is fine for small projects

3
Difference between human and Computers
– Humans have judgement & free will
– They will not follow any instruction they
determine not required or nonsensical
– Computers will do EXACTLY what is told with
no judgement or sanity checks

4
Some Programming Terminologies

Source code
– The stuff you type into the computer. The
program you write.

Compile (build)
– Making a program that the computer can
understand from the source code

Executable
– The compiled program that the computer
can run

Compiler
– Translates the entire program into machine
code before running the program

5
Introduction to C Programming
• C is highly portable – C programs written on one
computer can run also on other computers
without making any changes in the program.
• C is the basis for many other languages (Java,
C++, awk, Perl etc.)
• C is one of the easiest languages to learn.
• Creating a C Programme, you will need at least:
– Computer with an OS (e.g.Windows, Linux)
– Text editor (emacs, vi, NetBeans IDE etc)
– Compiler (gcc)

6
Getting Started with C
Let's look at a working C program below:

#include <stdio.h>
/*My first C program that prints My First
Programme */
int main( int argc, char** argv)
{
printf( “My First Programme.\n" );
getchar();
return 0;
}

7
Getting Started with C
• Let's look at the elements of the program
– Every full C program begins inside a function
called "main()". A function is simply a
collection of commands that do "something".
The main function is always called when the
program first executes.
– The #include is a "preprocessor" directive that
tells the compiler to put code from the header
called stdio.h into our program before actually
creating the executable. By including header
files, you can gain access to many different
functions—both the printf and getchar functions
are included in stdio.h.
8
Getting Started with C

The "curly braces" ({ and }) signal the
beginning and end of functions and other code
blocks. If you have programmed in Pascal, you
will know them as BEGIN and END.


The printf function is the standard C way of
displaying output on the screen. The '\n'
sequence is actually treated as a single
character that stands for a new line, the actual
effect of '\n' is to move the cursor on your
screen to the next line.

9
Getting Started with C

The next command is getchar(). This is another
function call: it reads in a single character and
waits for the user to hit enter before reading the
character. This line is included because many
compiler environments will open a new console
window, run the program, and then close the
window before you can see the output. This
command keeps that window from closing
because the program is not done yet because it
waits for you to hit enter. Including that line gives
you time to see the program run.

10
Getting Started with C

At the end of the program, we return a value
(return 0 ) from main to the operating system
by using the return statement. This return
value is important as it can be used to tell the
operating system whether our program
succeeded or not. A return value of 0 means
success.

11
Getting Started with C

Comments are important in a program and are
used to explain sections of code. When you tell
the compiler a section of text is a comment, it will
ignore it when running the code, allowing you to
use any text you want to describe the real code.
To create a comment in C, you surround the text
with /* and then */ to block off everything
between as a comment OR // to comment a
single line.

12
Variables
Variables Declaration
• It is permissible to declare multiple variables
of the same type on the same line; each one
should be separated by a comma operator. If
you attempt to use an operator undefined
variable, your program will not run, and you
will receive an error message informing you
that you have made a mistake.

13
Variables
Variables Names
• All variable definitions must include two
things: variable name and its data type.
• It must start with an alphabet and can contain
letter, underscores and digits. Both uppercase
and lowercase letters can be used.
• Certain keywords like int, float, struct, if, while
cannot be used as variable names and the
variable names should not be very long.

14
Variables
Variables Names

Primary Data types in C
– int – Integer
– char – Character
– float - A floating point number
– double - double precision floating
point number

15
Variables
Variables Names

The main difference among these data
types is the amount of memory allocated
for storing these. The maximum and
minimum values that can be stored in
these variables depends on the version of
C and the computer system being used.

Try to find out some of the typical values for


these data types.

16
Variables
Declaration format

Format for declaring a variable in C is :
var_type variable_name;

E.g.
– int counter;
– float total, amount, interest;
– char answer;

There is no Boolean type in C, you should use


int, unsigned char or char type

17
Variables
Declaration format
• Type int

int is an integer data type, a variable declared
of this type can have any value of whole
numbers.

#include <stdio.h>
void main()
{
int i = 20;
printf("This is my integer: %d \n", i);
}

The %d is used to tell printf() routine to
format the integer i as a decimal number
18
Variables
Declaration format
• Type float

Floating point variables can store a value containing
a decimal point. Floating point numbers may contain
both a whole and fractional part .
#include <stdio.h>
void main() {
float f = 3.1415;
printf("This is my float: %f \n", f);
}

The %f is to tell printf() routine to format the float
variable f as a decimal floating number. Now if you
want to see only the first 2 numbers after the floating
point, you would modify your printf() routine call to
be like this %.2f. 19
Variables
Declaration format
• Type double

The type double is similar to float except that it is
used whenever the accuracy of a floating point
variable is insufficient.
double var_name;
double x;

Variables declared to be of type double can store
approximately twice as many significant digits as a
variable of type float. You can use similar method
as in the previous example to print a double.

20
Variables
Declaration format
• Type char

Character variables are used to store character
values particularly single characters.

Note: The keyword void is used to denote nothing. It
is more useful when declaring functions which do
not return a value.
#include <stdio.h>
void main() {
char c;
c = 'd';
printf("This is my character: %c \n", c);
}

The %c is to tell printf() routine to format the variable
as a character.
21
Variables
Constants Declaration

The term constant means that it does not change
during the execution of a program. This can be
any literal number, single character or string of
characters

Unintended modifications of constants are
prevented from occurring, the compiler will catch
attempts to reassign new values to constants.

22
Variables
Constants Declaration

There are three ways for defining a constant in C.
– First method is by using #define statements.
#define statement is a preprocessor
directive . It is used to define constants as
follows :
#define TRUE 1
#define FALSE 0
#define PI 3.1415

Wherever the constant appears in a program, the
preprocessor replaces it by its value

23
Variables
Constants Declaration

The second method is by using the const keyword
in conjunction with a variable declaration.

const float PI = 3.1415;


Here, the compiler will throw an error whenever the
variable PI declared as a constant is assigned a
new value.

24
Variables
Constants Declaration

The third method is by using enumerated data type,
enum followed by a list of values enclosed in curly
braces. This defines a set of constants.

E.g. enum answer {FALSE=0, TRUE=1};

25
Variables
Reading input of Variable

Using variables in C for input or output can be a bit
of a hassle at first, but bear with it and it will make
sense. We'll be using the scanf function to read in a
value and then printf to read it back out. Let's look
at the program and then pick apart exactly what's
going on. You can even compile this and run it if it
helps you follow along.

26
Variables
Reading input of Variable

Example
#include <stdio.h>
int main()
{
int a_number;
printf( "Please enter a number: " );
scanf( "%d", &a_number );
printf( "You entered %d", a_number );
getchar();
return 0;
}

"%d" -- this tells the scanf function to read in an
integer while & – the string tells scanf what variables
to look for. Above it looks for integer i.e.&a_number
27