• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Caleb Curry

Making Money and Investing

  • Home
  • Make Money Online
  • How to Buy Bitcoin
  • Get Started with Real Estate Investing
  • How to Start a Website
  • Courses
    • Full Time Income Online
  • Sponsorship

C Programming Tutorial 10 – Print Variables using Printf()

October 17, 2018 by Caleb Curry Leave a Comment

Make sure you read the last blog on Variables, Expressions, and Statements! Are you new here? Start from the beginning of the series!

In the last blog 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?

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

We can also add a newline character in here:

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.

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.

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:

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

Later there will be a blog devoted to format strings, conversion/format characters, and format specifier (begins with a % followed by a conversion character).

In the next blog we are going to learn about taking input from the user! Check it out!

Filed Under: Uncategorized Tagged With: c programming, print variables using printf()

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

my face to show my face

My name is Caleb Curry and I started my online business teaching software development and doing digital marketing for brands. Entrepreneurship has changed my life and has allowed me to earn a full time living from anywhere. I now have the freedom and energy to pursue skills and opportunities that I find interesting. This website is all about my journey.

Get my top Python Tips and Tricks

50+ Python Tips and Tricks!

Subscribe to our newsletter to get notes and latest content by email.

    We won't send you spam. Unsubscribe at any time.

    Follow Me!

    • Instagram
    • YouTube

    Recent Posts

    Copyright © 2021 · Genesis Sample on Genesis Framework · WordPress · Log in