Why C gave a weird output when adding floats and integers? [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 4 months ago.
Improve this question
I am learning C right now and I understand that I can't add an integer with a decimal like so:
#include <stdio.h>
int main() {
printf("%d",15+9.0);
return 0;
However when running this I expected some sort of an error. Instead, I got a weird output:
-1866308488
Can someone help me understand why it gave me this output?

As #PaulMcKenzie said, C expected an int based on the %d format specifier. You gave it a double instead. C often gives unexpected behavior instead of throwing an error like Java or C#. What happens to a double variable when %d is used in a printf? says that the resulting behavior is undefined and OS-specific.

Related

Programming 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 2 years ago.
Improve this question
Sorry if question sounds stupid to you or it is too easy but i really do not know what im missing here (or im just stupid and dont understand the errors i am getting).
Im still starting to learn about C programming.
So what im trying to do is to find out how many times will "Red" print on the screen.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
for(i=4;i<15,i=i+3); { printf(“Blue\n”);
printf(“Red\n”);
printf(“White\n”);
}
return 0;
}
The errors i am getting are:
Error expected ';' before ')' token
Error stray '\223' in program
Error stray '\' in program
I was trying to find examples on the internet but nothing appears
Every help is appreciated. After all I'm learning.
Your for statement has a comma where it should have a semicolon. The "i<15" part should be followed by a comma.
(Also, since your entire for statement has a semicolon immediately after it, before the block, Red will only be printed once.)

How can I detect my coding mistake when I receive this message [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
link to the code: http://gyazo.com/f0f4004eb606607ecaa021b5e22e6e06
I am getting the following error when i am running th code.
"error: expected identifieror '(' "
I use gedit to write this code.
I would appreciate some support guys ;)
Thanks in advance!,
Vicente
There's shouldn't be a semicolon in the int main(void); declaration.
Try replacing line 4 with: int main(void) instead.
Also, please read up on C function declaration syntax
https://msdn.microsoft.com/en-us/library/c4d5ssht.aspx.
int main(void);
^you should not do this.
And you forgot to put ; after this statement -
int height=n
And also n is not declared in your program.

Why It showing Hello two times try in on Turbo C v 3.0 [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 8 years ago.
Improve this question
although the value of NULL 0. explain with good example.
void main()
{
int i;
for(i=0;NULL;i++)
{
printf("Hello");
}
printf("Hello");
}
This is a well-known bug in the Turbo C 3.0 compiler.
But note one thing: currently the behaviour of your function is undefined as main should always have an int return type. Formally, a standards compliant compiler is permitted to do anything with your program!
If you adjust your program so it has no undefined constructs then, on a standard compliant compiler, it will be guaranteed to output "hello" exactly once.

int value changing when passing it into a function [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 8 years ago.
Improve this question
I'm passing in an int to a function in my lame program. It's passing in a number to convert it to a binary representation as an int array.
typedef int bool;
bool* conv2bin(int num)
{
blah blah blah return binary as bool array
}
I pass in 78 and if I printf() immediately after it's passed in, I get 781237412753-124?
I'm new to C (coming from C++) so please tell me if I'm doing something really dumb?
This seems like it should be really easy but it isn't...?
EDIT:
Have I done goofed:
printf("%d", num);
EDIT 2:
It has to be something with the int because at the end of the function, it checks to see if we subtracted numbers sufficiently to get to num==0 but it says we're not at 0. It's doing really weird things. It also says that the binary is 0000000001001111, and it should be 0000000001001110.
Edit 3:
Wow I suck. Thank you Floris! It's been a long day.
Guessing here…
Your printout starts with the correct two digits: 78.
But if you do not include a \n at the end of your formatting string, then the next thing you print will be concatenated. As will the next thing, and the next.
I suspect your problem will disappear when you change your print statement to
printf("%d\n", num);

Type expected declaration or statement at end of input [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 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.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
I am making a C program that is a simple calculator without a GUI, called "Quical". (Check out the code on Github). I am somewhat new to C, and so I am making some syntax errors. One of the errors is this:
expected declaration or statement at end of input
Another one of the errors that comes up is this:
else without a previous if
Here is my code.
Hopefully, this can shed some light as to why I am getting these syntax errors. Any help would be much appreciated.
Your braces don't match. You have something like
main()
{
some statement
{
}
another
{
}
and it ends.
It is saying it wants a statement here. Try that and see what the next error is.

Resources