DCA1102-PROGRAMMING IN C - DE Unit 1
DCA1102-PROGRAMMING IN C - DE Unit 1
1_Introduction
Hello and welcome to the session of Programming in C. In this session you will be introduced to C
programming. Its efficient features and its basic structure. C is a procedural programming language
which was developed by Dennis Ritchie. It was basically developed for writing system level
applications and hence the popular operating system UNIX has been developed. Today, the market
is being led by two most popular languages, Java and Python. Having said that, the demand for C
language has not come down a bit. The reason behind this is its extensive usage in the development
of operating and embedded systems. The syntax of C language is very easy and can be efficiently
used to develop many system level applications. In fact, it is used as an introductory programming
language that is taught at the beginner level of problem-solving. Modularity is a technique of
breaking down a larger program into several fragments.
Now, C language allows the program to be broken down into smaller units, which can be run
individually with the help of functions. Now, the next feature of C programming is its portability.
Now the same fragment can be run in different environments. Now C programs can be run on
different platforms with or without modifications, which makes the language more portable. As C is
a compiler-based language, it is faster than Java or Python, which are interpreter based.
The difference between the compiler and interpreter is that compiler takes the whole program as its
input and then produces the output, whereas interpreter takes instruction by instruction as its input
and then produces the output. Also, C language has many inbuilt functions in its library. It also allows
the user to define his own functions and then add them to the library and use his own functions in
implementation. C Language uses dynamic memory allocation, which utilizes and manages the
memory. The data structure can be manipulated by several functions like CAlloc MAlloc, Realloc and
free functions of C language. Also C is a case sensitive language, meaning a lower-case character is
different from a higher-case character.
For example, a lower-case letter X that is small X is different from the higher-case X, which is a
capital X. So C is a highly case sensitive language. The basic structure of C program consists of six
main sections which are documentation, link, definition, global declaration, main and sub program
sections. The documentation section is the first section of the C program. Now these are the sect of
comment lines which are included in between the delimiters here. So these are the two delimiters,
which we can see. Any lines which are included in between these two delimiters are considered as
comments. Now
The documentation section usually consists of these set of comment lines, which will give the
overview of the code. It can be a description of the program, or it can be logic of the program. You
can write the author name. You can have a file name, etcetera. Now these comment lines can be
included anywhere in the program as long as they are included in between the delimiters. The next
section that we have is the link section. Now the link section provides instructions to the compiler to
link functions from the system library. For example, if we're using a mathematical function called a
square root, then we have to declare a header file, which is called as Hash includes math dot.h.
Here, we have declared a header file called as Hash includes stdio.h.
That means the program is capable of using all the standard input output operations, which are
included in this particular header file. In this section, all the constants whose value does not change
throughout the program can be defined. Now these constants can be defined by using hash define.
Now, PI is the constant whose value has been assigned as 3.14. Now whenever I'm using this
constant PI, the value of PI will be replaced by 3.14. There are many variables which are used
throughout the program.
Now variables can be both locally declared or can be globally declared. Local variables are those
variables which are used within that particular function. Global variables or global functions are such
which can be accessed throughout the program. Now the declaration of global variables or the local
variables doesn't change, except that the global variables are declared before main function or the
main section in the global declaration section. So here, if you observe, area is a global function and A
is an interior variable which is a global variable. Both area and the variable A can be accessed
throughout the program.
Every C program needs to have a main function and each main function consists of two parts, which
is, declaration part and execution part. Declaration part is where all the variables are declared. For
example, when you see or observe this example, you have the main function defined by using the
main keyword. Now int a is equal to 10 is the statement where the variable A is declared. Now the
execution part begins with the curly open brace and ends with the curly close brace. Now both
variable part or the declaration part and the execution part must be included within the curly brace.
Sub program section is the section where we define all the user defined functions. Here
In the example, we have a user defined function, add. Now this is a user defined function which can
be called anywhere within the program, and this user defined function add will return its value to
the called function. This is a sample program which demonstrates all the sections. First is the
documentation section. Here the comments have been included within the delimiters. Now next is
the link section where header files are included. Here is the definition section, where num is a
constant which has been defined with the value twenty. Here is the global declaration section where
int function has been declared.
This is the main section where both declaration and executable parts can be seen. Here is the sub
function which is defined. So function here is a global function which is declared in the global
declaration Section and defined after the main function. This is a simple C programmed to display a
statement. The first line is a single comment line and thus I am using a backslash symbol here. Next
we have a header file, which is included, which is a standard input output library. Then we have a
main function here and the written type of main function is void, meaning
It is not returning any value. Next, we have an open curly brace, which indicates the beginning of the
main function. Then we have a printf statement. Printf is a function which is an standard output
function. Now here, whichever string that would be included within the double quote will be
displayed onto the output console. Now, every variable and executable part of the main function has
to be terminated with a semi colon. Now getch here is a function which holds the output value. The
ending curling brace indicates that this is the ending of the main function.
So every C program must have a main function with a beginning curl brace and ending curl brace.
This is a program to check the eligibility to vote. The logic behind the program is to display a
statement that whether you are eligible to vote or not eligible to vote. If the age is less than 18, then
you are not eligible for voting. If the age is greater than 18, then you are eligible to vote. So, we have
a main function here, where num is declared as an interior variable. You are displaying a statement
onto the console to enter the age. The value is read through a scanner statement here.
Then, if the value of num is less than 18, then you are displaying a statement that you are not
eligible for voting, else, you're displaying a statement that you can vote. This is a program to add two
variables. So we have a variable A and we have a variable B.Both A and B has to be added and then
the value of A plus B has to be stored in another variable, which is C and then we have to display the
value of C. So firstly, I am declaring three variables A B C here. Then, in the first printf statement, I
am entering the value for A and then that value will be read through a scanf statement here. Next,
I'm entering the value of B and that value will be read through an input standard input function scanf
again. Then both A and B will be added and the value will be stored in the variable C, and then lastly,
I'm displaying the statement that some of A and B is C. thank you and we reached the end of the
session.
Programming in C_1.2_Constants
Hello and welcome to the session. The classical method of learning any language is to first learn its
basics. Learning programming language is no different. So before we write programs in C language,
we must first learn the basic building blocks of the language. In this session, we will discuss in detail
about the constants, different forms of constants, which form one of the basic building block of C
programming language. A constant is a quantity, whose value do not change and this quantity can be
stored in computer’s memory location. Now, the constants can be basically divided into two broad
categories. One is numeric constants and character constants.
The classification is based actually, on the kind of data that we use. What kind of data do we use?
We either use numbers or we use alphabets. So based on this, the constants are divided into
numeric constants and character constants. Now, when I say numeric constants, the kind of
numbers that we use are either positive number or a negative number or a fractional number. So
numeric constants here are basically divided into two categories which are Integer constants, where
we can use the positive number or positive value or a negative value or Real constants where we use
fractional value.
Now also, we have character constants which are not only alphabets, but we also can use alphabets,
digits or special symbols. Now this is divided again into single character constants where we can use
either a number or a digit or a special symbol. Or we can have string of characters which form string
constants. Firstly, decimal integer is such integer constant whose combination lies between zeros
through nine. So any number which has a combination of zero through nine digits is considered to
be a decimal in integer constant. First and foremost rule of any integer constant is that it must have
at least one digit.
It can be either a positive integer constant or a negative integer constant. If it is a positive integer
constant like shown in the example plus 54, it is preceded by a positive sign. Any negative integer
constant is preceded by a negative sign. You can also have any number which is not preceded either
by a positive sign or a negative sign, but in case, any number is not preceded by a positive sign or a
negative sign is considered to be a positive integer constant by default.
So, such kind of numbers is called as decimal integers, which form within the combination of zero
through nine digits. Octal Integers are those integer constants, which are formed within the
combination of zero through seven digits. To differentiate a decimal integer constant and an octal
integer constant, we represent an octal number with a leading zero. Now, for example, 34 here can
be a decimal number because it forms within the combination of zero through nine digits.
Or it can also be an octal integer because it is formed within the combination of zero through seven
digits. So to differentiate it as an octal integer constant or an octal number we represented with a
leading zero. So now 034 is an octal integer constant. It is not a decimal number. If it is a decimal
integer constant, then we do not precede the number with zero. A Hexadecimal integer constant is
preceded by zero small x or zero capital X, as shown in the example, a hexadecimal number is a
combination of 16 different symbols. Now the first 10 symbols in a hexadecimal number are
represented by 0 to 9 digits.
The next five symbols in hexadecimal number system, are represented by A to F that is 10, 11, 12,
13, 14 15. In hexadecimal numbers are represented by A, B, C, D, E, F respectively. For example, 12
in decimal are represented as C in hexadecimal. The next variation of numeric constants is real
constants, which is otherwise called as floating-point constants, which comes in two forms. One is
fractional form. Other is exponential form. The rules that are observed while forming a fractional
real constant is that it must at least have one digit. It must have a decimal point. If you observe in
the example, all the numbers that are represented here have a decimal point. The real constant can
either be positive or it can be negative.
The default sign of the real constant is positive. That is, any number which is not preceded either
with positive sign or with negative sign is considered to be a positive number and also there must be
no comma or no blank space within the real constant. You can have a decimal point, but you cannot
have a comma or a blank space within the real constant. We also have an exponential form of
representation of real constants. This form is generally used when the real constants are either too
small or too large. In exponential representation, the real constant is represented in two parts.
One is mantissa part, which is appeared before the letter E. Now this letter E can either be small or
can be large. The other part is exponent part, which is followed by the letter E. To represent a real
constant in exponential form, the mantissa and exponent must be separated by the letter E and the
mantissa part can either be positive or can be negative. The default sign of mantissa part is positive.
The exponent part must have at least one digit. Again, the digit after, you know, in the exponent part
can either be positive or can be negative. The default sign of exponent part is also positive.
There must be no commas or blank spaces within the real constants here, and we have to see to it
that the mantissa part and the exponent part must and should be separated by either small letter e
or capital Letter E. Now, in the example that we see here, 68 followed by, eight zeros can be
represented in an exponential form as 68e8, representing eight zeros. 68 is the mantissa part. Or
you can also express it as 6.8 E9 6.8 being mantissa and nine being exponent part. Character
constants are of two types. One is single character constants. Other is string character constants. In
single character constants
A constant can be a single alphabet, or it can be a single digit. Or it can be a special symbol, which
are enclosed within single inverted commas or single quotes, as we call. The maximum length of the
character that we can have within this single quote is only one. That's the length of the character
that we can have. The characters cannot be followed by even a blank space or by even a comma,
because even comma and a blank space are also considered as a character. String character
constants are sequence of characters which are enclosed within double quotation marks or double,
inverted commas.
Now the characters here, maybe string of letters, or numbers or special characters or combination of
any of these. You can also have multiple blank spaces within the double quotation marks, which
forms a string. And also we have to note that the letter A, which is in the single quotation mark,
which is a character, is different from the letter A, which is in double quotation marks, which is a
string. So, you can have even a single character which is enclosed within the double quotation
marks, which forms a string. These are some of the backslash character constants that we use,
basically to format the output onto a output console. These are called as backslash characters
because the characters here are followed by the backslash.
And if we observe that these backslash character constants are considered to be single character
constants and that's the reason why they are represented within the single quotes. Now, each of the
backslash character constant here has a definite meaning attached to it. And among these, new line
character or the tab character constants are the ones which we use within the printf statement.
Now, slashn which is called as a new line character instructs the control to jump to the new line if
used within the printf statement. And also, if a printf statement uses slasht, it instructs the control to
take one tab space. We also commonly use this backslash followed by backslash for using single
comment lines and we also use null character in the concept of strings. Thank you as we have
reached the end of the session.
Hello and welcome to the session. In this session, you will learn how to represent the integer data,
the various modifiers used along with integer data and how to represent integers in the memory.
You will also learn about the usage of variables, and their naming rules and how to define and
declare these variables. Data representation in computer is dependent on the word size of the
machine. This helps the machine to represent specific data to occupy certain range of values as we
understand that there are different kinds of data, such as integers or float values or characters that
we use. This word size allows us to further understand what a range of values a specific data can
occupy within the computer's memory.
One such data being integer, integers can be defined as whole numbers with certain range of values
with specific range of values. Now these integers can further be classified as signed numbers and
unsigned numbers or signed integers or unsigned integers. Signed, meaning which are the numbers
which are represented using sign, which can be positive numbers or negative numbers. Unsigned
meanings which the numbers which do not have any specific sign, particularly whole numbers. How
much memory can be occupied by an integer type is always dependent on the machine’s
configuration.
An integer type can occupy from two bites to four bites to eight bytes of memory. Now this always
depends on the processor, which allocates the memory. A 16 bit processor will usually allocate a two
bites of memory for an integer value. A 32 bit processor will allocate four bite of memory to an
integer value and a 64 bit processor will allocate eight bytes of memory for an integer value. As it is
known, that integers will occupy certain range of values. C language allows us to alter this range by
adding certain type modifiers. Now, the type modifiers alter the meaning of a base type, to fit into
our need more precisely.
Now the modifiers that we can see here are short and long, which are usually preceded before the
data type that we use. Now short here will have the lesser range of values than Int and long has the
higher range of values than Int. This table represents the kind of modifiers that are used along with
the integer type. Here in the table, the first column represents the integer type, along with the
modifiers like Int or signed or short int, or signed short int, or long int or signed long int. The second
column represents the number of bits that are occupied in the memory.
And the third column represents the range of values. Now the word size depends on the machine. It
is usually said that the integer data type occupies two bites of data in the computer's memory. Two
bites, meaning one bite has eight bits. Two bites have 16 bits of data. Now, when we say bits, it is
nothing but a binary digit. A binary value can either be zero or can be one. Every information in the
computer's memory is stored in the form of binary value, whether it is an alphabet or a character, or
it can be anything. Now coming back to the table here. Now, as we say that a as we can see unsigned
int here. Now unsigned int has a size of 16 bits or it can occupy 16 bits of memory, which ranges
from 0 to 65,535. Now this is calculated by calculating two to the power of 16, two raises to 16 will
give zero you know 65,536. That's the value. So the range of values that you can have is from two to
the power of 0 to two to the power of 16, minus one, which is for which is usually for sign that we
say. So we can have zero to 65,535 range of values for an unsigned int. For a signed int or for just a
normal int, it can be both. It will have the range of values for negative numbers as well as positive
numbers.
So half of 65,535 will be negative range of values. Half of 65,535 will be range of positive values. So
the range of negative values for a signed int will be minus 32,768 to 0, and positive values will be 0
to 32,767. The same holds good for short int, which occupies 8 bits, because it will occupy lesser
range of values than integer. For long int it would be 32 bits because it will occupy higher range of
values than the integer value. A doubt that may arise while using this integer values and fractional
values is, can we assign fractional values to integer type, or can we store fractional values into
integer type? Though we might assign a fractional value to integer type, it will not be stored in
memory as fractional value, but it will be truncated.
That is, all the values after the decimal point will be truncated and will again be stored as an integer
value. Having said that, we can always store and integer value by using float data type but we
cannot, store a fractional value using an integer data type. A variable is nothing but whose quantity
may vary during the program execution. The names that are given to the computer memory location
are nothing but the variable names where we store different constant values such as integer
constants or real constants, etcetera.
Now a constant stored in a location with a particular type of variable name can hold only that type of
constant, for example, an integer variable cannot hold float values. An integer variable can store
only integer values. Now the general syntax of variable declaration is type, that is the type of
variable followed by the variable list, where we can have more than one variable in a single
statement, for example, now, there are certain rules that are followed while naming the variables
and assigning them the values. Now, the first rule says that every variable name must begin either
with a letter or with an underscore. Now, the first example area is, begin with a letter A, which is a
capital letter.
We can also use a smaller-case letter or a lower-case letter while, naming. The second example in
second example, area has begun with or the variable name has begun with underscore, which is also
a valid variable name. The next rule says that variables are case sensitive, meaning the lower-case
letters and, the upper-case letters that are used in variables are completely considered differently.
In the example here, the first example car. We have used all the lower-case letters for the variable
name. In the second example, we have used all upper-case letters for variable name.
In the third example, we have used the combination of upper-case letters and the lower-case letters.
The compiler considers all these three examples as three complete different variable names, and
also, we can have variable names that are constructed with digits and letters. We can have the we
can have a variable name with the combination of these, and also we cannot use any of the special
symbols other than underscore while constructing a variable name. While naming the variables, we
cannot be using keywords as variable names.
For example, we cannot use main as a variable name or define as a variable name because they're
the keywords of C programming language. And also, variable names can be of any length. But it is
preferred to have eight characters up to eight characters as the variable length. Now that we know
how to construct variable names, by using different rules or by following different rules, we also
have to know how to declare these variables. Now vary, every variable in C program must be
declared. Now the syntax of declaring a variable name is, data type, followed by variable name. Now
you can have a single variable, or you can have a variable list. In the example here int X Y Z Now X Y Z
here are different variables of type integer. All X Y Z variables will hold only integer values.
We can divide this statement into int x int y and int z. But C program allows you to put all the similar
type of variables under one single statement and thus we can have int X Y Z and all the variable
names must be or all the variables must be separated by a comma. Now, in the second example,
char flat, comma, ch flat and ch are the variables of type character. Now, while declaring the
variables, no memory will be allocated for a variable. Now this happens only after we define the
variable. For example, when we say int a, then memory will not be allocated to the variable A. But
when you say int a is equal to 20 then the memory will be allocated to the variable A. Now, after
declaring the variable, we also must assign the values to the variable.
Now, these variables or sorry, these values can be any constant value, meaning it can have a real
value an integer value or a character value. Depending on the type of the variable, the variable will
hold that particular value. Now, initialization of variable means assigning a value to the variable. We
also have to assign the value to the variable. Now, assigning the value of variable is done by the
syntax. Say data type, followed by its variable name, equal to symbol or this is this is how we do the
assignment and the value that we have to allocate or assign to the variable. Now, for example, int x
equal to 50 means we are, assigning a value 50 to the variable x of type integer. We can also do
multiple assignments in a single statement.
But here in this example, X and Y both should be of type integer. Now, in this statement, we are
declaring and assigning the values to the variables in a single statement. Now, when we say int x
equal to 50 comma y is equal to 30, we are assigning 50 to variable X, and we are assigning 30 to
variable Y and now X and Y are of both integer type. In the next example, we are assigning value or
character value character constant value X to flag, which is of type char and also we're assigning a
value capital I to ch, which is a variable name of type char.
Now where can we declare these variables? Can the variables be declared throughout the program?
Now the variables can be declared as local variables or as formal parameters or as global variables.
Now variables are called as local variables when they are declared inside the function. For example,
inside main function if I am declaring two variables, say int a and b which, says a b, are the variables
which are of type integer. Now they're called as local variables for the main function, and the scope
of these variables only will only be within the main function. We can also use variables, or we can
also declare variables as formal parameters while defining a function. We can declare variables
outside the functions wherein these variables are called as global variables and the scope of these
global variables will be throughout the program. Thank you as we have reached the end of the
session.