DCA1102-PROGRAMMING IN C - DE Unit 3
DCA1102-PROGRAMMING IN C - DE Unit 3
1_Data Types
Hello and welcome to the session. As we use different kinds of data in our program, we need to
declare the variables with certain data type such that these variables will hold certain specific value.
C Language provides us various data types. So, in this session we would see various data types which
are provided by C language and their usage, and how to implement these data types in a program.
All the variables in C program have to be declared before we use them, and it has to be declared
along with its type and the size of the data. So, declaring a variable would determine its type and the
size of the data, which is associated with variables. For example, int variable here that is used in the
example determines that the variable var is of type integer and can hold integer value, and this
variable will be allocated with the memory of the size integer. So, for example, in a machine where
the memory for an interior data type is allocated with 2 bytes, variable var will also be allocated 2
bytes of data.
As we now understand that different data types will hold different kinds of value. That is, integer
data type will hold integer value or a char data type will hold character value or a float data type will
hold fractional value. The data types can be broadly classified into primary data type and secondary
data type. Now, Primary data type is a primitive data type which cannot be manipulated where
secondary data types are user defined data types, which can be manipulated. That means all the
user defined data types are considered to be secondary data types.
I said C data types are classified into primary data type and secondary data type. This is to
understand which data type falls under which category. That means we have data types which can
be manipulated, which are called a secondary data type. We also have data types which cannot be
manipulated, which are called as primitive data types or primary data types. Data types such as
character, integer, float, double, void cannot be manipulated by the user or the programmer. Having
said that, user also can have his own data types, which are array, pointer, structure, union, and
enumerated data types, which he can make use of in the program that he is implementing. Every
data type will be having certain range of values, depending on the word size of the machine. Now
integer occupies 2 bytes of memory, and will have certain range of values.
So, if the processor is 16-bit processor, then interior will have, 2 to the part of 16rrange of values,
depending on whether the integer is signed and unsigned the range of values vary.
Now this possible range can be altered by modifiers such as short and long. The declaration of short
int and long int is as same as a normal integer variable declaration.
So, variable can be declared as int by using int data type. In the same way, the variable can also be
declared as short int, or long int.
Now short int will reduce the range of values or possible range of values of integer, long int will
increase the range of values or possible range of values of interior data type.
So, if integer occupies 2 bytes of memory, then short int reduces the memory to 1 byte and long int
will increase the memory to 4 bytes. So just by adding these two qualifiers along with interior, will
alter the memory that is allocated to the integer data type.
To store any integer values, as we use integer data type. In the same way, to store fractional
numbers, we use float data type. Now float data type can be denoted by using float key word, so this
impacts of declaring float variable is by using float keyword and the variable name. Here In the
example, num1 is a variable of type float. We also have different variations of float values, like long
double and double. In case if we have to use longer precession than the float values we use double
and long double. Double is as same as a float data type. As float data type occupies 4 bytes of
memory, double occupies double the space that is 8 bytes of memory.
Now, long double will extend the precision further, and it occupies 10 bytes of memory. So float data
type occupies 4 bytes of memory, double occupies 8 bytes of memory, whereas long double
occupies 10 bytes of memory. This table here shows the number of bytes that are occupied by float,
double and long double. We can see that, long double occupies more number of bytes in the
memory than double and float data type, whereas float occupies 4 bytes of memory and double
occupies 8 bytes of memory. This program demonstrates the usage of float data type in the
program. This program calculates the average of N numbers that means we will enter N values, it
will total the N values divided by the total number of values and then store the value as average
value. So, we need certain inputs, and we also need certain variables to store this average value.
So now in this program, the first statement #defines N 10 will assign value 10 to the symbolic
constant N. Now we come to the main function where the execution of the program actually begins.
In the main function, firstly we declare certain set of variables. There is a variable count which is
declared as integer data type. We also have 3 variables, sum, average, and number, which are
declared as float values or float data type. That means, sum, average, and number will store
fractional values, or now have the capability of storing fractional values. Initially, sum is assigned to
0, and count is also assigned to 0.
So, the initial value of the variable sum and count will be 0. Now, we're using a while loop to execute
our logic. Now, in while loop, we have a condition count<N, meaning if the value of count is less than
N, then the control will get inside the while loop. If the condition count is less than N is false, then
the control will jump on to the statement, which is after the while loop. So, as of now, I'll clear this.
Now, inside the while loop as 0, now let us assign the values to count N. Now, count will have value
0, and N is having the value 10. So 0 is less than 10, the condition is true. 0 is less than 10, so the
control gets inside the while loop. Inside the while loop, we have scanf statement directly, we do not
have any printf statement, so there is no display statement onto the console.
We can directly use a scanf statement to read the values. So, uh, you have an output console where
we enter certain values, and those values will be read through scanf statement, and that value will
be put in the place of number. So number is the variable that will take up all the values that you'll be
entering onto the output console. As number is a float value, we are using a format specifier %f in
scanf statement. Now here after reading the value through scanf statement, the next statement that
we have is sum = sum + number. Now this statement will take the initial value sum, which is 0 + and
adds it with the number that you have entered. Now, let us suppose the number that I have entered
is 1, so 0 + 1 will be added and will be assigned to sum variable itself. So sum’s new value here will
be 1, and also the count value will be incremented. The count is a counter variable which keeps track
of the number of values that we are entering, so count value will be 1, and then again, we get back
to the loop to check now if the condition is true or false.
So now count value is 1. 1 is less than 10 again, so the condition is true. Again, we get to the while
loop and we enter the next value to be 2.3. Now, in the next statement sum’s previous value and
newly entered number will be added and then now that value will be stored in to sum.
So this statements keep iterating till the while condition goes false. Once count becomes more than
10 or the value of count becomes more than 10, the control will directly jump to the next statement
after while or control will come out of while loop where the average value is calculated by sum
divided by N. The sum value are displayed on to the console, which gives N = 10, and sum is equal to
whatever sum of the values that you have entered, and it also displaced the average value.
We have reached the end of the session. Thank you.
Hello and welcome to the session. In this session, we would see the various input/output operators
and functions that C language uses and how these input/output functions treat the data such as
character data or a string data or a numerical data, and transfer this data from input devices to the
output devices. And also, we will learn how to write programs by using these input/output functions,
which can handle a single character or a numerical value or a string. Any programming language uses
certain set of reserved words called as keywords, so even C programming language uses certain set
of keywords. Whose meaning is definite, whose meaning is fixed and whose meanings cannot be
changed. So, we cannot use these keywords as variable names.
These are the set of keywords that C programming language uses. You can see various keywords that
you are acquainted with. For example, we have int, which is a key word which is used as a data type.
We have float, which is a key word which is used as a data type. We also have certain keywords like
for, which is used for looping. We have goto. We have case, which is a keyword and which cannot be
used as a variable name. We also have unsigned, which we can identify as a modifier. We can have
sizeof which is again used to calculate the size of a memory of given operand. We also have
modifiers such as short. We have a keyword, which is called as return. So basically, these are the
keywords which we cannot use in the program, other than as keywords so we cannot use them as a
constant or as a variable name or as a function name.
We have various input/output functions or various input/output operations which transfer the data
between the standard input and the standard output. What we mean by standard input here is the
normal keyboard, and standard output is the normal output console. Now getchar is one function
which is used to read one character from the standard input that is the keyboard, and it returns the
character it reads to the output console. Now there is a possibility that, there are no more
characters that are available. So, in such case special value, which is called as end of file, is returned.
So, to use this standard input function which is getchar function, we have to assign this getchar
function to character variable. So a variable has to be declared as a character and then we have to
assign this getchar function to the variable. For example, here, if we see that variable c has been
declared of type char, that means now we can store any character data type or character value.
So now c has been assigned with getchar function, so what getchar function does is it takes any
value from the standard input such as keyboard. So if you press any key from the keyboard, that
value will be taken by getchar function and returns its ASCII value to where it is assigned. So the
value c will actually have an integer value, which in turn will be converted to a char value and then
that value is returned. Now for example, if we press a key, say a, that a will be converted into ASCII
value. Again, that ASCII value will be converted to a in case if you are displaying the value of c.
As we have an input function, standard input function, which is a getchar function, which takes one
character at a time from the standard input and then returns the value. We also have putchar
function, which is a standard output function which takes or which writes one character to the
standard output. Now, as we mentioned, standard input is a keyboard. Here, standard output can be
considered as a user screen or output console. To use putchar function, we must pass a character
variable as a parameter within the putchar function. So this character variable must already be
declared previously in the program. For example, we are declaring a variable c of type character. So,
c is having a data type of character, and this c is passed as a parameter to putchar function. So,
putchar function takes the value of c and writes it on to the output console.
We also have formatted input function, which is a scanf; this is called as a formatted input function
because it reads formatted input from a standard input such as a keyboard. So this is the most
commonly used function to take any kind of input. So, scanf function is mostly used in the program
to read the values from the keyboard or the standard input. The scanf function is formatted input
function, which allows the programmer to accept the input from the standard input devices like
keyboards, and stores them in variables. So the scanf statement will read the input from the
standard input device in to one or more variables. Scanf statement can be used by using the scanf
keyword and by using control string our list of arguments as its parameters. The control string here is
nothing but the format specifier that we use for that specific data.
So this contains certain formatting information and we also have list of arguments that would
represent individual input data items. For example, if we declare an integer variables say, int a to
which we have to input the value through keyboard. So if we pass that particular data item through
scanf function by using its associated format specifier, the value that a user gives through the
keyboard will be stored into that particular variable. Here, the value that you entered, like, say 10,
will be entered in to the variable a. Also the scanf statement, which uses the argument list here, are
nothing but or represent the pointers, which point to the address locations of the data items into
which we input the values. And also this control string can consists of control characters. It may also
include a whitespace characters or non-whitespace characters. And usually this control characters
are preceded by % or modulus sign. Here are certain format specifiers or control characters that are
listed. So each format specifier or control character is preceded by a modulus operator or a %
operator along with the character.
Now, each of the format specifier specifies certain format of data. For example, %d is used to format
a decimal and interior data. %s is used for string data, or %p is used for a pointer format of data, etc.
So this is a program to demonstrate scanf function. Now, this program reaches different kinds of
data, which is integer, float, character and string data. So firstly, we have a different variable which
are declared with different data types. I is a variable, which is declared as int. f is declared as float. C
is declared as char and str is declared as a string. Now, in the scanf statement here, we have a
control string, which has different format specifiers. We can observe that these former specifiers are
not separated by comma after the control string and before we list the arguments, there is a comma
operator, and all the arguments here are separated by comma operators again.
The first format specifier is for the first variable i. The second format specifier is for the second
argument f. The third format specifier is for the third argument and the fourth format specifier is for
the fourth argument.
Here, i is an integer value. We have used format, specifier as %d. f is afloat value. We have used
format specifier %f. As c and str are char and string data types, we have used %c and %s as the
format specifiers. Unlike scanf function, which reads the values from the standard input, we have
printf function, which writes the data onto the standard output device. printf is the library function,
and this function can be used to output any combination of single characters or strings or any
numerical values as such. The syntax of printf function is as same as the scanf function, but with a
little variation that we use printf keyword instead of scanf. Now printf also has a control string and a
list of arguments. This control string is again gives the formatting information where we use a
different format specifiers and this list of arguments are represent individual output data items.
So the syntax of printf function is a similar to the scanf function, except that we use printf keyword.
Inside the printf function, we pass control string and the various arguments as its parameters.
This control string can consist of format specifiers, string of characters and also constants.
Let us consider the first example of printf statement here. Now here, printf statement consists or
takes only control string as its argument. Now this control string consists of various characters or
string of characters, which also includes certain symbols or special symbols such as comma. It can
also take a blank space. Now this control string is included within the double quotes. This also has a
backslash character constant, which is \n, which instructs the compiler to take the control to the
next line. In the second printf statement, we can see that there is a control string along with
argument, which is a data item say, i. Now inside the control string, we have used a format specifier
%d, which takes the value of the data item i, meaning whichever value that is assigned to the data
item i, will be returned to this particular format specifier.
So if i=10, then the printf statement displays i is 10, and takes the control to the next line on output
console. Now, in the third printf statement, we have only format specifier that is included in the
control string, and we have a value 10 as an argument. So this value 10 is returned to the format
specifier %d. So this printf function will display value 10 onto the output console without any display
statement. In the fourth statement, we have i+j that is an expression as the parameter. Now the
value of i+j is return to the format specifier %d, and that value will be displayed on to the output
console.
Again, these are the various former specifies that are used in printf function. These are similar to the
format specifiers that are used in the scanf function. Now, in scanf function, format specifiers are
used to read the values. In printf function, format specifiers are used to display the values.
So here %d is used to print a decimal value. %s is used to print a string value. %o is used to print the
octal value, etc. Apart from printf and scanf functions, we also have gets and puts functions, which
basically are used to display strings or to read string values. So, each of these functions will take a
single argument, and that argument must be an array of characters. So basically, these strings will
take string as its arguments, so gets function will take string as its argument to read the string and
puts function will take string as an argument to display the string. This program demonstrates the
usage of gets and puts function. Now, as we know that gets function is basically used to read a string
and puts function is used to display the string. Here line is nothing but a string which is declared with
the help of array of characters.
So we do not have a direct data type for string, the declaration of a string can be done with the help
of char data type. Now gets function will take the string as a parameter which is a single parameter
and reads the string value from the standard input and puts function will also take line, which is a
string as a single parameter, and display the string onto the output console.
We have reached the end of the session. Thank you.
Programming in C_3.3_Type Casting & Conversion
Hello and welcome to the session. In this session, we will learn how to convert different data types
by using type conversion technique. So we will learn about implicit and explicit type conversions. We
will also learn about character data type and their range of values, which the processor provides.
Type Conversion can be done in two ways. One is by using implicit conversion, which is basically
used by the compiler and other is the explicit conversion. Now in implicit conversion, compiler
automatically converts a lower type to a higher type. For example, lower type can be float and
higher type can be double.
Now, why we need a type conversion or implicit type conversion is that, while evaluating the
expressions, now expressions consists of mixture of constants and different kinds of variables.
Now, while evaluating the expressions, these expressions generate intermediate values which have
to be converted to a proper type. So this compiler uses a technique called as implicit conversion to
convert a lower type data to a higher type data. Consider an expression which has to be evaluated,
say x = li / i + i * f - d. Now this expression uses various variables which are of different data types. So
i and x are of data type int, f is of float data type, d is of double data type, and li is of long int data
type.
Now, while evaluating the expression, we have to keep presidents and associativity rules in mind.
Now, while scanning the expression, we can see that the division operator and multiplication
operator are at the same level of presidents and are in higher presidents to the operators plus and
minus which are again of the same level. So, while scanning the expression, the associativity is left to
right. So whichever operator comes first, that expression will be evaluated. So here li divided by i will
be evaluated first, as division operator has the higher presidents and, comes first when we consider
the associativity of the expression. But here, li is of type long int, and i is of type int, so implicitly I
value will be converted to long int and long int and long int will be divided and the result will be long.
So the intermediate result that is generated by evaluating expression li divided by i will lead to a long
int value. In the same way again when we scan operator plus, this expression will not be evaluated
because plus has the lower presidents than division and multiplication. Next, we have, multiplication
symbol which is scan, so we evaluated the expression i * f. But here, i is an interior value and f is a
float value, so i will be implicitly converted to float value, which will result into a float value as an
intermediate value.
Now, after evaluating this expression completely, the result will be in double data type or the result
will have double value. Now, this value is assigned to a variable which is of type int, so the final
result will be converted to the type of the variable to which it is assigned to. So here, double will be
converted to integer data type. During the final assignment of the value, the following changes has
to be observed. For example, if we have float value as the result and it is assigned to integer data
type variable, then this causes a truncation of the fractional part that is the values after the decimal
point will be truncated. Now double to float will cause a rounding off the digits and long int to int
will cause dropping of the excess higher order bits.
We next have explicit conversion. Explicit conversion is different than the implicit conversion, where
here, the type conversion is forced, and it is not automatic. Explicit conversion is also called as a
typecasting. Now, the general form of explicit conversion is a type name within the parenthesis and
expression. Now, this need not be an expression always, this can also be a value. So here, in the
example, x is equal to int, which is included in the parameters 7.5. Now 7.5 is a fractional value and
we want it to convert in to integer value. So this will trunk it all the values after the decimal point, so
that this will result or this will assign a value 7 to the variable x, which is of type integer. Similarly, we
can also typecast while we are dividing two values that is two fractional values here, 21.3 will be
converted to integer data type by truncating the fractional value which would be 21. And we also
will be converting a fractional value 4.5 into integer, which will again truncate the value after the
decimal point.
So, two integers will be divided which will result in an integer and will be assigned to the variable a.
So here are a few examples for typecasting. The first example 8.5 is a fractional value which is
converted to interior by truncating the values after the decimal point. Now the next expression, we
have two fractional values which are divided. So, 21.3 will be type casting to integer, which will be 21
and 4.5, is a fractional value which is converted into integer, which will give you 4. So, 21 divided by
4 will be 5, which is assigned to the variable a. Now we can also do division in floating point more.
Now here In the next expression, we can see that a+b which is a parenthesised, this expression is
parenthesised here is being type casted, so the result of a+b will be converted to an integer here.
Now in this example, a+b is not parenthesised, so we can consider this as or the compiler considers
this as int a+b. So, the value of a will be type casted to integer and then will be added to b.
Here, before converting the value into cos, what happens is the value of x is converted to double and
then the cos value will be found. The expressions which contain one of the operand as integer and
one of the operand as a real are considered to be mixed-mode arithmetic expressions.
So, if one of the operand is of real type and the other is of integer type, then the real operation is
performed and the results will always be a real number. So if you see an example here in this
arithmetic expression, where two values are being divided, 25 is an integer .value, and 10.0 is a
fractional value.} So, though 25 is an integer value as one of the operand is of real, the result will
always be a real value, whereas here in the next expression 25 / 10.0, we have two integer values, so
the value or the result will also be an integer value.
The variables, which are declared of type character, will hold a single character, and these variables
are declared by using a keyword char. Now this char can also be signed or unsigned, just like Integer
data type. But unlike an interior data type, which occupies two bytes of data, char occupies only one
byte of data. But because it can be signed or unsigned and also comes with different modifiers, this
can also have different ranges. So unsigned characters can have a range of values in between 0 to
255 and signed characters will have range of values in between -128 to 127 that is we can have 128
to 0 negative ranges of values and 0 to 127 positive range of values.
Now, the syntax to declare any character data type is to use a char keyword associated with a
variable name. So, for example, we can have char ch, which is assigned with a value ‘a’, and you can
observe that this is a single character which is, included in between the single inverted commas or
single quotes. So you can assign any character to the variable ch. Now this program demonstrates
how to use a char data type. Now, before seeing this, we have to understand that every character
has ASCII value associated with it. So now in this program, if we see the first line, the variable c is
declared of type char and is assigned with a character A. In the next line, we has a small a as a
variable, which is of type integer and is given a value 65.
Now, while displaying the statements here in the first printf statement, we are using %c as a format
specifier and the data item that we are using here is c. So, the value of c will be returned to the
format specifier %c and thus A is displayed on to the output console because c is assigned with the
value A. In the next display statement, we have %d as a format specifier, and c as the data item.
That means to say that we want to display the ASCII value of the character. So the ASCII value of the
characters c is 65. So, 65 will be displayed on to the output console. In the third printf statement, we
can see that we're using a format specifier %c, but we are using the data item A. Now here A is
assigned with the value 65. So, what compiler does is it considers 65 as the ASCII value and as we
have used the format specifier %c, the character value that is linked with that ASCII value will be
returned to the format specifier %c. So thus A will be displayed on to the console. We have reached
to the end of the session. Thank you.