Professional Documents
Culture Documents
A Simple C Program
/* File: hello.c
Print ‘Hello, world’ on the screen */
#include <stdio.h>
int main() {
printf(“Hello, world\n");
return 0;
}
• 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
• 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
• 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
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
C:/Ch> 1+2*3
7
C:/Ch> sin(0.5)
0.4794
C:/Ch> printf("hello, world")
hello, world
C:/Ch>
• Examples of Commands
• 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/
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).
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)
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.
Interactive Solution
> (20-0.2*5*9.81)/5
2.0380
>
/* 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:
Add an alias
alias(“go”, “cd C:/Documents and Settings/Administrator/eme5”); // for Windows
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”);