INTRODUCTION TO ‘C’ LANGUAGE
What is C language?
The
C programming language is a standardized programming language developed in the
early 1970s by Ken Thompson and Dennis Ritchie for use on the UNIX operating
system. It has since spread to many other operating systems, and is one of the
most widely used programming languages. C is prized for its efficiency, and is
the most popular programming language for writing system software, though it is
also used for writing applications.
Important Features
of C Language
i.
C is a system programming language which provides
flexibility for writing compilers, operating systems, etc.
ii.
ii. It can
also be used for writing the application programs for scientific, engineering
and business applications.
iii.
iii. C is famous for its portability, meaning that
program written in C for one computer can be easily loaded to another computer
with little or no changes.
iv.
iv. C supports variety of data types like integers,
float point numbers, characters, etc. v. C is a procedure oriented language
which is most suited for structured programming practice.
v.
vi. It
provides a rich set of built in functions
Why use C?
C (and its object
oriented version, C++) is one of the most widely used third generation
programming languages. Its power and flexibility ensure it is still the leading
choice for almost all areas of application, especially in the software
development environment.
Many applications are written in C or C++, including the compilers for other programming
languages. It is the language many operating systems are written in including
Unix, DOS and Windows. It continues to adapt to new uses, the latest being Java,
which is used for programming Internet applications.
C has many strengths, it is flexible and portable, it can
produce fast, compact code, it provides
the programmer with objects to create and manipulate complex structures (e.g
classes in C++) and low level routines to control hardware (e.g input and
output ports and operating system interrupts).
It is also one of the few languages to have an international standard,
ANSI C.
An Example C Program
/* This program prints a one-line message */
#include <stdio.h>
int main() { printf("Hello World\n");
return 0; }
/* This program ... */ The symbols /* and */ delimit a comment. Comments are
ignored by the compiler1, and are used to provide useful information for humans
that will read the program.
main()
C programs consist of one or more functions. One and only one of these
functions must be called main. The brackets following the word main indicate
that it is a function and not a variable.
{ } braces
surround the body of the function, which consists of one or more instructions
(statements).
printf()
is a library function that is used to print on the standard output stream
(usually the screen).
"Hello
World\n" is a string
constant.
\n is
the newline character.
; a
semicolon terminates a statement.
return 0; return the value zero to the operating system.
C Character Set
C character set
Every language has its own character set. The character set of the C language consists of basic symbols of the language. A character indicates any English alphabet, digit or special symbol including arithmetic operators. The C language character set includes
1. Letter, Uppercase A ….. Z, Lower case a….z
2. Digits, Decimal digits 0….9.
3. Special Characters, such as comma, period. semicolon;
colon: question mark?, apostrophe‘
quotation mark “ Exclamation mark
! vertical bar | slash / backslash \ tilde ~ underscore _ dollar $ percent %
hash # ampersand & caret ^ asterisk * minus – plus + <, >, (, ), [,],
{, }
4. White spaces such as blank space, horizontal tab, carriage
return, new line and form feed
As
every language contains a set of characters used to construct words,
statements, etc., C language also has a set of characters which include alphabets,
digits, and special
symbols. C language supports a total of
256 characters.
Every C program contains statements. These statements are constructed using
words and these words are constructed using characters from C character set. C
language character set contains the following set of characters...
- Alphabets
- Digits
- Special
Symbols
4. Alphabets
5. C language supports all the alphabets from the
English language. Lower and upper case letters together support 52 alphabets.
6. lower case letters - a to z
7. UPPER CASE LETTERS - A to Z
8. Digits
9. C language supports 10 digits which are used
to construct numerical values in C language.
10. Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8,
9
11. Special Symbols
12. C language supports a rich set of special
symbols that include symbols to perform mathematical operations, to check
conditions, white spaces, backspaces, and other special symbols.
13. Special Symbols - ~ @
# $ % ^ & * ( ) _ - + = { } [ ] ; : ' " / ? . > , < \ | tab
newline space NULL bell backspace verticaltab etc.,
14. Every character in C language has its
equivalent ASCII (American Standard Code for Information Interchange) value.
15.
Commonly used characters in C with thier
ASCII values
C program to print all the characters of
C character Set
#include<stdio.h>
#include<conio.h>
int main() {
int i;
clrscr();
printf("ASCII
==> Character\n");
for(i = -128; i <= 127; i++)
printf("%d ==>
%c\n", i, i);
getch();
return 0;
}
Variable and Identifiers
Variable
A variable is an identifier that may be used to store data
value. A value or a quantity which may vary during the program execution can be
called as a variable. Each variable has a specific memory location in memory
unit, where numerical values or characters can be stored. A variable is
represented by a symbolic name. Thus variable name refers to the location of
the memory in which a particular data can be stored. Variables names are also
called as identifiers since they identify the varying quantities.
Variable Declaration:-
Refer to
the part where a variable is first declared or introduced before its first .
Variables should be declared either outside a function or at
the start of a block of code, after the opening { and before any other
statements.
Int
miles, yards; /*
global variables */
main()
{
float kilometres; /* local variables */
For Ex : sum = a+b. In this equation sum, a and b are the
identifiers or variable names representing the numbers stored in the memory
locations.
Rules to be
followed for constructing the Variable names(identifiers)
1. They must
begin with a letter and underscore is considered as a letter.
2. It must consist of single letter
or sequence of letters, digits or underscore character.
3. Uppercase and lowercase are
significant. For ex: Sum, SUM and sum are three distinct variables.
4. Keywords are not allowed in
variable names.
5. Special characters except the
underscore are not allowed.
6. White space is also not allowed.
Variable Definition :- is the part where the variable is
assigned a memory location and a value.
How to Define Variable in C ?
syntax : datatype variable_name
Example : int a;
Variable Types There are a number
of ‘built-in’ data types in C. These are listed below. Where a shorter version
of the type name exists, this is given in brackets; essentially the base type
int is implicit whenever short, long, or unsigned are used.
short int (short)
unsigned short int (unsigned short )
char
unsigned char
signed char
int
unsigned int (unsigned)
long int (long)
unsigned long int (unsigned long )
float
double
long double.
The range of values that can be
stored in variables of these types will depend on the compiler and computer
that you are using, but on an IBM PCs and the Borland Turbo C compiler the
ranges are:
short int -128 → 127 (1 byte)
unsigned short int 0 → 255 (1 byte)
char 0 → 255 or -128 → +127
2 (1 byte)
unsigned char
0 → 255 (1 byte)
signed char
-128 → 127 (1 byte)
int -32,768
→ +32,767 (2 bytes)
unsigned int 0 → +65,535 (2 bytes)
long int
-2,147,483,648 → +2,147,483,647 (4 bytes)
unsigned long int 0 → 4,294,967,295 (4
bytes)
float single precision
floating point (4 bytes)
double double precision floating
point (8 bytes)
long double extended precision floating point
(10 bytes)
Identifiers in C
The
identifiers in C is the name given to variable, constant or function. There are
some rules that should be kept in mind while naming the identifiers:
·
An identifier can have alphanumeric characters
and underscore (i.e. A-Z, a-z, 0-9, _).
·
The first letter of an identifier can be an alphabet or
underscore. Identifiers can’t start with numbers.
·
Name of an identifier can’t be a keyword i.e. int,
float, double, goto, etc.
·
Identifiers are case sensitive. So, var and VAR are different.
·
Identifier names cannot contain any special character
Note:
You can start the name of an identifier with _(Underscore) but for best
practices, please avoid as it confuses the compiler with system-defined names.
C Identifiers
In C programming
language, programmers can specify their name to a variable, array, pointer,
function, etc... An identifier is a collection of characters which acts as the
name of variable, function, array, pointer, structure, etc... In other words,
an identifier can be defined as the user-defined name to identify an entity
uniquely in the c programming language that name may be of the variable name,
function name, array name, pointer name, structure name or a label.
The identifier is a user-defined name of an entity to identify
it uniquely during the program execution
Example
int
marks;
char studentName[30];
Here, marks and studentName are identifiers.
Rules for Creating Identifiers
1. An identifier can contain letters (UPPERCASE and lowercase), numerics & underscore symbol only.
2. An identifier should not start with a
numerical value. It can start with a letter or an underscore.
3. We should not use any special symbols in
between the identifier even whitespace. However, the only underscore symbol is
allowed.
4. Keywords should not be used as identifiers.
5. There is no limit for the length of an
identifier. However, the compiler considers the first 31 characters only.
6. An identifier must be unique in its scope.
No comments:
Post a Comment