Very simple program in C not working [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 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;

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.

an integer changed itself for no reason, could it be for memory issues? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 4 years ago.
Improve this question
I have a long program that has an int max in it. It wasn't working and i found out that max changed itself to 0 for no reason since after its first value is never changed.
I used a lot of prints to find out where it happens and for some reason happens here:
printf("max is: %d\n",max);
qtail->block=0;
printf("max is: %d\n",max);
Before this instruction, max has its correct value, and after it max is 0. How?? That pointer has absolutely nothing to do with max, maybe I ran out of stack memory and the program started rewriting itself?...block is an int too but inside a struct
The problem happened because of undefined behaviour; by using
printf("%p : %p", &(qtail->block), &max);
I saw that max and the qtail pointer had the same address and corrected what caused it.

Error when running C-program after first input [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
The program shuts down after I enter the first input
#include<stdio.h>
int main(void)
{
int biology,chemistry;
printf("\nEnter marks for maths");
scanf("%d",biology);
printf("\nEnter marks for tech1");
scanf("%d",chemistry);
return(0);
}
C function parameters are always "pass-by-value", which means that the function scanf only sees a copy of the current value of whatever you specify as the argument expression.
If you passed biology, then it would only see an uninitialized value. On the other hand &biology is a pointer value that refers to the variable i.e scanf can use this to modify biology.
The scanfwould need to be modified as follows
scanf("%d", &biology);
scanf("%d", &chemistry);
To understand this in detail read Why does scanf require &
You are passing incorrect arguments to scanf() calls. You must pass the address of the variables (see scanf()'s documentation) to match %d format.
scanf("%d", &biology);
...
scanf("%d", &chemistry);
You should also check the return code to see if the scanf() calls succeeded.

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);

Resources