C Programming Tutorial 12 – C Basics Part 4 – Arithmetic Expressions

In the previous video we learned how to take input and put it on the screen.

Now, let’s create a new variable, area, and make a formula.
Before we start working on that formula, we need to discuss operators. An operator is a thing that takes some value and does something with it. We’ve discussed the assignment operator. The assignment operator takes a value and assigns it to a variable.

When it comes to writing formulas, we have to use arithmetic operators. You’ve seen some of these I’m sure, but just to make sure you know the most basic important ones:

+, -, /, and *.

We also have to consider the order of operations. / and * happen first from left to right, and then + – happen next.

We can also use parenthesis to force certain operators to go first.

Now, let’s write that formula.

int area = 3.14159 * (radius * radius);

Now, do you guys notice anything wrong with this code? This is a perfect example of what some may consider to be a logical error. A logical error is when your program compiles, but it doesn’t do what you expect it to do.

Take a moment to see if you can spot the problem. Let’s run it and see what happens.

The problem is that we are storing this data as an integer. This is giving us results we are not expecting because values are being truncated to be an integer. Truncation is when we cut off the decimal values and do not round. so if we have 1.999999, it gets truncated to 1.

To fix this, we have to introduce some new data types. This int here is an example of a data type. A data type is a description of data that tells C how to interpret that data. To use decimals, we want to use a new data type called double.

When we set everything to double, we are a step closer, but there is another problem. These format strings, the %i is telling C that we are expecting integers. We are also going to have to change that to a new format which uses %d. Now everything should work as expected.

And this is our very first useful program, it takes a number, runs it through a fancy pants mathematical algorithm and spits it back out. You can take what you’ve learned from this video and write software to solve some of your math homework.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Support me! http://www.patreon.com/calebcurry

Subscribe to my newsletter: http://bit.ly/JoinCCNewsletter

Donate!: http://bit.ly/DonateCTVM2.

~~~~~~~~~~~~~~~Additional Links~~~~~~~~~~~~~~~

More content: http://CalebCurry.com
Facebook: http://www.facebook.com/CalebTheVideoMaker
Google+: https://plus.google.com/+CalebTheVideoMaker2
Twitter: http://twitter.com/calebCurry

Amazing Web Hosting – http://bit.ly/ccbluehost (The best web hosting for a cheap price!)

Comments

Andis Aboltins says:

Value of a radius can only be whole number in this case.How to write the program to accept both, whole and decimal numbers?

Codingmaster says:

so doubles are floats ?

Hafsa Khalid says:

iy’s blur..

Yaroslav Birchenko says:

Your really cool man, thanks for these awesome possum vids. Satisfaction))))

Rajasthani Rasoi Ghar says:

but we can also do printf(“the number is %fn” , radius *2*3.14);

Navdeep Kaur says:

Is it correct to use %d instead of %i for a single integer value??

average Joe says:

#include
int main()
{
double pepsi;
printf(“enter the price of one pepsi “);
scanf(“%f”,&pepsi);
printf(“u entered %f”,pepsi);
return 0;
}

it says u entered 0.000000

Subrat Srivastava says:

Is double a data type same as int, float, char etc??? What is the memory it takes??

Write a comment

*

Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.


Do you like our videos?
Do you want to see more like that?

Please click below to support us on Facebook!

Send this to a friend

▷ Other ReviewsVehicles▷ Show Cars▷ Motorbikes▷ Scooters▷ Bicycles▷ Rims & Tires▷ Luxury BoatsFashion▷ Sunglasses▷ Luxury Watches▷ Luxury Purses▷ Jeans Wear▷ High Heels▷ Kinis Swimwear▷ Perfumes▷ Jewellery▷ Cosmetics▷ Shaving Helpers▷ Fashion HatsFooding▷ Chef Club▷ Fooding Helpers▷ Coktails & LiquorsSports▷ Sport Shoes▷ Fitness & Detox▷ Golf Gear▷ Racquets▷ Hiking & Trek Gear▷ Diving Equipment▷ Ski Gear▷ Snowboards▷ Surf Boards▷ Rollers & SkatesEntertainment▷ DIY Guides▷ Zik Instruments▷ Published Books▷ Music Albums▷ Cine Movies▷ Trading Helpers▷ Make Money▷ Fishing Equipment▷ Paintball Supplies▷ Trading Card Games▷ Telescopes▷ Knives▷ VapesHigh Tech▷ Flat Screens▷ Tech Devices▷ Camera Lenses▷ Audio HiFi▷ Printers▷ USB Devices▷ PC Hardware▷ Network Gear▷ Cloud Servers▷ Software Helpers▷ Programmer Helpers▷ Mobile Apps▷ Hearing AidsHome▷ Home Furniture▷ Home Appliances▷ Tools Workshop▷ Beddings▷ Floor Layings▷ Barbecues▷ Aquarium Gear▷ Safe Boxes▷ Office Supplies▷ Security Locks▷ Cleaning ProductsKids▷ Baby Strollers▷ Child Car Seats▷ Remote ControlledTravel▷ Luggages & Bags▷ Airlines Seats▷ Hotel Rooms▷ Fun Trips▷ Cruise Ships▷ Mexico Tours