programming experience let’s get a feel for everyones’ programming experience. query languages...

66
Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge of conditional statements Query knowledge of subroutines S1- 1 A.Honey (Oct 2003)

Upload: harvey-armstrong

Post on 27-Dec-2015

238 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Programming experience

Let’s get a feel for everyones’ programming experience.Query languages known

Query knowledge of looping constructs

Query knowledge of conditional statements

Query knowledge of subroutines

S1-1A.Honey (Oct 2003)

Page 2: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Program categorization

There are several ways to categorize programming languages, however the one I wish to discuss is relevant to scripting, interpreting, and compiling.

Can anyone explain the differences and give examples?

S1-2A.Honey (Oct 2003)

Page 3: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Scripted language

A scripting language is used to coordinate and combine components that are written in some other language and cannot usually itself be used for writing those components. The programs you create are read by another program which then executes the statements you created. (Perl, Tcl/Tk, HTML, XML)

S1-3A.Honey (Oct 2003)

Page 4: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Scripted language

A scripting language is used to coordinate and combine components that are written in some other language and cannot usually itself be used for writing those components. The programs you create are read by another program which then executes the statements you created. (Perl, Tcl/Tk, HTML, XML)

Interpreted language

An interpreter is a computer program that executes other programs. Most interpreted languages use an intermediate representation, typically called bytecode, which is then executed by a bytecode interpreter. The interpreter would most likely be written in a compiled language. There are more-or-less two programs involved, one to convert to bytecode and one to run that bytecode. (Basic, Java, Python)

S1-4A.Honey (Oct 2003)

Page 5: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Compiled language

A compiler does not execute its input program (the source code) but translates it into executable machine code (also called object code) which is output to a file for later execution. The ‘executable program’ consists of a set of CPU instructions that run directly on the processor chip and, therefore the compiler must translate the source code into instructions for a particular CPU. Hence, a program written for a Pentium PC (Intel) will not run on a Mac (PowerPC)! (C, Fortran, Cobol)

An interpreter or script program may run on both. Those types of programs are what is called platform independent.

Which is fastest?

So how many programming languages are there?

S1-5A.Honey (Oct 2003)

Page 6: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

ABC Blue ConstraintABEL C CurlAda C++ DADL C-sharp DatabaseAleph Caml DataflowAlgol 60 Cecil DeclarativeAlgol 68 CHILL DelphiAPL Clarion Directories AppleScript Clean DOS batchASP Clipper DylanAssembly CLU EAwk Cobol EiffelBASIC CobolScript ElastiCBefunge Cocoa ErlangBETA Compiled EuphoriaBigwig Component Pascal ForthBistro Concurrent Fortran

S1-6A.Honey (Oct 2003)

Page 7: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

FP Interpreted MLFrontier Java Modula-2Functional JavaScript Modula-3Garbage Collected LabVIEW MotoGoedel Lagoona MultiparadigmHardware Description Leda MumpsHaskell Limbo NETHistory Lisp ObfuscatedHTML Logic-based Object-OrientedHTMLScript Logo Objective-CHyperCard Lua ObliqICI m4 OccamIcon Markup Open sourceIDL MATLAB OzImperative Mercury ParallelIntercal Miranda PascalInterface Miva Perl

S1-7A.Honey (Oct 2003)

Page 8: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

PHP Rexx SQLPike Rigal SqueakPL RPG T3XPL-SQL Ruby Tcl-TkPliant S-Lang TempoPOP-11 SAS TOMPostscript Sather TRACPowerBuilder Scheme TuringProcedural Scripting UMLPrograph Self VBAProlog SETL VBScriptProteus SGML VerilogPrototype-based Simula VHDLPython Sisal VisualREBOL Smalltalk Visual DialogScriptReflective snobol Visual FoxProRegular Expressions Specification Water

S1-8A.Honey (Oct 2003)

Page 9: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Wirth XML XOTcl YAFL Yorick Z

The above was NOT an exhaustive list!

S1-9A.Honey (Oct 2003)

Page 10: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Lcc-win32

Why lcc-win32 rather than IC4 for these sessions?

Interactive C is truly interactive requiring that your computer be connected to a robot controller. If not then it becomes very tedious to write a program, as every time you press enter an attempt is made to send the statement to the controller and if it is not there then a delay of many seconds will occur.

Also the lcc-win32 has a 300+ page tutorial, which will become very useful when you start training your team members in the C programming language.

I intend to devote one of the later sessions to IC4. If for some reason this does not occur then the botBall workshop in Honolulu will be sufficient.

S1-10A.Honey (Oct 2003)

Page 11: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Learning a programming language

So what does one need to learn in order to become proficient in a programming language?

S1-11A.Honey (Oct 2003)

Page 12: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Learning a programming language

So what does one need to learn in order to become proficient in a programming language?

The Syntax which is the arrangement of words and symbols to create a working statement structure.

S1-12A.Honey (Oct 2003)

Page 13: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Learning a programming language

So what does one need to learn in order to become proficient in a programming language?

The Syntax which is the arrangement of words and symbols to create a working statement structure.

The acceptable words of the language.

S1-13A.Honey (Oct 2003)

Page 14: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Learning a programming language

So what does one need to learn in order to become proficient in a programming language?

The Syntax which is the arrangement of words and symbols to create a working statement structure.

The acceptable words of the language.

The acceptable punctuation.

S1-14A.Honey (Oct 2003)

Page 15: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Learning a programming language

So what does one need to learn in order to become proficient in a programming language?

The Syntax which is the arrangement of words and symbols to create a working statement structure.

The acceptable words of the language.

The acceptable punctuation.

The structure of a program.

S1-15A.Honey (Oct 2003)

Page 16: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Learning a programming language

So what does one need to learn in order to become proficient in a programming language?

The Syntax which is the arrangement of words and symbols to create a working statement structure.

The acceptable words of the language.

The acceptable punctuation.

The structure of a program.

Looping constructs.

S1-16A.Honey (Oct 2003)

Page 17: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Learning a programming language

So what does one need to learn in order to become proficient in a programming language?

The Syntax which is the arrangement of words and symbols to create a working statement structure.

The acceptable words of the language.

The acceptable punctuation.

The structure of a program.

Looping constructs.

Conditional statements and expressions.

S1-17A.Honey (Oct 2003)

Page 18: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Learning a programming language

So what does one need to learn in order to become proficient in a programming language?

The Syntax which is the arrangement of words and symbols to create a working statement structure.

The acceptable words of the language.

The acceptable punctuation.

The structure of a program.

Looping constructs.

Conditional statements and expressions.

How to create subroutines or there equivalent.

S1-18A.Honey (Oct 2003)

Page 19: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Learning a programming language

So what does one need to learn in order to become proficient in a programming language?

The Syntax which is the arrangement of words and symbols to create a working statement structure.

The acceptable words of the language.

The acceptable punctuation.

The structure of a program.

Looping constructs.

Conditional statements and expressions.

How to create subroutines or there equivalent.

How to declare and define variables and subroutinesS1-19A.Honey (Oct 2003)

Page 20: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Learning a programming language

When learning a programming language the syntax has to be correct or, either an error will be generated (especially true for a compiled language) or the program will not behave as expected and/or the program will ‘crash’!

S1-20A.Honey (Oct 2003)

Page 21: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

How is a C program organized?Tutorial pdf page 13, doc page 1

A C program consists of one or more source files called modules.

So why would you break a program into multiple modules?

S1-21A.Honey (Oct 2003)

Page 22: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C modules

A large complex program ‘needs’ to be broken into modules. Not only does this allow a team of programmers to work more-or-less independently, on various pieces of a large program, but also it allows the program to be more readily understood and maintainable.

In addition, with judicious modularization, a module may prove to be useful in another program, thereby, reducing the amount of new code that is written for each new program.

Of course if multiple programmers are working on different pieces of a program then they must agree, in advance, upon the inputs and outputs (parameters) to each smaller piece of the program (functions).

S1-22A.Honey (Oct 2003)

Page 23: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C functions

In C, the equivalent of a subroutine is called a function.

A C program may consist of an almost unlimited number of functions. However, at least one function must exist and it must be called ‘main’.

Typically, if you find that there is a set of statements that you need to use several times, in various places in your program, then you would probably create a function for that code. Or perhaps to simply make the program more easily read/understood. Or if that code maybe useful in another program.

A C function is typically a small piece of code that: has zero or more parameters; has zero or more declarations; contains zero or more program statements, which accomplish a desired task; and may or may not return a result.

S1-23A.Honey (Oct 2003)

Page 24: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C function parameters

For now we will defer a discussion of function parameters. Suffice it to say the function parameters allow data to be passed in to and/or out of a function, such that the statements of that function can manipulate the data.

We will return to this topic when the need arises.

S1-24A.Honey (Oct 2003)

Page 25: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C function declarations

Declarations are the means by which we create variables and provide their specifications to the compiler.

A variable allows us to ‘tell’ the compiler information about the data we want to manipulate or examine, within the statements of the program.

So what is a variable?

S1-25A.Honey (Oct 2003)

Page 26: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C function declarations

Declarations are the means by which we create variables and provide their specifications to the compiler.

A variable allows us to ‘tell’ the compiler information about the data we want to manipulate or examine, within the statements of the program.

So what is a variable?

A variable is essentially the ‘name’ of a data value that is stored in a particular memory location. It is the compiler and linker which determine the exact memory location used for a given variable.

S1-26A.Honey (Oct 2003)

Page 27: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C variables

Lets backup a little. Even though you may not know how to do this yet, assume you have created and compiled a C program. Where is that program?

S1-27A.Honey (Oct 2003)

Page 28: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C variables

Lets backup a little. Even though you may not know how to do this yet, assume you have created and compiled a C program. Where is that program?

Right it is on a disk drive.

For a compiled C program what is on the disk drive?

S1-28A.Honey (Oct 2003)

Page 29: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C variables

Lets backup a little. Even though you may not know how to do this yet, assume you have created and compiled a C program. Where is that program?

Right it is on a disk drive.

For a compiled C program what is on the disk drive?

S1-29A.Honey (Oct 2003)

source code, object code, and executable program!

Now you ‘run’ the program. What happens to the program?

Page 30: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C variables

Lets backup a little. Even though you may not know how to do this yet, assume you have created and compiled a C program. Where is that program?

Right it is on a disk drive.

For a compiled C program what is on the disk drive?

S1-30A.Honey (Oct 2003)

source code, object code, and executable program!

Now you ‘run’ the program. What happens to the program?

Right it is loaded into the computer’s memory.

Page 31: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C variables

Lets backup a little. Even though you may not know how to do this yet, assume you have created and compiled a C program. Where is that program?

Right it is on a disk drive.

For a compiled C program what is on the disk drive?

S1-31A.Honey (Oct 2003)

source code, object code, and executable program!

Now you ‘run’ the program. What happens to the program?

Right it is loaded into the computer’s memory.

So what is memory?

Page 32: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C variables

Computer memory can be visualized as a large set of switches that can be either on or off (in actual fact they are the equivalent of microscopic transistors which do function like switches). If we think of each switch as being numbered then the numbers are analogous to the addresses of the memory locations. Each memory address refers to…

S1-32A.Honey (Oct 2003)

Page 33: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C variables

Computer memory can be visualized as a large set of switches that can be either on or off (in actual fact they are the equivalent of microscopic transistors which do function like switches). If we think of each switch as being numbered then the numbers are analogous to the addresses of the memory locations. Each memory address refers to…

the equivalent of how many switches?

S1-33A.Honey (Oct 2003)

Page 34: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C variables

Computer memory can be visualized as a large set of switches that can be either on or off (in actual fact they are the equivalent of microscopic transistors which do function like switches). If we think of each switch as being numbered then the numbers are analogous to the addresses of the memory locations. Each memory address refers to…

the equivalent of how many switches?

think win32

S1-34A.Honey (Oct 2003)

Page 35: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C variables

Computer memory can be visualized as a large set of switches that can be either on or off (in actual fact they are the equivalent of microscopic transistors which do function like switches). If we think of each switch as being numbered then the numbers are analogous to the addresses of the memory locations. Each memory address refers to 32 switches as that is the data width on the current generation of IBM type PCs.

So why do we care about the data width?

S1-35A.Honey (Oct 2003)

Page 36: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C variables

Computer memory can be visualized as a large set of switches that can be either on or off (in actual fact they are the equivalent of microscopic transistors which do function like switches). If we think of each switch as being numbered then the numbers are analogous to the addresses of the memory locations. Each memory address refers to 32 switches as that is the data width on the current generation of IBM type PCs.

So why do we care about the data width?

The speed of memory is typically much slower than the processor so, if you can get more information in a single memory read or write, then your the entire system becomes more efficient. And, you can update your video card faster

S1-36A.Honey (Oct 2003)

Page 37: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C variables

As we will eventually see, a C program may contain variables of practically any type imaginable. However, there are only a few basic variable types, from which all other variables are derived.

Within this session we will only address two of those basic types, namely, integer and floating-point variables.

So why do we need to tell the compiler that a given variable is of a specific type?

S1-37A.Honey (Oct 2003)

Page 38: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C variables

As we will eventually see, a C program may contain variables of practically any type imaginable. However, there are only a few basic variable types, from which all other variables are derived.

Within this session we will only address two of those basic types, namely, integer and floating-point variables.

So why do we need to tell the compiler that a given variable is of a specific type?

It basically comes down to the representation of integers and float-point values in memory. They are NOT the same!

The compiler MUST know the type of the variable so it correctly interprets the data, in a given memory location.

S1-38A.Honey (Oct 2003)

Page 39: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Simple C program S1_pgm1

Lets create a simple program.

I assume everybody has downloaded and installed lcc-wn32.

You should have an icon on your desktop for wedit

If not then try start->Programs->lcc-win32

If not then open the ‘bin’ folder wherever you installed LCC and the executable should be there.

S1-39A.Honey (Oct 2003)

Page 40: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Simple C program

The program you created calls the function printf but where did that function come from? You didn’t create it.

S1-40A.Honey (Oct 2003)

Page 41: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Simple C program

The program you created calls the function printf but where did that function come from? You didn’t create it.

There are hundreds of functions in C that exist in either other source files or as object modules (the output from the compiler). A programmer may use those functions. It is the job of the linker to find those functions and include them in the compiled program. So how does the linker know which functions to include?

S1-41A.Honey (Oct 2003)

Page 42: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Simple C program

The program you created calls the function printf but where did that function come from? You didn’t create it.

There are hundreds of functions in C that exist in either other source files or as object modules (the output from the compiler). A programmer may use those functions. It is the job of the linker to find those functions and include them in the compiled program. So how does the linker know which functions to include?

That word ‘include’ was the clue as there was a preprocessor statement ‘#include <stdio.h>’ at the top of your program. That was a directive to include the module for standard input and output. Within the .h file there is information pertaining to the functions in the module. This also allows the compiler to determine if you have the parameters to the functions correct.

S1-42A.Honey (Oct 2003)

Page 43: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Simple C program S1_pgm2

So modify your program by adding another printf statement but without any arguments.

Then recompile the program.

What happens?

S1-43A.Honey (Oct 2003)

Page 44: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Simple C program

So modify your program by adding another printf statement but without any arguments.

Then recompile the program.

What happens?

You get a compiler error telling you that you used the printf function incorrectly. The compiler knows what to expect as there is information in stdio.h regarding the functions’ parameters.

S1-44A.Honey (Oct 2003)

Page 45: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Simple C program

So modify your program by adding another printf statement but without any arguments.

Then recompile the program.

What happens?

You get a compiler error telling you that you used the printf function incorrectly. The compiler knows what to expect as there is information in stdio.h regarding the functions’ parameters.

It is the preprocessor which responds to the statements that are preceded with ‘#’. For the include directive, the preprocessor finds the .h file and feeds it into the compiler. The compiler then uses that information while parsing/translating the program.

S1-45A.Honey (Oct 2003)

Page 46: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Simple C program

So modify your program by adding another printf statement but without any arguments.

Then recompile the program.

What happens?

You get a compiler error telling you that you used the printf function incorrectly. The compiler knows what to expect as there is information in stdio.h regarding the functions’ parameters.

It is the preprocessor which responds to the statements that are preceded with ‘#’. For the include directive, the preprocessor finds the .h file and feeds it into the compiler. The compiler then uses that information while parsing/translating the program.

Subsequently, the linker will use that information to find the actual function code. S1-46A.Honey (Oct 2003)

Page 47: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Simple C program S1_pgm3

So lets modify your program again to return to the subject of variables.

Remove the faulty printf statement and add declarations for an integer variable and a floating-point variable.

Anybody know what is needed?

S1-47A.Honey (Oct 2003)

Page 48: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Simple C program

So lets modify your program again to return to the subject of variables.

Remove the faulty printf statement and add declarations for an integer variable and a floating-point variable.

Anybody know what is needed?

int ivar;

float fvar;

S1-48A.Honey (Oct 2003)

Page 49: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Simple C program

So lets modify your program again to return to the subject of variables.

Remove the faulty printf statement and add declarations for an integer variable and a floating-point variable.

Anybody know what is needed?

int ivar;

float fvar;

The declarations tell the compiler that there are two variables that may be used in the program and, that those variables have certain storage and representation requirements. The compiler does not necessarily allocate storage for variables until it encounters program statements which use them.

S1-49A.Honey (Oct 2003)

Page 50: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Simple C program S1_pgm4

Now we will modify the program to define the variables that we declared. For now we will just assign some constants.

ivar = 1111.6;

fvar = 1111.6;

What do you think happens when these statements are compiled?

S1-50A.Honey (Oct 2003)

Page 51: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Simple C program

Now we will modify the program to define the variables that we declared. For now we will just assign some constants.

ivar = 1111.6;

fvar = 1111.6;

What do you think happens when these statements are compiled? The compile finds memory locations for the variables and writes the values into those locations.

What is stored in ivar?

S1-51A.Honey (Oct 2003)

Page 52: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Simple C program

Now we will modify the program to define the variables that we declared. For now we will just assign some constants.

ivar = 1111.6;

fvar = 1111.6;

What do you think happens when these statements are compiled? The compile finds memory locations for the variables and writes the values into those locations.

What is stored in ivar? Remember we told the compiler that ivar was an integer variable.

S1-52A.Honey (Oct 2003)

Page 53: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Simple C program S1_pgm5

Let’s modify the program to display the values stored in the variables.

Can you explain the result?

S1-53A.Honey (Oct 2003)

Page 54: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

Simple C program

Let’s modify the program to display the values stored in the variables.

Can you explain the result?

Because we told the compiler that ivar was an integer it only reserved memory for an integer and it only wrote the integer part of the value into that memory, truncation occurred!

S1-54A.Honey (Oct 2003)

Page 55: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C variables

I beseech you to NOT use single letter variable names!

Why not?

S1-55A.Honey (Oct 2003)

Page 56: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C variables

I beseech you to NOT use single letter variable names!

Why not?

If you have to search through your program for a variable and it is a single letter then it becomes very tedious.

S1-56A.Honey (Oct 2003)

Page 57: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C variables

So, when we define a variable, the compiler finds some memory location for the variable and notes the name of the variable along with the address of that memory. Then, when the variable is used in the program, the compiler causes the contents of that memory to be either written or readback. The amount of memory needed and how it is used is completely determined by the variables type.

S1-57A.Honey (Oct 2003)

Page 58: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

More C program structure

As you have probably surmised by now, C programming statements appear to terminate with ‘;’ This is true in most cases.

S1-58A.Honey (Oct 2003)

Page 59: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

More C program structure

As you have probably surmised by now, C programming statements appear to terminate with ‘;’ This is true in most cases.

Blocks of code (programmer generated groups of statements) are typically enclosed in braces ‘{…}’ This will become more evident when we learn about conditional statements and looping constructs. The braces are also used to delineated the beginnings and endings of functions.

The arguments to function calls (e.g. printf) must be enclosed in parentheses.

S1-59A.Honey (Oct 2003)

Page 60: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C variables S1_pgm6

Let’s now add statements to use the variables.

ivar = ivar * fvar;

fvar = ivar * fvar;

What do you think will be the result in each case?

S1-60A.Honey (Oct 2003)

Page 61: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C variables S1_pgm7

Let’s now add statements to use the variables.

ivar = ivar * fvar;

fvar = ivar * fvar;

What do you think will be the result in each case?

Add in printf statements to see if you are correct.

S1-61A.Honey (Oct 2003)

Page 62: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C variables S1_pgm8

Let’s delete most of the program and experiment with user entered variable values.

The first method is to provide the value when the program starts. These are called ‘command line arguments’.

The compiler always supplies at least one argument to the main function. Add in a printf to see what it is.

S1-62A.Honey (Oct 2003)

Page 63: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C variables S1_pgm9

Let’s delete most of the program and experiment with user entered variable values.

The first method is to provide the value when the program starts. These are called ‘command line arguments’.

The compiler always supplies at least one argument to the main function. Add in a printf to see what it is.

Now add statements to display two more arguments. We will enter the arguments from the configuration window. On the project pulldown, select configuration and then click on the Debugger tab. Enter two space separated strings.

S1-63A.Honey (Oct 2003)

Page 64: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C variables S1_pgm10

The second method for getting user values is to query for keyboard input. So modify your program to request values using the scanf function.

S1-64A.Honey (Oct 2003)

Page 65: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C variables S1_pgm10

The second method for getting user values is to query for keyboard input. So modify your program to request values using the scanf function.

There is a new syntactical item in what we have done. That is, the ampersands in front of the variables that were the arguments to the scanf function.

S1-65A.Honey (Oct 2003)

Page 66: Programming experience Let’s get a feel for everyones’ programming experience. Query languages known Query knowledge of looping constructs Query knowledge

C variables S1_pgm10

The second method for getting user values is to query for keyboard input. So modify your program to request values using the scanf function.

There is a new syntactical item in what we have done. That is, the ampersands in front of the variables that were the arguments to the scanf function.

The ‘&’ tells the compiler to pass the address of the variable, which is actually the address of the memory location. This is called passing by reference. Why do you think this is necessary?

S1-6A.Honey (Oct 2003)