Basic C Program Structure – C program basic introduction – Free Study Material

Programs start out as source code, a collection of instructions usually written in a high level programming language. A source file containing source code is nothing more than a plain text file that can be edited by any text editor. However, many developers and programmers utilize modern Integrated Development Environment (IDE) that provide a text editor with code highlighting: various elements are displayed in different colors to make the code more readable and elements can be easily identified.Mistakes such as unclosed comments or curly brackets can be readily apparent with such editors. IDEs can also provide automated compile/build features and other tools that make the development process easier and faster. Some languages are compiled languages meaning that a source file must be translated into machine code that a processor can understand and execute. This is actually a multistep process. A compiler may first preprocess the source file(s) and perform some pre-compiler operations. It may then transform the source code into another language such as an assembly language, a lower-level more machine-like language.Ultimately, the compiler transforms the source code into object code, a binary format that the machine can understand. To produce an executable file that can actually be run, a linker may then take the object code and link in any other necessary objects or precompiled library code necessary to produce a final program. Finally, an executable file (still just a bunch of binary code) is produced. Once an executable file has been produced we can run the program. When a program is executed, a request is sent to the operating system to load and run the program. The operating system loads the executable file into memory and may setup additional memory for its variables as well as its call stack (memory to enable the program to make function calls). Once loaded and setup, the operating system begins executing the instructions at the program’s entry point.In many languages, a program’s entry point is defined by a main function or method. A program may contain many functions and pieces of code, but this special function is defined as the one that gets invoked when a program starts. Without a main function, the code may still be useful: libraries contain many useful functions and procedures so that you don’t have to write a program from scratch. However, these functions are not intended to be run by themselves. Instead, they are written so that other programs can use them. A program becomes executable only when a main entry point is provided. This compile-link-execute process is roughly depicted in Code Sample of above image. An example of a simple C program can be found in Code  along with the resulting assembly code produced by a compiler and the final machine code represented in hexadecimal. In contrast, some languages are interpreted, not compiled. The source code is contained in a file usually referred to as a script.Rather than being run directly by an operating system, the operating system loads and execute another program called an interpreter. The interpreter then loads the script, parses, and execute its instructions. Interpreted languages may still have a predefined main function, but in general, a script starts executing starting with the first instruction in the script file. Adhering to the syntax rules is still important, but since interpreted languages are not compiled, syntax errors become runtime errors.A program may run fine until its first syntax error at which point it fails. There are other ways of compiling and running programs. Java for example represents a compromise between compiled and interpreted languages. Java source code is compiled into Java bytecode which is not actually machine code that the operating system and hardware can run directly. Instead, it is compiled code for a Java Virtual Machine (JVM).
This allows a developer to write highly portable code, compile it once and it is runnable on any JVM on any system (write-once, compile-once, run-anywhere). In general, interpreted languages are slower than compiled languages because they are being run through another program (the interpreter) instead of being executed directly by the processor. Modern tools have been introduced to solve this problem. Just In Time (JIT) compilers have been developed that take scripts that are not usually compiled, and compile them to a native machine code format which has the potential to run much faster than when interpreted. Modern web browsers typically do this for JavaScript code (Google Chrome’s V8 JavaScript engine for example). Another related technology are transpilers.
Transpilers are source-to-source compilers. They don’t produce assembly or machine code, instead they translate code in one high-level programming language to another high-level programming language. This is sometimes done to ensure that scripting languages like JavaScript are backwards compatible with previous versions of the language. Transpilers can also be used to translate one language into the same language but with different aspects (such as parallel or synchronized code) automatically added. They can also be used to translate older languages such as Pascal to more modern languages as a first step in updating a legacy system.

Leave a Reply