Jump to content

[C language] sum of two integer inside printf function

ratiw

so I'm at the beginning of this wonderful and powerful language, according to deitel & deitel, for a program summing two integers you can either write the sum in one standalone instruction:

 

sum = integer1 + integer2;

printf( "Sum is %d\n", sum );

 

or you can calculate it directly in the printf function:

 

printf( "Sum is %d\n", integer1 + integer2 );

 

I understand the first instructions where the = operator assigns the sum of integer1 plus integer2 to the variable sum and the next instructions displays the value of sum but I don't get the second instruction where the sum is done inside printf becuase in this case no value is assigned to sum so does %d automatically takes the value of integer1 + integer2 skipping the variable sum? How does it work? also what is the name of %d? thaaaaaaaaaaaaaaaaaaaaanksss!!!

 

Link to comment
Share on other sites

Link to post
Share on other sites

%d takes whatever the matching parameter is and treats it as an integer. If the parameter is an expression it will be evaluated and its result is taken, that's generic pretty much anywhere.

 

What you give to printf is a format string, and the %s are placeholders.

https://en.wikipedia.org/wiki/Printf_format_string

F@H
Desktop: i9-13900K, ASUS Z790-E, 64GB DDR5-6000 CL36, RTX3080, 2TB MP600 Pro XT, 2TB SX8200Pro, 2x16TB Ironwolf RAID0, Corsair HX1200, Antec Vortex 360 AIO, Thermaltake Versa H25 TG, Samsung 4K curved 49" TV, 23" secondary, Mountain Everest Max

Mobile SFF rig: i9-9900K, Noctua NH-L9i, Asrock Z390 Phantom ITX-AC, 32GB, GTX1070, 2x1TB SX8200Pro RAID0, 2x5TB 2.5" HDD RAID0, Athena 500W Flex (Noctua fan), Custom 4.7l 3D printed case

 

Asus Zenbook UM325UA, Ryzen 7 5700u, 16GB, 1TB, OLED

 

GPD Win 2

Link to comment
Share on other sites

Link to post
Share on other sites

When you're calling a function with "integer1 + integer2", the result of that operation becomes the argument that's assigned to the parameter of that function.

 

Imagine you have a function like this

function(int n)  {  }

 

When you call function(i1 + i2), the result of that addition is simply assigned to the "n" variable that goes into the function.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

none of the above answers actually answer my questions. Open to clearer replies thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

All the %d does is print the value of whatever is passed in after the fact. 

 

So, yeah, in your second example, it skips the variable, because you've never defined it -- instead, it just evaluates "in place" and then uses that value.

 

Variables aren't magic things, really, they're just containers for something.  Think of it like this.  Assigning "sum" here tells it "hey, find this container and use the contents" (since the contents are what matter).  If we just pass in 42, we've given the contents directly.  With me so far?

So all we're doing with the second example is giving the contents with "some assembly required".

Link to comment
Share on other sites

Link to post
Share on other sites

When using printf, %d is a placeholder that means, "a decimal or integer number will go here". Whatever is after the comma is used to fill in the placeholder.

 

The number that replaces the %d is whatever is after the comma. So as long as integer1 + integer2 come out to be a integer or a decimal number, they will take the place of %d. The math is done before the substitution happens.

 

If you have multiple %d's listed in your printf, you can put multiple integers or decimal numbers separated by commas after the string, and they will be used in order.

 

Doing the math within the printf avoids creating the variable 'sum'. The math is done, the substitution is made, it prints the sentence, and it moves on... the result is not stored anywhere for you to use later.

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, ratiw said:

none of the above answers actually answer my questions. Open to clearer replies thanks!

if the answer from Kilrah and Eigenvektor isnt clear enough you have to word your question better

MSI GX660 + i7 920XM @ 2.8GHz + GTX 970M + Samsung SSD 830 256GB

Link to comment
Share on other sites

Link to post
Share on other sites

Less technical:

%d essentially means it's expecting an integer...I think they used d originally from the word "decimal".

%d specifically doesn't have a name...it's part of the format string

Like you could have "%d %d %d" and then in that case you need to give the print function 3 integers.

 

There is no need for the sum variable, it's sort of like reading math, you could have x = 1 + 2, f(x)...or you could write it f(1 + 2) .

 

The way I like to approach reading code is to treat it as a math equation really.  Some things are just almost implied.

 

Like

y = i1 + i2

z = i3 + i4

 

a = (i1 + i2) * (i3 + i4)

b = y + z

 

Both a and B are equivalent, and internally if you were calculating it you would effectively be doing the same thing.  The compiler pretty much handles everything, as there really isn't an operation where it's add a few numbers and multiply together...instead if breaks things down to move i1 to known location in memory and then add i2 to that memory location.

 

 

 

More technical (not strictly correct, but close enough):

All variables are essentially just areas in memory/CPU registers

When you call something like printf it loads the value into the correct CPU registers.  So in the case of having

su

sum = i1 + i2
printf("%d", sum);

Might compile to something like
...
mov ecx, i1
add ecx, i2
mov eax, ecx
mov ebx, "%d"
call printf

Assuming that printf takes in the CPU register ebx as first argument and eax as the second argument.  Not though eax is essentially just a piece of CPU memory that is 32/64 bits (so it's essentially holding an integer value).  Notice how ecx register is holding the "variable"...it would do this because you might need to use the sum later on

Now something like this
printf("%d", i1 + i2);

Now this would more likely compile into something like this
mov eax, i1
add eax, i2
mov ebx, "%d"
call printf

I mean strictly speaking though compiler optimizations would actually condense everything down.

3735928559 - Beware of the dead beef

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, ratiw said:

none of the above answers actually answer my questions. Open to clearer replies thanks!

What exactly is still unclear to you? Maybe try to be more clear about what you don't understand, rather than insulting the people trying to help you.

 

printf is a function that takes multiple arguments. The first one is a string, that you want to print. That string can contain placeholders like %d, %s etc. It also takes a variable number of additional arguments, that are used to replace these placeholders. %d is simply a format specifier that says in integer is expected here.

 

In your example you're performing some math, than pass the result into printf as the first additional argument.

sum = i1 + i2
printf("something %d", sum)

 

The arguments that you pass to printf will be assigned to some other variable inside that function. Imagine the signature of the printf method looks something like this (not correct, but hopefully enough to understand what I'm trying to explain)

 

printf(string output, int arg1) {
   // do something to replace placeholders
   // print the resulting string to the screen
}

 

So if you call printf like this

printf("something %d", i1 + i2)

 

Then internally the result of that addition is simply assigned to "arg1", which serves exactly the same purpose as if you'd manually assigned it to sum beforehand.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

On 3/24/2023 at 10:15 PM, ratiw said:

in this case no value is assigned to sum so does %d automatically takes the value of integer1 + integer2 skipping the variable sum?

In the second case the variable "sum" doesn't exist and it doesn't need to, nor does it need to be specifically called "sum" in the first example. The name of a variable is completely irrelevant in C as long as it's not a duplicate. The only difference between the two examples is that in the first one you store the result of the addition inside a variable (sum) and then print that variable. In the second case the result is assigned to a temporary hidden variable inside the printf function which is then lost once the function has finished executing.

 

It might be easier for you to grasp if you consider "+" to be a function taking two integers as arguments and returning an integer:

int sum(a, b){
  return a+b;
}

int main(){
  printf("%d", sum(1,2));
  return 0;
}

 

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×