Basics Of C Programming Language
Introduction
Today computers have pervaded every field. Automation is the key concept that is driving the world. Any kind of job requires some amount of knowledge of IT and programming. C is a high level programming language, which every programmer should know. Hence in this Artical, we will be studying the C language constructs in detail. To start with let us understand the difference between the words software, program and command.
Instructions to a Computer
When a computer is started, it automatically does some processes and comes to a particular screen. How does this happen? The answer is simple. The operating system software exists inside the computer. The operating system is referred to as system software.
This software starts up the computer and performs some initial settings before giving us an operational screen. To achieve this, the operating system is made up of a set of programs. Every program tries to give solution to one or more problems. Each program is set of instruction to solve the problem it tries to address. Thus a group of instructions make up a program and a group of programs make up software.
Let’s consider an analogy to get more clarity: One of our friends comes home and we serve the Strawberry Milk Shake. He finds it so tasty that he too wants the recipe. So, we give him the recipe as:
- Take some milk
- Put Strawberry Crush
- Blend and serve chill
Now if our friend follows these instructions as they are, he too will be able to make a wonderful Milk Shake.
Let us analyze these instructions –
.Take the first instruction: Is it complete? Does it answer the question as to ‘Where’ the milk has to be taken?
.Take the second instruction; again here it is not very clear as to where the Strawberry Crush should be put
Luckily our friend was intelligent enough to understand in spite of these doubts in the recipe. But if we have to publish this recipe, then should we not be a bit more careful? Well, certainly yes. Let’s modify the Steps like this:
- Pour 1 glass of Milk into the Mixer Jar
- Mix some Strawberry Crush to the Milk in the Mixer
- Close the Lid of the Jar
- Switch on the Mixer and start blending
- Stop the mixer
- If the Crush is fully mixed with milk then switch off the mixer else start on the Mixer again
- Once done, pour the contents of the Mixer Jar into another bowl and put it into the refrigerator
- Leave it for sometime, then take it out and serve
Compare both the recipes for the Milk Shakes and judge yourself which set of instructions is more complete. Surely, the second set will be the one, which anyone could read and understand.
Similarly, the computer also processes the data based on the set of instructions given to it. Needless to say, the set of Instructions given to the computer also should be meaningful and complete. Apart from this, they should be:
- Sequential
- Finite
- Accurate
Each of the instruction in the instruction set is known as a Command and the set itself is known as a Program.
Now let’s consider the case of making the computer to add 2 numbers for us.
The program for it could be
- Take the first number and remember it
- Take the other number and remember it
- Perform the ‘+’ operation between the first and the second number; also remember it as the result.
- Then display the result
- Stop
The instruction set given here adheres to all the rules mentioned above. So, this instruction set is a program and will successfully make the computer accomplish the task of adding two numbers.
Note: Just like our remembering capability is known as memory, the capability of the computer to remember data given to it is also known as memory. It is only because of this that the computer can take the data at an instance and work with it at another instance, i.e. it first registers the data in its memory and then reads its memory to retrieve values and work with them.
When the job to be given to the computer is big and complicated then all the commands cannot be put into one program, they need to be broken into a number of programs. All the programs are finally integrated to work together. Such an integrated set of programs is known as software.
C Programming Language
Dennis Ritchie from Bell Laboratories created C , in the early 1970s. C Programming language was initially used on a system with UNIX operating system. C was derived from a development process, of an older language BCPL, developed by Martin Richards. BCPL was evolved into a language B, written by Ken Thompson, which was the originator of C Programming language.
While, BCPL and B do not support data-types (they are typeless), C provides a variety of data types. The main data- types are characters, integers and floating-point numbers.
C is closely associated with the UNIX system yet C is not tied to any one operating system or machine. C has been effectively used to write programs in different domains.
C was used for systems programming. A system program is associated with the operating system of the computer or its support utilities. Operating Systems, Interpreters, Editors, Assembly programs are usually called systems programs. UNIX Operating System was developed using C. C is now being used by many programmers for kinds of tasks because of its portability and efficiency. C compilers are available for almost all computers. Codes written in C on a one machine can be compiled and run on another machine by making a few or no changes. C compiler produces fast and error-free object code.
C also offers the speed of an assembly language. Programmers can create and maintain library of functions, which can reused by other programs. Thus large projects can be managed easily, with minimum efforts.
C -A Middle Level Language
C is thought of as a middle-level language because it combines elements of high-level languages and functionalities of an assembly (low-level) language. C allows manipulation of the basic elements of a computer i.e. bits, bytes, addresses etc. Also, C code is very portable, that is, software written on one type of computer can work on another type of computer. Although C has five basic built-in data types, it is not strongly typed language as compared to high-level languages. C allows data type conversions. It allows direct manipulation of bits, bytes, words, and pointers. Thus, it is used for system-level programming.
C – A Structured Language
The term block-structured language does not apply to C. A block-structured language permits procedures and functions to be declared inside another procedure or function. C does not allow creation of functions within functions so it’s not a block-structured language. However, it is referred to as a structured language because it is similar in many ways to other structured languages like ALGOL, Pascal and the likes.
C allows synthesis of code and data. This is a distinguishing feature of any structured language. It refers to the ability of a language to collect and hide all information and instructions, necessary to perform a specific task, from the rest of the program. This can be done using functions or code blocks. Functions are used to define and separate, tasks required in a program. This allows programs act as a unit. Code block is a logically connected group of program statements that is treated like a unit. A code block is created by placing a sequence of statements between opening and closing curly braces as shown below.
do
{
i = i + 1;
.
.
.
} while (i < 40);
Structured language support several loop constructs, such as while, do-while, and for. These loop constructs help the programmers to control the flow of the program.
The C Programming Language Structure
C has few keywords, 32 to be precise. These keywords, combined with the formal C syntax, form the C language. But many C compilers have added more keywords to use the memory organization of certain preprocessors.
Some rules for programs written in C are as follows:
- All keywords are lower cased
- C is case sensitive, do while is different from DO WHILE
- Keywords cannot be used for any other purpose, that is, they cannot be used as a variable or Funcation name.
- main() is always the first function called when a program execution begins.
main()
{
/*this is a simple progra*/
int i = 0;
i = i + 1;
.
.
}
Function Definition
C programs are divided into units called functions. The sample_code has only one function main(). The operating system always passes control to main() when a C program is executed.
The function name is always followed by parentheses. The parentheses may or may not contain parameters.
Delimiters
The function definition is followed by an open curly brace ({). This curly brace signals the beginning of the function. Similarly a closing curly brace ({) after the statements, in the function, indicate the end of the function. The opening brace ({) indicates that a code of block is about to begin and the closing brace ({) terminates the block of code. In sample_code, there are two statements between the braces. In addition to functions, the braces are also used to delimit blocks of code in other situations like loops and decision-making statements.
Statement Terminator
Consider the line int i = 0 in sample_code Is a Statement. A statement in C is terminated with a semicolon ( ; ). A carriage return, whitespace, or a tab is not understood by the C compiler.
There can be more than one statement on the same line as long as each one of them is terminated with a semi-colon. A statement that does not end in a semicolon is treated as an invalid line of code in C.
Comment Lines
Comments are usually written to describe the task of a particular command, function or an entire program. The compiler ignores them. In C, comments begin with /* and are terminated with */, in case the comments contain multiple lines. Care should be taken that the terminating delimiter (*/*) is not forgotten. Otherwise, the entire program will be treated like a comment. In sample_code, “This is a sample program” is a comment line. In case the comment contains just a single line you can use / / to indicate that it is a comment. For example:
int a=0; //Variable 'a' has been declared as an integer data type
The C Library
All C compilers come with a standard library of functions that perform the common tasks. In some installations of C, the library exists in one large file while in others it is contained in numerous small files.
While writing a program, the functions contained in the library can be used for various tasks. A function written by a programmer can be placed in the library and be used in as many programs as and when required. Some compilers allow functions to be added in the standard library, while some compilers require a separate library to be created.
Compiling and Running a Program
The various stages of translation of a C program from source code to executable code are as follows:
- Editor/Word Processor
The source code is written using an editor or a word processor. The code should be written in the form of standard text files, as C accepts source code only in this form. Some compilers supply programming environments (see appendix) that include an editor. - Source Code
This is the text of the program, which the user can read. It is the input for the C compiler. - C Preprocessor
The source code is first passed through the C preprocessor. Preprocessors, act on statements beginning with #. These statements are called directives (explained later). The directives are usually placed at the start of the program, though they can be placed anywhere else. The directives are short names given to a set of code. - Expanded C Source Code
The C preprocessor expands the directives and produces an output. This is called the expanded C source code. This expanded C source code is then passed on the C compiler. - C Compiler
The C compiler translates the expanded source code into the machine language, which is understood by the computer.
If the program is too large it can be put in separate files and each of the files can be compiled separately. The advantage of this separation is that if code in a file is changed, the entire program need not be recompiled. - Linker
The object code along with support routines from the standard library and any other separately compiled functions are linked together by the linker into an executable code. - Loader
The executable code is run using the system’s loader
3 Comments