Skip to main content

Introduction to C Programming


C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was first implemented on the DEC PDP-11 computer in 1972. The main features of C language include low-level access to memory, simple set of keywords, and clean style, these features make C language suitable for system programming like operating system or compiler development.Many later languages have borrowed syntax/features directly or indirectly from C language. Like syntax of Java, PHP, JavaScript and many other languages is mainly based on C language. C++ is nearly a superset of C language having object oriented features.
 Ken Thompson (left) with Dennis Ritchie (right, the inventor of the C programming language)
In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard.When C was first written the standard was set by its authors Kernighan and Ritche - hence "K&R C". In 1990, an international ANSI standard for C was established which differs from K&AMPR C in a number of ways. The latest C standard is ISO/IEC 9899:2011, also known as C11 as the final draft was published in 2011. Before C11, there was C99. The C11 final draft is available here. See this for complete history of C standards.

  • The C programming language is a structure oriented programming language, developed at Bell Laboratories in 1972 by Dennis Ritchie
  • C programming language features were derived from an earlier language called “B” (Basic Combined Programming Language – BCPL)
  • C language was invented for implementing UNIX operating system
  • In 1978, Dennis Ritchie and Brian Kernighan published the first edition “The C Programming Language” and commonly known as K&R C
  • In 1983, the American National Standards Institute (ANSI) established a committee to provide a modern, comprehensive definition of C. The resulting definition, the ANSI standard, or “ANSI C”, was completed late 1988.
C programming language standards:
  • C89/C90 standard – First standardized specification for C language was developed by the American National Standards Institute in 1989. C89 and C90 standards refer to the same programming language.
  • C99 standard – Next revision was published in 1999 that introduced new features like advanced data types and other changes.
C11 and Embedded C language:
  • C11 standard adds new features to C programming language and library like type generic macros, anonymous structures, improved Unicode support, atomic operations, multi-threading and bounds-checked functions. It also makes some portions of the existing C99 library optional and improves compatibility with C++.
  • Embedded C includes features not available in C like fixed-point arithmetic, named address spaces, and basic I/O hardware addressing.
  • Operating systems, C compiler and all UNIX application programs are written in C language
  • It is also called as procedure oriented programming language. The C language is reliable, simple and easy to use. C has been coded in assembly language.
Features of C programming language:
C language is one of the powerful language. Below are some of the features of C language.
  • Reliability
  • Portability
  • Flexibility
  • Interactivity
  • Modularity
  • Efficiency and Effectiveness
Uses of C programming language:
The C programming language is used for developing system applications that forms a major portion of operating systems such as Windows, UNIX and Linux. Below are some examples of C being used.
  • Database systems
  • Graphics packages
  • Word processors
  • Spreadsheets
  • Operating system development
  • Compilers and Assemblers
  • Network drivers
  • Interpreters
Which level is C language belonging to?
There are 3 levels of programming languages. They are,
  1. Middle Level languages:
    Middle level languages don’t provide all the built-in functions found in high level languages, but provides all building blocks that we need to produce the result we want. Examples: C, C++
  2. High Level languages:
    High level languages provide almost everything that the programmer might need to do as already built into the language. Example: Java, Python
  3. Low Level languages:
    Low level languages provides nothing other than access to the machines basic instruction set. Example: Assembler
C language is a structured language:
Structure oriented language:
  • In this type of language, large programs are divided into small programs called functions
  • Prime focus is on functions and procedures that operate on the data
  • Data moves freely around the systems from one function to another
  • Program structure follows “Top Down Approach”
  • Examples: C, Pascal, ALGOL and Modula-2
Object oriented language:
  • In this type of language, programs are divided into objects
  • Prime focus is in the data that is being operated and not on the functions or procedures
  • Data is hidden and cannot be accessed by external functions
  • Program structure follows “Bottom UP Approach”
  • Examples: C++, JAVA and C# (C sharp)
Non structure oriented language:
  • There is no specific structure for programming this language. Examples: BASIC, COBOL, FORTRAN
Key points to remember in C language:
  1. The C language is a structure oriented programming language developed by Dennis Ritchie.
  2. The C language is belonging to middle level programming language.
  3. Operating system programs such as Windows, Unix, Linux are written in C language.
  4. C89/C90 and C99 are two standardized editions of C language.
  5. C has been written in assembly language.
Beginning with C programming:
1)Finding a Compiler:
Before we start C programming, we need to have a compiler to compile and run our programs. There are certain online compilers like http://ideone.com/ or http://codepad.org/ that can be used to start C without installing a compiler.
Windows: There are many compilers available freely for compilation of C programs like Code Blocks  and Dev-CPP.Borland C, Turbo C etc
Linux: For Linux, gcc comes bundled with the linux,  Code Blocks can also be used with Linux.

This Blog has been written based on Linux and all the given examples have been compiled on the Ubuntu flavor of the Linux system.

If you are using Linux or UNIX, then check whether GCC is installed on your system by entering the following command from the command line −
$ gcc -v
If you have GNU compiler installed on your machine, then it should print a message as follows −
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr .......
Thread model: posix
gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)
If GCC is not installed, then you will have to install it yourself using the detailed instructions available at https://gcc.gnu.org/install/


2) Writing first program:
Following is first program in C
/* my first program */
#include <stdio.h>
int main(void)
{
printf("Hello World");
return 0;
}

Output:Hello World

Let us analyze the program line by line.
Line 0: [ /*my first program / ] In a C program, multi line comments are included between / * */. Comments are ingored by the compiler.Comments improve the readability and helps in documentation.It is a good practise to add commands at various sections of the program.Single line commands can also be added with //.
Line 1: [ #include <stdio.h> ] In a C program, all lines that start with # are processed by preprocessor which is a program invoked by the compiler. In a very basic term, preprocessor takes a C program and produces another C program. The produced program has no lines starting with #, all such lines are processed by the preprocessor. In the above example, preprocessor copies the preprocessed code of stdio.h to our file. The .h files are called header files in C. These header files generally contain declaration of functions. We need stdio.h for the function printf() used in the program.
Line 2 [ int main(void) ] There must to be starting point from where execution of compiled C program begins. In C, the execution typically begins with first line of main(). The void written in brackets indicates that the main doesn’t take any parameter . main() can be written to take parameters also. We will be covering that in future posts.
The int written before main indicates return type of main(). The value returned by main indicates status of program termination.
Line 3 and 6: [ { and } ] In C language, a pair of curly brackets define a scope and mainly used in functions and control statements like if, else, loops. All functions must start and end with curly brackets.
Line 4 [ printf(“Hello world”); ] printf() is a standard library function to print something on standard output. The semicolon at the end of printf indicates line termination. In C, semicolon is always used to indicate end of statement.
3)Compile and Execute C Program

Let us see how to save the source code in a file, and how to compile and run it. Following are the simple steps −
  • Open a text editor and add the above-mentioned code.(vi,emac,gedit)
  • Save the file as hello.c
  • Open a command prompt and go to the directory where you have saved the file.
  • Type gcc hello.c and press enter to compile your code.
  • If there are no errors in your code, the command prompt will take you to the next line and would generate a.out executable file.
  • Now, type a.out to execute your program.
  • You will see the output "Hello World" printed on the screen.
$ gcc hello.c

$ ./a.out

Hello, World!
Make sure the gcc compiler is in your path and that you are running it in the directory containing the source file hello.c.

Comments

Popular posts from this blog

Files in C

A file is a repository of data that is stored in a permanent storage media, mainly in secondary memory. In order to use the files we should learn how to read information from a file and how to write information into a file. A very important concept in C is the stream.The stream is a common logical interface to the various devices( files).A stream is linked to a file while using an open operation. A stream is disassociated from a file while using a close operation. The current location, also referred to as the current position, is the location in a file where the next fie access will occur.There are two types of streams text and binary. The following are some difference between text and binary files ·Text file is human readable because everything is stored in terms of text. In binary file everything is written in terms of 0 and 1, therefore binary file is not human readable. ·A newline(\n) character is converted into the carriage return-linefeed combination before being written to the d…

KTU C programming question paper and evaluation scheme

APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY SECOND SEMESTER B.TECH DEGREE EXAMINATION, MAY 2017 CS 100 COMPUTER PROGRAMMING (CS, IT) SCHEME OF EVALUATION
PART A 1 An identifier is a sequence of characters invented by the programmer or to identify or name a specific object. The sequence of characters may be letters, digits, and special character ‘_’known as an underscore Rules: i)Identifiers should start with alphabets. ii)Identifiers are case sensitive iii)A numeric digit should not be the first character iv)Identifier name should not be a keyword v)Identifier may be of any reasonable length 1mark






2mark 2 Associativity defines the direction, left to right or right to left in which operator act upon its operands Unary operators have associativity is from right to left. For examplein the expression &--x, pre decrement works first and then address of operator works Direction + example<

Linear and Binary Search in C

Searching is one of the most common problems that arise in computing. Searching is the algorithmic process of finding a particular item in a collection of items. A search typically answers either True or False as to whether the item is present. On occasion it may be modified to return where the item is found. Search operations are usually carried out on a key field. Consider searching for a given value k in an array A of size n. There are 2 basic approaches: sequential search and binary search.
Linear (Sequential) Search When data items are stored in a collection such as a list or array , we say that they have a linear or sequential relationship. Each data item is stored in a position relative to the others. In C array, these relative positions are the index values of the individual items. Since these index values are ordered, it is possible for us to visit them in sequence. This process gives rise to our first searching technique, the sequential search. Starting at the first item in …