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"
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 8 months ago.
Improve this question
I was trying to multiply a double and an int but it shows nothing.
My code is:
#include <stdio.h>
int main()
{
double vergi = 0.01;
int fiyat = 20;
double sonuc = fiyat * vergi;
printf("%s", sonuc);
}
The %s specifier is for a String. As per the doc says, you should use the %f specifier in printf for a double result.
Wrong conversion type in printf.
You want to print variable which type is double.
This is correct - printf("%lf\n", sonuc);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
i wrote code, and it gives me answers like 0.00 and 1.00, not actual math answer. Where i made mistake? (im begginer, dont scream on me :) )
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b;
float x;// score
// Request 1
printf("Zadanie 1(12)\n");
// Calculate the square root of the given interval
printf("Oblicz pierwiastek rownania w podanym przedziale\n");
// Give me a number a
printf("Podaj liczbe a:\n");
scanf("%d",&a);
// Give me a number b
printf("Podaj liczbe b:\n");
scanf("%d", &b);
x=&a-&b;
//answer
printf("%f", x);
system("PAUSE");
return 0;
}
x = a - b
not
x = &a - &b
Explanation: The & operator gives you the memory address of a, which you need to give to scanf, so that it can place data there. But you do math on the actual value of a.. which is just a.
because of pointer arithmetic:
x = &a - &b;
computes the distance between the addresses of a and b, which are probably close since they're auto variables of the same type declared close-by. My guess is that you could get 1 or -1 (or any other integer value but not 0 since a and b are located in 2 separate addresses) then put in a float.
(the difference of addresses of 2 consecutive integers is 1, independently of the size of the integer)
You need of course to do:
x = a - b;
You've been misled by the fact that scanf needs a pointer on a or b to be able to write a value when reading the input.
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 7 years ago.
Improve this question
int main()
{
// Initialize & Declare variable
int m = 5;
// Allocates memory for storage of an integer variable
int *itemp;
// Stores memory address of variable m in memory address itemp
itemp = &m;
// Notice after declaring pointer you don't need to reference it as a pointer
// asterick is also known as indirection operator
// indirect reference: Accessing the contents of a memory cell through a pointer variable that stores it's address
// We can rewrite the contents in the memory cell as such
*itemp = 35;
printf("%d",*itemp);
// Doubles the value of m
*itemp = 2 * *itemp;
printf("%d",*itemp);
return 0;
}
It's returning 3570 instead of 70, which is what the book says it should be returning. What am I doing wrong?
The program is correct. It is printing what it is coded to print.
To clarify,
You have two printf()s, printing 35 and 70.
You don't have a "seperator" [for example, a newline (\n)] in your printf()s to distinguish the outputs of two print statements.
Result: You're seeing the final output 3570 as the combination of the output from two print statements, 35 and 70.
Solution: Add a \n or \t at the end of the format string supplied in printf() to add a visual seperator after each printf() to avoid confusion.
Just do
printf("%d\n",*itemp);
You are seeing 35 and 70 as output in the same line either add a space between them or a newline.
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 8 years ago.
Improve this question
I'm struggling to get Char to work. It keeps returning an error.
#include <stdio.h>
#include <cs50.h>
int main (void)
{
int tower_height;
char #;
// Inputs
do {
printf("Let's build! Give me a number between 0 and 23 'inclusive'.\n");
tower_height = GetInt();
}
while
(tower_height < 0 || tower_height > 23);
// Outputs
for (tower_height = 0; tower_height <= 23; tower_height++)
printf ("%c = tower_height - 2\n");
}
C identifier names may contain letters, underscore, and digits, as long
as the first character isn't a digit, and as long as the identifier
isn't a keyword. They may not contain #.
# is not a valid variable name.
As pointed out, # is not a valid variable name.
You can see how # is properly used in the first line of your code: #include <stdio.h>
Instead, call your char variable something that uses letters and numbers eg: char c;
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.