Skip to main content

Introdution to C

Introduction to C
C Language is a powerful programming language developed by Dennis Ritchie at Bell Laboratories in 1970. C Language is a general purpose high-level language.
Most of its principles and ideas were taken from the earlier language B, BCPL and CPL. CPL (Common Programming Language) was developed jointly between the Mathematical Laboratory at the University of Cambridge and the University of London Computer Unit in 1960s.CPL was too large for use in many applications. In 1967, BCPL (Basic Combined Programming Language) was created as a scaled down version of CPL retaining its basic features by Martin Richards. Ken Thompson at Bell Labs, USA wrote his own variants of BCPL and called it B. In due course, the designers of UNIX modified it to produce a programming language called C. In 1970, a co-worker of Ken Thompson, Dennis Ritchie developed C Language by taking some of the generality found in BCPL and the B language into a more powerful language called C. Ninety percent of the code of the UNIX operating system and of its descendants is written in C. The name C comes being the successor of B and BCPLIn 1983, an ANSI standard for C emerged consolidating its international acceptance.

Features of C

C is powerful: The increasing popularity of C is due to its many desirable qualities. It is a robust language whose rich set of built-in functions and operators can be used to write any complex program. The C compiler combines the capabilities of an assembly language with features of a high-level language and therefore it is well suited for writing both System programs and Application programs.

C is a small language: It has only 32 keywords and its strength lies in its built-in functions and is easy to learn.

C is a structured programming language: language is well suited for structured programming, thus requiring the user to think of a problem in terms of function modules or blocks.

C is quick: A well written C program is likely to be as quick as or quicker than a well-written program in any other high-level language.

C is a core language: A number of common and popular programming languages like C++, Java, Perl, PHP etc are based on C. Having learnt C, it will be much easier to learn languages that are largely or in part based upon C.

C is stable: The ANSI standard for C was created in 1983. The language has not been undergone much changes then. The latest standard is C99 with only minor changes over ANSI C.

C is extendable: Another important feature of C is its ability to extend itself. A C program is basically a collection of functions that are supported by the C library. We can continuously add our own functions to the C library.

Compiling and Running C programs

A high-level source program must be translated first into a form the machine can understand. This is done by software called the Compiler. The compiler takes the source code as input and produces the machine language code for the machine on which it is to be executed as output.
Compiling and executing a C program requires you to work with four kinds of files:
1. Regular source code files. These files contain function definitions, and have names which end in ".c" by convention.
2. Header files. These files contain function declarations (also known as function prototypes) and various preprocessor statements. They are used to allow source code files to access externally-defined functions. Header files end in ".h" by convention.
3. Object files. These files are produced as the output of the compiler. They consist of function definitions in binary form, but they are not executable by themselves. Object files end in ".obj” in some operating systems (e.g. Windows, MS-DOS),
4. Binary executables. These are produced as the output of a program called a "linker". The linker links together a number of object files to produce a binary file which can be directly executed. They generally end in ".exe" on Windows.
C Program
C Program can be considered as collection of functions. A function is a sub program that contains instructions or statements to perform a specific computation. It is easy to define the functions and use it in C.A C program may contain several functions one of which must be main (). This function is the starting point of the program. Other functions are called from main. After executing the statements in a function the program control will be returned to the calling function. The function may be a user defined function or a built in library function.
The main components of a C program are

Preprocessor directives
Global Variables
User defined functions
Main function

The general syntax of a function is

returnvalue functioname(arguments)
{
variable decalarations
executable statements
}




Eg: void bio(void)
{
printf(“Neethu\n”);
printf(“CEK”);
}

void main(void) /* program execution begins here */
{
clrscr(); /* calling a built in function to clear the screen */
bio();/* calling the bio function to print the name and college */
}
Output of the above program is:
Neethu
CEK

The use of functions in C serves many advantages:
1. It facilitates top-down modular programming. In this programming style, the high level of the overall problem is solved first while the details of each lower-level function are addressed later.

2. The length of a source program can be reduced by using functions at appropriate places.

3. It is easy to locate and isolate a faulty function for further investigations.

4. A function may be used by many other programs. This means that a C programmer can build on what others have already done, instead of starting from scratch.

Comments

Popular posts from this blog

Writing a Bash Shell Script

Bash shell scripts in Linux are text files that contain a series of commands that can be executed by the Bash shell. Bash (Bourne Again Shell) is a popular shell in Linux and UNIX systems, and shell scripts are used to automate tasks, configure systems, or perform a sequence of operations. How to Write a Bash Shell Script Create a New File: You can create a new script using any text editor like nano , vim , or gedit . gedit myscript.sh Write the Script: A basic shell script begins with a "shebang" ( #!/bin/bash ) to specify the interpreter that will be used to execute the script. The rest of the file contains the commands to be run. Example of a simple script: #!/bin/bash # This is a comment echo "Hello, World!" # Print "Hello, World!" #!/bin/bash : Specifies that the script will be executed using the Bash shell. echo "Hello, World!" : A command that prints the string "Hello, World!" to the terminal. Comments: Any line starting ...

Different syntax for writing arithmetic expressions in bash shell

#!/bin/bash echo "Enter two numbers" read a b s=`expr $a + $b` echo "Sum1=$s" s=$[$a+$b] echo "sum2=$s" ((s=$a+$b)) echo "sum3=$s" ((s=a+b)) echo "sum3=$s" let s=$a+$b echo "sum4=$s" let s=a+b echo "sum4=$s" Note:bash shell support only integer arithmetic.zsh support operations on real numbers.We can use bc in bash shell to do real arithmetic. Eg: echo "$a*$b"|bc # where a and b are real Mathematical Operators With Integers Operator Description Example Evaluates To + Addition echo $(( 20 + 5 )) 25 - Subtraction echo $(( 20 - 5 )) 15 / Division echo $(( 20 / 5 )) 4 * Multiplication echo $(( 20 * 5 )) 100  % Modulus echo $(( 20 % 3 )) 2 ++ post-increment (add variable value by 1) x=5 echo $(( x++ )) echo $(( x++ )) 5 6 -- post-decrement (subtract variable value by 1) x=5 echo $(( x-- )) 4 ** Exponentiation x=2 y=3 echo $(( x ** y )) 8

Basic Linux Commands For Beginner's

Basic Linux Commands for Beginners Linux is an Operating System’s Kernel. You might have heard of UNIX. Well, Linux is a UNIX clone. But it was actually created by Linus Torvalds from Scratch. Linux is free and open-source, that means that you can simply change anything in Linux and redistribute it in your own name! There are several Linux Distributions, commonly called “distros”. A few of them are: Mint Ubuntu Linux Red Hat Enterprise Linux Debian Fedora Kali Linux is Mainly used in Servers. About 90% of the Internet is powered by Linux Servers. This is because Linux is fast, secure, and free! The main problem of using Windows Servers are their cost. This is solved by using Linux Servers. Forgot to mention, the OS that runs in about 80% of the Smartphones in the World, Android, is also made from the Linux Kernel. Yes, Linux is amazing! A simple example of its security is that most of the viruses in the world run on Windows, but not on Linux...