What is C : -
The C is a structured procedure programming language is a widely used programming language for creating computer programs.
This powerful computer programming language was developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories.
C programming is used basically for System programming.
C language is a middle level language.
It is a structured language.
C language is a case sensitive language.
All syntax written in c language is in lower case.
C is the basis for C++.
The statements, commands used in C are also available in C++. Thus learning C is a first step towards learning C++ which is quite similar to c with some additional features.
As you all know we always talk about ANSI C, ISO-C, C99,c88 ,etc…but we dont know the actual differences between these all version of c language. So here i am going to explained all versions of c language.
Brief History :
• Root of c language is started in 1966 when Martin Richards has designed BCPL(Basic Combined Programming Language), that was a procedural, imperative, and structured computer programming language .
• After that in 1669 Ken Thompson and Dennis Ritchie has designed B programming language at Bell Labs
K&R C :
In 1978, Brian Kernighan and Dennis Ritchie published the first edition of The C Programming Language. Which is known to C programmers as “K&R”. For most of the year this book was taken as the informal standard of c programming language.
K&R introduced several language features:
• standard I/O library
• long int data type
• unsigned int data type
• compound assignment operators
Here function declarations did not include any information about function arguments. Here function parameter type checks were also not performed.
After the publication of K&R C, some other vendors has added several unofficial features to the language, like
• void functions (i.e. functions with no return value)
• functions returning struct or union types (rather than pointers)
• assignment for struct data types
• enumerated types
So after this all, the large number of extensions and lack of agreement on a standard library, together with the language has created so many issue. So now all need to standardize the specification of c programming language.
ANSI C and ISO C (C89 and C90) (Standard c):
In 1983, the American National Standards Institute (ANSI) formed a committee, X3J11, to establish a standard specification of C. In 1989, the standard was ratified as ANSI X3.159-1989 “Programming Language C”. This version of the language is often referred to as ANSI C, Standard C, or sometimes C89.
In 1990, the ANSI C standard (with formatting changes) was adopted by the International Organization for Standardization (ISO) as ISO/IEC 9899:1990, which is sometimes called C90.
Therefore, the terms “C89″ and “C90″ refer to the same programming language.
C89 is supported by current C compilers, and most C code being written today is based on it. Any program written only in Standard C and without any hardware-dependent assumptions will run correctly on any platform with a conforming C implementation, within its resource limits.
C99 :
After the ANSI/ISO standardization process, the C language specification remained relatively static for some time. But The C standard was further revised in the late 1990s, leading to the publication of ISO/IEC 9899:1999 in 1999, which is commonly referred to as “C99″.
C99 introduced several new features, including inline functions, several new data types (including long long int and a complex type to represent complex numbers), variable-length arrays, support for variadic macros (macros of variable arity) and support for one-line comments beginning with //, as in BCPL or C++. Many of these had already been implemented as extensions in several C compilers.
You can find the detail Specification of this version C99 – ISO 9899:1999: available online
After this all in In 2007 another revised version of c programming language is come with name C1X.
in 2008 one more standard for Embedded C comes.
note : GCC, Sun Studio such compiler support all version of the c programming language. By default in gcc c89 is used you can also change the mode of gcc for different version.
Why use C?
C has been used successfully for every type of programming problem imaginable from operating systems to spreadsheets to expert systems - and efficient compilers are available for machines ranging in power from theApple Macintosh to the Cray supercomputers. The largest measure of C's success seems to be based on purely practical considerations:
1. the portability of the compiler;
2. the standard library concept;
3. a powerful and varied repertoire of operators;
4. an elegant syntax;
5. ready access to the hardware when needed;
6. and the ease with which applications can be optimised by hand-coding isolated procedures
C is often called a "Middle Level" programming language. This is not a reflection on its lack of programming power but more a reflection on its capability to access the system's low level functions. Most high-level languages (e.g. Fortran) provides everything the programmer might want to do already built into the language. A low level language (e.g. assembler) provides nothing other than access to the machines basic instruction set. A middle level language, such as C, probably doesn't supply all the constructs found in high-languages - but it provides you with all the building blocks that you will need to produce the results you want!
Uses of C:
C was initially used for system development work, in particular the programs that make-up the operating system. Why use C? 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 (I nearly said sexy) - 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.
The Edit-Compile-Link-Execute Process
Developing a program in a compiled language such as C requires at least four steps:
1. editing (or writing) the program
2. compiling it
3. linking it
4. executing it
We will now cover each step separately.
Editing
You write a computer program with words and symbols that are understandable to human beings. This is the editing part of the development cycle. You type the program directly into a window on the screen and save the resulting text as a separate file. This is often referred to as the source file (you can read it with theTYPE command in DOS or the cat command in unix). The custom is that the text of a C program is stored in a file with the extension .c for C programming language
Compiling
You cannot directly execute the source file. To run on any computer system, the source file must be translated into binary numbers understandable to the computer's Central Processing Unit (for example, the 80*87 microprocessor). This process produces an intermediate object file - with the extension .obj, the .obj stands for Object.
Linking
The first question that comes to most peoples minds is Why is linking necessary?The main reason is that many compiled languages come with library routines which can be added to your program. Theses routines are written by the manufacturer of the compiler to perform a variety of tasks, from input/output to complicated mathematical functions. In the case of C the standard input and output functions are contained in a library (stdio.h) so even the most basic program will require a library function. After linking the file extension is .exe which are executable files.
Executable files
Thus the text editor produces .c source files, which go to the compiler, which produces .obj object files, which go to the linker, which produces .exe executable file. You can then run .exe files as you can other applications, simply by typing their names at the DOS prompt or run using windows menu.
Add Comments to a Program
A comment is a note to yourself (or others) that you put into your source code. All comments are ignored by the compiler. They exist solely for your benefit. Comments are used primarily to document the meaning and purpose of your source code, so that you can remember later how it functions and how to use it. You can also use a comment to temporarily remove a line of code. Simply surround the line(s) with the comment symbols.
In C, the start of a comment is signalled by the /* character pair. A comment is ended by */. For example, this is a syntactically correct C comment:
/* This is a comment. */
Comments can extend over several lines and can go anywhere except in the middle of any C keyword, function name or variable name. In C you can't have one comment within another comment. That is comments may not be nested. Lets now look at our first program one last time but this time with comments:
main() /* main function heading */
{
printf("\n Hello, World! \n"); /* Display message on */
} /* the screen */
This program is not large enough to warrant comment statements but the principle is still the same.
Keywords And Identifiers
Character Set:
Character Set Are The Set Of Alphabets, Letters And Some Special Characters That Are Valid In C Language.
Alphabets:
Uppercase: A B C .................................... X Y Z
Lowercase: A B C ...................................... X Y Z
Digits:
0 1 2 3 4 5 6 8 9
Functions:F1-F12
F1=Help
F2=Save
F3=Open Files
F10=File Menu
Spl Characters:
~=Tilde
`=Apostrophe
!=Factorial
@=At The Rate Off
#=Hash
$=Dollar
%=Remainder
^=Caret(Power) Ex:2^3=2*2*2=8
&=Ampersand
*=Asterisk
( )=Paranthesis
{ }=Braces
[ ]=Curly Brackets
.=Dot/End Point
: =Colons
;=Semi Colons
“=Double Quotations’
‘=Single Quotations’
/=By/Ablick/Back Slash
\ =Foreword Slash
Graphical Keys:
Esc:To Cancel The Applications
Tab:Goto Next Option
Caps Lock:On/Off Button Of The Upper Case Letters
Num Lock: On/Off Button Of The Numerical Keypad
( Back Space):To Delete The Left Side Characters
Del/Delete: To Delete The Right Side Characters
Home:The Cursor Goes To First Position Of The Line
End: The Cursor Goes To End Position Of The Line
Print Screen:To Print /Get The Screen As A Image(Photo)
Navigation Keys(Arrow Keys:
Moving The Cursor From One Place To Other Place
Alt+Shift+Ctrl=Supporting Keys
Ex:99/25=3,99%25=24 25)99(3(Quotient) (/)
75
24(Remainder)(%)
White Space Characters:
Blank Space, New Line, Horizontal Tab, Carriage Return And Form Feed
Constants
The term constant means that it does not change during the execution of program.In the language C, constant and is the data with a constant value that does not change in the program. For example, in the program "100" "3.14" "'A'" "" Hello "" and the like, if you write data directly, and constant. Moreover, also called a literal constant. Constant expression is an expression consisting only of constants. There are four basic types of constants in C. They are:
1.Integer constants
2.Floating-point constants
3.Character constants
4.String constants
Integer and floating-point constants represent numbers. They are often referred to as numeric-type constants. The following rule applies to all numeric type constants:
• Comma and blank spaces cannot be included within the constants.
• Constants can be preceded by a – or + sign, if desired. If either sign does not precede the constant it is assumed to be positive.
• The value of a constant cannot exceed specified minimum and maximum bounds. For each type of constant, these bound vary from one C compiler to another.
Variables
Variables are means for location in memory used by a program to store data. The size of that block depends upon the range over which the variable is allowed to vary.
For example, on personal computer the size of an integer variable is two bytes, and that of a long integer is four bytes.
A variable region is temporarily remember a number or string value, such as covered by the program. To identify the variables, you have a name unique to every single variable. This is called a variable name. Before using a variable, use variables to what is called a variable declaration that you have to reveal the names and data types that can be stored in the variable variable.
The format for declaring a variable in C.
[Storage-class] type data variable name [= initial value];
Storage class and the initial value can be omitted.
The same data type and storage class variable can be declared, separated by commas.
[Storage-class] type data variable name [= initial value] variable [= initial value] variable [= initial value];
In C the size of a variable type such as an integer need not be the same on all types of machines. When we declare a variable we inform the compiler of two things, the name of the variable and the type of the variable. For example, we declare a variable of type character with the name i by writing:
char i;
On seeing the "char" part of this statement the compiler sets aside one bytes of memory to hold the value of the character. It also sets up a symbol table. In that table it adds the symbol i and the relative address in memory where those one byte was set aside. Thus, later if we write: i = 'x'; we expect that,at run time when this statement is executed, the value 'x' will be placed in that memory location reserved for the storage of the value of i.
Following are the rules for naming the variables:
1. All variables must be declared before they can appear in executable statement.
2. A declaration consists of a data type followed by one or more variable names separated by commas.
Example: int a,b,c;
3. Variables can be distributed among declarations in any fashion. The above declaration can be written as
int a;
int b,c;
4. Integer type variables can be declared to be short integer for smaller integer quantities or long integer for larger integer quantities.
Example:
short int a,b,c;
long int a,b,c;
5. An integer variable can also be declared to be un signed by writing unsigned int.
Example: unsigned int;
Keywords:
Keywords Are The Reserved Words Used In Programming. Each Keywords Has Fixed Meaning And That Cannot Be Changed By User. For Example:
Int Money;
Here, Int Is A Keyword That Indicates, 'Money' Is Of Type Integer.
As, C Programming Is Case Sensitive, All Keywords Must Be Written In Lowercase. Here Is The List Of All Keywordspredefined By Ascii C.
Keywords In C Language(32)
Auto Double Int Struct
Break Else Long Switch
Case Enum Register Typedef
Char Extern Return Union
Continue For Signed Void
Do If Static While
Default Goto Sizeof Volatile
Const Float Short Unsigned
Besides These Keywords, There Are Some Additional Keywords Supported By Turbo C.
Additional Keywords For Borland C
Asm Far Interrupt Pascal Near Huge Cdecl
All These Keywords, Their Syntax And Application Will Be Discussed In Their Respective Topics. However, If You Want Brief Information About These Keywords Without Going Further Visit Page: List Of All C Keywords.
Identifiers
In C Programming, Identifiers Are Names Given To C Entities, Such As Variables, Functions, Structures Etc. Identifier Are Created To Give Unique Name To C Entities To Identify It During The Execution Of Program. For Example:
Int Money;
Int Mango_Tree;
Here, Money Is A Identifier Which Denotes A Variable Of Type Integer. Similarly, Mango_Tree Is Another Identifier, Which Denotes Another Variable Of Type Integer.
Rules For Writing Identifier:
1. An Identifier Can Be Composed Of Letters (Both Uppercase And Lowercase Letters), Digits And Underscore '_' Only.
2. The First Letter Of Identifier Should Be Either A Letter Or An Underscore. But, It Is Discouraged To Start An Identifier Name With An Underscore Though It Is Legal. It Is Because, Identifier That Starts With Underscore Can Conflict With System Names. In Such Cases, Compiler Will Complain About It. Some System Names That Start With Underscore Are _Fileno, _Iob, _Wfopen Etc.
3. There Is No Rule For The Length Of An Identifier. However, The First 31 Characters Of An Identifier Are Discriminated By The Compiler. So, The First 31 Letters Of Two Identifiers In A Program Should Be Different.
Tips For Good Programming Practice:
Programmer Can Choose The Name Of Identifier Whatever They Want. However, If The Programmer Choose Meaningful Name For An Identifier, It Will Be Easy To Understand And Work On, Particularly In Case Of Large Program.
C Programming Variables And Constants
Variables
Variables Are Memory Location In Computer's Memory To Store Data. To Indicate The Memory Location, Each Variable Should Be Given A Unique Name Called Identifier. Variable Names Are Just The Symbolic Representation Of A Memory Location. Examples Of Variable Name: Sum, Car_No, Count Etc.
Int Num;
Here, Num Is A Variable Of Integer Type.
Rules For Writing Variable Name In C
1. Variable Name Can Be Composed Of Letters (Both Uppercase And Lowercase Letters), Digits And Underscore '_' Only.
2. The First Letter Of A Variable Should Be Either A Letter Or An Underscore. But, It Is Discouraged To Start Variable Name With An Underscore Though It Is Legal. It Is Because, Variable Name That Starts With Underscore Can Conflict With System Names And Compiler May Complain.
3. There Is No Rule For The Length Of Length Of A Variable. However, The First 31 Characters Of A Variable Are Discriminated By The Compiler. So, The First 31 Letters Of Two Variables In A Program Should Be Different.
In C Programming, You Have To Declare Variable Before Using It In The Program.
Constants
Constants Are The Terms That Can't Be Changed During The Execution Of A Program. For Example: 1, 2.5, "Programming Is Easy." Etc. In C, Constants Can Be Classified As:
Integer Constants
Integer Constants Are The Numeric Constants(Constant Associated With Number) Without Any Fractional Part Or Exponential Part. There Are Three Types Of Integer Constants In C Language: Decimal Constant(Base 10), Octal Constant(Base 8) And Hexadecimal Constant(Base 16) .
Decimal Digits: 0 1 2 3 4 5 6 7 8 9
Octal Digits: 0 1 2 3 4 5 6 7
Hexadecimal Digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F.
For Example:
Decimal Constants: 0, -9, 22 Etc
Octal Constants: 021, 077, 033 Etc
Hexadecimal Constants: 0x7f, 0x2a, 0x521 Etc
Notes:
1. You Can Use Small Caps A, B, C, D, E, F Instead Of Uppercase Letters While Writing A Hexadecimal Constant.
2. Every Octal Constant Starts With 0 And Hexadecimal Constant Starts With 0x In C Programming.
Floating-Point Constants
Floating Point Constants Are The Numeric Constants That Has Either Fractional Form Or Exponent Form. For Example:
-2.0
0.0000234
-0.22e-5
Note:Here, E-5 Represents 10-5. Thus, -0.22e-5 = -0.0000022.
Character Constants
Character Constants Are The Constant Which Use Single Quotation Around Characters. For Example: 'A', 'L', 'M', 'F' Etc.
Escape Sequences:
Sometimes, It Is Necessary To Use Newline(Enter), Tab, Quotation Mark Etc. In The Program Which Either Cannot Be Typed Or Has Special Meaning In C Programming. In Such Cases, Escape Sequence Are Used. For Example: \N Is Used For Newline. The Backslash( \ ) Causes "Escape" From The Normal Way The Characters Are Interpreted By The Compiler.
Escape Sequences
Escape Sequences Character meaning
\B Backspace
\F Form Feed
\N Newline
\R Return
\T Horizontal Tab
\V Vertical Tab
\\ Backslash
\' Single Quotation Mark
\" Double Quotation Mark
\? Question Mark
\0 Null Character
String Constants
String Constants Are The Constants Which Are Enclosed In A Pair Of Double-Quote Marks. For Example:
"Good" //String Constant
"" //Null String Constant
" " //String Constant Of Six White Space
"X" //String Constant Having Single Character.
"Earth Is Round\N" //Prints String With Newline
Enumeration Constants
Keyword Enum Is Used To Declare Enumeration Types. For Example:
Enum Color {Yellow, Green, Black, White};
Here, The Variable Name Is Color And Yellow, Green, Black And White Are The Enumeration Constants Having Value 0, 1, 2 And 3 Respectively By Default. For More Information About Enumeration, Visit Page: Enumeration Types.
C Programming Data Types
In C, Variable(Data) Should Be Declared Before It Can Be Used In Program. Data Types Are The Keywords, Which Are Used For Assigning A Type To A Variable.
Data Types In C
1. Fundamental Data Types(Basic Or Primary)
• Integer Types
• Floating Type
• Character Types
Important notice: in c there is no direct string data type, declare the string by using char, read (display) that sting by using format code is %s.
2. Derived Data Types
• Arrays
• Pointers
• Structures
• Enumeration
3. Empty Data Types:
• Void
4. User Defined Date Types;
• Typedef
• Enum
Syntax For Declaration Of A Variable
Data_Type Variable_Name;
Integer Data Types
Keyword Int Is Used For Declaring The Variable With Integer Type. For Example:
Int Var1;
Here, Var1 Is A Variable Of Type Integer.
The Size Of Int Is Either 2 Bytes(In Older Pc's) Or 4 Bytes. If You Consider An Integer Having Size Of 4 Byte( Equal To 32 Bits), It Can Take 232 Distinct States As: -231,-231+1, ...,-2, -1, 0, 1, 2, ..., 231-2, 231-1
Similarly, Int Of 2 Bytes, It Can Take 216 Distinct States From -215 To 215-1. If You Try To Store Larger Number Than 231-1, I.E,+2147483647 And Smaller Number Than -231, I.E, -2147483648, Program Will Not Run Correctly.
Floating Types
Variables Of Floating Types Can Hold Real Values(Numbers) Such As: 2.34, -9.382 Etc. Keywords Either Float Or Double Is Used For Declaring Floating Type Variable. For Example:
Float Var2;
Double Var3;
Here, Both Var2 And Var3 Are Floating Type Variables.
In C, Floating Values Can Be Represented In Exponential Form As Well. For Example:
Float Var3=22.442e2
Difference Between Float And Double
Generally The Size Of Float(Single Precision Float Data Type) Is 4 Bytes And That Of Double(Double Precision Float Data Type) Is 8 Bytes. Floating Point Variables Has A Precision Of 6 Digits Whereas The The Precision Of Double Is 14 Digits.
Note: Precision Describes The Number Of Significant Decimal Places That A Floating Values Carries.
Character Types
Keyword Char Is Used For Declaring The Variable Of Character Type. For Example:
Char Var4='H';
Here, Var4 Is A Variable Of Type Character Which Is Storing A Character 'H'.
The Size Of Char Is 1 Byte. The Character Data Type Consists Of Ascii Characters. Each Character Is Given A Specific Value. For Example:
For, 'A', Value =97
For, 'B', Value=98
For, 'A', Value=65
For, '&', Value=33
For, '2', Value=49
Here Is The List Of All Ascii Characters In C Language.
Qualifiers
Qualifiers Alters The Meaning Of Base Data Types To Yield A New Data Type.
Size Qualifiers:
Size Qualifiers Alters The Size Of Basic Data Type. The Keywords Long And Short Are Two Size Qualifiers. For Example:
Long Int I;
The Size Of Int Is Either 2 Bytes Or 4 Bytes But, When Long Keyword Is Used, That Variable Will Be Either 4 Bytes Of 8 Bytes. Learn More About Long Keyword In C Programming. If The Larger Size Of Variable Is Not Needed Then, Short Keyword Can Be Used In Similar Manner As Long Keyword.
Sign Qualifiers:
Whether A Variable Can Hold Only Positive Value Or Both Values Is Specified By Sign Qualifiers. Keywords Signed And Unsigned Are Used For Sign Qualifiers.
Unsigned Int A;
// Unsigned Variable Can Hold Zero And Positive Values Only
It Is Not Necessary To Define Variable Using Keyword Signed Because, A Variable Is Signed By Default. Sign Qualifiers Can Be Applied To Only Int And Char Data Types. For A Int Variable Of Size 4 Bytes It Can Hold Data From -231 To 231-1 But, If That Variable Is Defined Unsigned, It Can Hold Data From 0 To 232 -1.
Constant Qualifiers
Constant Qualifiers Can Be Declared With Keyword Const. An Object Declared By Const Cannot Be Modified.
Const Int P=20;
The Value Of P Cannot Be Changed In The Program.
Volatile Qualifiers:
A Variable Should Be Declared Volatile Whenever Its Value Can Be Changed By Some External Sources Outside Program. Keyword Volatile Is Used To Indicate Volatile Variable.
No comments:
Post a Comment