LECTURE 13

308--208 PHASE 2

  1. Learn simple DOS commands
  2. Learn to start Turbo C
  3. Learn Turbo C editor
  4. Understand structure of C program
  5. Understand philosophy of C :
  6. Learn C syntax
  7. Do 3 C  assignments

Some of the statements in various high­level languages are illustrated in the following table. Each section of code represents the calculation of an employee's salary.

Examples of High­Level Languages

LANGUAGE EXAMPLE STATEMENTS

Pascal:

if hours <= 40.0 then
   salary := hours*payrate
else
   salary := 40.0*payrate +
   (hours ­ 40.0)*payrate*1.5; 

FORTRAN 90:

if (hours <= 40.0) then
   salary = hours*payrte
else
   salary = 40.0*payrte +
   (hours ­ 40.0)*payrte*1.5
endif

Ada:

if hours <= 40.0 then 
   salary := hours*payrate; 
else 
   salary := 40.0*payrate +
   (hours ­ 40.0)*payrate*1.5;

BASIC:

if h > 40.0 then 200
let s = h*p
go to 250
200 let s = 40.0*p + (h ­ 40.0)*p*1.5
250

COBOL:

if hours is less than 40.0 or
_ hours is equal to 40.0,
   compute salary = hours*payrate
else
   compute salary = 40.0*payrate +
   (hours ­ 40.0)*payrate*1.5

PL/I:

if hours <= 40.0 then 
   salary = hours*payrate; 
else 
   salary = 40.0*payrate +
   (hours ­ 40.0)*payrate*1.5;


ADVANTAGES OF C

Turbo C :

DISADVANTAGES OF C


TURBO C COMPILER :

Hot Key Menu Equivalent Function
F1 Help Displays a Help screen
F2 File/Save Saves active edit or file
F3 File/Open Opens file
F4 Run/Go to cursor Executes to cursor location
F5 Window/Zoom Zooms the active window
F6 Window/Next Cycles through open windows
F7 Run/Trace into Traces into subroutines
F8 Run/Step over Steps over subroutine calls
F10 Activates the menu bar
ALT-F1 Help/Previous topic Displays previous Help screen
ALT-F3 Window/Close Closes active window
ALT-F5 Window/User screen Displays User screen
ALT-F9 Compile/Compile Compiles active program
ALT-Spacebar menu Goes to System menu
ALT-C Compile menu Goes to Compile menu
ALT-D Debug menu Goes to Debug menu
ALT-E Edit menu Goes to Edit menu
ALT-F File menu Goes to File menu
ALT-H Help menu Goes to Help menu
ALT-O Options menu Goes to Options menu
ALT-R Run menu Goes to Run menu
ALT-S Search menu Goes to Search menu
ALT-W menu Window Goes to Window menu
ALT-X menu File/Exit Exits Turbo Pascal to DOS
CTRL-F1 Help/Topic search Gives language-specific help while in editor
CTRL-F2 Run/Program reset Resets running program
CTRL-F7 Debug/Add watch Adds a watch expression
CTRL-F8 Debug/Toggle breakpoint Clears or sets conditional breakpoint
CTRL-F9 Run/Run Executes active program


TURBO C OPERATIONS

Function Keystroke
Movement Commands Ctrl S or left arrow
Character left Ctrl D or right arrow
Character right Ctrl A or Ctrl left arrow
Word left Ctrl F or Ctrl right arrow
Word right Ctrl E or up arrow
Line up Ctrl X or down arrow
Line down Ctrl R or PgUp
Page up Ctrl C or PgDn
Page down Ctrl Q/S or Home
Beginning of line Ctrl Q/D or End
End of line Ctrl Q/E or Ctrl Home
Top of window Ctrl Q/X or Ctrl End
Bottom of window Ctrl Q/R or Ctrl PgUp
Beginning of program Ctrl Q/C or Ctrl PgDn
End of program
Insert and Delete Commands
Delete line Ctrl Y
Delete block Ctrl K/Y
Delete to end of line Ctrl Q/Y
Delete character left of cursor Ctrl H or Backspace
Delete character under cursor Ctrl G or Delete
Insert line Ctrl N
Block Commands
Copy block to edit file Ctrl K/C
Copy block to Clipboard Edit/Copy or Ctrl lns
Delete block (not saving to Clipboard) Edit/Clear or Ctrl Del
Delete block (saving to Clipboard) Edit/Cut or Shift Del
Hide/display block Ctrl K/H
Mark block begin Ctrl K/B
Mark block end Ctrl K/K
Mark single work Ctrl K/T
Move block from Clipboard Edit/Paste or Shift Ins
Move block to edit file Ctrl-K/V
Read block from disk Ctrl K/R
Write block to disk Ctrl K/W
Miscellaneous
Find Ctrl Q/F or Search/Find
Find and replace Ctrl Q/A or Search/Replace
Invoke main menu F10
Language help Ctrl F1
Open file F3 or File/Open
Save file Ctrl K/S or F2 or File/Save


FORTRAN PROGRAM

PROGRAM SINE

! Prints values of y=sin x for x=0,10,...90 degrees.

implicit none

real :: scalef, degree, radian, y
integer :: i

scalef = 3.141593/180.0
print *,'DEGREES SINE FUNCTION'
do i = 0,9
   degree = i*10.0
   radian = scalef*degree
   y = sin(radian)
   print 5,degree,y
5  format (' ',F7.1,F12.4)
end do
stop
END PROGRAM SINE


EQUIVALENT PROGRAM IN C

/* Prints values of y=sinx for x=0,10,...90 degrees. */

#include <stdio.h>
#include <math.h>

main()
{
   float scalef, degree, radian, y;
   int i;
   scalef = 3.141593/180.0;
   printf ("DEGREES SINE FUNCTION\n");
   for (i=0; i<=9; i=i+1)
   {
      degree = i*10.0;
      radian = scalef*degree;
      y = sin(radian);
      printf ("%f7.1%f12.4\n",degree,y);
   }
}


FORTRAN FORM VS. C FORM

FORTRAN 77 ---> RIGID FORMAT (Improved with FORTRAN 90)

C ---> FREE FORMAT


DATA TYPES IN C

int same as INTEGER ; float same as REAL

Most expressions are the same as FORTRAN :
(Where op is an arithmetic operator + - * / %)

BUT THERE ARE DIFFERENCES :


CHAR TYPES IN C

1) char in C like CHARACTER*1 in FORTRAN

Form Meaning
\0 null character
\b backspace
\f form feed (top of page)
\n new line
\r carriage return
\t tab
\v vertical tab
\' single quote ( ' )
\" double quote ( " )
\\ backslash ( \ )
\% percent sign ( % )

2) char(n) in C like CHARACTER (LEN=n) in FORTRAN


A Complete C Program

Example #1 :


#include <stdio.h>

main( )
{
   printf("Hello, world\n");
}

Example #2 :

#include <stdio.h>

main()
{
   printf("The value of PI is about 3.14159 \n");
}

or

#include <stdio.h>
#define PI 3.14159

main()
{
   printf("The value of PI is %f \n", PI);
}


Go back to lecture menu

Go back to main page


Copyright © McGill University 1996. All rights reserved.