Why does casting this double to an int give me 0? [closed] - c

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
Sorry this has been confusing me for hours and nothing comes up on google about this. I'm programming in C, trying to convert decimal to binary, and I'm confused as to why this keeps giving me the right amount as input but the integer value is always 0.
even if I properly cast input as an int, it doesn't work (ie: int integer = (int) input;
main() {
double input;
scanf("%d", &input);
printf("input: %d \n", input); // this will print fine
int integer = input; // stores nums before decimal
printf("integer:%i\n", integer); // this always prints 0

Because you are using the wrong specifier to read -> scanf() or print -> printf() the double, do this
if (scanf("%lf", &input) == 1)
{
printf("input: %f\n", input);
printf("integer: %d\n", (int) input);
}

You need To Correct Format Specifiers.
main(){
double input;
scanf("%lf",&input);
printf("input: %lf \n", input);
int integer = input;
printf("integer:%i\n", integer); }
Some Format Specifiers
Or you can read from here

Related

Why is my C program skipping over if statements? [closed]

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 3 years ago.
Improve this question
I am a new coder and have no idea what I am doing please help!
The code is reading and taking inputs just fine until it reaches the scanf(" %c", &i);then it skips to the Amount print seemingly ignoring my if statements.Is there somthing wrong with my use of scanF?
Here is the code:
#include <stdio.h>
int main(){
printf("BANK ACCOUNT PROGRAM!\n--------------------\n");
char W,F,A,i,D;
int t=1;
double b,d;
while (t=1){
printf("Enter the old balance:\n");
scanf(" %lf", &b);
printf(" %lf", b);
if(b>=0)
{
printf("Enter the transactions now!\n Enter an F for the transaction type when you are finished.\n");
printf("Transaction Type (D=deposit, W=withdrawal, F=finished):\n");
scanf(" %c", &i);
if(i=F){
printf("Your ending balance is");
printf(" %lf", b);
printf("\n Program is ending!");
return 0;
}
if(i=W){
printf("Amount:");
scanf(" %f", &d);
b= b-d;
printf(" %f",b);}
}
if(b<0);
{
printf("The balance must be maintained above zero!\n");
}
}
return 0;
}
Because your comparison is wrong
if(i=F){ // this is assignment, not comparison
should be
if(i=='F'){ // note also it's comparison to character 'F'
In addition to wrong comparison operators, your way of comparing two chars is also wrong. If you want to check whether the input char i equals to 'F' , you should initialize the char variable F with character 'F', otherwise it would be just unintialized variable and comparison with that variable would be wrong.
You should add these lines just after you initialize F and W.
F = 'F';
W = 'W';
By the way, you can use another variable names like,
char var1 = 'F';
char var2 = 'W';

%d in scanf not working. number 4223092 comes up [closed]

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 3 years ago.
Improve this question
Can anyone help me with this code? I don't see any problem, but somehow it doesn't work. When I typed my favorite number and press enter, the number 4223092 came up.
int target;
int after;
#include <stdio.h>
int main() {
printf("What is 6 x 4?: ");
scanf("%d", &target);
if (target == 24) {
printf("Correct!\n");
printf("By the way what is your favorite number?: ");
scanf("%d", &after);
printf("%d is my favorite number too!\n", &after);
} else {
printf("Wrong!\n");
printf("By the way what is your favorite number?: ");
scanf("%d", &after);
printf("%d is my favorite number too!\n", &after);
}
return 0;
}
In your code
printf("%d is my favorite number too!\n", &after);
you don't need the &. You want to print the value, not the address.
Just to let you know, in it's current form, passing a int * as argument to %d invokes undefined behaviour. So, you cannot justify the output in any ways.
%d expects an argument of type int, not int *. Passing an incompatible type of argument invokes the UB.
Quoting C11, chapter §7.21.6.1, P9
[...] If any argument is
not the correct type for the corresponding conversion specification, the behavior is
undefined.
The printf function does not require the address of a variable to print to stdout. Just pass the name of the variable like so:
printf("%d is my favorite number too!\n", after);
The %d format specifier looks to display an integer value. If you pass something else, like an int * in your case, it will give you strange and unexpected results.
And to make this answer more complete, to print a pointer variable, use the %p format specifier.
The value 4223092 is the address of the "after" variable.
In C language you do not need a pointer to print a value. You only need the value of the variable. So whenever you are printing a variable just print the variable and not the pointer (address) of the variable.
So just change the following lines in your code
printf("%d is my favorite number too!\n", &after);
To
printf("%d is my favorite number too!\n", after);

Scanning and printing char variables in C [closed]

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 4 years ago.
Improve this question
Why is printing x is always equal to zero?
#include <stdio.h>
int main()
{
unsigned char num, x, nnum, y = 0;
printf("please enter the number of bits u want to clear \n");
scanf("%d%d", &x, &y);
printf("%d%d", x, y);
return 0;
}
I have tried to put them separately but after taking a variable from y the x becomes a zero.
I know that they will store eventually in the stack and every char is 1 byte, so when printing them
they should both have the value that I put in it.
In your code,
scanf("%d%d",&x,&y);
is undefined behavior. The conversion specifier and the supplied arguments do not match. With scanf() family, %d expects an argument of type pointer to a signed integer, whereas you are supplying a pointer to an unsigned char.
You need to use
scanf("%hhu%hhu",&x,&y);
The format specifier for a char is %c. Using %d leads to undefined behavior (overwrites the next variable on the stack).
If you want an 8bit unsigned integer then use uint8_t (stdint.h) and the SCNu8 and PRIu8 macros from inttypes.h.
The data type is char, so basically the scanf for the char is %c. Either you will seperate variables of it. If your x is a character then you would need to seperate into the next line. The scanf for integer is a %d.

No Error but why we don't get input for second scanf() statement [closed]

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 6 years ago.
Improve this question
#include<stdio.h>
int main()
{
char ch;
int i;
scanf("%c", &i);/* i know this must be reversed for next input but I want to know the impact of this code if we use scanf type char for integer data type*/
scanf("%d", &ch);
printf("%c %d", ch, i);
return 0;
}
No Error but why don't get input for second scanf() statement
You declared i to be an int and ch to be a char
In your scanf you have %c for i and %d for ch while %c is for chars and %d for ints.
So just turn them around and you'll be fine.
With printf you have the correct format.

C program won't compile despite using proper specifier? [closed]

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
For reasons that I don't understand at the moment, this code will not compile despite having everything it needs in order to do the task.
#include <stdio.h> /* printf */
#include <math.h>
double resistance;
double voltage;
double current;
double wattage;
int main()
{
printf("type in resistance\n");
scanf("%f",resistance);
printf("type in current");
scanf("%f",current);
//voltage = resistance*resistance*current;
printf("%f Volts",resistance*resistance*current);
// return voltage;
}
The thing I don't understand is why won't it compile? I keep getting "wrong specifier" message when compiling. I tried both %lf and %f but neither of them works.
Change the lines
scanf("%f",resistance);
scanf("%f",current);
to
scanf("%lf", &resistance);
scanf("%lf", &current);
The %f conversion specifier expects the corresponding argument to have type float *, but both expressions resistance and current have type double. The expressions &resistance and &current have type double *, so you need to use the %lf conversion specifier (which expects arguments of type double *).
You want to assign a floating value to your double vars by using scanf. The arguments need to be pointer :
int main() {
printf("\ntype in resistance\n");
scanf("%lf",&resistance);
printf("\ntype in current");
scanf("%lf",&current);
//voltage = resistance*resistance*current;
printf("\n%f Volts",resistance*resistance*current);
// return voltage;
}
It is not the printf, it is the scanf:
scanf("%f", resistance);
This should be:
scanf("%lf", &resistance);
%lf because it is a double, %f is for floats. And &resistance because scanf is expecting a pointer to whatever, so it could write there. When you write %f, printf expects just a float, scanf a pointer to float. %lf and it's double/double*.
As per the standard of C99, there is no exclusive format specifier for float. Whether you use a float or double, it doesn't count. What matters is that when you use a float variable printf automatically promotes it to a double and displays it. So %f indicates a double or a floating-point value and %Lf is used for a long double value.
Reference: http://www.cplusplus.com/reference/cstdio/printf/
int main() {
printf("Type in Resistance: ");
scanf("%lf", &resistance);
printf("Type in Current: ");
scanf("%lf", &current);
printf("%f Volts",resistance*resistance*current);
}

Resources