C is a popular programming language and widely used programming language for creating computer programs. C is a general purpose structured programming language which is powerful, efficient and compact.
It was developed at AT and T’s Bell Laboratories of USA in 1972. It was designed and written by Dennis Ritchie. It was not made the ‘Official’ Bell Labs language. Pascal is C’s pretty sister because syntax of Pascal & C language is very similar.
C was an offspring of the Basic Cambridge Programming Language (BCPL) called B language.
1. It is reliable, simple and easy to use.
2. Programs written in ‘C’ are efficient and fast. This is due to its variety of data types and powerful operators.
3. There are only 32 keywords and its strength lies in its built-in functions.
4. ‘C’ helps in developing structured programs.
5. ‘C’ is highly suitable for Recursive programming.
6. It is also suitable for Graphics programming.
7. C is highly Portable language.
8. It is highly suitable for writing both system software and business packages.
9. C is format free language. No line numbers are needed. Need not to place statements on a specified location on a line.
10. We can add our own functions to the C library. Important feature of C is its ability to extend itself.
11. Pointer implementation is available.
12. Bitwise manipulation is also possible (Means AND, OR and NOT).
13. Standard libraries for Input / Output (I/O), String manipulation, Arithmetic operations etc.
14. C stands in between the assembly language and High Level Languages (HLL). C has all the advantage of assembly language and it has all the significant features of modern High Level Languages. Then, it is also called a Middle Level Language.
15. C is a user-friendly language.
C is a popular language because –
1.
It is Reliable and
Structured Language.
2.
Simple and Small size.
3.
Easy to use and Extensive
use of function calls.
4.
Easy to Learn and User
friendly Language.
5.
Low level (Bitwise)
programming readily available.
6. Capable of pointers implementation
– extensive use of pointers for memory, array, structures and functions.
C is case sensitive language. C is also sequential language.
- It is a robust language whose rich set of built-in functions and operators can be used to write any complex program.
- Programs written in C are efficient and fast.
- Its variety of data types and powerful operators. It is many times faster than BASIC.
- C is highly portable. This means that C programs written for one computer can be run on another with little or no modification.
- Portability is important if we plan to use a new computer with a different operating system.
- C has ability to extend itself. A C program is basically a collection of functions that are supported by C library. Because of availability of a large number of functions, the programming task becomes simple.
Year
|
Language
|
Developer
|
Remarks
|
1960
|
ALGOL
|
International Committee
|
First computer language to use a block structure.
Too general, Too abstract.
It was widely used in Europe.
|
1963
|
CPL
|
Cambridge University
|
Hard to learn, difficult to implement.
|
1967
|
BCPL
|
Martin Richards at Cambridge
|
Primarily for writing system software.
Could deal with only specify problems.
Typeless System programming language.
|
1970
|
B
|
Ken Thompson at AT & T
|
Could deal with only specific problems.
It used to create early versions of UNIX O.S.
Typeless system programming language.
|
1972
|
C
|
Dennis Ritchie at AT & T
|
Lost generally of BCPL & B restored.
Added the Data types powerful features.
It was developed along with the UNIX O.S.
C was used mainly in academic environment.
C compilers for commercial use.
Also known as Traditional C language.
|
All programming is divided
two categories:
1.
High Level Language /
Problem Oriented Language.
2.
Low Level Language / Machine
Organization Language.
1. HLL (High Level Language):-
It designed to give a better programming efficiency that is faster
program development.
Examples:- FORTRAN, Basic Pascal etc.
2. LLL(Low Level Language):-
It designed to give a better machine efficiency that is faster
program execution.
Examples:- Assembly Language & Machine Language etc.
C is a middle level
language. It was designed to have both: -
Relatively good programming
efficiency.
Relatively good machine efficiency.
Relatively good machine efficiency.
#include <stdio.h> // Preprocessor Directive….
void main( ) // In-built Function…..
{
/*………. Printing begins……….. */
printf ("Welcome to C programming");
/* ……… Printing ends…………. */
}
Preprocessor Directive commands to be executed before compiling. Include statements are a form of
preprocessor directive, stdio.h is the header file which must
be included for all programs which are going to have either an input from a
file or keyboard or output to a file or monitor.
#include
<Header file name>
The main( ) is a
special function used by the C System to tell the computer where the program
starts. Every program must have exactly one main function. If we use more than one main function, the compiler
cannot understand which one marks the beginning of the program.
The empty pair( ) of
parentheses immediately following main indicates that the function main has no arguments (or parameters).
The opening brace “{“ in
the second line marks the beginning of the function main and the closing brace “}”
in the last line indicates the end of the function. All the statements between
these two braces form the function body.
The function body contains a set of instructions to perform the given task.
The lines beginning with /*
and ending with */ are known as Comment lines are not executable
statements and therefore anything between /*
and */ is ignored by the compiler.
The comment line
/* =
= = = /* = = = */ = = = = */ (Not Valid Statement and Display error)
Since comments do not affect the execution speed and the size of a
compiled program, we should use them liberally in our programs. They help the
programmers and other users in understanding the various functions and
operations of a program and serve as an aid to debugging and testing.
C uses two types of
comments:
1.
Multi-line comments (/*-----------------
----------------*/)
2.
Single line comments (//--------------------)
The printf( ) is a
predefined standard C function for printing output. Predefined Function means that it is function that has already been
written and compiled, and linked together with our program at the time of
linking. The printf( ) function
causes everything between the starting and the ending quotation marks to be
printed out.
Note: - The print line ends with a semicolon. Every statement in C should
end with semicolon
(;) mark.
The information contained between the parentheses is called the
argument of the function. “\n” means
end of the string. This combination is collectively called the newline character and goto new line.
main( ) function:-
int main( )
void main( )
The keyword void means
that the function does not return any information to the operating system and int means that the function returns an
integer value to the operating system.
A character denotes
any alphabets, digits or special symbols used to represent information.
Alphabets
|
A,B,C,…..…...X,Y,Z
|
a,b,c,……….x,y,z
|
Digits
|
0,1,2,……………………………………9
|
|
Special Symbols (31)
|
~,’,!,@,#,%,^,&,*,(,),-,+,_,=,|,\,{,},[,],:,;,”,`,<,>,,,.
,?,/
|
Constants:-
When you declare a constant, it is a bit like a variable
declaration except the value cannot be changed. This quantity can be stored at
locations in the memory of the computer. It is a quantity that does not change.
Variable:-
Every variable has a name and a value. It can be considered as a
name given to the location in memory where this constant is stored. The name
identifies the variable, while the value stores data. Every variable must start
with an alphabet.
Example:- 4x+y=19
Here 4 & 19 is cannot
change, these are Constants.
x, y can vary or change hence these are
Variables.
There are two types of Constants used in C:
- Primary Constant.
- Secondary Constant.
C supports a very large number of data types. The type of a value
establishes the following information about it:
A data type is defined as a
finite set of values along with set of rules for permissible operations. Data
in C belongs to one of following types:
Mostly used Data type:
a)
Integer (int) – A whole number.
b)
Floating point (float) – A number with a fractional part.
c)
Character (char) – A single character.
Variable names are a name
given to locations is the memory of computer where different constants are
stored. These locations can contain integer, real or character constants.
A constant stored in a memory location with an integer variable
name must be an integer constant.
OR
A variable is a data name that may be used to store a data value.
A variable may take different values at different times during the execution of
a program. Variable must be defined before they are used in a program. Some old
implementations of C only use the first 8 characters of a variable name. Most
modern ones don’t apply this limit though.
Rules of Variables:-
1. A variable name is any
combination of 1 to 8 alphabets, digits or underscores. Some compilers allow
variables names whose length could be up to 40 characters. Still, it would be
refer to stick to the rule of 8 characters.
2.
The first character in the
variable name must be an alphabet.
3.
No commas or blanks are
allowed within a variable name.
4.
No special symbols other
than an underscore can be used in a variable name.
5.
Keywords and Reserved words
are not allowed.
6.
Both uppercase and lowercase
letters are allowed.
Its meaning has already been explained to the c complier. The
keywords cannot be used as variable names because if we do so we are trying to
assign a new meaning to the keywords. C keeps a small set of keywords for its own use. These keywords
cannot be used as identifiers in the program – a common restriction with modern languages. There are 32 keywords
available in c only. Some keywords are
following: -
Every C word is classified as either a keyword or an identifier.
All keywords have fixed meanings and these meanings cannot be changed. Keywords serve as basic building blocks
for program statements. All keywords must be written in lowercase.
Identifiers refer to the names of variables, functions and arrays. These are
user-defined names and consist of a sequence of letters and digits, with a
letter as a first character. Both uppercase and lowercase letters are permitted,
although lowercase letters are commonly used. The underscore character is also
permitted in identifiers. It is usually used as a link between two words in
long identifiers.
Rules for identifiers:-
1.
First character must be an
alphabet (or underscore).
2.
Must consist of only
letters, digits or underscore.
3.
Only first 31 characters are
significant.
4.
Must not contain white
space.
Four types of
instructions are used in C.
1. Type declaration Instruction:- It is used to declare the type of
variables used in C program.
2. Input/output Instruction:- To perform the function of supplying
input data to a program & obtaining the output results from it.
3. Arithmetic Instruction:- To perform arithmetic operations
between constants & variables.
4.
Control Instruction:- To control the sequence of execution of
various statements in a c program.
1. Program written in C language is called source program. It is compiled and gets converted into object program. Compiler does the function of converting source program into object program.
2. The object program is linked. Linker does the work of linking. After linking executable program is obtained.
3. The object program has extension .c
4. The linked program has extension .obj
5. The executable program has extension .exe
6. Alt+F9 is used to compile the file and check the errors.
7. Ctrl+F9 is used to run/execute the file.
8. Alt+F5 is used to see the previous output of any program.
Example:-
// W.A.P to print the sum of
two numbers.
#include<conio.h>
void main( )
{
int a, b, c=0;
clrscr( ); // In-build function used to Clear Screen
a=10;
b=20;
c= a + b;
printf ("Sum of two numbers= ");
printf ("%d", c);
getch( ); // In-build function.
}
First two lines in program are called Header files. Library functions are grouped category-wise and
stored in different files known as header files. If we want to access the
functions stored in the library, it is necessary to tell the compiler about the
files to be accessed.
#include <
filename >
Filename is the name of the library file that contains the
required function definition. Preprocessor directives are placed at the
beginning of a program.
The words a, b, c are variable names that are used to store
numeric data. The numeric data may be either in integer form or real number
form. In C, all variables should be declared to tell the compiler what the variable names are and what type of data they hold.
int a, b, c;
a, b, c are integer (int)
variable. The int word is a keyword and cannot be used as variable names.
a=10;
b=20;
c=a + b;
These statements are called assignment statements.
printf ("%d", c);
This statement contains two arguments. The first argument "%d" tells the compiler that the value of the second argument c should be printed as a numeric
integer. Note that these arguments are separated by a comma.
The format specifier %
indicates the type of variable to be displayed as well as the format in which
that value should be displayed.
printf ( ) Statement:- printf is a standard C function – called from main
function.
printf ("<format string>", <list of variable>);
<format string>
In case of printf ( )
function, the first parameter is always a string ("Hello World") but after that you can
include as many parameter of any type that you want to. That is, the printf function is usually of the form:
printf ("Any string / Message");
printf("Any string / Message format specifier", variables);
scanf ( ) Statement:-
scanf ("<format string>", & variablename1, &
variablename2….);
scanf ( ) function is used to read the values of variables through keyboard. The
ampersand symbol (&) [used before each variable name] is an operator that specifies the
variable name’s address. We must always use this operator, otherwise unexpected
results may occur. The rule is that scanf
processes the control string from left
to right and each time it reaches a specifier, it tries to interpret what has been typed as a
value. If you input multiple values then these are assumed to be separated by
white space – i.e. spaces, newline or tabs.
scanf ("%d %d", &x, &y);
Here, it will read in two integer values into x and y. The integer
values can be typed on the same line, or on different lines, as long as there
is at least one white space character between them.
Rules Applicable to all
C Statements:-
Blank spaces may be inserted
between two words to improve the readability of the statement. No blank spaces
are allowed within a variable, constant or keyword.
All statements are entered
in small case letters.
C has no specific rules for
the position at which a statement is to be written. That’s why it is often
called a free-form language.
Any C statement always ends
with a semicolon (;).
Constants may be assigned names as well. Its format is
#define <symbolic
name> <constant value>
Symbolic name have the same form as variable names. No blank space
between the hash sign (#) and the word define is permitted.
Example:-
#define Pi 3.14
#define total 30
Note: #define statements must not
end with a semicolon. #define statement is known
as preprocessor.
Advantages
of Constant Declarations are:-
- The value of constant can be changed in the program by making only one modification. For example, the value Pi could be changed in the definition.
- The chance of error by typing in a wrong value is reduced.
- There is no need to repeat the value of same constant in every expression.
- The readability of the program is also increased.
Errors/bugs are very common while developing a program. If you
don’t detect them and correct them, they will cause a program to produce the
wrong results. There are three types of
errors – syntax, logical and run-time errors.
1.
Syntax errors: - These errors occur because
of wrongly typed statements which are not according to the syntax or
grammatical rules of the language.
Example: - printf ("Hello") //Semicolon Error…
2.
Logical errors: - These errors occur
because of logically incorrect instructions in the program.
Example: - let us assume that in a 1000 line program, instead of an
instruction to multiply two numbers the program is wrongly written to perform
addition. This logically incorrect instruction will obviously produce wrong
result. Detecting such errors is difficult.
3.
Runtime errors: - These errors occur during
the execution of the program through the program is free from syntax and
logical errors. Some of the most common reasons for these errors are:
a)
When you divide a number by
zero.
b)
When you find the logarithm
of a negative number.
c)
When you find the square
root of a negative integer.
Some Control Codes or
Escape Characters:-
i.
\f formfeed
ii.
\b backspace
iii.
\a beep sound and delay
iv.
\n new
line
v.
\r carriage
return
vi.
\t horizontal
tab
vii.
\’ single
quote
viii.
\0 null
It is a special file which contain C’s in built functions which are called Standard library function. Usually header
files have an extension.h. There are
several header files available within C Compiler.
Some header files are as follow:
1.
stdio.h:-
Its full form is Standard Input Output. Header. This header
file contains definition of constants, macros and types.
In this file printf( ), scanf( ), fprintf( ), fscanf( ), fopen( ),
fclose( ), feof( ) functions are defined.
2.
conio.h:-
Its full form is Console Input Output. Header.
In this header file getch( ), clrscr( ) functions are defined.
3.
string.h:-
This header file store string related functions.
This header file store string related functions.
In this header file strcpy( ), strcat( ), strlen( ), strrev( ),
strcmp( ) functions.
4.
math.h:-
This header file
contains mathematical functions.
In this header file sqrt( ), pow( ), floor( ), cell( ), sin( ),
cos( ) functions.
5.
stdlib.h:-
Its full form is Standard Library. Header.
It contains exit( ), malloc( ), calloc( ), toupper( ), tolower( )
functions.
6.
ctype.h:-
The ctype.h
header file defines macros and constants and declares a global array used in
character classification.
7.
time.h:-
This header file
declared the time related functions.
It contains clock( ), ctime( ), time( ) etc. functions.
0 comments:
Post a Comment