chapter 1 – getting started 2: getting started outline a simple c program run program hello.c in...

24
Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Chapter 2: Getting Started Outline A Simple C Program Run Program hello.c in ChIDE Startup in Ch Compile and Link Program hello.c in Windows and Unix in a Command shell Compile and Link Program hello.c Using ChIDE Command chmod in Unix Commonly Used Commands A Practical Engineering Problem of Solving Acceleration Setup Command Search Paths

Upload: tranquynh

Post on 28-Apr-2018

222 views

Category:

Documents


2 download

TRANSCRIPT

Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach

Chapter 2: Getting Started

Outline

A Simple C Program

Run Program hello.c in ChIDE

Startup in Ch

Compile and Link Program hello.c in Windows and Unix

in a Command shell

Compile and Link Program hello.c Using ChIDE

Command chmod in Unix

Commonly Used Commands

A Practical Engineering Problem of Solving Acceleration

Setup Command Search Paths

Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach

A Simple C Program

/* File: hello.c

Print „Hello, world‟ on the screen */

#include <stdio.h>

int main() {

printf(“Hello, world\n");

return 0;

}

Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach

• Comments

– Text surrounded by /* and */ is ignored by computer

– Used to describe program

• #include <stdio.h>

– Preprocessing directive - tells computer to load

contents of a header file

– <stdio.h> allows standard input/output operations

Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach

• int main()

– C programs contain one or more functions, exactly one of which must be main

– Parenthesis used to indicate a function

– int means that main "returns" an integer value

– Braces indicate a block (this is a left brace “{“ )

• The bodies of all functions must be contained in braces

• printf( “hello, world\n" );– Instructs computer to perform an action

• Specifically, prints string of characters within quotes– Entire line called a statement

• All statements must end with a semicolon– \ - escape character

• Indicates that printf should do something out of the ordinary• \n is the newline character• \a is the character for a sound.

Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach

• return 0;

– A way to exit a function

– return 0, in this case, means that the program terminated normally

– The returned value of 0 is displayed in the exit status in ChIDE

• Right brace }

– Indicates end of main has been reached

Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach

Executing Program hello.c Using Ch in ChIDE

Click “Run” or “Start” to execute the program

Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach

Startup a Ch Shell

Startup in UnixIf Ch is the login shell, you can readily use the Ch language environment. If not, you can type command ch at a terminalprompt to launch the Ch language environment.

Startup in WindowsThere are five ways to get into the Ch language environment. For example, to start Ch Standard Edition 6.1

– Click the icon Ch Standard on the Desktop screen

– Click Start->Programs->SoftIntegration Ch 6.1 Standard->Ch 6.1.

– Click Start, followed by Run, then type ch.exe.

– Go to the MS-DOS prompt, and type ch.

– In ChIDE, click Ch

Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach

• Run program hello.c using gcc in Unix/Linux/Mac OS X> cc hello.c

Or

> gcc hell.c

/* ... create a.out */> a.out

Hello, world

• Run program hello.c in Ch.

> hello.c

Hello, world

Run Program hello.c

Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach

Compile hello.c Using Visual C++ in Windows and gcc in Linux from ChIDE

Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach

A C program can be executed without compilation in a Ch

language environment. C programs are called command filesor simply commands in Ch. A command file shall have both read and execute permissions.

Command chmod in Unix/Linux/Mac OS X can be used to

change the permission mode of a file.

• Make a file, say hello.c, executable

> chmod +x hello.c

• Make a file readable

> chmod +r hello.c

• Make a file both executable and readable

> chmod +xr hello.c

Command chmod in Unix

Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach

Accept the input values and print out the results directly or use the

function printf() in C.

C:/Ch> 1+2*3

7

C:/Ch> sin(0.5)

0.4794

C:/Ch> printf("hello, world")

hello, world

C:/Ch>

Interactive Command Mode

Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach

Commonly Used Commands

Ch supports most Unix and Windows commands

Command Usage Description

cd cd Change to the home directory

cd dir Change to the directory dir

cp cp file1 file2 Copy file1 to file2

ls ls List contents in the working directory

mkdir mkdir dir Create a new directory dir

pwd pwd Print (display) the name of the working directory

rm rm file remove file

chmod chmod +x file Change the mode of file to make it executable

chide chide file.c Edit and execute program file.c

vi vi file Edit file

Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach

• Examples of Commands

C:/Documents and Settings/Administrator> mkdir CE13

C:/Documents and Settings/Administrator> cd CE13

C:/Documents and Settings/Administrator/CE13> pwd

C:/Documents and Settings/Administrator/CE13

C:/Documents and Settings/Administrator/CE13> cp C:/Ch/demos/bin/hello.c hello.c

C:/Documents and Settings/Administrator/CE13> ls

hello.c

C:/Documents and Settings/Administrator/CE13> chide hello.c

Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach

• Files in Ch

C:/Ch> pwd

C:/Ch

C:/Ch> ls

bin/ demos/ docs/ include/ license/ README.TXT sbin/

config/ dl/ extern/ lib/ package/ release/ toolkit/

C:/Ch> cd docs

C:/Ch/docs>

C:/Ch/docs> ls

README.TXT chguide.pdf chinstall.pdf chref.pdf man/

Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach

A Sample Problem:

The system in Figure1 (a) consists of a single body with mass m moving on a

horizontal surface. An external force p acts on the body. The coefficient of

kinetic friction between body and horizontal surface is . The freebody diagram

for the system is shown in Figure1 (b).

Figure1: The system diagram and FBD of a sample problem

Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach

The nomenclature related to the modeling of the system is listed below.

m -- mass of the body x -- position of the bodyv -- velocity of the bodya -- acceleration of the bodyg -- gravitational acceleration -- friction coefficient f -- friction force N -- normal force

p -- applied external force

Equation of motion:

The equation of the motion of the system can be derived based on the Newton's

second law.

N = mg (1)

f = N (2)p-f = ma (3)

From equation (1), (2) and (3), the formula for calculating the acceleration

of the rigid body can be derived as follows.

a = (p- mg)/m (4)

Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach

Problem Statement:

For the system shown in Figure1(a), given m = 5 kg, g = 9.81 m/s2, = 0.2.

The external force p is expressed as a function of time t,

p(t) = 20 when t >= 0

calculate the acceleration a when t = 2 seconds.

Solutions.

1. Interactive solution.

2. Write a simple C program to print out the value of acceleration

directly.

Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach

> (20-0.2*5*9.81)/5

2.0380

>

Interactive Solution

Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach

/* File: accel.c */

#include <stdio.h>

int main() {

printf("Acceleration a = %f (m/s^2)\n", (20-0.2*5*9.81)/5);

return 0;

}

Program 1: A simple C program.

Acceleration a = 2.038000 (m/s^2)

Output:

Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach

• Finding Commands in Ch Shell

– The system variable _path of string type contains the directories to be searched for the command.

– When a command shell is launched, the system variable _pathcontains some default search paths.

– The user can add new directories to the search paths for the command shell by the following statements.

Create an individual startup file .chrc in Unix and _chrc in Windows

in your home directory by typing the command

> ch –d

in a Ch command shell.

Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach

Windows:

adding the following statement in the startup file _chrc in the user’s home directory can add C:/Documents and Settings/Administrator/eme5 directory to the command search paths.

_path = stradd(_path, “C:/Documents and Settings/Administrator/CE13;”);

Unix, Linux, and Mac machines:

adding following two statements in the startup file .chrc in the

user’s home directory can add /home/harry/eme5 directory and

the current working directory to the command search paths.

_path = stradd(_path, “/home/harry/CE13;”);

Add an alias

alias(“go”, “cd C:/Documents and Settings/Administrator/CE13”); // for Windows

alias(“go”, “cd /home/harry/eme5”); // for Unix

Run programs without typing the

complete directory

Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach

Open startup file _chrc in Windows or

.chrc for Unix for editing through ChTE

Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach

The function main() is optional for a Ch script program. Like a command file,

a script file shall have both read and execute permissions.

/* File: welcome.ch */

printf(“Welcome to Ch\n”);

Ch Scripts

Created by Harry H. Cheng, 2009 McGraw-Hill, Inc. All rights reserved.

C for Engineers and Scientists: An Interpretive Approach

A program with #!/bin/ch can be invoked by other programs such as Bash,

C shell. Statements, functions, and commands can be grouped as a script

file or script in Ch

#!/bin/ch

/* File: hello.ch */

#include <stdio.h>

int main() {

printf(“Hello, World\n”);

return 0;

}

Ch Scripts (Cont.)