Translate

Labels

Showing posts with label ABOUT C. Show all posts
Showing posts with label ABOUT C. Show all posts

Friday, 28 December 2012

FUTURE OF C LANGUAGE


Although the C may be a base for successful object oriented extensions like C++ and Java, C still continues to remain and be used with same qualities as ever. C is still a preferable language to write short efficient low-level code that interacts with the hardware and OS. The analogy may be the following one.

The old C programmers sometimes used assembly language for doing jobs that are tedious or not 

possible to do in C. In future, the programmers in other programming languages may do the same.


 They will write the code in their favorite language and for low-level routines and efficiency they


 will code in C using it as an assembly language. 

ANSI C STANDARD


                Although K& R C had a rich set of features it was the initial version and C had a lot to grow. The [Kernighan and Ritchie 1978] was the reference manual for both the programmers and compiler writers for almost a decade. Since it is not meant for compiler writers, it left lot of ambiguity in its interpretation and many of the constructs were not clear. One such example is the list of library functions. Nothing significant is said about the header files in the [Kernighan and Ritchie 1978] and so each implementation had their own set of library functions. The compiler vendors had different interpretations and added more features (language extensions) of their own. This created many inconsistencies between the programs written for various compilers and lot of portability and efficiency problems cropped up.
To overcome the problem of inconsistency and standardize the available language features ANSI formed a committee called X3J11. Its primary aim was to make “an unambiguous and machine-independent definition of C” while still retaining the spirit of C. The committee made a research and submitted a document and that was the birth of ANSI C standard. Soon the ISO committee adopted the same standard with very little modifications and so it became an international standard. It came to be called as ANSI/ISO C standard or more popularly as just ANSI C standard.
                Even experienced C programmers also doesn’t know much about ANSI standard except what they frequently read or hear about what the standard says. When they get curious enough to go through the ANSI C document, they stumble a little to understand the document. The document is hard to understand by the programmers because it is meant for compiler writers and vendors ensures accuracy and describes the C language precisely. So the language used in the document is jocularly called as ‘standardese’. For example to describe side effects, the standard uses the idea of ‘sequence-points’ that may help confusing the reader more. L-value is not simply the ‘LHS (to =) value’. It is more properly a "locator value" designating an object.
            ANSI standard is not a panacea for all problems. To give an example, ANSI C widened the difference between the C used as a ‘high-level language’ and as ‘portable assembly language’. The original [Kernighan and Ritchie 1978] is more preferred even now by the various language compilers to generate C as their target language. Because it is less-typed than ANSI C. To give another example, many think ‘sequence-points’ fully describe side-effects and the belief that knowing its mechanism will help to fully understand side-effects. This is a false notion about sequence-points of [ANSI C 1989]. Sequence points doesn’t help fully understand side-effects.

A BRIEF HISTORY OF C


C language is the member of ALGOL-60 based languages. As I have already said, C is neither a language that is designed from scratch nor had perfect design and contained many flaws.
CPL (Combined programming language) was a language designed but never implemented. Later BCPL (Basic CPL) came as the implementation language for CPL by Martin Richards. It was refined to language named as B by Ken Thompson in 1970 for the DEC PDP-7. It was written for implementing UNIX system. Later Dennis M. Ritche added types to the language and made changes to B to create the language what we have as C language.

C derives a lot from both BCPL and B languages and was for use with UNIX on DEC PDP-11 computers. The array and pointer constructs come from these two languages. Nearly all of the operators in B is supported in C. Both BCPL and B were type-less languages. The major modification in C was addition of types. [Ritchie 1978] says that the major advance of C over the languages B and BCPL was its typing structure. “The type-less nature of B and BCPL had seemed to promise a great simplification in the implementation, understanding and use of these languages… (but) it seemed inappropriate, for purely technological reasons, to the available hardware”. It derives some ideas from Algol-68 also.

Thursday, 20 December 2012

INTRODUCTION


Dennis ritchie at AT & T bell laboratories developed the C language three decades ago.C language is the general purpose language .it is efficient, flexible and portable language.c has wide diversity of operators and commands.c has been effectively utilized for the development of system software like, operating systems, compilers, text processor, and database management systems.C is well suited for developing applications programs too.c language is composed of the following basic types of elements:

  •  constants,
  • identifiers,
  • operators,
  • punctuation and
  •  keywords.


A SAMPLE C PROGRAM:

c programs are made up of functions.A program cannot be written without function.A function may be pre-defined or user-defined.The C program code is given below:

#include<stdio.h>   /*preprocessor statement*/
main()                       /*function header  statement*/ 
{
printf("hello");        /*function call statement*/   
}

here, main() is calling function and printf() is the called function.
After the execution the program will display the message"hello".main() function is the user defined one.When the C program runs the control is transferred to this function.
This is called as the program's entry point.The program has two function, one is the user-defined which is main()function and the other one is pre-defined which is printf()function
The first line in the program #include<stdio.h> is a preprocessor statement.#include is a preprocessor directive .Preprocessor is a software program that will expand the source code
while it is compiled.Stdio.h (standard input and output header file, the content of the pre-defined output and input function like printf() and scanf() etc..., 
The C compiler is able to recognize the pre-defined functions only because of their  declaration in the appropriate header files.

Statements
Each and every line of a C program can be considered as a statement. There are four types of 
statements.They are 

  • preprocessor statement,
  • function header statement,
  • declaration statement,
  • executable statement.


#include<stdio.h> -> preprocessor statement
main()                       ->function header statement 
{
int a,b,c;                   ->variable declaration statement 
int sub(int,int);       ->function declaration statement     
a=17;                         ->executable statement
}  

Input and output statements
As we have seen already,the function printf() is used to display the results on the standard 
output(screen).Actually,the first parameter of the printf() function is a string which is used to control the output and hence it can be called as"control string".This parameter is used to format the output for display and hence we also call  it as a "formatting string".
for example:
To print a value of integer datatype
int x; /*variable xis declared as integer*/
x=17;
printf("%d",x);
 In the above example,'%d' is used as a formatting character within the control string of printf() function to display the value of an integer.The control string of printf() function can take three types of characters.They are,

  • ordinary characters,
  • formatting characters,
  • escape sequence characters.


In case of printf("hello"); statement, the control string has ordinary characters only

FORMATTING SPECIFICATIONS
FORMATTING CHARACTERS DATA TYPE
%d                                 Int
%f                                   Float
%c                                 Char
%s                                 Char[]
%ld                                 Long int
%lf                                 Long float
ESCAPE SEQUENCE CHARACTERS

ESCAPE SEQUENCE NON-GRAPHIC CHARACTER
\a                 Bell
\b                 Back space
\n                 New line/ line feed
\t                 Horizontal tab
\v                 Vertical tab
\\                 Back slash
\’ or \”         Single/ double quotes
\o                 Octal number
\x                 Hexa decimal number
\0                 Null
\f                 Form feed
\r                 Carriage return


Input (scanf())
To read a value from the keyboard(standard input),the function scanf() is used.The protype of scanf() is similar to the protype of the printf().It can take the variable number of the parameter.To read a value of an integer variable is given below:

int x;
scanf("%d",&x);

while the scanf() function is being executed,the system waits for the user's input.The user has to provide data through keyboard .The data will be placed in the location of x only after the "enter " key is pressed on the keyboard The second parameter of the above scanf() function &x,which represents the address of x.& is the address of operator and when it is being used with a variable,it provides the address of the variable.