Transcript for:
Understanding Short Data Type in C

[Music] hi viers welcome to Nares Technologies this is Shas in the last session so we discuss the basic concepts of data types and today first discussion about a First Data type integer data type divides into six classifications so now the first CL classification is a short type short again divided into two subclassifications right so already we discussed here it is signed data type and second one is a unsigned data type short is a data type used to store an integer value right and here it is divided into two types signed and unsigned so what is a signed data type and what is a unsigned data type means using signed we can store only a positive value but using signed data type A positive value and negative value also we can store using unsign we can store only positive value right and what is the size of shot either it is a signed short or it is unsigned short it occupies two bytes memory how many bytes it occupies 2 bytes memory so using 2 bytes memory so what is the minimum value we can store and what is the maximum value we can store just look at the memory allocation process here suppose 2 bytes memory I'm taking this is the first bite this is the first bite I'm taking first bite and this is a second bite is a second byte so using 2 bytes memory what is the minimum value we can store right so the data will be stored in the form of binary values so if you place all our zeros all are zeros the minimum value is a zero and and the maximum value what is the maximum value we can store using 2 bytes memory using 2 bytes memory is we need to place all ones all or ones this is the value is a 65,535 65,535 so using 2 bytes memory I mean minimum value a minimum integer value we can store is a zero and the maximum integer value we can store is a 65,535 right so here it is a simply how can we understand means is a 2 bytes means 1 byte is equal to 8 bit 1 byte is equal to 8 Bits so 2 bytes is nothing but a 16 bit so we have to calculate 2^ 16 value 2^ 16 value is a 65,536 but here it is a storing value starts with a zero so here it is a 0 to 65,535 65,535 so what is that unsigned data type and what is the signed data type means unsigned always stores a positive value so the range of unsigned data type is 0 to 65,535 here it is 2 bytes so we need to find out the value 2^ 16 the value is a 65535 sorry 65536 and here it is a range 0 to 65,535 this is a minimum value we can store and this is a maximum value we can store and next one come to this point here it is using two bytes using two bytes signed data type now what is the minimum value and what is the maximum value here it is 65,536 by 2 just divide the value 65536 by 2 if you divide so what is the value this is 32768 and here is 3 2 76 8 this is half half of 65536 so negative values always starts with A Min -1 -2 -3 and so on - 32768 is okay so negative values up to - 32768 but here it is a positive values starts with 0 1 2 and so on up to 3276 7 only sir why because we have to include 0er also right 0 + 3276 7 so total 32768 values so range - 327682 + 32767 + 3 to 767 so these are the limits of short data type either signed data type or unsigned data type using signed data type so what is the meaning minimum value we can store is a Min - 32768 and what is the maximum value we can store is a 32767 and here it is using unsigned shot the minimum value we can store is a zero and the maximum value we can store is a 65,535 so these are the limits of short data type and next important discussion is in how many ways we can declare the short data type and the format specifiers of a short data type so what are these C now I just want to declare assigned data type variable in my program assigned data type variable in my program how here suppose these are signed declarations signed declarations and followed by unsigned declarations also we'll see see in the Declaration of variable if you don't specify whether the variable is assigned variable or unsigned variable by default that is signed variable by default that is signed that means the variable accepts both a positive value and negative value that is called signed here it is simply you can declare like this variable short a this is Declaration of variable variable able a is of type short type right here it is suppose if you want to write integer also we can add short integer a these two of type short type only sir where is the signed or unsigned I'm not specifying so by default these two variables are signed variables only so here it is Sir can I write explicitly yes you can signed short a it is allowed and signed complete declaration last one signed shot integer a in these many ways we can declare assigned variable into your C program sir unsigned declarations here it is unsigned declarations we must specify explicitly these are unsigned declarations only two ways first one is a unsigned short a and next one unsigned short integer a so these are so total six declarations about short data type first four comes under signed short declarations and second means what remaining two declarations comes under unsigned declarations right in these many ways we can declare and the next important thing is format specifier we can also call it as a format string in a c language so what is a format specifier and what is its importance right if you want to read the information or if you want to display the information formatting is very very important right in which format you have to read the information in which format you have to print the information that you have to specify to the computer so this is called formatting specifier so that's why the name format specifier simply for all the signed declarations all the four signed declarations the format specifier is percentage D these are called format specifiers format specifiers and next one for these two for unsigned declarations format specifier is a person percent is U percent is U these four signed declarations we are processing with the help of the specifier is percentage D and remaining two declarations unsigned declarations we are specifying with a percentage U these are signed and unsigned Declarations of short type and here it is format specifiers we are using So based on this so we'll see right the program pramming on short data type so now we'll see so some of the examples on a short data type right see for example I just want to print a small value on the console here it is we are writing the program execution starts from the main method execution starts from the main method and here it is I'm declaring one variable variable is a short variable short a equals to some value 10 we are storing short a equals to 10 and next if you want to clear the screen clrc clear screen remember clear screen always we need to write after Declaration of all the local variables already we discussed in the local variables concept all the local variables of a block you must place in the beginning statements so after Declaration of all the variables only you have to clear the screen next clear screen function is available in a library is a header file C N.H so I'm including that and next I want to print so what is the function print F function inside the the double quotes we have to write that format specifier what format specifier short format specifier by default it is a signed if you don't mention whether a variable is a signed variable or unsigned variable by default it is a signed variable so format specifier is a signed format specifier and here we are writing the variable that what you want to print a is a variable we just want to print so the value variable value it will print here in this place in this place so to this program the output is Right 10 on the console it will print the value 10 okay so this is a simple program here format specifier is very very important with the help of format specif ERS only we are reading the elements and we are printing the elements here simply I'm declaring one short variable right variable name is a a with the value 10 so 10 it will print next we'll see WR some complex programs okay see so first of all I'm writing right the limits of a signed data type I mean signed short data type in the form of circle observe because based on that Circle only now we are writing this program observe here it is this is a signed short Circle limits are - 3 to 7682 + 32767 + 32767 these limits I'm writing in the form of circle observe in the form of circle we are writing this is signed short Circle signed short that is nothing but percentage D any value you want to count either positive value or negative value starts with a zero counting always starts with a zero positive values counting in a clockwise Direction so like that and the value maximum value is a 32767 positive values counting in a clockwise Direction remember negative values counting in anticlockwise Direction -2 -3 -4 and so on up to - 3276 8 some more I'm writing - 32767 - 3276 and so on see these are negative values in a counterclockwise direction or anticlockwise Direction just limits of a short data type I have written right in the form of circle now based on this now we'll see some programs on a short data type observe here clearly hash include header files I'm including stdio.h and one more header file Co N.H for clear screen purpose Co N.H inside that Main inside the main and here it is we are declaring one short variable short x equal to some 32769 we are storing 32769 and here clear screen clear screen and we are printing the message print F percentage D that x value we are printing observe here clearly the statements executes one by one from top to bottom from top to bottom it will execute and here it is execution starts from the main function the first statement execution here it is X gets memory allocation at any location X get memory allocation X get memory allocation but what is the value you are storing 32769 is the value you are storing what is the value 32769 you are storing So based on this it will count 32769 is a positive value now so it starts counting positive values here 1 2 3 4 5 6 like that it will keep on Counting 3276 7 32768 32769 so 32769 value is equals to - 32767 it will not give any error message it will store one garbage value in the memory so what is the value - 32767 what is the value - 32767 will go and store in the memory garbage value next it will clear the screen next we are printing so whenever we are printing x value it has to collect so from this it will collect that x value what is that x value - 32767 is a x value it will fetch that value from where from the memory it will not look at the Declaration area declaration statement already completed through out no use next we are at the third statement we are printing the X x value we are printing the x value so what we are printing - 32767 we are collecting but it will not print directly what it will do according to the format specifier what format specifier you have given percentage D you have given so percentage D Circle it will check sir where is the percentage the circle here it is this is percentage the circle in this circle the value is present or not it will check what is the value - 32767 value is present or not it will check in the circle yes - 32767 is there so it will print the value output is a - 32767 sir here we are violating the rules now right will it give any error messages no error messages it will store one garbage value into the memory remember right that we should understand because we are doing the mistake so we should analyze clear right we'll see one more program see here it is just like this program so we'll see some more complex programs okay see suppose now I'm changing that x value x value to -4 x value to - 4 4 but here it is explicitly I'm specifying very clearly that this is unsigned short integer unsigned short integer value x value but I'm initializing with the value four so actually unsigned will take only positive values now yes but here it is whenever we are trying to assign the negative value according to that Circle so what is that circle now we have to discuss about unsigned Circle also so unsigned Circle we are writing first the value is a 0 to 65,535 this is unsigned range we discussed already 0 to 65,535 I'm just writing in the form of circle same values form of circle this is percentage U is unsigned Circle and here it is a counting starts with a zero only positive values are there 3 4 4 and so on and so on what is the maximum value is a 65,535 and some more values 65 34 653 6532 values we are writing so this is the range just that's we are writing that range in the circle same story negative values counting in a anticlockwise direction positive values counting in a clockwise Direction one is a clockwise Direction and second one is a counterclockwise direction right positive values counting and negative values counting so here now I'm trying to store the value minus 4 so as usual first X gets memory allocation X get memory allocation x value is a minus 4 right first X memory will be allocated X memory will be allocated but it has to for the minus 4 value but what we have given unsigned you have given so in which Circle it will check in this circle or in this circle this is a signed Circle and this is unsigned Circle so in this circle it will check in this circle it will check so here it is we need to count that -4 value -4 is a negative value so -1 -2 -3 -4 -4 value is equals to 6 5 32 so in the memory so what value will go and store is a 6532 will go and store 65 32 will go and store and now here so we are printing percentage U what is the format specifier I have given percentage U I have given percentage U so first it will fetch that x value so what is that x value please don't look at that first statement in this program because first statement is used to allocate the memory only once you allocate the memory from the second statement wherever you use that variable always it will collect the information from the memory only the best example thing generally for example right I just deposit some amount 8,000 rupees into my account today right I visit the bank and I have to write one receipt right for payment and Bank management first will follow that receipt to perform the transaction by just look at that receipt so they will deposit 8,000 rupees into my account the next day I just want to withdraw 5,000 rupees Bank management will follow receipt or my amount in the memory the concept is later on so they will collect that information from the memory they will not maintain the receipts in the same way so once you declare the variable once you declare the variable in the first statement which is used to allocate the memory only once memory allocation has been completed from the second statement it will follow the memory only so whenever you are printing x value it will fetch that x value 6532 it will fetch so it will collect that 6532 and directly it will not print again it will check what format specifier you have given what is the format specifier percentage U you have given so percentage u means what in this circle it will check percentage U Circle in this circle 6532 is present or not right suppose if it is a present right so then it will print that value yes is there now 653 2 is present so in the output in the output it will print that message 6532 65 32 it will print remember okay and see for example one more thing we'll see this is unsigned value only but I'm printing 65 38 we are storing now the value is 65538 and we are printing percentage uux and one more we are printing print F percentage DX percentage DX we are printing now observe the concept what value will go and store into the memory is first in the memory what value will go and store so you have given ansign and the value is a 65 38 so in which Circle it will check in the second Circle it will check in the percentage U Circle it will check so it will start counting 0 1 2 3 4 and keep on Counting 653 65 34 65 35 65 36 65 37 65 38 so 65 38 value is equal to two is equals to two so here it is in the memory the value two will go and store right it will not store 65 38 right so while counting in that Circle the value 65 38 is equals to two right so in the memory now two is there so first statement completed next is control come to that second statement clear screen and next that control come to that third statement we are printing x value what is the x value two it will collect that value two and here it is what format specifier you have given percentage U you have given what is the format specifier percentage U you have given right so percentage U Circle it will check in this circle the value two is present or not yes the value two is present value two is present so directly it will print the value value two output is a two here next the control come to that fourth statement again it will fch that x value from the memory here it is it will collect that x value from the memory that is two and next format specifier it will check what is a format specifier now percentage D is a format specifier so we need to check in the signed Circle this is a percentage D Circle in this circle two is present or not yes of course here it is in this circle also two is present in this circle also two is present so here also it will print the value two it will print the value two that's it okay so we'll see one last example just look at this see that example suppose here I'm writing some more things this is 3 2 76 and this is 32765 and this is 32764 32764 now here unsigned I'm declaring the value - 32772 this is the value I'm storing 32 772 is a value I'm trying to assign into variable X but X is of type what unsigned remember and here it is we are printing that value print F percentage d x x value we are Printing and once again we are printing that x value but I want to change the format specifier that is percentage u x value we are printing two times first time we are giving that percentage D and second time we are printing the percentage U but sir what value we go and store into X first X get memory allocation at some location what value will go and store unsigned unsigned value minus 3272 so in unsigned Circle - 3272 you have to count - 3 to 72 is a -1 -2 - 3 -4 like that -32 72 you have to count sir it is Heche now yes that's why compare these two just compare these two circles almost same but this is completely positive just this is divided into positive and negative but exactly how many numbers in both the circles are same so while counting so this counting and this counting almost same -1 - 2 -3 -4 and so on keep on Counting - 3 to 768 - 3 to 769 - 32770 - 32771 and this is - 32772 - 32772 is equals to 3 2764 here it is somewhere 32764 will be present in this circle also both circles almost same so here it is - 3272 in place of that what value will go on store 3 to 764 will go and store 3 to 764 will go and store now we are printing the x value so as usual it will collect the x value this is output it will collect the x value what is that x value 32764 not 3272 what is the value 32764 it will collect after collecting 32764 what format specifier you have given is also important what is the format specifier percentage D percentage d means what this is the circle in this circle 32764 is present or not yes 32764 is present so it will print 32764 only 32764 next again X collecting what is it x value 32764 but what format specifier you have given percentage U is a format specifier see here percentage U is a format specifier in this format specifier 32764 is present or not yes once again here it is there so it will print the value once again 32764 it is printing so any relation between input and output no relation we are storing - 3272 and we are printing x value x value one time we have given percentage D and one time we have given percentage U but nowhere we are getting that output is a minus 3272 so computer processing is a completely different depends on the what values we are storing C language will not provide any compile time errors if you are violating the rules instead of giving error messages right it is producing some unexpected results but remember right in a interview point of view they will ask such type of questions only simple questions they will never ask whether you are perfect in a data types are not they will ask the questions such type of questions very complex questions they will ask okay so this is the completely about a short data type remaining data types we'll see in the next session so thank you all thank you for watching for more videos please subscribe to Nares it [Music]