Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
#include <stdio.h>
int main() {
int a = 3;
float b = 6.412355;
printf("%.*f\n",a,b);
return 0;
}
Why the output is;
6.412
What is the effect of .* here ?
The . means that the next characters indicate the precision to use. The * means to read the value from the argument list; in your case, it will read a. The value is 3, so the next argument is printed to 3 decimal places.
In printf function, the format %[flags][width][.precision][length]specifier of this question is .precision, it has two choices number or *.
When *, it means The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
For more details, see http://www.cplusplus.com/reference/cstdio/printf/
#include <stdio.h>
int main() {
int a = 3;
float b = 6.412355;
printf("%.*f\n",a,b);
return 0;
}
It substiutes the value of a to the *,implying a precision.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have the following C code
#include <stdio.h>
int main() {
int var = 9; /* actual variable declaration */
printf("Address of var variable: %x\n", var);
return 0;
}
if var is 1 - 9 it prints 1 - 9. no problems
if var is 10 - 15 it prints 1 - f. Doh
This appears to be treating an int as a hexadecimal value. why is it doing this.
The specefier x or X is for Unsigned hexadecimal integer and not for addresses (pointers). for addresses you need to use the p character specefier.
if you want the address of the var . use p which is the conversion specifier to print pointers. see below: e.g.
int var = 9;
printf("%p\n", (void *) &var);
it seams "%x" in the string makes it print the value as a hexidecimal. I needed to use "%d"
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I need to convert defined constants to their integer values so I came up with this macro (STR_TO_CONST) for doing so...
#define STRFY_VAL(s) #s
#define STRFY_KEY(s) STRFY_VAL(s)
#define STR_TO_CONST(s) atoi(STRFY_KEY(s))
It works, but I'm wondering if there are any potential problems with it as I've never encountered this macro before despite having searched considerably for something like it.
The reason you never encountered this is that it's utterly pointless, but let's explain by example. Say you have the following:
#define FINALANSWER 42
// ...
int x = 2 * STR_TO_CONST(FINALANSWER);
now, this is semantically no different from:
int x = 2 * FINALANSWER;
That's because preprocessor macros are ultimately just textual replacement happening before you actually compile. Therefore, FINALANSWER is just as good as an integer constant as 42 is.
Your "solution" to a non-existing problem just adds overhead in that it adds a new string constant to your code and an unnecessary function call as well.
I'm wondering if there are any potential problems with it (?)
Yes. Using atoi() to initialize a global results in something like "error: initializer element is not constant"
int x = STR_TO_CONST(123); // error
int y = 123; // no error
int main(void) {
return x + y;
}
Hide warnings. Only 1 line generated a useful warning
"warning: overflow in implicit constant conversion [-Woverflow]"
int main(void) {
int a = STR_TO_CONST(123456789012345); // no warning
int b = 123456789012345; // warning
return a + b;
}
Range issue. With 32-bit int, the below will likely exceed atoi() range resulting in undefined behavior with no warning.
int main(void) {
long long z = STR_TO_CONST(123456789012345);
return !z;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Question 1.
#include <stdio.h>
int main(void)
{
int c;
while((c=getchar())!='\0')
{
putchar(c);
}
}
Input
Hello C.
Tell me about you.
Output
Hello C.
Tell me about you.
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
and it continues with status-time limit exceeded.
Question 2.
#include <stdio.h>
int main(void)
{
float a;
a=46.43253;
printf("\n%d",a);
printf("\n%f",a);
return 0;
}
Output
536870912
46.432529
Output- 536870912
46.432529
In general using incorrect format specifier triggers undefined behavior - which is what you have when you use %d in printf for printing float. In this case, you can expect any output usually.
However, it may also be the case that since you have specified to read the float number as integer (e.g. by using %d specifier), it simply interpreted the result as integer - hence the strange number (since floats and integers are stored differently).
If you are interested why the second printf prints a number slightly different from yours, this may help you.
This block is fine:
float a;
a=46.43253;
This block is also fine:
printf("\n%f",a);
The problem is with this block:
printf("\n%d",a);
Particularly this part:
"\n%d"
Please keep in mind you declared a float and using the integer syntax to output it. That's why you are getting the negative output
If it is a case where you don't want to change the "%d," then simply cast it as a float before output
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
scanf("%lf",&alpha);
alpha = (alpha * PI)/180;
if(alpha==PI/2)
{
printf("0");
}
i also defined PI and declared alpha...it just skipping this if and i don't know why
Equality and floating point numbers do not go down well. You have rounding errors.
Need to put in some tolerance.
#include <stdio.h>
double PI = 3.14159265359;
int main (void)
{
double alpha = 90.0;
scanf("%lf",&alpha);
alpha = (alpha * PI)/180;
if(alpha==PI/2.0)
{
printf("0");
}
}
Enter 90 and the 0 gets printed.
Works as expected, did you declare alpha as float?
Then you would have to change the scanf to "%f" to get correct results.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Let us say that the English alphabets A to Z has value start from 1. A = 1, B = 2, C = 3 and so on. Write a program which calls a function which accepts the name of a person which is constant character array and returns an integer value with sum of the Alphapets. What is the benefit of passing the name of the person as const char array?
Suppose somebody else provides me a function that takes non-const char * and does the job. What the function is actually implemented is like this:
int get_int_sum(char *name)
{
int sum;
//codes to calculate sum of alphas
name[0] += 1;
//continue
return sum;
}
When I call the function using
char my_name[] = "Yu Hao";
int reuslt = get_int_sum(my_name);
Even if I got the result I want, my_name is changed to "Zu Hao" without my notice. However, if a function has a prototype of
int get_int_sum(const char*name)
I am sure that the string I passed will not be modified.
One advantage is that elements in a array are protected from changing its value.
For example, here is simple code.
int Your_function(const char * a)
{
a[3] = 'A'; // this statement causes compile error.
// do something
return 0;
}