Programming course in MAC ( part 3 )
If you don't read previous article and not understand about coding then please go back in tech section and learn from starting Part 1 .
An integer has a range of
values that it can hold, and therefore a specific amount of
memory is allocated for an integer. So I like to keep that
comparison in mind, right? Sized box for right size value. This values stored. We have X, Y, Z age GPA, and exact. If this variable to get
a print F statement, I can do that. The value of X. Now, again, that's just a string going to be output,
but here I'm going to put percent C X, and you'll recall that X was
the first variable we declared. I can do the same thing with Y and Z.
So print F the value of Y. And again, percent C is
our character entity. That's where the actual
value is going to go. The value is going to be substituted
for that percent C C being a character entity for a character or chart variable. And then the final char value, which is Z again, percent C because it's a
chart or character variable Z. So that's going to retrieve
the values stored in X, Y, and Z. Let's go ahead and compile this. This is called variables dot C. So GCC variables dot C dash O M.
We'll simply call the
executable variables. All
right. No errors. That's good. So now we should be able
to run this with dat slash you see the value of X, the
value of Y and the value of a, what I didn't do there was
use new line characters. Let's recompile it with
the new line characters. So our output looks a little bit better. I'm just going to use the up arrow to
find the previous command we'll compile again, and we'll run the new
executable that looks a little better. The value of X, the value
of Y and the value of L. So we've been able to
retrieve the char values, but what about the integers
and the floats? Well, print backslash M Mark is
Percent D for integer years old, and this time the variable is age.
So that'll make that substitution. Let's do the flooding
point variable print again, we'll start the backslash. And my GPA in college
was since this is a float percent F comma, and
we're going to use GPA, all right, let's save these new changes
and let's execute again. You have to compile first cause
our source code has changed.It show some errors here, print F declared here in it. Let's see where our error is. And I see it just the type of,
Instead of print F I just
typed print. And by the way, these errors are pretty helpful because
they tell you exactly what's wrong here. It even says here, note,
did you mean print F? So I was able to look at that and know
exactly what I did wrong. It's actually giving us all of the
precision available in the floating point number. We can change that if
we want to for less precision. So let's change this to 0.2 F let's compile again
And there. You can see we get 3.17. The
only other change I don't make this, just eliminate that space there in the
string. And now everything will come out. Just perfect. All right. So
we've declared variables. We've used character entities
to output those variables, and we've used both integers
and floating point numbers. Sometimes you want to hold a value, but you want to ensure it doesn't
change to the life of your program. Instead of calling this a variable, we call this a constant
because it's value is constant. We're still going to be interested
in the right size or the right amount of memory being allocated
for the constant. But we know that if we try and change the
value of a constant during the life of
Our program, we're actually
going to get an error. All right, let's take a look at Constance. Now inside C creates Constance using a
pre-processor directive called define. So we could define,
for example, length at 10, we could define with at 25, and we could even define new line as backslash ed what's going to
happen is any time that the C processor encounters
one of these constants, it's going to substitute the
value. So let's, for example, declare a variable, an
integer called area, which will be length times with, and let's initialize it with wet length
times with using the constants in our
Pre-processor directive area
equals length times width by convention. Our constants are going
to be in all capitals. Alright, so let's print
out the results here. So we use print F the area is, and since this is an integer, we're going to use our friend percent D area. All right, looks like
we've got everything here. Let's go ahead and compile this using GCC. It's called constant stats. See, we'll just make our executable
be called Constance compiles. No problem.
And then we get the area is two 50. So that worked just fine. Since new line is a character, we could use that constant as well. Let's say roses are red so percent see for character. And then new line roses are roses are red. Violets are blue. Again, character entity
percent. See new line is, are constantly let's
recompile on one test, no errors on compile. And there you
can see we've got roses are red. Violets are blue. Probably want to add one
more new line in there.
I'll add it just here, this way, the area and the poem are
on separate lines and let's recompile for the last time. And that looks great. Perfect
output. I know what you're thinking. I suck at math. Doesn't really matter. What we're going to be doing is
using the mathematical operators to perform the math for us. C is
very good at math and very fast. So let's take a look at the different
arithmetic operators that are available to you in the C programming language, in order to demonstrate
mathematical operators, we need to have a few values in memory. So let's declare a couple
of integer numbers.
This article is uploaded on ArshamXtarr.blogspot.com. Please Do not try to reupload this article. It is fully copyrighted protected.

0 Comments