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?


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(“%in”, 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(“%in”, 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 %in.

printf(“The value of x is: %in”, 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: %inThe value of y is: %in”, 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!

Subscribe to my newsletter:


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

More content:

Amazing Web Hosting – (The best web hosting for a cheap price!)

Nguồn: https://taibangoc.com/

Xem thêm bài viết khác: https://taibangoc.com/tong-hop/


  1. Learn Javascript – http://bit.ly/JavaScriptPlaylist
    Learn Java – http://bit.ly/JavaPlaylist
    Learn C# – http://bit.ly/CSharpTutorialsPlaylist
    Learn C++ – http://bit.ly/CPlusPlusPlaylist
    Learn C – http://bit.ly/CTutorialsPlaylist

  2. Does it matter if during "printf("The value of x is: %inThe value of y is: %in", x, y);" step that i capitalize X or Y. Like when he says 4.33 that "the value of "y" is gonna be imputed with %i". How does the program know this since its written in the Argument part of printf.

  3. I fucked up while reading K&R and made a text to ascii code converter by accident, can't think of a use for this but here u go
    int i;
    while((i = getchar()) != EOF) // input from keyboard, looped
    printf("%d ", i); // output character/string's integer value
    return 0;

  4. Caleb, I enjoy all your stuff. Focusing on this tutorial, using the "%i" command in vim/linux works perfectly.
    However when writing the same line in note++ the following error is returned when trying to compile.

    gcc subscribe.c
    gcc subscribe.c
    Process started (PID=12456) >>>
    subscribe.c: In function 'main':
    subscribe.c:16:3: error: too few arguments to function 'printf'
    In file included from subscribe.c:1:
    c:mingwincludestdio.h:462:38: note: declared here
    _CRTIMP __cdecl __MINGW_NOTHROW int printf (const char *, …);
    <<< Process finished (PID=12456). (Exit code 1)

    Is this a mingw thing?
    Any feed back would be appreciated.

  5. Your milage may vary, but:
    gcc subscribe.c -o subscribe
    [return key]
    Now you have a file name that actually makes since and is easy to recall.

  6. Just leaving a like and a comment to show that I have watched the particular video and following the playlist. You're awesome teacher _ ./. _ Sorry for boring comment, I have no time to write a vanilla comment for specific videos of the playlist since I am busy following the playlist and haven't run into any issues yet. I will comment if I find any 🙂

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


Please enter your comment!
Please enter your name here