27
High-Level Programming Fundamentals of Instructor: Miss A. Mark

1292411593C Lect 1_Intro

Embed Size (px)

Citation preview

Page 1: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 1/27

High-LevelProgramming

Fundamentals

of 

Instructor: Miss A. Mark

Page 2: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 2/27

Page 3: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 3/27

3

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

Page 4: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 4/27

4

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

Page 5: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 5/27

5

Some Programming Terminologies

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

 program you write. Compile (build)

 –  Making a program that the computer canunderstand 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

Page 6: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 6/27

6

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)

Page 7: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 7/27

7

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;}

Page 8: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 8/27

8

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.

Page 9: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 9/27

9

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.

Page 10: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 10/27

10

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 thewindow 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 givesyou time to see the program run.

Page 11: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 11/27

11

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 programsucceeded or not. A return value of 0 means

success.

Page 12: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 12/27

12

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.

Page 13: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 13/27

13

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 undefinedvariable, your program will not run, and you

will receive an error message informing you

that you have made a mistake.

Page 14: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 14/27

14

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 uppercaseand 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.

Page 15: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 15/27

15

Variables

Primary Data types in C – int – Integer  – char – Character 

 – float - A floating point number  – double - double precision floating

point number 

Variables Names

Page 16: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 16/27

16

Variables

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 inthese 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.

Variables Names

Page 17: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 17/27

17

Variables

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

Declaration format

Page 18: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 18/27

18

Variables

• 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 

Declaration format

Page 19: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 19/27

19

Variables

• 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.

Declaration format

Page 20: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 20/27

20

Variables

• 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 methodas in the previous example to print a double.

Declaration format

Page 21: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 21/27

21

Variables

• 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.

Declaration format

Page 22: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 22/27

22

Variables

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.

Constants Declaration

Page 23: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 23/27

23

Variables

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

Constants Declaration

Page 24: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 24/27

24

Variables

The second method is by using the const keywordin 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.

Constants Declaration

Page 25: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 25/27

25

Variables

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};

Constants Declaration

Page 26: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 26/27

26

Variables

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'sgoing on. You can even compile this and run it if it

helps you follow along.

Reading input of Variable

Page 27: 1292411593C Lect 1_Intro

8/7/2019 1292411593C Lect 1_Intro

http://slidepdf.com/reader/full/1292411593c-lect-1intro 27/27

27

Variables

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 

Reading input of Variable