1

Im really new to C and coding in general so sorry if this is a simple problem.

All I'm trying to do is input some numbers, get them to be added up, and then print the sum in two different ways. However for some reason the printf("%li", z); line of code is only printing the amount of digits in the number plus 1.

Could someone explain to me why exactly it is doing this, please? The why what's important, I'm not actually too bothered about getting it to work since I'm just playing around and practising. Thanks!

My code is as follows:

#include <cs50.h>
#include <stdio.h>

int main(void) {
    long x = get_long("what is x: ");
    long y = get_long("what is y: ");
    long z = printf("%li\n", x + y + y);
    printf("%li", z);
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
UgaBuga
  • 13
  • 2
  • 1
    The return value of `printf` is the number of characters that were printed, not the printed value itself. (That can't be done. What should the value of `printf("Hi!")` or of `printf("%s, %s!", a, b)` be?) – M Oehm Jan 10 '21 at 15:41
  • 1
    `printf()` returns the number of characters that are printed, its +1 because you print the `newline` character – alex01011 Jan 10 '21 at 15:41
  • Did you look at the documentation for [printf](https://en.cppreference.com/w/c/io/fprintf)? The return value is the *number of characters transmitted to the output stream* – Kevin Jan 10 '21 at 15:41
  • You "want" (for suitable definition of "want"): `long z; printf("%li\n", z = x+y+y); printf("%li\n", z);` – pmg Jan 10 '21 at 15:48

2 Answers2

1

The mistake is in the line

long z = printf("%li\n", x + y + y);

The reason printf("%li", z); prints the number of the digits in the number plus one is that printf is a function that returns the number of characters written on the output stream (stdout -- the console -- in your case) and here printf("%li\n", x + y + y); you're trying to write x+y+y followed by \n (which is an additional character).

printf prints out a formatted string on a file called stdout. Read this for more details.

Please, if you are new to the C language, I advise against trying to learn more "advanced" stuff like printf or scanf (and *printf or *scanf function family) without having the proper basics (variables, pointers, dynamic memory allocation, files). If you want to try out small programs to see if they succeed you can use a debugger (gdb). But in order to understand the use of those functions you need to know some basic things, or you will end up with magic syntax like scanf("%s %d", mystr, &myint) and you wouldn't know why mystr does not need the & whereas myint does. So, please, if you don't have these basics, learn them before going on with IO on files (stdout and stdin are files).

LuxGiammi
  • 631
  • 6
  • 20
  • Thanks for the detailed answer, I really appreciate it! And ill look into the other stuff, I'm self-teaching myself so yeah, id missed all of that. – UgaBuga Jan 10 '21 at 16:16
  • @UgaBuga You're welcome. A lot of books and tutorials use `printf` and `scanf` without explaining anything about pointers and other important things needed to understand them properly. If you find no good books that explain things from the basics, *at least* keep in mind that those IO functions uses 1. Files (stdout and stdin) 2. pointers 3. variadic arguments; so it's natural to not fully understand what they do at first, but when you will have completed your book, come back to those functions and understand that there is no magic under those functions. – LuxGiammi Jan 10 '21 at 16:27
  • One more thing: it might be more time consuming (and more abstract) using a debugger for inspecting variables rather than using `printf`, but in my opinion it is worth the more time and effort. If you find yourself stuck with the debugger, my personal advice is to use `printf` only at the beginning and hard code everything else (or use arguments: https://stackoverflow.com/questions/4176326/arguments-to-main-in-c), at least until you study IO on files. – LuxGiammi Jan 10 '21 at 16:30
1
#include <cs50.h>
#include <stdio.h>

int main (void)
{
long x = get_long("what is x: ");
long y = get_long("what is y: ");
long z = x + y + y;
printf("%li", z);
}
nycaff
  • 144
  • 1
  • 5