Before we delve into the sheer delicacies of C, here are
some important facts which we should know about.
1. Dennis Ritchie invented C programming
Language in the year 1972. Before C saw the first light, there were
couple of programming languages available in that time such as BCPL (Basic
Combined Programming Language) & B. Ritchie inherited features of them and
added some of his own and developed C .
2. Now one thing we must know is Programming Language (PL)
Classification.
Mainly PLs are classified into 4 kinds.
A. Machine Language:
Which is made of only 1s (one) and 0s (zero). Computer can understand only
machine language. YES! terms like "Java","C", "C++", "VC++" all are alien to a computer unless they are converted to the language a computer really understands. Once
upon a time, when a computer programmer had to write his instruction in machine
language. A machine language code may look like this:
00011
10100011101001 100110001
But the fact about it is that, it was
damn tough to code.
Detecting and correcting errors
were extremely hard. So scientists went for a new invention of languages and
that is Assembly Language.
B. Assembly Language:
It made the programmers feel relieved
‘cause it was much easier in sense of
everything. It was easy to code, remember, detecting & correcting
errors was a great easy. Assembly Language is little English-like and it had some
small functions (Pnemonics) which were
easy to remember than machine code 001010 etc.
For example:
MOV AX,X
ADD AX,Y
Notice, here MOV & ADD are Pnemonics, which have inner meanings. MOV instructs the computer to
move a variable’s(X) value to another place in CPU (AX). ADD tells it to add
one variable Y with that AX. But we’ll agree that it’s much readable &
approachable than machine language.
Note: Machine Language & Assembly Language both
are very much machine dependent, that means any certain program has to be
different for different machines. Suppose we’ve written a program in
Machine Language and checked it on a computer and found it running well. But
the same code may not run on our friend’s machine. This shows how ancient
programmers had to struggle to get their programs running on different
machines. Today’s Windows98/XP run on any x86 machine. But 30-40 years ago, a program
used to run on only one machine. Assembly & Machine Languages are sometimes
called Low Level Language.
C. After Assembly,
there came the High Level Language. The commonly used high level
languages are Basic, Fortran, Cobol etc. They are also called 3rd
Generation Language(3GL). This type of
languages are more user-centered rather than machine-centered. Any program
written in 3GL can run on any platform,
any machine with little or no modification.
D. And finally came 4GL
or 4th Generation Language. Yes! Today’s Java, C#, VB, VC++ fall
under its umbrella.
3. In 2.A, we said a computer can understand machine
language which contains only 1s and 0s. So any program written on any
programming language on earth has to get converted to a series of 1s & 0s.
And for this conversion, we need some softwares. 2 kind of converters are
available….Compiler & Interpreter. There are a number of C compilers available in the market. We are
using TURBO C Compiler Version 2.0 and it is a product of Borland International
Corp.
Note: C programming Language is a very flexible one.
It has the characteristics of a High Level Language & Low Level Language as
well. Thus some authors love to call ‘C’ language a Middle Level Language.
Structure of A
C Program:
1. A C program consists of one or more distinct unit called Functions.
2. C Functions may consists of some valid statements. A
function should have an opening
Brace & a
closing one. For example: main( ).
3. A C function along with its statement block can be put
anywhere in the program.
The statements
block within a function should be enclosed within a pair of { }.
4. All C statements are entered in small case letters. C is
case sensitive. So
Main() is different from main().
5. C is a free form language. A statement can be written
anywhere within a line. A
statement can be
split over multiple lines.
6. A Valid C statement always ends with a semi-colon (;).
7. Every C program must have at least one function named main().
Program Execution
starts from this main().The
function main() can appear anywhere within a program.
But general
practice is to place it at the top of program to increase readability.
C Data Types:
C data types are divided into 2 categories. Primary and Secondary Data
types.
Primary data types are listed at the left while the secondary data types are listed at the right.
|
|
C Variables :
We use computers to process some data and get information
from there. So computers are fed with data. But a machine has to place those
Data in a safe location and then make any further calculation according to its
user’s will. So Variable comes in rescue. A Variable can hold data for
further use. As we discussed earlier, a data can be of different genre. A data can be a character, or an
integer, or a floating point numbers, or
a series of characters (String). So variable’s type also changes according to
data type.
For example: An Integer Variable can hold an integer not an
floating point data.
Rules for constructing Variables names:
1. A variable name can have characters upto 31.
2. A variable name
can have Alphabets, Numbers, and no special characters except
underscore( _ ).
3. No blanks and commas are
allowed within a variable name.
4. The first character of the name should be an alphabet.
5. The variable name should not be a keyword.
Note: A
Variable is a an entity whose value can vary. A variable’s value can be
changed if the user wants to do so. A
variable’s name doesn’t have any relationship with the type of data it
is holding. The name can be anything on earth. Usually i , j, k, l ,sal, big etc. name are used. But user can come out of
this conservativeness and use any name to their variable. C is very much case
sensitive, so , I is different
from i .
Example:
My_name_is_john
basic_salary
peNTium_cOmPuTeR
i
jjjjjjjjjjjjj
y_2002_m_jan
Some wrong variable name:
386_is_good_number (
variable name should not start with integer)
I love ice cream
(variable name can’t have any blank space)
I_went_there,_today
(a name should not have any Comma)
C Constant :
What A variable can hold is a constant. Suppose a
variable basic_salary holds a
value of 2890. This 2890 is no doubt a
constant because 2890 is always 2890, it
can never be 123 or –45 or any other value. But the variable basic_salary can hold new value 1200
erasing the previous (2890) one if the
user wants. So basic_salary is an entity whose value can be changed , so
it is an example of Variable. But what value it can store at a time is a
constant.
We need to remember, an integer
variable can hold only integer constant, float variable can hold float
constant, character variable can hold character constant. There are some
constructing rules for each of them.
Examples:
+1234 (integer constant)
-23
(integer constant)
-345.45645 (float constant)
‘c’
(character constant)
“India” (string
constant)
‘!’ (character constant)
‘2’
(character constant, not integer because of those inverted commas)
“234.234” (string constant, not float because of those inverted commas)
Some wrong constants:
I (it will be rather treated as a
variable)
India
(it is an example of
a variable)
@
(it’s neither constant nor a varible )
Notes: Remember, 2 is different from ‘2’.
Notice the placement of inverted commas. The first one (2) is an integer
constant, whereas the second one (‘2’) is a character constant. Wherever
you find a pair of inverted commas, consider it as constant. Similarly I is different from ‘I’. The first one will be treated as a variable,
whereas the second one is a character constant. A string is a collection of
characters . So “India” is a string/character constant.
Variable Declaration:
Suppose, we have to make a program which
will deal on employee’s details. So we have to collect each employee’s name,
age, sex ,salary etc. As each data (for example: 'Chandan', '22', 'M', '2200') will be
fed to computer, so we need variables to hold them. Notice that… the attributes 'name'
will need a character type variable, 'age' need integer type, 'sex'
need character and salary need float type variable. So the questions
arises that how C Compiler will distinguish between different types of
variables?
Well, the answer is by declaring them. Declaration
is done to give each variable a different status. Declaration separates
variables from each other.
Example:
char name; // This is a comment
int age; // Each C statement ends with a semi colon
char sex;
float salary;
int age; // Each C statement ends with a semi colon
char sex;
float salary;
The first statement make the compiler understand that we
want to use a character variable whose name is 'name' which will hold
character values. The second one, we want to use a variable to hold some
integer values. And so on.
Now we have declared some variables so that they can hold
some useful data.
C Keywords :
Just go through those declarations lines once again.
int age;
char sex;
float salary;
char sex;
float salary;
Now what are those char, float, int ?
Those words are nothing but Keywords. Keywords are
the words whose meaning are already explained to compiler. Those words are also
called ‘Reserved Words’. There are as many as 32 keywords in C. I am
giving a list of them.
auto, double, float, register, struct, volatile, break, default, for, return, switch, while, case, do, goto, short, typedef, char, else, if, signed, union, const, enum, int, sizeof, unsigned, continue, extern, long, static, void
Those word are a small library to C compiler. C compiler
knows all of them and also knows what to do when you mention them in any part
of your program.
Notes: The keywords can’t be used as
variable name. If we try to use them as a variable, we are just trying to assign
new meaning to keywords which the C compiler won’t allow us to do.
Some Wrong statement:
float int;
char int;
int char;
char int;
int char;
All of the above declarations are wrong. Because, in every statement, we have tried to use
keywords as variable names.
The first C program :
Now we are armed with knowledge of declaring variables,constants .Let’s write our first C code. It is a small average calculating program. It calculates average of three numbers.
main()
{
int a;
int a;
int b;
int c;
int avg;
a=10;
b=10;
c=20;
avg=(a+b+c)/3;
printf(“\n the average is %d”,avg);
int c;
int avg;
a=10;
b=10;
c=20;
avg=(a+b+c)/3;
printf(“\n the average is %d”,avg);
}
Now, let’s check the program bit by bit.
We have started the program with a function "main()" as
we discussed earlier that, every C program should have at least this function.
Program Execution starts from "main()". The "main()" function obviously contain some statements. So
it needs two pair of braces { }.
In the next four lines, we’ve declared four variables for
our job. Three variables will hold 3 integer values and the fourth one will
hold the calculated average value. So, four variables were declared. Next 3
lines contain an assignment operation, i.e we put 3 integer value in those
variables. And the next line calculates the average and stores it in the avg
variable. And finally printf() function outputs the average value on the
screen. How printf() function works - will be discussed a little later.
But before that let's do some modifications to our program.
main()
{
int a,b,c,avg;
a=10,b=10,c=20;
avg=(a+b+c)/3;
printf(“ %d”,avg);
a=10,b=10,c=20;
avg=(a+b+c)/3;
printf(“ %d”,avg);
}
Notice that, this is very compact & valid form of our
earlier code. Any statement can be written anywhere in a program. That is why C is called a Free-Form
Language. You don’t have to maintain certain alignment rules while writing
codes.
NOTE on Variable & Constant
1).Why should we
declare variables in any program?
First of all, I want to clarify that a program may not have
any variables declared at all. But this type of program may not fulfill
our demand. Rather real life programs do have some variables declared
within them.
I am giving an example.
main()
{
clrscr(); /* this actually
clears the screen */
printf("this program doesn't do any good");
printf("this program doesn't do any good");
}
The very above program just clears the screen first and
prints the message thereon. But think, whether this serves any good
purpose to us. It is not doing any calculation for us.
Why we need computers? We use it to process data resulting
information. That is, we are to give data(input) to computer and it'll
process it. As computer processes or does some calculation on data, we
should include some calculation statements in our program. So Computer has
to store this input data anywhere inside it. This data actually get stored
within the memory. Variables are used to identify this data. When I say "char ch='S'; " , it actually tells
the computer to reserve a space in memory(say location number 1022) to hold
a character value 'S' and computer identifies this place with the
variable name ch.
ch
|------|
| S |
|------|
1022
|------|
| S |
|------|
1022
( the above picture occurs in memory )
So there is an address location 1022 in memory which stores
character 'S'. As the computer can identifies the address 1022 with
variable name ch, a variable is also called an identifier.
If we need this data to show on screen we may use the
statement-
printf("%c",ch);
So, we need variables to store some data. Once it is
declared we can do anything(calculations etc.) on it. The value of the variable can change anywhere within the program. Remember, in C, declaration
of any variable should be at the beginning of a block or function
or program but never in the middle of same.
main()
{
printf("\n we are going to
declare...");
char ch;
char ch;
}
This program will give you an error. Here declaration
happens at the end of a program. It should be as follows.
main()
{
char ch;
printf("\n we are going to declare...");
printf("\n we are going to declare...");
}
2) Differences
between Variables and Constants
A computer needs data to work on. Data are basically two
types, variable and constant. Variable is a type which can store some data or
value. And that very data is a constant. For example, when we say,
int i=5;
we’re declaring a variable and assigning a value 5 to it. If
it was
int i=9888;
computer might have assigned a new value 9888 to the
variable i. That means, 'i'
is an entity which can hold data and this data can vary or change for that entity. That's why the container of this changeable data is
called a "variable". So here i is a variable as its value can be changed if we wish. Now what is 5 or 9888 which we put into variables? 5 or 9888
can't change itself. I mean 5 is 5, it will remain 5 for ever, it
will never be 4. So, 5,-789, 0 etc all are constants as they never get changed.
As a variable can
store some data, we better give it a name for obviously some good reasons, otherwise it would have been difficult to
distinguish between two variables. In our example, 'i' was the name of a variable. In C language, there are some rules that binds us when we
name a variable.
3) Keywords and
Variables
In C, there are 32 keywords. Keywords are some words those
are very much known to the compiler. When we say,"int i ;", it means we're
declaring an integer variable to store an integer value, but how compiler
understands this? Because the compiler knows the word "int", and so also knows what to do when somebody
uses this. Similarly:-char, float, void, double, long, short, signed, unsigned,
do, while, for, if, else etc all are examples of keyword. Meaning of
those words are already explained to the compiler. We would not use those
words while naming a variable. Suppose we gave, "int int=90;",
here we wanted to declare a variable whose name would be "int". But
the compiler would give an error message.
Some faulty declarations:-
int float=9.0909;
int signed=8;
float if=90; char volatile=9;(volatile is a keyword)
char else; float double;
double do=87; double double;
float if=90; char volatile=9;(volatile is a keyword)
char else; float double;
double do=87; double double;
Check out the next part Here
No comments:
Post a Comment