Advertisements
Is C dead?
The C language has been the choice for test and measurement applications for years. But with the popularity of object-oriented languages such as C++ and C#, some may wonder if C has a place in tomorrow's test systems. If you do have a significant amount of C code investment, it is important to understand not only the key C programming language strengths and weaknesses but also best practices when integrating C code into applications written in another language.
Introduced more than 35 years ago, the C language has been used in test and measurement, embedded systems, and device driver development for decades. Initially created to address code reuse challenges commonly associated with assembly languages, the C programming language was developed to be platform-independent by abstracting the hardware layer. Because C often is only one layer away from the hardware, it generally is accepted that C provides much of the speed and memory efficiency associated with assembly while increasing code portability and decreasing development time.
The Strengths of C
C programming language strengths include the following:
Powerful Low-Level Capability to Optimize Code
Because C often provides direct access to operating-system or hardware-specific function calls, you have a greater ability to control which specific operations and how many operations are performed at the machine level. Optimized compilers and the capability to call assembly code also help you control program size and execution speed.
Highly Portable and an Extensive Set of Code Reuse Tools
You can compile a C program that adheres to a recognized industry standard, like ANSI C, and is hardware- and OS-independent for a wide variety of computer platforms and operating systems with minimal change to its source code. As a result, the language has amassed a sizable programming community and millions of lines of example code and hardware drivers that have been written in C.
General-Purpose and Industry-Specific Development Environments
Many general-purpose C development environments, such as Microsoft Visual Studio, provide additional out-of-the-box functionality, such as database connectivity APIs, in addition to standard C programming language functions. Industry-specific textual development environments like NI LabWindows/CVI also offer functionality commonly used in vertical application areas including extensive analysis libraries such as signal ramps, filters, and PID control algorithms as well as support for a diverse set of I/O device drivers.
With the capabilities to perform direct hardware manipulation, easily control how data is stored in memory, and reuse millions of lines of preexisting textual math code, the C language is well suited for developing applications with strict space and performance requirements or applications involving extensive signal processing and analysis. Specifically, device drivers, embedded systems, and real-time applications can benefit from the increased speed and processing available in C if the code is written efficiently.
The Challenges of C
C programming language challenges include the following:
Object-Oriented Programming
With complex applications, there usually is a requirement to group data together as a structure. This is a standard feature in most high-level languages including ANSI C.
As an extension, object-oriented languages offer support not only for creating custom data types but also for defining specific methods that work on that data. In particular, object-oriented programming features the concept of organizing an application into distinct modular components called objects. Programmers can encapsulate real-world test hardware, such as data acquisition devices and instruments, as objects.
Software objects model real-world entities, and like real-world objects, each software object is defined by a state and behavior. The state of the object is the set of variables you would use when describing the object. Likewise, objects have certain unique behaviors or actions they are capable of performing. As a result, once you create an object, you have a new data type on which distinct operations can be performed.
For example, a test station may be responsible for testing multiple components on a circuit board. In software, you can organize your application to model this behavior. You can create a class to represent a component and a method that is part of the class to test the component. You then can create a class to represent the circuit board, which is made up of an array of components. To test your board, you simply call the method responsible for testing each of the components that make up a board.
In addition to the base functionality of C, C++ contains some language extensions that make object-oriented programming more convenient. While C emphasizes simplicity, efficiency, and speed, C++ focuses more on abstraction. C++ does not force object-oriented design but allows for it if you deem it feasible. You can implement object-oriented programming in C as well but C++ makes it simpler and less error-prone.
Memory Management
With the low-level memory access available in C comes the responsibility for handling memory allocations and deallocations. Lack of clean-up code that explicitly deallocates memory can significantly influence the performance and determinism of an application. Along with other languages, .NET languages handle many low-level tasks such as memory management. You can allocate new memory on demand and then rely on the garbage collector to dispose of the memory when you no longer need it.
Parallel Execution
Multithreading was a key advance in the history of textual languages. With the capability to virtually execute multiple function calls at once, you can create rich user interfaces that seamlessly perform file I/O, database logging, and hardware control without seeing a performance decrease.
Reusing Existing C Code
There are many ways to integrate existing C code in external programming languages. The most common include the following:
Directly Compile and Link C Source Files
Most C++ compilers support directly calling C code from a C++ source file. First, you must ensure that the C and C++ compilers define basic types such as int, float, or pointer in the same way. The C++ language provides a linkage specification with which you declare that a function or object follows the program linkage conventions for a supported language. The extern keyword indicates to the C++ compiler that the following declaration is a C function.
Call Precompiled C DLLs
You also can create C code in a dynamic link library (DLL) and then call it from other languages that support DLLs. A DLL contains code and data that can be used by more than one program at the same time. For example, calling a DLL from .NET requires Platform Invoke (P/Invoke) to call unmanaged code from the managed world.
To reuse C functions, you need to wrap them with DllImport in C# or Declare Function in Visual Basic .NET and then ensure that all the .NET data types match the data types in the DLL. For example, character arrays become strings, C pointers become IntPtr, and so on.
Although most, if not all, automated test applications require access to hardware, the managed world of .NET isolates you from the underlying hardware. The runtime engine of the .NET Framework, called the Common Language Runtime (CLR), acts as a buffer between your application and the actual memory on the host computer, preventing direct access to memory and registers.
To control devices such as GPIB boards, serial ports, and data acquisition devices, your program still needs direct memory access. This is a situation where using a C device driver in a .NET environment would be ideal. The C code would run in the low-level kernel mode of your OS and not in the user mode to which .NET applications are restricted.
Systems that allow engineers to select the best software tool for the job and integrate diverse code modules and technologies into one solution help maintain legacy code relevance while enabling development teams to take advantage of the latest software platforms. Implementing highly modular and interoperable systems leads to decreased cycle time, cross-functional support teams, and reusable components whether or not the components are developed in C or other common programming languages.
The Bright Future of C
The C programming language has enduring value especially for test applications because of its direct access to memory and minimal software over-head--important aspects lacking from most modern .NET applications. In addition, C gives developers the freedom to write code for a large number of platforms: everything from microcontrollers to advanced process control systems. With the limited number of language restrictions and the capability to develop diverse applications, C is convenient and effective for many test and measurement tasks, giving test engineers one more reason to keep their C/C++ skills sharpened.