Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

C

Okay, so now we are starting with my favorite programming


language, C. C is undoubtedly the most popular programming
language in this world. It is a relatively low-level, imperative
programming language which is very efficient and is widely used.
The fact that most modern operating system kernels are written
in C makes it super cool.
Lets have a proper Wikipedia style definition of C:
C is a general-purpose, imperative computer programming
language.
Lets analyze this definition:
1. General-purpose: Because it can be used to do a variety of
task that programmer wants. It is not created for only one
purpose. For example, suppose we have a maid in our house
for washing clothes. Then she only washes clothes, she does
not wash utensils or cleans the floor because we do not pay
her for that. Her special-purpose is washing clothes. Then
she is not general-purpose. If we hire a maid and say that
she will be paid for all things she would do, then she would
wash clothes, utensils, clean the house without any tantrum.
Then she will be a general-purpose maid, because the
purpose of hiring her is not clearly stated.
2. Imperative: Imperative programming languages are those
that define the task to be stated in terms of statements.
These statements change the program state and describe
how the program works.

Levels of programming languages


Let us see one more important term now. We earlier stated that C
is relatively low-level. So what exactly are these levels? Lets look
at that.
Consider a hierarchy system, in which programmers are on top
and machines are on the bottom.
High-level programming
Human

language

Middle level programming

Machine

language

Now, any language which humans can easily understand, i.e., it


has many English words and is easy for us to understand but
difficult for machine (thats why we need compilers) is called a
High level programming language. For example, python.
Any language which a machine can easily understand is a lowlevel language. Now, we know that machines are dumb, they only
understand voltage signals. So we code a low-voltage as zero and
high voltage as one (or vice-versa) and thus machine can
understand binary. Now with microprocessors, machines can also
understand a specific set of instructions that microprocessor
understands. This code is called assembly. As machine can easily
understand it but humans cant, they are low-level.
Now if we make a compromise. If we take a language which is
difficult for normal humans to understand, i.e., you have to know
how to understand the language. It will not be very close to
English. So now, we can make a small compiler and it will be easy
to convert this to assembly or binary code. Thus both humans and
machines have made some compromise. So language is not easy
for both. Thus, it is a middle level language. C++ is a middle level
language.
C is relatively low-level, i.e., it is somewhere between middle level
and low level.

Compilers and Interpreters


By now you must be thinking, what is a compiler exactly? Lets
get to it.
Consider you have to talk to a Japanese person. You dont know
Japanese and he doesnt know either English or Hindi. So you both
cannot understand each other. You would need a translator who
knows both Japanese and English or Hindi to let you both know
what the other person said. Similarly, we dont know machine
language and computer doesnt know the programming language

we are writing in. So as in above case, we need someone to


translate the instructions so that computer can understand it and
then translate computer output so that we can understand it. This
translator is compiler. It takes the code, checks if it makes sense,
and translates it to object code which computer can understand.
Now the translator person can be of two types:
1. He can listen to your one sentence; translate it to the
Japanese person. Then listen to other sentence, and again
translate it. Whenever he doesnt understand what you said,
he will stop translating and ask you to correct what you said.
2. He can listen to you say one full paragraph, translate it
completely in Japanese and then ask you to correct wherever
you made mistakes.
Again comparing the translator to compilers, we see that a
compiler can either convert the code line by line, stop wherever a
mistake is seen, asking you to correct it; or it can translate the
whole code at once and give the lines wherever mistakes are
there. The first type is called interpreter and second is called
compiler. Interpreters are good at pointing out the mistakes but
are usually slower than compilers. Java is one language which is
both compiled and interpreted.

C program
Lets analyze a basic C program. It will take two numbers from
user and add them. Then it will print the result.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.

#include <stdio.h>
#include <conio.h>
int main()
{
int a;
int b;
int c;
printf(Enter the value for a: );
scanf(%d, &a);

12.
13.
14.
15.
16.
17.
18. }

printf(Enter the value for b: );


scanf(%d, &b);
c = a + b;
printf(Sum of a and b is: %d, c);
getch();
return 0;

Now let us analyze the code line by line.


Line 1 includes a header file called stdio.h and line 2 conio.h.
What are these header files anyway? Well they are our way of
being lazy :D. There are some basic functions which we all will
most probably be performing in our program, for example,
printing an output. It would be too tiring to have to write the code
again and again for how to print an output. Thus most compilers
wrote functions and wrote them in a file. Now whenever we
include that file, the definition of that function gets included in our
code, so we can easily perform it without having to write the
whole code.
The include statement has a # (called hash or pound) before it. It
indicates the compiler that this line has to be translated before
compiling the rest of the program, because otherwise, the
definition of that function will not be present during compiling.
Any instruction of this type, with a # in front of it, is called a preprocessor directive. Pre-processor, because it is executed before
processing of the program and directive is a synonym for
instruction.
File name is written in angle brackets. there is a space between
include and angle bracket. There is no space between # and
include, nor between angle brackets and file name. There are
many include files available in a standard compiler, like stdio.h,
conio.h, math.h, etc.
There are many other pre-processor directives like #define,
#ifdef, but well deal with that later. Also, we can make our own
header files for our personal functions. That also will come later.

One important point, conio.h is a dos header. This means that it is


needed for only those programs that are executed in Windows
environment. When well code on Linux command line later on,
i.e., our Ubuntu virtual machine, we will not include it. Nor will we
write getch() in the end.
Now we come to main(). As the name clearly suggests, it is the
main part of the program where all execution takes place. It is the
main function of a program and it automatically gets called
whenever we execute a program. We will deal with functions later.
So for now just keep this in mind that a program has a main. It will
be clear later.
Now notice the int written before main(). It is one of the basic
data types of C. They are:
1.
2.
3.
4.
5.

int
float
char
void
double

A data type is actually a definition of what kind of value is being


talked about. It defines a range of values and anywhere we use
these terms, we indicate the complier that the value will be in this
range.
int is used to store integer type data, i.e., positive or negative
integers (numbers without the decimal sign). The size normally
depends on the machine being used but it is generally 2 bytes. It
normally holds values of the range -32768 to 32767. If we add the
word unsigned before it, then it holds all positive numbers only. So
the range becomes 0 to 64535.
float is used for storing floating point numbers, i.e., numbers with
decimal part or even exponents of 10, for example 2X10 5. It
stores numbers in normal decimal form, 0.00002 or stores it as
2E5, where e can be small or capital and stands for exponent. The
number before the power of 10 part is called mantissa. It can

generally store numbers from -2147483647 to 2147483647, but it


usually depends on machine. Normally needs 4 bytes.
char is a data type for character values. There are about 256
characters and so char normally has values 0 to 255, with each
value assigned a character. It is internally treated as an integer,
so even arithmetic can be performed on it. Normally it needs 1
byte. The following range should always be remembered:
1. 97-122: small letters a-z
2. 65-90: capital letters A-Z
3. 48-57: digits 0-9
void is a nothing data type. When we want to deal with situations
where nothing is there to give or take, we use void. It will be clear
when we use functions.
double is like a big brother to float. It has double precision, i.e.,
more digits can be there after decimal point. It is hell big.
We have special optional specifiers in C, which modify the range
of int, char and double (only long) (except void of course :P). They
are:
signed: specifies that the value can have a plus minus sign.
unsigned: restricts to only positive values.
short: can be used to shorten the capacity to save memory
long: can be used to extend the range by taking up more memory
http://en.wikipedia.org/wiki/C_data_types: Read the table on this
page, it will be clear.
Coming back to the code, for now assume that a function has a
data type written before it, which is called return type, which
specifies the value it returns. It can be any data type, including
the complicated ones we will discuss later. The fact will be clear
when functions will be done. There will be no space between main
and ().
Now we have written int a, int b, int c. These are called variables,
because they are able to vary, i.e., we can change their values

during the program run. a can be in one case 5, in the other case
8. It all depends on user input. It is not fixed so it is a vary-able :D.
When we say int a; we are declaring that we will be using a
variable called a. So it is called variable declaration. If we say int
a=5; then we are giving an initial value to a. So this is called
variable initialization. This can also be done by declaring a in one
line and initializing it in the other, like int a; a = 5;
Variables are actually empty boxes whom we have given names.
They refer to a memory location and so a does not have an
existence of its own. Its just a name we have given to a memory
location in the computer. We may give it another name if we dont
like the first one. But yes, we cannot give same name to two
boxes. It would be confusing which one is referred to.
Just like we can store anything in a box if it fits in it, we can store
any value in a variable if it fits the data type. Thus, data type
describes the capacity of our box.
Another interesting thing. We know that char is smaller than int.
So if we can place a big object in a box, I can obviously place the
smaller one as well. Thus we can place a char value in an int
variable; an int value in a float variable and so on.
But, what if we try to place the bigger value in smaller variable?
Well, C is like a short-tempered strong man, who would just push
the object in the box even if it breaks. So if we try to put a float in
an int, it removes the decimal part forcefully and places it. This is
called implicit type conversion, as data type is being converted
and implicit as it is done without programmer asking the code to
do so.
We can also tell the compiler to do this explicitly if the need
arises. We can simply write the needed data type in brackets
before the variable in assignment statement, for example int a =
(int)b; where b is a float variable. This process is called type
casting. Type casting will not have a space between casting
operator () and variable name.

Notice line 5. We gave a curly bracket there and even in the end
of the code. These two brackets specify what we call body of a
function. All statements are written between these brackets.
These are also the scope of the variable declared in them. Any
variable declared within { } cannot be used outside its own { }.
We have a getch() statement. getch stands for get character.
Because Windows immediately exists after completing execution,
we may not be able to see the output. So we give a getch() call,
which waits until we press any key on the keyboard. So we get
time to see the output. It is not needed in Linux. Its definition is in
conio.h.
We also have a return 0 statement. It returns a value 0 to the call
of main, i.e., the operating system that the execution was
successful. It will be clear when functions is discussed.
We also have the statement c = a + b; It adds the two numbers a
and b and store their result in c. There should be space between
all variables and operators. Operators will be discussed in next
lecture Im tired now. Just remember, always, the value on right
hand side goes to the variable in left hand side of =.
Now the only remaining statements are the printf and scanf
functions. Always remember, anything with () is a function. There
is no space between printf and (). printf is short for printing
function. It is used to print an output or a message to the screen.
Strings are written in . Any variable which has to be printed has
to be specified by giving the short form of its type to identify the
type of value to print. They are %d for int, %f for float, %c for
char.
scanf stands for scanning function. It is used to scan the values
inputted by the user on the console. It only specifies the type of
value to be taken in the string by %d, %f etc. Then it gives a
reference to that variable. Explaining references will take time, so
in next lecture. Remember always, you cannot give a statement
to print in scanf. Never ever do that.
So, it is wrong to write scanf(enter number:%d, &a);

It is a blunder. You cannot give a string to scanf, it only scans. So


only thing in should be %d stuff.
Well thats it for this lecture. Next time we will get in details of
printf, scanf, deal with variable scope, come to operators, explain
constants and then hopefully start with if else.
In case of any statement which is not clear, please copy it to a
word file and write your doubts in it as well. Please make sure
there is line space between two doubts. They will be covered in
next lecture.
There is a question paper attached along with the lecture. It is
expected that you will solve all those questions and write
appropriate codes for them.
Thank you for reading

You might also like