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>
int main() {
int j=65;
printf("j>=65?%d:%c\n",j);
return 0;
}
Ok it is understood that in place of %d ,value of j will be printed but why %c is replaced by ö ,i am unable to understand the output of this program , explain the printf statement.
You put doublequotes in a wrong place: you quoted the entire expression, rather than making your format string a conditional:
printf((j >= 65 ? "%d\n" : "%c\n"), j);
Your j >= 65 ? ... : ... expression is part of the string literal. C compiler does not "see" it as anything related to j. Hence the format string contains two format specifiers, with a single printed item; that's undefined behavior.
UB manifests itself in different ways; on your particular system a junk character 'ö' gets printed. This, however, is not a guaranteed behavior - on other systems you may get a different output, or a crash. See this Q&A for an explanation of UB.
This is enough I suppose for explaining whatever you have shown. And the behavior you see can be anything given that it is undefined.
From standard
The fprintf function writes output to the stream pointed to by stream,
under control of the string pointed to by format that specifies how
subsequent arguments are converted for output. If there are
insufficient arguments for the format, the behavior is undefined.
Emphasis mine.
The short answer for this is that this is undefined behavior, the character that gets printed could be anything and the program may even crash.
The longer answer is that old compilers did not check printf strings against the arguments passed, and so by default compilers to not treat this as an error. If you enable the correct warnings (-Wformat) it will complain about this at compile time, and with -Werror the warning will be escalated to an error. Because this is not checked at compile time, as many arguments as are needed are fetched from where they should be on the call stack. This means that first argument after the last specified argument probably has to do with the return address for the stack frame or something, but after that you start to push into unallocated memory. Either way, the behavior is undefined.
If you're interested in more details, this stack overflow answer explains it well.
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 22 days ago.
Improve this question
I am getting this segmentation error and I can seem to find where it is occuring. I am trying to create a list of float values through an array of floats. It is one of my assignment from college class.
my code
I have tried to comment out the float array all together to see if that is where the error is occuring but it seems the error always happens after scanf and it asks for a user size of the array.
The code segfaults because scanf() expects a pointer to store the data that was read.
In your case scanf("%d", size); with size initialized to 0 (see size = 0) means scanf should store the data at address 0 which is an invalid pointer to write and read from (NULL pointer).
However, it doesn't really matter that you're trying to write to a NULL pointer, any other fixed pointer would most likely cause a segfault too (i.e. if size were 2 or 242353252 it would still segfault). Segfault simply means your program accessed an invalid memory location ; and there are A LOT of invalid memory locations that could be accessed by your program.
That's why your program segfaults.
To get the address of a variable use the & operator:
scanf("%d", &size);
Now scanf receives the address of whatever address size is stored at.
scanf("%d", &numList[i]);
Is also wrong, but for a different reason: %d expects an int variable, but you're passing the address of a float variable.
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
I made this small code in C:
#include <stdio.h>
int alo();
int main()
{
printf ("%d",alo());
}
int alo(int i, int x)
{
return (x);
}
but it seems when I run it without giving any argument to alo it prints a completely random number.
I just want to know, since I suppose it can't be 100% random, what number is this in reality and where does it come from, and also if I can predict it. Note that this is pure curiosity.
I guess it has something to deal with memory reading, but how this particular bug exactly functions?
Your alo function accepts (i.e. requires!) two parameters, but you called it without any arguments. The behavior is undefined in this case.
You declared your function (before main) without a prototype. This means that it is your responsibility to supply the correct number of arguments of correct type in all calls to alo. The compiler will not help you to verify the correctness of such calls.
For this reason a good idea is to1 always declare your functions with prototypes
int alo(int i, int x);
so that the compiler will help you to ensure that you are calling your function properly.
In practice the implementation of alo blindly follows its parameter passing convention and reads the storage location that is supposed to contain the value of parameter x. The resultant value is retuned from alo.
What kind of storage location is used for passing x depends on the implementation details. It could be a memory area or a CPU register. Whatever residual/garbage value was residing in that storage location before the call is returned from alo.
OK, Google for:
you own hardware architecture, (CPU, registers, memory etc),
assembler,
machine code,
instruction set,
debugger,
stack,
calling convention,
parameters,
arguments
When you understand the above, you are equipped to investigate the UB on the computer that it manifests on. Next box, next architecture, next week, it may behave differently:(
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 6 years ago.
Improve this question
I have a peculiar problem where my program crashes at strcmp.
Upon gdb core analysis I see that both the strings being checked are sane meaning their pointers are not NULL and they contain finite null terminated values.
However one of the strings is garbage (not the one the variable is intended to contain)
Leaving aside the fact that it is garbage, I really want to know why strcmp would crash for 2 sane strings ? What could be the possible causes of this ?
Thanks in advance!
EDIT:
An example,
a = strcmp(b,c);
(gdb) p b
$92 = 0x7f58d3a36b89 "H\205\300uRH\215}\320\350\a\300\361\377A\276"
(gdb) p c
$93 = 0x2041e48 "MAIN"
The values for b and c seem OK, but due to optimizations, especially around such functions as strcmp() that may undergo intense macro expansion, the actual values might not be available to the debugger.
The problem might be easy to spot in the source code, you should post the source to the offending function.
The value 0x7f58d3a36b89 printed by gdb for variable b is surprisingly large! You could try and modify your code this way:
static char bb = *b;
static char cc = *c;
a = strcmp(b, c);
Forcing an access to b and c before the strcmp() may move the crash up and let you verify if b is indeed what gdb prints it to be.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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.
Closed 9 years ago.
Improve this question
#include<stdio.h>
void main(){
if(printf("Hello World")){
}
}
this is editted which i copied from the compiler ...
Try this its truly working and print as Hello World
You have to remember that a function call is an expression, and that in C you can have any expression as the condition for an if statement.
Not sure what the problem is here.
printf() is just a function. It returns a value (an int that says how many characters were printed).
So when used as the controlling expression in an if, of course it's called ("evaluated") since the if needs to know the return value.
Calling printf() has the usual side-effect of generating output.
I've tried running this and got following Build messages
However, printf() returns the number of characters it has printed , Unless you put printf("") as condition, it will evaluate to be true. ;)
printf("Hello World") is an expression not statement, so you don't need semi-colon there.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I just started getting a weird printf output, has anyone ever seen this? Any idea what it could be caused by?
http://imgur.com/4Mt6xdi
Edit
Here's the code. I'm new to c so if anything (even if it's not causing the error) looks wrong or uncommon please tell me.
printf(f2,"%8.31f\t%8.31f\n",x[0],x[2]);
fprintf(f2,"%8.31f\t%8.31f\n",x[1],x[2]);
In the code you write:
if (x[0]*oldx<0)
{
printf(f2,"%8.31f\t%8.31f\n",x[0],x[2]);
fprintf(f2,"%8.31f\t%8.31f\n",x[1],x[2]);
}
where f2 is a pointer to FILE, which shall not be passed as the first parameter of printf. Just remove it.
At least one problem is on lines 96-97:
printf(f2,"%8.31f\t%8.31f\n",x[0],x[2]);
fprintf(f2,"%8.31f\t%8.31f\n",x[1],x[2]);
The first line should call fprintf, not printf.
Any compiler should give you at least a warning for calling printf with a FILE* as the first argument. Did you see such a warning? If so, why did you ignore it?
Compiling with additional warnings enabled should show you a number of other problems. Fix those before doing anything else.