Académique Documents
Professionnel 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
#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
cl hello.c
cc hello.c
>
gcc hell.c
Or
1+2*3
sin(0.5)
printf("hello, world")
world
Usage
Description
cd
cd
cd dir
cp
cp file1 file2
ls
ls
mkdir
mkdir dir
pwd
pwd
rm
rm file
remove file
chmod
chmod +x file
chide
chide file.c
vi
vi file
Edit file
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
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).
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
>
Output:
Acceleration a = 2.038000 (m/s^2)
in a Ch command shell.
// for Unix
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);