Vous êtes sur la page 1sur 21

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>
Preprocession 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
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 Unix
If Ch is the login shell, you can readily use the Ch language
environment. If not, you can type command ch at a terminal
prompt to launch the Ch language environment.

Startup in Windows
There 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


Run program hello.c using Visual C++ in Windows
>

cl hello.c

/* ... create hello.exe */


> hello.exe
Hello, world

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


>

cc hello.c

>

gcc hell.c

Or

/* ... create a.out */


> a.out
Hello, world

Run program hello.c in Ch.


> hello.c
Hello, world

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

C for Engineers and Scientists: An Interpretive Approach

Interactive Command Mode


Accept the input values and print out the results directly or use the
function printf() in C.
C:/Ch>
7
C:/Ch>
0.4794
C:/Ch>
hello,
C:/Ch>

1+2*3
sin(0.5)
printf("hello, world")
world

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
C:/Documents
C:/Documents
C:/Documents
C:/Documents
C:/Documents
hello.c
C:/Documents

and
and
and
and
and
and

Settings/Administrator> mkdir eme5


Settings/Administrator> cd eme5
Settings/Administrator/eme5> pwd
Settings/Administrator/eme5
Settings/Administrator/eme5> cp C:/Ch/demos/bin/hello.c hello.c
Settings/Administrator/eme5> ls

and Settings/Administrator/eme5> 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 body
v -- velocity of the body
a -- acceleration of the body
g -- 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

Interactive Solution

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

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

C for Engineers and Scientists: An Interpretive Approach

Program 1: A simple C program.


/* 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;
}

Output:
Acceleration a = 2.038000 (m/s^2)

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 _path
contains 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

Run programs without typing the


complete directory
Windows:
adding the following statement in the startup file _chrc in the users home directory can
add C:/Documents and Settings/Administrator/eme5 directory to the command search paths.
_path = stradd(_path, C:/Documents and Settings/Administrator/eme5;);
Unix, Linux, and Mac machines:
adding following two statements in the startup file .chrc in the
users home directory can add /home/harry/eme5 directory and
the current working directory to the command search paths.
_path = stradd(_path, /home/harry/eme5;);
Add an alias
alias(go, cd C:/Documents and Settings/Administrator/eme5); // for Windows
alias(go, cd /home/harry/eme5);

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

// for Unix

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

Ch Scripts
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);

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

Vous aimerez peut-être aussi