Jump to content

Ch (computer programming)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Chuser (talk | contribs) at 06:17, 4 May 2011 (added inline citations for the article). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Ch (/[invalid input: 'icon']ˌsˈ/) is a cross-platform C and C++ interpreter originally designed by Dr. Harry H. Cheng as a scripting language for beginners to learn math, computing, numerics, and programming in C/C++.

Now developed by SoftIntegration, Inc., Ch is written in C and runs under Windows, Linux, Mac OS X, FreeBSD, AIX, Solaris, QNX and HP-UX. It supports C90 and major C99 features, but it does not support the full set of C++ features. C99 complex number, IEEE-754 floating-point arithmetic, VLA features were supported in Ch before they became part of the C99 standard [1][2] [3] [4].

Ch is now used and integrated into curriculum by many universities and schools for learning computing and programming in C/C++ [5] [6] [7] [8]. Ch can also be run as an interactive shell to execute C statement, C script file, Unix commands, and Windows commands (under windows only). [9]


Features

C/C++ interpreter

Ch supports the 1999 ISO C Standard (C99) and C++ classes. It is a superset of C with C++ classes. C99 major features such as complex numbers, variable length arrays (VLAs), IEEE-754 floating-point arithmetic and generic mathematical functions are supported. Wide characters in Addendum 1 for C90 is also supported.

The following C++ features are available in Ch:

  • Member function
  • Mixed code and declaration
  • The this-> pointer
  • Reference type and pass-by-reference
  • Function-style type conversion
  • Class
  • private/public data and functions in class. Ch is compatible with C++ that by default, members of a class definition are assumed to be private until a 'public' declaration is given
  • Static member of class/struct/union
  • Const member functions
  • The new and delete operators
  • Constructors and destructors
  • Polymorphic functions
  • The scope resolution operator ::
  • The I/O cout, cerr, cin with endl
  • Arguments for variadic functions are optional

Ch supports classes in C++ with the following additional capabilities:

  • Classes inside member functions
  • Nested functions with classes
  • Pass member function to argument of pointer-to-function type of functions

Embeddable scripting

Ch can interface with existing C/C++ libraries and call C/C++ functions from Ch script.[10] As a C/C++ interpreter, Ch can be used as a scripting engine for your applications. The pointer to array or variables can be passed and shared in both binary C space and ch scripting space. It extends your applications with a C compatible scripting language. One of the embedded ch scripting application is Mobile-C. Mobile-C has been used for collaborative visualization of distributed mesh model. [11] [12]

Shell programming and cross-platform scripting

Ch is a C-compatible shell similar to C-shell (csh). It can be used as login shell. Ch has a built-in string type (string_t) for automatic memory allocation and de-allocation. It makes C/C++ easy of scripting. It supports shell alias, history, filename wildcarding, piping and iteration etc. [13]

2D/3D plotting and numerical computing

Ch has built-in 2D/3D graphical plotting features and computational arrays for numerical computing. A 2D linear equation of the form b = A*x can be written verbatim in Ch. [14]

Examples

"Hello, world!" in Ch

There are two ways to run Ch code. One is:

   printf("Hello world!\n");

Another is:

   #include <stdio.h>
   int main() {
       printf("Hello world!\n");
   }

Ch also supports interactive shell command and C statements.

 > int i, *p, **p2     // i is an integer, p pointer, p2 double pointer
 > i=10                // i is assigned value 10
   10
 > p=&i                // p points to address of i
   00D847C0
 > *p                  // the memory pointed by p has value 10
   10
 > p2=&p               // p2 points to address of p
   00D84D30
 > **p2                // the memory pointed by the pointer at p2 has value 10
   10
 >

Numerical computing in Ch

   #include <stdio.h>
   #include <array.h>
   int main() {
       array double A[2][3] = {1, 2, 3,
                           4, 5, 6};
       array double B[3][2];
       printf("A= \n%f \n", A+A);
       B = 2*transpose(A);
       printf("B= \n%f \n", B);
   }

The output is:

   A=
   2.000000 4.000000 6.000000
   8.000000 10.000000 12.000000
   B=
   2.000000 8.000000
   4.000000 10.000000
   6.000000 12.000000

Shell programming

Find and compile all .c files into .o in the current directory for which the .o file is old or absent:

   #!/bin/ch
   #include <sys/stat.h>
   struct stat cstat, ostat;
   string_t c, o;
   foreach (c; `find . -name "*.c"`)
   {
       o=`echo $c | sed 's/.c$/.o/'`;
       stat(o, &ostat); stat(c, &cstat);
       if (ostat.st_mtime > cstat.st_mtime)
       {
           echo "compiling $c to $o";
           gcc -c -o "$o" "$c";
       }
   }

2D/3D plotting in Ch

To plot a sine wave:

  #include <math.h>
  #include <chplot.h>
  int main() {
       int numpoints = 36;
       array double x[numpoints], y[numpoints];
       linspace(x, 0, 360); // assign x with values from 0 to 360 linearly
       y = sin(x*M_PI/180);
       plotxy(x, y, "Ch plot", "xlabel", "ylabel");
   }

See also

References

  1. ^ Cheng, Harry (1993). "Handling of Complex Numbers in the Ch Programming Language". Scientific Programming: 76–106.
  2. ^ Cheng, Harry (1993). "Scientific Computing in the Ch Programming Language". Scientific Programming: 49–75. {{cite journal}}: Cite journal requires |journal= (help)
  3. ^ Cheng, Harry (1995). "Extending C and FORTRAN for Design Automation". ASME Trans., Journal of Mechanical Design: 390–395. {{cite journal}}: Cite journal requires |journal= (help)
  4. ^ Cheng, Harry (1995). "Extending C with arrays of variable length". Computer Standards & Interfaces: 375–406. {{cite journal}}: Cite journal requires |journal= (help)
  5. ^ Cheng, Harry (2009). "C for the Course". ASME Mechanical Engineering Magazine: 50–52. {{cite journal}}: Cite journal requires |journal= (help)
  6. ^ Huber, Tom (2010). "An Introduction to C and Ch: Your One-Stop Shop for Scientific Computing". IEEE Computing in Science and Engineering. {{cite journal}}: Cite journal requires |journal= (help)
  7. ^ Liu, Li; Wang, Zhaoqing; Jiang, Xusheng (2010). "Anchor-based programming teaching embedded with Ch platform". Mechatronics and Embedded Systems and Applications (MESA), IEEE/ASME International Conference: 49–52. {{cite journal}}: Cite journal requires |journal= (help)
  8. ^ Furman, B.; Wertz, E. (2010). "A first course in computer programming for mechanical engineers". Mechatronics and Embedded Systems and Applications (MESA), IEEE/ASME International Conference: 70–75. {{cite journal}}: Cite journal requires |journal= (help)
  9. ^ Wang, Gary (2002). "Ch Solves Portability Headaches". IEEE Spectrum. {{cite journal}}: Cite journal requires |journal= (help)
  10. ^ Wilson, Matthew (2004). "Open-RJ and Ch". Dr. Dobb's Journal. {{cite journal}}: Cite journal requires |journal= (help)
  11. ^ Cheng, Harry (2009). "Speeding-Up Software Development Using Embedded Scripting". Dr. Dobb's Journal: 8–8. {{cite journal}}: Cite journal requires |journal= (help)
  12. ^ Wang, Li-rong; Bo, Yo; Hagiwara, I. "Mobile-C based agent management for collaborative visualization of distributed mesh model". Computer-Aided Industrial Design & Conceptual Design, 2009. CAID & CD 2009. IEEE 10th International Conference. {{cite journal}}: Cite journal requires |journal= (help)
  13. ^ Campbell, Matt (2003). "Ch, A C/C++ Interpreter -- New possibilities for people who like C and Unix". MACTECH, the journal of Apple technology. {{cite journal}}: Cite journal requires |journal= (help)
  14. ^ Glassborow, Francis (2001). "The Ch Language Environment Version 2.0". C Vu Magazine: 36–37. {{cite journal}}: Cite journal requires |journal= (help)


Further reading