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.
Related
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 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 can't understand this code.
please help me in explaining how this code works.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
P
getch();
}
I'll play this silly game. Your teacher is having a joke with you.
By the magic of https://www.naclbox.com/gallery/turboc :
Consider:
Then note the hidden macro definition on line 6 (note the column number):
Voila!
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.
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
I'm trying to do a example of exponent en " DEV C++ " compilator and I can't get the output
this is the code :
#include <stdio.h>
#include <math.h>
int main (void)
{
double base, exponent, result;
printf("La base:\n");
scanf("%lf",&base);
printf("El exponente:\n");
scanf("%lf",&exponent);
result = pow(base, exponent);
printf("%.1lf^%.1lf = %.21f", base, exponent, result);
return 0;
}
and i get this :
what could be the problem ?
Sincerilly,
NIN.
You showed us the code from exponente.c (I guess), but the error message in the screenshot says:
error in math.h
included from teorema.c
So you should read the error messages very carefully and look for the buggy code in the other file.
It's important to remember that I'm using DEV c++, which have lot of errors and libraries which doesn't work.
I download GCC and MinGW for windows. I compile the same source code in MS-Dos and it worked.
I guess finally I going to use just GCC and I'll forget DEV 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
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.