Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Generic User Avatar

Use printf function to sum and display two integers


  • Please log in to reply
3 replies to this topic

#1 sorjil

sorjil

  •  Avatar image
  • Members
  • 17 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:07:12 AM

Posted 25 March 2023 - 09:13 PM

I'm at the beginning of this wonderful and powerful language! according to deitel & deitel, for a program that sums two integers you can either write the sum in one instruction and then display the result in another instruction like this:

sum = integer1 + integer2;

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

or you can calculate the sum directly in the printf function and display it all in the same:

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

My doubt/questions:

I understand the first instruction where the = operator assigns the value of integer1 plus integer2 to the variable sum and the next instruction displays the value of the variable sum but I don't get the last instruction where the sum is done inside printf because in this case there is no value assigned to sum and there is no variable sum used.

I wonder: in the last instruction does %d automatically take the value of integer1 + integer2 and it skips the variable sum? How does it work in this instruction without actually using the variable sum? also what is the name/use of %d?



BC AdBot (Login to Remove)

 


#2 ctigga

ctigga

  •  Avatar image
  • Members
  • 204 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:01:12 AM

Posted 26 March 2023 - 02:45 PM

Hello!

 

I don't get the last instruction where the sum is done inside printf because in this case there is no value assigned to sum and there is no variable sum used.

 

The printf function does not require a variable named sum to operate.  The value of sum only matters to printf if "sum" is explicitly referenced as a parameter (such as the first example you referenced)

 

 

I wonder: in the last instruction does %d automatically take the value of integer1 + integer2 and it skips the variable sum? 

Yes, exactly.  A temporary (unnamed) variable is used to store the result of (integer1 + integer2).  The variable "sum" has no significance to printf unless it is explicitly specified in the call to the printf function.

 

 

What is the name/use of %d?

 

%d (in the context of printf) is a type of format specifier.

A format specifier tells printf what type of data you are providing.

%d (or %i) mean the same thing: a signed integer.  Whenever printf sees %d it know it will be reading a variable that is an unsigned integer in size and displaying it as a signed integer in the output.

 

There are many other format specifiers and modifiers that can be combined to format the display of data, as needed.

Keep reading the book -- this will likely be covered a little later on.

 

https://cplusplus.com/reference/cstdio/printf/?kw=printf

 

Good luck!



#3 sorjil

sorjil
  • Topic Starter

  •  Avatar image
  • Members
  • 17 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:07:12 AM

Posted 27 March 2023 - 07:54 AM

 

 

Good luck!

 

Thanks for your help!



#4 ctigga

ctigga

  •  Avatar image
  • Members
  • 204 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:01:12 AM

Posted 28 March 2023 - 02:30 AM

 

Thanks for your help!

 

 

My pleasure. 

One small correction to my prior post:

 

"Whenever printf sees %d it knows it will be reading a variable that is a unsigned signed integer in size and displaying it as a signed integer in the output."

 

(I'm sure you understood from my initial message, but wanted to highlight the correction for anyone else who may stumble across this post in the future)


Edited by ctigga, 28 March 2023 - 02:34 AM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users