Constants & Variables in C Programming Language | Decoding Computers

Constants & Variables in C Programming Language | Decoding Computers


Both paternal as well as maternal grandparents love their grandchildren more than their own kids There’s a widely used proverb in Hindi best describing it “Love for interest is more than principle amount itself” and this is true literally also You ask someone who’s into Money Lending business even Banks They all love earning Interest on the money rather than the Principle amount itself C’mon, let’s write a program code (in C) today which will calculate the interest amount and with this program we’ll learn all about Variables & Constants in C Here’s the program we’ll talk about today That looks to be quite complicated, right ? But, trust me, it’s not (complicated).. C’mon, let’s first understand it’s various components Once, you get to know them & their purpose, the whole program will seem like child’s play Let’s start with understanding ‘Identifiers’ which are marked right now on your screen An Identifier is nothing but a unique name given to different types of C entities like Constants Variables Functions Structures, etc.. A few basic rules to give these (Identifier) names are : An Identifier name can be composed of Lower Case Letters Upper Case Letters Digits & UnderScore Remember : None else than these not even a Space The first letter of an identifier should be either a Letter or an UnderScore But, it is discouraged to start the identifier name with an UnderScore though it is legal It is because the identifier that starts with UnderScore can conflict with some system file names such as ‘_fileno’, ‘_iob’, ‘_wfopen’, etc.. Keywords are not allowed to be kept as Identifier names Identifiers are also Case-Sensitive in C e.g., ‘amount’ & ‘Amount’ are two different identifiers in C Same name cannot be kept twice as identifiers So, the identifier names have to be unique Like we don’t keep same names for kids in same house There is no rule for the length of an identifier So, you can keep it as lengthy as you like Hehehe, yeah, you can keep that too.. But if it is meaningful, then it will be easier for you to understand & work on, especially when the program code is large Just like in our program here ‘amount’ ‘rate’ ‘acc_type’ ‘interest’ which clearly tells us what these names stand for Though it is legal, but depending on the OS there maybe problem if the name is bigger than 31 char But who keeps such long names anyways.. Okay, now let’s learn the different types of values that can be assigned, calculated or printed i.e., let’s learn about ‘Data Types’ Basically, when you define what type of data is being referred to, the computer allocates that much memory space You didn’t get that, did you ?? C’mon, let me explain Broadly Data Types are of two types in C Primary & Derived Primary data type means that data can either be ‘int’ ‘float’ ‘char’ or ‘void’ The numbers in front show the memory assigned respectively ‘floating point’ means ‘decimal numbers’ Since the ‘amount’ is in round figures In our program so, we define it as ‘int’ type ‘rate’ & ‘interest’ can be in decimals so, we define them as ‘float’ type And since the ‘acc_type’ was of saving type so, we define it as ‘char’ S But remember to write the letter within single quotes Note: Write the Data Type BEFORE the Identifier Name Derived Data Types are nothing but Primary Data Types , just a little twisted or grouped together like ‘array’, ‘structure’, ‘union’ & ‘pointer’ Don’t worry, we’ll learn all about these in later sessions Now comes, ‘Constants’ & ‘Variables’ In simple terms, ‘Constants’ are those entities whose value NEVER changes in the program There can be any type of Constants like integer float character string octal hexadecimal, etc.. To define a Constant, we have to add a Keyword ‘const’ before the Data Type OR we can also define it as Preprocessor Directive, like this A ‘Variable’ is a storage place which has some memory allocated to it Basically, it is used to store some form of data Unlike defining a Constant, no Keyword is required (generally) to be written before the Data Type Now, pls note that we’ve written these two Variables in different form This is where we’re defining the Variable and assigning it a memory (location) as it can have any value later in the program And this one here is called a Declared Variable because we’re not only defining the variable here but also declaring a value before its first use Let’s see the Operators used in C Plus Minus Multiplication Division Remainder / Percentage Equal To Less Than Greater Than Ampersand Less Than Equal To Greater Than Equal To Double Ampersand Double Plus and so on… We’ll be using them as our program becomes complex For now, we’ll be using only only Arithmetic Operators, i.e., Plus, Minus, Multiplication, Division & Remainder Let’s see what these are These are called Format Specifiers What they do, is evident from their name Obviously, they format the output the way we want In C, ther’re many Formet Specifiers, like But, we’ll be using them as we go further in our complex programs For now, we’ll be usig ‘%d’ Now, someone has to tell the computer that when it’s written like this , then which value to be printed So, it is here that we tell it that whatever value is there in this variable, it is to be printed Same way, for this, we tell it to take the float value of this & print it Similarly for these also, respectively Now, we must ensure that these (Format Specifiers) and these (Variables) are of same Data Types, respectively Let’s now Compile & Run our program Right now, its giving so many places after decimal So, we introduce (a Format Specifier) here and here, to get more User-Friendly output like this Voila!!! The programs run to really long lengths.. Say, later in your program , you need to change the ‘amount’ variable & then again calculate the interest You can do it like this Let’s see the output Excellent !! Now suppose we wanted to change rate of interest later in program , we would probably write this It will not work as i told you in the beginning that Constants cannot be changed once defined in the prog These are basics which will come in handy and we would be learning in later sessions as and when the need arises

4 thoughts to “Constants & Variables in C Programming Language | Decoding Computers”

Leave a Reply

Your email address will not be published. Required fields are marked *