Understanding Printf Formatting in C for Mathematical Operations

  • Thread starter Spectre32
  • Start date
In summary, the equations E = [(delta * beta) / gamma] + (c / b) and F = (d / b) % d can be solved by plugging in the given values for delta, beta, gamma, c, b, and d. The result for E is 5.333, but the result for F is 2 due to the truncation of the integer division for c/b. To avoid this, one can cast the ints into doubles before performing the calculations. However, in this case, only one of the ints needs to be cast as the remaining int(s) will be promoted automatically.
  • #1
Spectre32
136
0
Ok perhaps I'm not grasping how C does math. I have these few questions:

E = delta*beta/gamma+c/b

F = (d/b)%d;


The print formats for those are %2.3lf and %3d respectively. Here are the values:

delta = 4 .000
beta = 4.0
gamma = 3.0
c = 2
b = 3
d = 7


c,b,d are type int and the rest are type double. On my answer sheet they says the answers for those two are E = 5.333 and F = 2 i have the spaces down, but I'm having a problem getting to those answers.

Any help would be great.
 
Computer science news on Phys.org
  • #2
E = [(delta * beta) / gamma] + (c / b) = 5.333 + 0. The result of c / b is truncated. Similarly, F = (d / b) % d = 2 % 7 = 2.
 
  • #3
hmmm i think i wa sjust screwing something minor then up. THanks
 
  • #4
you can cast the ints into doubles if you wish

... + (double) c / (double) b

actually you only need to cast one of them as when an operation involves a mixture of ints and doubles, the remaining int(s) will be promoted automatically.

However, with your formula, the c/b part is calculated first, and as they are both ints, an integer calculation is done. then the answer to that calculation is promoted to a double, so that it can be added to the other doubles.
 
Last edited:
  • #5
well see these are like test questions, and I wanted to know how to do them in the proper way. If it was up to me everything would be double.
 

FAQ: Understanding Printf Formatting in C for Mathematical Operations

What is printf formatting?

Printf formatting is a method used in programming languages, such as C, to control the output of text or variables to a standard output stream. It allows for the formatting of data in a specific way, such as adding decimal places or padding with zeros.

How do I use printf formatting?

To use printf formatting, you need to include the "stdio.h" header file in your code. Then, you can use the "printf" function to specify the format of your output and the variables you want to include in the output. For example, printf("%d", num) would print the variable "num" as an integer.

What are the most commonly used format specifiers in printf formatting?

The most commonly used format specifiers in printf formatting are "%d" for integers, "%f" for floating-point numbers, "%c" for characters, and "%s" for strings. There are also other specifiers for different data types and formatting options, such as "%ld" for long integers and "%0.2f" for specifying the number of decimal places.

How do I include special characters in printf formatting?

To include special characters, such as tabs or newlines, in printf formatting, you can use escape sequences. For example, "\t" represents a tab, "\n" represents a newline, and "\b" represents a backspace. These escape sequences can be added to the format string within the printf function.

What happens if there are not enough arguments for the printf formatting?

If there are not enough arguments for the printf formatting, the program will encounter an error and may produce unexpected results. It is important to ensure that the number of arguments matches the number of format specifiers in the printf statement to avoid errors.

Back
Top