croc logo

Neon-komputadór

Computer Users Manual, Ministry of Foreign Affairs and Cooperation, Democratic Republic of East Timor
2003


Languages

English
Portuguese

Index

Introduction
Chapter I: Hardware and Software
Chapter II: Networks and Communications
Chapter III: Operating Systems
Chapter IV: Applications

Chapter V: Basic Coding and Programming

Introduction to Programming
Logical Sets and Procedures
Extendable Hypertext Mark-Up Language (xhtml)
Structured Query Lanuage (SQL)
Visual Basic and C

Chapter VI:Basic Systems Administration
Appendicies: Ministry Policy

Ministry Hompage

Visual Basic and C

It makes sense to discuss Visual Basic and C together, both to note their similarities and to give the user an introduction to two programming languages simultaneously. Visual Basic is the primary programming language for Microsoft Applications. It is a version of BASIC (Beginners All-purpose Symbolic Instruction Code) which was originally written in 1964. Bill Gates (of Microsoft fame) and Paul Allen developed a version in the mid-1970s and had is ported to various hardware and operating system platforms, such as Apple, Atari, Commodore and IBM-PC. Microsoft developed their version of BASIC to become Quick Basic and with the development of Graphic User Interfaces, Visual Basic.

As with all programming languages it is highly recommended that the programmer writes out the program first in pseudo-code that is, using the material from the section on Logical Sets and Procedures, before translating to the peculiar syntax of a particular programming language. Pseudo-code should also help in documentation and comments in the actual final program code.

C is the programming language that the UNIX and Linux operating systems were written in. It is the most commonly used programming language in the world. It was written in 1972 by AT&T so that programmers could work with a high-level style that could access low-level issues. During the 1970s different versions of C developed, and in 1983 the American National Standards Institute (ANSI) formed a committee to standardize C. This was published in 1989. In the meantime, AT&T developed C++ as an elaboration and enhancement to C with minor differences (to express simply, C++ is object-orientated, whereas C is procedure-orientated).

The first area of difference to note is that C is case sensitive. Thus 'Variable', 'variable' and 'varIAble' are all different. Upper case variables are usually used for pre-processor definitions (e.g., constants) or as part of character strings. In C, there are only functions, whereas Visual Basic has both subroutines (subs) and function calls. In fact, in C the entire program is expressed as a function, whereas in Visual Basic the entire program is a subroutine. Further, in C, every statement is terminated by a semi-colon ";". The main advantage is that long statements can be broken up over several lines to aid readability.

The basic structure of the two programming languages follows. Comments are differentiated by a single quotation mark at the start of the line in Visual Basic and "/*" to open and "*/" to close in C (which can be across multiple lines. C requires the inclusion of "stdio.h", which is the "standard input and output" library and expresses itself as a function "main( )". The return(0) statement is optional, but it is safest to always use it as it depends on the compiler. The curly brackets in C are used to group statements together as a function, or as the body of a loop. Such groups are called "compound statements" or a "block".

Visual Basic
Sub main
'This is the introductory program
End Sub

C/C++
#include 
main( )
{ /* This is the introductory program */
return(0)}

The general data types found in both languages have similar declarations. In Visual Basic, the data types String, Integer, Double and Single are referred to in C as char, int, double and float. "Double" and "Single/float" refer to real numbers - single/float is a real number and double is a double precision. In Visual Basic variables are expressed as dimensions, whereas in C the data type is expressed with the data object name. In both Visual Basic and C multiple objects of the data type can be declared in a single statement that separates the object names with commas. Data objects can be declared anywhere in a program, although it is a good idea for readability to declare them at the beginning of a function. Arrays are declared as per the example in the previous section for C ("datatype dataobject[arraylength]) and similar in Visual Basic (Dim dataobject(arraylength) As datatype. In Visual Basic and C, an array must start with the element index of 0.

A major difference in the two programming languages is the way that strings of characters are declared. In Visual Basic the String datatype is a character or a chain of characters. In C, a single character is expressed by the datatype 'char' and a string is an array of char (for example: char Firstname[20]). There is also a special null terminator in C ('\0') which concludes a character array, so that arrays should be designed to be one character longer than the expected maximum. This makes string manipulation in C somewhat more difficult than in Visual Basic. Whereas Visual Basic simply has to compare the names of the variables (e.g., If Name 1 = Name2 Then ...) in C this has to achieved by a strcmp function call (e.g., if ( strcmp ( Name1, Name2) == 0) ...).

Every variable must have a name. Variables are not allowed to be named after keywords in the programming language in which they are written, so "main" can not be used as a variable name in C. In C, as mentioned, upper case characters are avoided as these are usually reserved for preprocessor commands. Global variables are those that are declared outside of any program functions and are used throughout the program. These have to be declared as such in Visual Basic (e.g., "Global NumberCount As Integer"). Local variables are declared within a function and are only used within that function. Efficient programming style recommends against excessive use of global variables. There are also external variables, which are declared inside one file, but are used by another. Static variables are a class of local variable whose value is kept once a function is complete and revived when the function is called again. In C, they are declared as "static datatype dataobject;". This is different to Visual Basic, where 'Static' refers to what C describes as a 'constant', where the variable is by convention declared in capitals ("const datatype DATAOBJECT = 'datavalue"). Finally, in C data objects (including arrays) can be initialized when they are declared.

There are minor differences in expressions between Visual Basic and C. One subtle difference in C is that between assignment and equality. Assignment makes a variable equal to a value. Equality tests the truth value of a statement. In both Visual Basic and C, assignment is the same (e.g., VBA, variable = value, or C, variable = value ;). The test of equality is slightly different (e.g., VBA if variable = value then, or C, if (variable == value)). Some other slight changes in the expressions include the use in C of '!=', when VBA uses '<>', '&&' compared to 'and', '||' compared to 'or', and '!' compared to 'not'. C has some shorthand methods for arithmetic (e.g., x++, increases the value of x by 1).

Selection and control statements in Visual Basic and C have only minor differences. As C writes if statements as a function, they must be enclosed in parentheses and the subsequent 'then' statement in Visual Basic is considered redundant (e.g., Visual Basic, if variable=value then ..., whereas C, if (variable=value)...). The else statement work similarly - in Visual Basic it forms part of the if statement block, whereas in C must be preceded by an if. (e.g., Visual Basic, if a>5 then b=1 else b=0 end if, in C., if (a>5) b=1 ; else b= 0 ; ). It is worth remembering in C there is no Boolean datatype - these are simply an integer that regards 1 as true and 0 as false. Finally, the major difference in Visual Basic's case and C's switch statement is the need to include break statements in C. In Visual Basic the break is implied with a successful result. In C if there is no break statement, any switch choice would also include all subsequent choices (which in some rare cases a programmer might actually want).

Visual Basic
Select Case SmallNumber	
Case 1
 Msg = "Ida"
Case 2
 Msg = "Rua"
Case 3
 Msg = "Tolu"
Else
 Msg = "Barak!"
End Select
 
C/C++

switch( smallnumber)
{ 
 case 1 : Msg = "Ida" ; break ;
 case 2 : Msg = "Rua" ; break ;
 case 3 : Msg = "Tolu" ; break ;
 case 4 : Msg = "Barak!" ; break ;
}

The syntax of while statements in Visual Basic and C are virtually identical, (Visual Basic, while x < 100, C, while (x < 100) and with strong similarities in do...while loops as well (Visual Basic, Do x = x+1 Loop While x <100 and C, do {x++ ;} while ( x < 100) ; ). Note that in C, the statement block for do, while must be in curly braces and the conditional expression must be in parentheses, as it is a function call. Furthermore, the for structure is also different. In C, the conditional statement can be any proposition whereas VBA only terminates when reaching an upper counter limit, and the counter can increased or decreased by any value. The two examples are: (VBA, For x = 1 to 100 y = y +1 Next, and in C, for (x = 1; x <=100; x++), y++ ;.

Standard output in Visual Basic and C is slightly different. Whereas VBA requires little more than the standard "Print variable" statement, to display a result to the screen, C uses the function printf (e.g., printf ("hello world!\n");), with the "\n" representing a newline marker. Standard inputs are handled in VBA through (normally) a print request and input (Print "Please enter an integer: " Get TheInput), wheras C carries this out with a printf and scanf functions. (printf "Please enter an integer: " ) ; scanf (" %d", theinput);). It should be mentioned here that C++ does this standard output and input differently slightly different with "cout << variable" and "cin >> variable" for inputs.

Functions are defined as section of code with one entry that accepts incoming values and return a single expression. In comparison, a sub-routine or procedure does not return a value. In the C programming language assumes a returned data type, a function name, parameters in parentheses, and a function body expressed inside curly brackets. In VBA functions must be declared as such, followed by the function name, parameters in parentheses, followed by the return value. For example in VBA, a function header could be expressed as Function functionname ( x As Integer, y As Integer, z As String) As Integer, whereas in C the same function would be int functionname ( int x, int y, z char); . A function is returned in C by using the return keyword follow by the variable name or value to return. In Visual Basic a function will execute all statements in the block before execution is passed back to the function caller, in C the return statement immediately returns to the function caller. Calling functions in Visual Basic and C is the same, except if a function doesn't receive any parameters it must still have the parentheses after the function name.


Ministry of Foreign Affairs and Cooperation, GPA Building #1, Ground Floor, Dili, East Timor

valid XHTML 1.0! valid CSS Level2! Level Triple-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0 Unicode encoded use any browser!

Website code and design by Lev Lafayette. Last update August 20, 2003

Hosted by www.Geocities.ws

1