Sunday, August 10, 2014

Basics of C

History of C

C  is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. according to Ritchie, the most creative period occurred in 1972. It was named "C" because its features were derived from an earlier language called "B", which according to Ken Thompson was a stripped-down version of the BCPL programming language.
Although C was designed for implementing system software, it is also widely used for developing portable application software.
C is one of the most widely used programming languages of all time and there are very few computer architectures for which a C compiler does not exist. C has greatly influenced many other popular programming languages, most notably C++, which began as an extension to C.
The origin of C is closely tied to the development of the Unix operating system, originally implemented in assembly language on a PDP-7 by Ritchie and Thompson, incorporating several ideas from colleagues. Eventually they decided to port the operating system to a PDP-11. B's inability to take advantage of some of the PDP-11's features, notably byte addressability, led to the development of an early version of C.
The original PDP-11 version of the Unix system was developed in assembly language. By 1973, with the addition of struct types, the C language had become powerful enough that most of the Unix kernel was rewritten in C. This was one of the first operating system kernels implemented in a language other than assembly. (Earlier instances include the Multics system (written in PL/I), and MCP (Master Control Program) for the Burroughs B5000 written in ALGOL in 1961.)
In 1978, Brian Kernighan and Dennis Ritchie published the first edition of The C Programming Language.[8] This book, known to C programmers as "K&R", served for many years as an informal specification of the language. The version of C that it describes is commonly referred to as K&R C. The second edition of the book covers the later ANSI C standard

The Structure  of a C Program

All C programs will consist of at least one function, but we can write a C programs that have several functions body . The only function that has to be present is the function  main. For more advanced programs the main function will act as a controling function calling other functions in their turn to do the dirty work! The main function is the first function that is called when your program executes. So main is the starting of execution of program.
The general form of a C program is as follows :

preprocessor directives
global declarations of user functions
global variables declarations
main()
{
   local variables to function main ;
   statements associated with function main ;
}        
definitions of functions like
function1()
{
   local variables to function 1 ;
   statements associated with function 1 ;
}
function2()
{
   local variables to function f2 ;
   statements associated with function 2 ;
}
.
.
.
etc
Note the use of the bracket set () and {}: () are used in conjunction with function names whereas {} are used with  the C statements that are associated with that function and used for statements block. A semicolon (;) is used to terminate C statements. C is a free format language and long statements can be continued, without truncation, onto the next line. The semicolon informs the C compiler that the end of the statement has been reached. Free format also means that you can add as many spaces as you like to improve the look of your programs.
A very common mistake made by everyone, who is new to the C programming language, is to miss off the semicolon. The C compiler will concatenate the various lines of the program together and then tries to understand them - which it will not be able to do. The error message produced by the compiler will relate to a line of program which could be some distance from the initial mistake.
The first C Progam:

1 /* This program prints the Welcome to C Programming. */
2  #include<stdio.h>  /* header file */
3    main()
4    {
5           printf(“Welcome to C programming.\n”);
6    }               /*End of program */

Analysis:
Comments: See the lines 1,2,6 of the program
Any things between /* and */ is treated as comments in C. These are for only programmer references . Comments are escaped by C compiler during compilation of program.

Preprocessor: See in line 2.
        #include<stdio.h>
stdio.h is an input output header file which contains the input/output and file handling functions which are used in C programs. The required header file should be included using #include preprocessor directive.
main():
C programs are made of functions. main() is a special function which should be there in each c program. Each C program starts executing from main() function. As program cannot have many starting points, C program should have main function once and only once. main() may call other function to perform the given task.
Comments:
Any things between /* and */ is treated as comments in C. These are for only programmer references . Comments are escaped by C compiler during compilation of program.

Preprocessor:
        #include<stdio.h>
stdio.h is an input output header file which contains the input/output and file handling functions which are used in C programs. The required header file should be included using #include preprocessor directive.

main():
C programs are made of functions. main() is a special function which should be there in each c program. Each C program starts executing from main() function. As program cannot have many starting points, C program should have main function once and only once. main() may call other function to perform the given task.

printf():
  printf  is an output function which has been defined in stdio.h file. What ever is put inside the function printf between  two double quotes is printed on the screen

New lines: Note that printf never puts new line automatically. To put new line we require to put ‘\n’ between double quotes inside ‘ printf’ function. Eg
printf(“Welcome to ”);
printf(“C programming”);

 Out put: Welcome to C programming
Using new line:
 printf(“Welcome to\nC programming”);
 Prints the line as
Welcome to
C programming.



HackerBoy

Compiled By Sabin Khanal

0 comments:

Post a Comment

 

Copyright @ 2013 Learn Programming.

Designed by Templateiy & CollegeTalks