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 :

• Declare everything
• Top-down structure
• Reserved keywords
• Lots of checking at compile time
• TC : lots of checking at execution time
• Integrated development environment ( TC )
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
```

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

• Newer, " structured " language
• Designed for better programming
• All identifiers must be declared
• Many control constructs
• Possible to program without GOTO
• Free-format source code
• Reserved keywords
• Heterogeneous data types
• Bit-oriented logical operators
• Almost as ubiquitous as FORTRAN

Turbo C :

• Good debugging
• Good graphics
• Inexpensive

• More things to learn
• Errors harder to pinpoint
• No built-in complex data type arithmetic

TURBO C COMPILER :

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
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()
{
int i;
scalef = 3.141593/180.0;
printf ("DEGREES SINE FUNCTION\n");
for (i=0; i<=9; i=i+1)
{
degree = i*10.0;
printf ("%f7.1%f12.4\n",degree,y);
}
}

```

FORTRAN FORM VS. C FORM

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

• Actual statements go in columns 7--72 only
• Labels go in columns 1--5 only
• One statement per line
• Long line must use continuation in column 6
• Keywords can be used as variables (IF=5)
• Spaces allowed in keywords and variables

C ---> FREE FORMAT

• Statements can go anywhere on a line
• Use punctuation to separate statements
• Multiple statements per line ( Don't abuse this ! )
• Some parts of statements separated by '' white space '' ( spaces, tabs, carriage-returns )
• Long lines usually can be split in nice places ( use \ if needed )
• Comments can be on same lines as statements
• Reserved keywords
• No spaces in keywords and variables

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 + - * / %)

• int op int => int
• int op float=> float
• float op int = float
• float op float=> float
• Precedences the same

BUT THERE ARE DIFFERENCES :

• " a % b " in C like " MOD ( A,B ) " in FORTRAN
• " pow( a,b ) " in C like " A**B " in FORTRAN

CHAR TYPES IN C

1) char in C like CHARACTER*1 in FORTRAN

• Char constants are single characters within single quotes : 'A'
• Special characters represented with backslash combinations :

 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 ( % )

• May be used like integers ( c - '@' turns A--Z into 1--26 )

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

• Strings terminated with \0 (null char)
• Thus, a string can have a ''length'' -- even be empty!
• String constants are enclosed within double quotes : "A"
• Use ' [i] ' to get at ith character : init = name[1]
• Illegal ( but not detected ! ) if i bigger than declared length

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

```