C Programming Tutorial 10 – C Basics Part 2 – Print Variables Using Printf

In the last video we created variables x and y and gave them values, and I told you what they would store. But what if you didn’t believe me and wanted to see for yourself? The easiest way to do this is to use the pritntf() function.

Now, we’ve used the printf() function to print a sentence in the terminal that said hello world, but now we are going to be learning how to print other things to the screen.

Let’s first start with printing a number. What if we pass in just the value 9001 in to printf?

printf(9001);

In order to run this to see what happens, we actually have to recompile our program. Every single time we make any change to our program we have to recompile.

You can see that when we run it we get a compiling error. Let’s go back to our code.

The reason we are getting a compiling error is because printf does not know how to work with numbers like this. In order to tell printf we want to print a number, we have to give the printf function what is known as a format string. A string is anything inside of quotes, usually consisting of characters, such as “Hello World!\n”

printf(“%i”, 9001);

We can also add a newline character in here:

printf(“%i\n”, 9001);

This works!

Now, we can actually do the same thing with variables. That’s because, if you remember me saying, an int variable can be used anywhere you are expecting an integer.

printf(“%i\n”, x);

This works great! The only problem is the program is not very descriptive when it runs. All it does is print the value. What if we want to do a bit more? We can actually add some of our own text inside of our string too. The only part that gets interpreted is the %i\n.

printf(“The value of x is: %i\n”, x);

If you want to print both variables, of course you could use two printf functions, but you can also use multiple variables inside of one printf function:

printf(“The value of x is: %i\nThe value of y is: %i\n”, x, y);

Each thing passed into the printf function is known as an argument. In this situation we have three arguments: our string, and two integers.

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

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

Subscribe to my newsletter: http://eepurl.com/-8qtH

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 – https://www.dreamhost.com/r.cgi?1487063 (The best web hosting for a cheap price!)

Comments

Mehdi Bellafdil says:

set number

Subrat Srivastava says:

Short, simple, awesome!!!!!!!!

陈卡尔 says:

Are %d and %i are the same ?

Mano8881 says:

Damn you’re right, some tutorials are just crap out there, someone just typing and not explaining a thing, thumbs up dude!

Kristoffer Salonga says:

Thanks! Very helpful!

Khan Sarbani says:

its over 9000!!!11

hightttech says:

Like your style.

awsaf # says:

./a.out is not working…

Santanu Bs says:

Can’t you put 9001 in double quotes

Codingmaster says:

for compiling just do gcc [name] & ./a.out

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   Rims & Tires   Luxury BoatsFashion   Sunglasses   Luxury Watches   Luxury Purses   Jeans Wear   High Heels   Perfumes   Jewellery   Cosmetics   Shaving Helpers   Fashion HatsFooding   Chef Club   Fooding Helpers   Coktails & LiquorsSports   Sport Shoes   Fitness & Detox   Golf Gear   Racquets   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   Printers   USB Devices   PC Hardware   Network Gear   Cloud Servers   Software Helpers   Programmer Helpers   Mobile Apps   Hearing AidsHome   Home Furniture   Home Appliances   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