Tuesday, November 07, 2017

C language Introduction

Definition of C Language

The C Language is developed for creating system applications that directly interact to the hardware devices such as drivers, kernels etc. C programming is considered as the base for other programming languages, that is why it is known as mother language.
It can be defined in the following ways:
1.      Mother language
2.      System programming language
3.      Procedure-oriented programming language
4.      Structured programming language
5.      Middle-level programming language

1) C as a mother language

            C language is considered as the mother language of all the modern languages because most of the compilers, Java virtual machine’s (JVMs), Kernels etc. are written in C language and most of the languages follow C syntax e.g. C++, Java etc. It provides the core concepts like array, functions, file handling etc. that is being used in many languages like C++, Java, C# etc.

2) C as a system programming language

            A system programming language is used to create system software. C language is a system programming language because it can be used to do low-level programming (e.g. driver and kernel). It is generally used to create hardware devices, Operating Systems, drivers, kernels etc. For example, the Linux kernel is written in C.
NOTE: It can’t be used in internet programming like Java, .net, PHP etc.

3) C as a procedural language


            A procedure is known as function, method, routine, subroutine etc. A procedural language specifies a series of steps or procedures for the program to solve the problem. A procedural language breaks the program into functions, data structures etc. C is a procedural language. In C, variables and function prototypes must be declared before being used.

4) C as a structured programming language

            A structured programming language is a subset of the procedural language. Structure means to break a program into parts or blocks so that it may be easy to understand. In C language, we break the program into parts using functions. It makes the program easier to understand and modify.

5) C as a middle-level programming language

                C is considered as a middle-level language because it supports the feature of both low-level and high-level language. C language program is converted into assembly code, supports pointer arithmetic (low level), but it is machine independent (the feature of high level). A low-level language is specific to one machine i.e. machine dependent. It is machine dependent, fast to run. But it is not easy to understand. A high-level language is not specific to one machine i.e. machine independent. It is easy to understand.

History of C Language

History of C language

History of C language is interesting to know. Here we are going to discuss brief history of C language. C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of AT&T (American Telephone & Telegraph), located in U.S.A. Dennis Ritchie is known as the founder of c language.

Language
Year
Developed By
Algol
1960
International Group
BCPL
1967
Martin Richard
B
1970
Ken Thompson
Traditional C
1972
Dennis Ritchie
K & R C
1978
Kernighan & Dennis Ritchie
ANSI C
1989
ANSI Committee
ANSI/ISO C
1990
ISO Committee
C99
1999
Standardization Committee

It was developed to overcome the problems of previous languages such as B, BCPL etc. Initially, C language was developed to be used in UNIX operating system. It inherits many features of previous languages such as B and BCPL. Let's see the programming languages that were developed before C language. 



Features of C Language

C is the widely used language. It provides a lot of features that are given below.
1.      Simple
2.      Machine Independent or Portable
3.      Middle-level programming language
4.      Structured programming language
5.      Rich Library
6.      Memory Management
7.      Fast Speed
8.      Pointers
9.      Recursion
10.  Extensible

1) Simple
            C is a simple language in the sense that it provides structured approach (to break the problem into parts), rich set of library functions, data types etc.
2) Machine Independent or Portable
            Unlike assembly language, c programs can be executed in many machines with little bit or no change. But it is not platform-independent.
3) Middle-level programming language        

            C is also used to do low level programming. It is used to develop system applications such as kernel, driver etc. It also supports the feature of high level language. That is why it is known as middle-level language.
4) Structured programming language
            C is a structured programming language in the sense that we can break the program into parts using functions. So, it is easy to understand and modify.
5) Rich Library
            C provides a lot of inbuilt functions that makes the development fast.
6) Memory Management
            It supports the feature of dynamic memory allocation. In C language, we can free the allocated memory at any time by calling the free() function.
7) Speed
            The compilation and execution time of C language is fast.
8) Pointer
            C provides the feature of pointers. We can directly interact with the memory by using the pointers. We can use pointers for memory, structures, functions, array etc.
9) Recursion
            In c, we can call the function within the function. It provides code reusability for every function.
10) Extensible
            C language is extensible because it can easily adopt new features.

Uses of C

C was initially used for system development work, in particular the programs that make-up the operating system. Mainly because it produces code that runs nearly as fast as code written in assembly language. Some examples of the use of C might be:
1.      Operating Systems
2.      Language Compilers
3.      Assemblers
4.      Text Editors
5.      Print Spoolers
6.      Network Drivers
7.      Modern Programs
8.      Data Bases
9.      Language Interpreters
10.  Utilities



            In recent years C has been used as a general-purpose language because of its popularity with programmers. It is not the world's easiest language to learn and you will certainly benefit if you are not learning C as your first programming language! C is trendy; many well established programmers are switching to C for all sorts of reasons, but mainly because of the portability that writing standard C programs can offer.

Installation of C software

There are many compilers available for C and C++. You need to download any one. Here, we are going to use Turbo C++. It will work for both C and C++. To install the Turbo C software, you need to follow following steps.
1.      Download Turbo C++
2.      Create TurboC++ directory inside C drive
3.      Double click on Application file.
4.      Click on start menu and then click on Turbo C++ 4.0 Window 7 Windows 8 64Bit Version.

1) Download Turbo C++ software
            You can download turbo C++ from many sites.
2) Create TurboC++ directory in C drive
            Now, you need to create a new directory turboc inside the C: drive.
3) Double click on application file and follow steps

            Now, click on the link located inside the C:\TurboC++
It will ask you to choose a start menu folder you can choose or click on Install.


After click on install, this application is installed on your windows system.


¾      Now C is installed, click on finish to finish the installation. 
After finishing the installation, the shortcut is generated on desktop or you can open by clicking on start menu and click on Turbo C++ 4.0 Window 7 Windows 8 64Bit Version.
4) Click on the Turbo C++ 4.0 Window 7 Windows 8 64Bit Version


In windows 7 or window 8, it will showing following console.






First C Program and Compilation and Execution of C program

Program 1.1: To write the first c program to print the “Hello C language”.

Open the C console and write the following code.

¾     Before a C program is compiled by a compiler, source code is processed by a program called preprocessor. This process is called preprocessing.
¾     Commands used in preprocessor are called preprocessor directives and they begin with “#” symbol.
¾     #include <stdio.h> includes the standard input output library functions. The printf() function is defined in stdio.h .
¾     #include <conio.h> includes the console input output library functions. The getch() function is defined in conio.h file.
¾     void main():  The main() function is the entry point of every program in c language. The void keyword specifies that it returns no value.
¾     The printf() function is used to print data on the console. The printf() function is used for output. It prints the given statement to the console.
¾     The syntax of printf() function is given below:


printf("format string",argument_list); 

¾     The format string can be %d or %i (integer), %c (character), %s (string), %f (float) etc.
¾     The semicolon (;) is a statement terminator in C to help the parser figure out where the statement ends.
¾     The getch() function asks for a single character. Until you press any key, it blocks the screen.

Compilation and Execution of C program
There are 2 ways to compile and run the c program, by menu and by shortcut.
By menu              
¾     Now click on the compile menu then compile sub menu to compile the c program.
¾     Then click on the run menu then run sub menu to run the c program.
By shortcut
¾     Or, press ctrl+f9 keys compile and run the program directly.
¾     You will see the output on user screen.

You can view the user screen any time by pressing the alt+f5 keys. Now press Esc to return to the turbo c++ console.
¾           If you run the C program many times, it will append the output in previous output. But, you can call clrscr() function to clear the screen. The clrscr() function is defined in conio.h file. So it will be better for you to call clrscr() function after the main method as given below:


#include <stdio.h>  
#include <conio.h>  
void main()
{  
   clrscr();  
   printf("Hello C Language");  
   getch();  
}  

 









Write a Program (WAP) to print the cube of the given number.


//Program

#include<stdio.h>  
#include<conio.h>  
void main()
{  
    int number;  
    clrscr();  
    printf("enter a number:");  
    scanf("%d",&number);  
    printf("cube of number is:%d ",number*number*number);  
   getch();  

}  

Output
enter a number:5
cube of number is:125

Explanation:
¾     In int number; int is the data type and number is the variable name (both discuss later).
¾     The scanf() function is used for input. It reads the input data from the console.
¾     The scanf("%d",&number) statement reads integer number from the console and stores the given value in number variable.
¾     The syntax of scanf() function is given below:

scanf("format string",argument_list)

The printf("cube of number is:%d ",number*number*number) statement prints the cube of number on the console.

Data Types in C

Data Types in C:

Types
Data Types
Basic Data Type
int, char, float, double
Derived Data Type
array, pointer, structure, union
Enumeration Data Type
Enum
Void Data Type
Void

A data type specifies the type of data that a variable can store such as integer, floating, character etc. There are 4 types of data types in C language.

1.7.1. Basic Data Types:


            The basic data types are integer-based and floating-point based. These are also called as scalar data types. C language supports both signed and unsigned literals. The memory size of basic data types may change according to 32 or 64 bit operating system. Let's see the basic data types. It size is given according to 32 bit OS.

Data Types
Memory Size
Range
Data Types

Memory       Size
Range
Char
1 byte
−128 to 127
short int
2 byte
−32,768 to 32,767
signed char
1 byte
−128 to 127
signed short int
2 byte
−32,768 to 32,767
unsigned char
1 byte
0 to 255
unsigned short int
2 byte
0 to 65,535
Short
2 byte
−32,768 to 32,767
long int
4 byte
−2147483647 to +2147483647
signed short
2 byte
−32,768 to 32,767
signed long int
4 byte
−2147483647
to +2147483647
unsigned short
2 byte
0 to 65,535
unsigned long int
4 byte
0 to 4294967294
Int
2 byte
−32,768 to 32,767
float
4 byte
1.2E-38 to 3.4E+38
signed int
2 byte
−32,768 to 32,767
double
8 byte
2.3E-308 to 1.7E+308
unsigned int
2 byte
0 to 65,535
long double
10 byte
3.4E-4932 to 1.1E+4932
Remaining data types will be studied later.