Setting values to use later

- In order for programs to do anything usefulobviously they require data.Data is nothing but memory labeledby what we call variables.Data types and variables are strongly linked together.Data type describes what type of storagewill be placed into that memory locationwhether it's a number or a letterand if it's a number, what type of numberand so on.In order for you to use a variableyou have to inform the compilerC in this casethat you have an intention of using that variable.

We call this declaration.Variables could be decimal numberscalled floats in C.For example, let's a variable called "height"which we wanted to be of type floator it can be a characterand we want the variable to be called letter.Or we wanted to refer to age as an integer number.How can you decide what'sthe most appropriate variable for a given instance?Let's look at the code.For example, if you want to look atthe variable called "priceOfGasInCalifornia"and I know it's 99 centsthe most appropriate type would be float.

Float is a decimal numberthat doesn't require too many digitsor in computerese, not too much higher precision.Another variable is "y"which is clearly defined as an integer.For now it has the value 0.If you look at line 19, later onthe same variable "y" gets a new value "12".Over time, one variable can receive many valuesdepending on what happens in the code.If you still like integersbut you don't need to usethe full memory size that they requirethere's another type of integer called "Short".

In this case, line 17the variable "a" is a short variablemeaning it's an integerbut doesn't require that much space.As you improve your programming skillsyou will be able to make a clear determinationabout the space requirementand the memory requirementsand how much memory your entire program is usingand this will inform your decisionabout variable types to use.Line 18 is another type of a decimal pointwhich in C it's called double.Double means it's a floatbut twice the memory requirements.

In this case radius 2.34567.Now that we've defined the variableswe can use them.In this case, all we do with those variablesis just printing them.Look at the focus I placeon the different formatting with a print+f.Print+F is a function that's already available in Cwhich allows you to format the printingfor specific strings of words, of charactersthat are labeled for the variables and datathat you print.If you look at line 21because the "priceOfGasInCalifornia"is a float, we're going to usethe designation F which is the %f.

On line 22 and 23because "y" and "a" are integersthe designation in print+F is "d".Line 24, because our designationfor "radius" is doublewe're using the directive "lf"which stands for long floator double the float.Unless you put the exact matching designationin the print+f you may either not print a value at allor you may print the wrong valueso it's really important that you matchthe type of variable with a type of directiveinside the print+f function.

Let's see how this works.When the program runsnotice a few interesting things.The default for the price of Californiais six decimal pointsunless otherwise specifiedthe default format in print+fis six digits of the decimal point.This is why 0.99 is 0.99 and for zeroes.Integers are unchanged.The double float is printing the same thingas the float.

That's why you have that extra zero.If we had the larger numbers thoughthey just would have been printed as well.

Resume Transcript Auto-Scroll

Author

Released

6/30/2014

Widely used for both systems and application development, C is a powerful language at the heart of many more modern languages, like JavaScript and Objective-C. In this course, professor Isac Artzi dissects the anatomy of C, from data types, strings, and variables to simple functions. He'll show how these basic elements fit together in control structures like loops and if-else statements—where the real logic behind your code comes into play. Plus, learn how C organizes information into arrays and how you can manage memory resources with pointers.

C is a great first step for new programmers, and a way to broaden and deepen your knowledge if you've already programmed for a while. Press Play to start learning.