int value changing when passing it into a function [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 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);

Related

Why C gave a weird output when adding floats and integers? [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 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.

messed up with loops cant find the logical mistake [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
Actually right now i am learning c and doing an excercise of looping and got messed up in a question.
my code is:
#include<stdio.h>
void main()
{
int i,j,k,spc,k;
printf("\enter the number of rows:");
scanf("%d",&rows);
spc=rows+4-1;
for(i=1;i<=rows;i++)
{
for(k=spc;k>1;k--)
{ printf(" ");
}
for(j=1;j<=i:j++)
printf("*");
printf("\n")
spc--;
}
}
https://www.w3resource.com/c-programming-exercises/for-loop/c-for-loop-exercises-14.php
and this is the reference for the answer by them whose excercise i am doing right now.
can you see any difference bw these codes.
please help me.
thank you
as i can see, you have small errors which you need to fix ,
first is, int i,j,k,spc,k;, here, 'k' is written twice, next is scanf("%d",&rows); but, rows is not declared anywhere, in this line,for(j=1;j<=i:j++), you missed a semicolon and added colon instead, so replace it with for(j=1;j<=i;j++) and the last one is, printf("\n") ,in this line, you missed a semicolon! and for the desired output, you just need to add a space in printf("*"); ,i.e, printf("* ");.Thats it.

Why the output of printf("%d","") is 173 in c language? [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 5 years ago.
Improve this question
#include<stdio.h>
#include<conio.h>
void main()
{
printf("%d","");//printing output
getch();
}
The output is 173 .I am not getting why the output is 173.
First, you're trying to print a string as decimal integer, which means the decimal you try to print is going to be the pointer to the string (actually a pointer to the array of characters) and not the string itself. To use an individual character use single-quotes, not double-quotes.
To accomplish what you're actually trying to do, do this:
printf("%d", ' ');
Note there is an actual space between the two single-quotes.
The result will be 32, which is the decimal value for the ASCII Space character.

Very simple program in C not working [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 relatively new to coding and am having a problem with a very small piece of code. It seems as if this should be simple to resolve, and I'm bothered that I can't figure it out myself. I was building a program to conduct a variety of conversions that I have to perform all the time and it was ouputting garbage. I backtracked and am testing all my functions. It seems that my functions weren't working, so I began testing each individual function as to whether or not it was correct.
I have one conversion here that I was running as a test code. It should take user int input and calculate ft from an input of miles. That seems pretty simple right? I thought so to.
Can someone please provide some insight as to why the very simple code below doesn't work?
#include <stdio.h>
int main(void)
{
float miles;
printf("Enter value in miles: ");
scanf(" %d", &miles);
printf("\n\n%.0d miles is equal to %.0d ft.", miles, ((miles)*5280));
return(0);
}
Use %f instead of %d in scanf function
You need just an integer. Change
float miles;
into
int miles;

What does double semicolon mean 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 9 years ago.
Improve this question
For example:
void thisIsAnExample(Hello* bye, char* name, int num, in* arr, int* sum){
GoodBye x;;
x.funName = name;
.
.
.
It doesn't mean anything. It's just an extra semicolon. You can delete it (leaving a single semicolon) without any effect on your program.
It has the meaning of an a statement followed by an empty statement.
In C each statememnt ends with ;. So a statement with a ; followed by one, is a statement followed by an empty statement.
A "double semicolon" does not have any special meaning in c. The second semicolon simply terminates an empty statement. So you can simply remove it.
Most likely a typo. Adds a null statement to your program.

Resources