Pascal/C II
 Volume Number: 10 Issue Number: 1 Column Tag: Pascal/C workshop

# The Pascal Programmer’s Guide To Understanding ‘C’

## Teach yourself to read another language - Part II

By Ken Gladstone, MacTech Magazine Technical Editor

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

This article is the second half of my Pascal Programmer’s Guide to Understanding “C”. If you haven’t already, I suggest you read the first half, which appeared in our December 1993 issue - otherwise you will probably be thoroughly confused by this half! That first half covered the following “C” concepts: comments, identifiers, operators, constants, program structure, and variable declarations and scope. So, let’s continue

## PARAMETER PASSING

One key difference between C and Pascal is that C always passes parameters by value, never by reference. Therefore, you may be wondering how a C function can ever modify a passed in parameter. It can’t - but you can accomplish the same thing by passing a pointer to the value you wish to modify, and having the function modify the pointed to value. Here is an example:

```/* 1 */
/************************ C Version *************************/

void doubleIt( int * pointerToIntParam )
{
(*pointerToIntParam) *= 2;
}

int main()
{
int myInt = 3;

doubleIt( & myInt );

return myInt;
}

/******************** End of C Version **********************/

(********************* Pascal Version ***********************)
program myProgram;
var
myInt: INTEGER;

procedure doubleIt( var IntParam: INTEGER );
BEGIN
IntParam := IntParam * 2
END;

BEGIN
myInt := 3;

doubleIt( myInt )
END.

(****************** End of Pascal Version *******************)
```

## OLD VERSUS NEW FUNCTION DECLARATIONS

So far, I’ve been showing functions as follows:

```int MyFunc( int a, char b, float c )
{
/* Code goes here */
}

```

This way of writing functions is an ANSI extension that allows C to perform parameter type checking when calling a function. Things weren’t always so nice. In the original K&R C, functions were written as follows:

```int MyFunc( a, b, c )
int a;
char b;
float c;
{
/* Code goes here */
}

```

In original C compilers, when calling a function, there was no checking of parameter types, or often even of the number of parameters! In old C, you could write a call to a function before it had ever been defined, declared or mentioned in any way! Now, C compilers have much stronger type-checking. For example, Think C has a compiler option to require you to write a function prototype for every function.

## FLOW CONTROL

So far, all of the examples that I’ve shown execute code sequentially - in fact, I’ve only shown declarations, assignment statements, function calls, and function return statements. Like Pascal, C has various loops and other constructs to control the flow of code. We’ll start with the while loop. The while loop in C is nearly identical to the one in Pascal, except that it needs parens around the test expression and it doesn’t have a DO keyword. Examples:

```/* 2 */

while ( i < j ) i *= 2;   // First C example

while i < j DO i := i * 2;  {Pascal Equiv.}

while ( i < j )  // C example w/compound statement
{
sysBeep( 1 );
i *= 2;
}

while i < j DO   {Pascal Equiv.}
BEGIN
sysBeep( 1 );
i := i * 2
END
```

Next we have the C do statement. This is a loop with the test at the end of each iteration, like the Pascal REPEAT statement, but the sense of the while test at the end is the opposite of the Pascal UNTIL test. Unlike the Pascal version, the C version needs braces if the loop contains a compound statement. And again, the while condition needs parens. Example:

```/* 3 */

do // C version
{
sysBeep( x );
++ x;
}
while ( x != 10 );

REPEAT  {Pascal equiv.}
sysBeep( x );
x := x + 1
UNTIL x = 10;

Next we have the for loop.  The for loop in C is far more general than the one in Pascal.
It looks like this:

for ( expr1; expr2; expr3 )
statement;
```

What it does is this: expr1 is an initialization that is performed before executing the loop for the first time. expr2 is a test that is performed before each iteration. As long as expr2 evaluates to non-zero, the looping continues. expr3 is a statement that is performed at the end of every iteration. C does not limit loops to simple count up and count down types. Any or all of the three expressions may be omitted, but the semicolons must remain. Any C for loop can be rewritten as follows:

```/* 4 */

expr1;
while( expr2 )
{
statement;
expr3;
}
Example:

for ( i = 10; i != 0; --i ) // C example
DoIt( i );

for ( i = 10; i; --i )    // An equivalent variation
DoIt( i );

i = 10; // Another equivalent variation
while ( i )
{
DoIt( i );// Could use pre or post decrement
-- i;
}

i = 10; // Yet another equivalent variation
while ( i )
DoIt( i-- );   // Must use post decrement

FOR i := 10 DOWNTO 0 DO   {Pascal equivalent}
DoIt( i );
```

The C if statement is very similar to the Pascal version: The else clause is optional, and the statements can be either simple or compound. The only difference is that C needs parens around the expression, it doesn’t use the THEN keyword, and as always, every statement needs a semicolon. Example:

```/* 5 */

if ( condition ) // C
DoOneThing();
else
{
DoAnother();
AndAnother();
}
```

IF condition THEN {Pascal version}

```/* 6 */

DoOneThing
ELSE
BEGIN
DoAnother;
AndAnother
END
```

C has a case statement that is very similar to the Pascal version. An example should suffice:

```/* 7 */

switch ( x )// C version
{
case 1:
case 2:
DoTheOneOrTwoThing();
break;  // Must explicitly leave each case
case 3:
DoTheThreeThing();
AndTheOtherThreeThing();// Purposely fall through
case 7:
DoTheThreeAndSevenThing();
break;
default:
DoTheDefaultThing();
}
```

CASE x OF { Pascal Version }

```/* 8 */

1, 2:  DoTheOneOrTwoThing;
3:
BEGIN
DoTheThreeThing;
AndTheOtherThreeThing;
DoTheThreeAndSevenThing; { In Pascal, we need this twice }
END;
7:
DoTheThreeAndSevenThing; {In Pascal, we need this twice}
OTHERWISE DoTheDefaultThing
END
```

The previous example used the C break keyword. This keyword is like the Pascal Leave statement, and can be used to break out of the innermost while, do, for, or switch. C also has a continue keyword that like the Pascal Cycle statement. It skips to the next iteration of the innermost while, do or for loop.

Finally, C also has the dreaded goto statement (nothing seems to split programmers into warring factions as well as a goto statement does). Unlike in Pascal, you don’t declare labels in C, you just stick ‘em in the code, and they follow the same syntax as other identifiers. Example:

```/* 9 */

{// C
MyLabel:
x := Function();
if ( x == 10 ) goto MyLabel;
}

LABEL 333; { Pascal }
BEGIN
333: x = Function;
IF x=10 THEN GOTO 333
END
```

## LIBRARY FUNCTIONS

Standard C has oodles of library functions, such as malloc() and fread(), that you would use if you were programming on any computer unless you are programming on a Macintosh which you are. So for the most part, you will use calls like NewPtr() and FSRead() instead. You’ll need to look at your C compiler manual if you are interested in the standard C libraries.

## STRINGS

Amazingly enough, standard C doesn’t really provide much built-in language support for strings. There are several standard C library functions that process strings, but no real string type or operators. C handles strings as simple arrays of the char type. In general, you would create a string in one of the following ways:

```{
char myString[100]; // 100 bytes of storage
char Another[] = "LetTheCompilerCountTheSize";
char * ptrToString;

ptrToString = NewPtr( 100 );
}

```

C also has a different way of representing strings than the Pascal way. Instead of having a length byte followed by a number of characters, C starts immediately with characters, and the string is considered to continue until the occurrence of a zero byte. So this declaration:

```char myString[] = "Foo";

```

creates four bytes of storage. It fills the first three with the word "Foo" and puts a zero byte in the fourth. This convention allows strings of arbitrary length.

This string representation doesn’t fit well with Pascal nor with the Mac toolbox, but don’t despair. C only uses this convention in two places: In string constants (like the "Foo" shown above) and in its library functions. The Mac solves the second problem by shunning the C library that is used by the rest of the world, in favor of its own toolbox. And the compilers on the Mac solve the first problem by introducing an ingenious extension, the \p escape sequence. Here is an example:

```char pascalString[] = "\pFoo";

```

This causes the compiler to insert a Pascal-style length byte at the beginning of the string. It still generates a zero-byte at the end, however. So the above declaration would use five bytes: The first byte contains a 3 (for the Pascal length), the next three bytes contain the string, and the final byte contains the C-style zero byte. So pascalString can be used as a Pascal string, and (pascalString+1) or &pascalString[1] can be used as a C string.

## THE PREPROCESSOR

C compilers include a preprocessor step that reads in the source file, expands macros, and then writes back a temporary file that is fed into the actual compiler. Keep in mind that preprocessor commands are purely compile-time, not run-time operations. They are similar to Pascal {\$ } compiler directives. Instead of being embedded within comments, C preprocessor instructions begin with a number sign ‘#’. Here is a (somewhat contrived) code fragment that includes many of the common preprocessor instructions:

```/* 10 */

#include <stdio.h>

#define DEBUG    // Delete this line before shipping program.
#define PI3.14159
#define square(a)( ( a ) * ( a ) )
#define cube(a)  ( ( a ) * square( a ) )
#define max(a,b) ( ( ( a ) > ( b ) ) ? ( a ) : ( b ) )

#pragma segment mySegment

double MaxSurfaceOrVolume( double radius )
/*
* This is a strange function which will return either the
* surface area or the volume of a sphere, whichever is
* larger, for a given radius.
*/
{
#ifdef DEBUG
printf( "Hey, we're in the MaxSurfaceOrVolume function" );
#else
printf( "Hey, we're running the non-debug version" );
#endif

#if 0
```

I could have a bunch of lines of code in here, and they

wouldn't ever be executed, or even compiled.

```/* 11 */

#endif

return max( 4 * PI * square( radius ),
4.0/3.0 * PI * cube( radius ) );
}
```

The following table describes the preceding preprocessor statements:

Preprocessor Statement Meaning

#include <stdio.h> Similar to the Pascal {\$I filename} directive. Paste the contents of the included file into here as if they had actually been typed into this file. The angle brackets generally tell the compiler to look for the include file in its list of “system” file folders. Include files are generally named with a .h at the end. They generally consist of things like typedefs, global variable definitions, function prototypes, preprocessor macros, etc. The Mac compilers provide header files that prototype all the toolbox functions so you don’t have to.

#include "myHeader.h" Same as above, but look in the list of “user” file folders instead of system file folders.

#define DEBUG Similar to the Pascal {\$SETC DEBUG = 1} directive. Define the existence of a preprocessor variable. The existence of the variable can be checked later.

#define PI 3.14159 A simple text substitution. Replace all future occurences of PI with 3.14159.

#define square(a) ((a)*(a)) A substitution that takes parameters. Keep in mind that while a macro like this may look like a function call, it is purely text substitution, and therefore incurs none of the overhead of a function call.

#pragma segment mySegment The #pragma feature allows compiler specific instructions that are not actually part of the C language. Each compiler has its own pragmas. They are used for such things as turning optimizations on and off, disabling compiler warnings, or in this case, telling the compiler in what code segment to put this code. They perform many of the same functions as the miscellaneous Pascal {* } directives.

#ifdef DEBUG Similar to {\$IFC } in Pascal. The subsequent statements will only be compiled if the variable is defined.

#else Similar to {\$ELSEC}. The subsequent statements will only be compiled in the “else” case of the preceding #if.

#endif Similar to {\$ENDC}. Ends a preprocessor #if or #ifdef construction.

#if 0 This is a quick way to disable a chunk of code. Change it to #if 1 to re-enable.

## SUMMARY

You should now know enough C to be able to read C code listings. If you would like to get some more practice at seeing the differences between C and Pascal, you may wish to check out Dave Mark’s first few “Getting Started” articles. Dave wrote both a C and Pascal version for all of his programs in the 1992 columns. And while we didn’t print all of the listings in the magazine, we did include them in the source code disks and in our CD-ROM. Beyond that, you’ll probably have to break down a buy a couple of C books.

Million dollar (no, we won’t pay you, even if you have a good answer!) bonus question: K&R say that the term define is used when actually creating storage for a variable, and that the term declare is used when describing the characteristics of a variable (and only possibly creating storage). So why is it that type declarations, which allocate no storage, are spelled typedef (short for type definition) instead of being spelled typedecl? Perhaps this has been discussed somewhere before, but not that I’ve seen. Personally, I like the C keyword, and think that K&R have the define and declare terms backwards throughout their book!

Community Search:
MacTech Search:

## Latest Forum Discussions

Fresh From the Land Down Under – The Tou...
After a two week hiatus, we are back with another episode of The TouchArcade Show. Eli is fresh off his trip to Australia, which according to him is very similar to America but more upside down. Also kangaroos all over. Other topics this week... | Read more »
TouchArcade Game of the Week: ‘Dungeon T...
I’m a little conflicted on this week’s pick. Pretty much everyone knows the legend of Dungeon Raid, the match-3 RPG hybrid that took the world by storm way back in 2011. Everyone at the time was obsessed with it, but for whatever reason the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for July 19th, 2024. In today’s article, we finish up the week with the unusual appearance of a review. I’ve spent my time with Hot Lap Racing, and I’m ready to give my verdict. After... | Read more »
Draknek Interview: Alan Hazelden on Thin...
Ever since I played my first release from Draknek & Friends years ago, I knew I wanted to sit down with Alan Hazelden and chat about the team, puzzle games, and much more. | Read more »
The Latest ‘Marvel Snap’ OTA Update Buff...
I don’t know about all of you, my fellow Marvel Snap (Free) players, but these days when I see a balance update I find myself clenching my… teeth and bracing for the impact to my decks. They’ve been pretty spicy of late, after all. How will the... | Read more »
‘Honkai Star Rail’ Version 2.4 “Finest D...
HoYoverse just announced the Honkai Star Rail (Free) version 2.4 “Finest Duel Under the Pristine Blue" update alongside a surprising collaboration. Honkai Star Rail 2.4 follows the 2.3 “Farewell, Penacony" update. Read about that here. | Read more »
‘Vampire Survivors+’ on Apple Arcade Wil...
Earlier this month, Apple revealed that poncle’s excellent Vampire Survivors+ () would be heading to Apple Arcade as a new App Store Great. I reached out to poncle to check in on the DLC for Vampire Survivors+ because only the first two DLCs were... | Read more »
Homerun Clash 2: Legends Derby opens for...
Since launching in 2018, Homerun Clash has performed admirably for HAEGIN, racking up 12 million players all eager to prove they could be the next baseball champions. Well, the title will soon be up for grabs again, as Homerun Clash 2: Legends... | Read more »
‘Neverness to Everness’ Is a Free To Pla...
Perfect World Games and Hotta Studio (Tower of Fantasy) announced a new free to play open world RPG in the form of Neverness to Everness a few days ago (via Gematsu). Neverness to Everness has an urban setting, and the two reveal trailers for it... | Read more »
Meditative Puzzler ‘Ouros’ Coming to iOS...
Ouros is a mediative puzzle game from developer Michael Kamm that launched on PC just a couple of months back, and today it has been revealed that the title is now heading to iOS and Android devices next month. Which is good news I say because this... | Read more »

## Price Scanner via MacPrices.net

Amazon is still selling 16-inch MacBook Pros...
Prime Day in July is over, but Amazon is still selling 16-inch Apple MacBook Pros for \$500-\$600 off MSRP. Shipping is free. These are the lowest prices available this weekend for new 16″ Apple... Read more
Walmart continues to sell clearance 13-inch M...
Walmart continues to offer clearance, but new, Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for \$699, \$300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBooks... Read more
Apple is offering steep discounts, up to \$600...
Apple has standard-configuration 16″ M3 Max MacBook Pros available, Certified Refurbished, starting at \$2969 and ranging up to \$600 off MSRP. Each model features a new outer case, shipping is free,... Read more
Save up to \$480 with these 14-inch M3 Pro/M3...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at \$1699 and ranging up to \$480 off MSRP. Each model features a new outer case, shipping is... Read more
Amazon has clearance 9th-generation WiFi iPad...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for \$80-\$100 off MSRP, starting only \$249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Apple is offering a \$50 discount on 2nd-gener...
Apple has Certified Refurbished White and Midnight HomePods available for \$249, Certified Refurbished. That’s \$50 off MSRP and the lowest price currently available for a full-size Apple HomePod today... Read more
The latest MacBook Pro sale at Amazon: 16-inc...
Amazon is offering instant discounts on 16″ M3 Pro and 16″ M3 Max MacBook Pros ranging up to \$400 off MSRP as part of their early July 4th sale. Shipping is free. These are the lowest prices... Read more
14-inch M3 Pro MacBook Pros with 36GB of RAM...
B&H Photo has 14″ M3 Pro MacBook Pros with 36GB of RAM and 512GB or 1TB SSDs in stock today and on sale for \$200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 Pro MacBook Pro (... Read more
14-inch M3 MacBook Pros with 16GB of RAM on s...
B&H Photo has 14″ M3 MacBook Pros with 16GB of RAM and 512GB or 1TB SSDs in stock today and on sale for \$150-\$200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 MacBook Pro (... Read more
Amazon is offering \$170-\$200 discounts on new...
Amazon is offering a \$170-\$200 discount on every configuration and color of Apple’s M3-powered 15″ MacBook Airs. Prices start at \$1129 for models with 8GB of RAM and 256GB of storage: – 15″ M3... Read more

## Jobs Board

*Apple* Systems Engineer - Chenega Corporati...
…LLC,** a **Chenega Professional Services** ' company, is looking for a ** Apple Systems Engineer** to support the Information Technology Operations and Maintenance Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
*Apple* / Mac Administrator - JAMF Pro - Ame...
Amentum is seeking an ** Apple / Mac Administrator - JAMF Pro** to provide support with the Apple Ecosystem to include hardware and software to join our team and Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.