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.
Related
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 8 months ago.
Improve this question
I would like to know why the loop below just execute one time?
Here is my code.
for(i=0;i<MAX;i++)
{
printf("name:");
gets(student[i].name);
printf("math score:");
scanf(" %d",student[i].math);
fflush(stdin);
}
At least the code contains a typo. You have to write
scanf(" %d", &student[i].math);
^^^
Also this call
fflush(stdin);
has undefined behavior.
And the function gets is unsafe and is not supported by the C Standard. Instead use function fgets or scanf.
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 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've got an Problem. Whats the error in this code? It will give an Segmentation fault: 11
{
double os;
double windows = 2;
printf("Network info\n");
printf("Are you on Linux, OS X, or Windows? (1,2,3): ");
scanf("%s", os);
printf("checking..\n");
if (os == windows){
printf("Getting informatin for windows..\n");
system("ipconfig");
}else{
printf("Getting info for either osx or Linux..\n");
system("ifconfig");
}
}
In your code, you should change
scanf("%s", os);
to
scanf("%lf", &os);
as, os is of double type. Using wrong format specifier (or wrong argument type) invokes undefined behavior.
Read the man page of scanf() for further information.
FWIW, for an integer value, it's best to use int datatype.
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;
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);