I wrote a simple code with dev but it does not return any thing.but with code block answer shows.what is the problem?
#include <stdio.h>
int main() {
int x,y,z;
scanf("%d%d",&x,&y);
while(z!=0){
z=x%y;
printf("%d",z);
}
return 0;
}
Two problems I can see:
1. Value to z is un-assigned. So garbage.
2. Value of z will not change, so it's infinite loop.
It invokes undefined behavior because z is used uninitialized.
while( z!= 0)
^
|
z is uninitialized
You may get any thing either expected or unexpected result. Program may crash also. On different compilers you may get different results, which is the case here.
in Your Code you was Assigned z but not initialized z and you are checking while(z!=0) so your code does not return value , fist assigned z to any value for example from scanf.
You are not able to see the output because it is closing the terminal / output window as soon as the program exits.
In code::block, they run a script to hold the output window until you press enter.
you can have same effect by using a getch() call at the end, before returning. this will wait for your input and give you a scope to see the result.
Besides, your program has several issues as other answers pointed out. fix them accordingly.
Related
I am a beginner is Computer Science and I recently started learning the language C.
I was studying the for loop and in the book it was written that even if we replace the initialization;testing;incrementation statement of a for loop by any valid statement the compiler will not show any syntax error.
So now I run the following program:
#include<stdio.h>
int main()
{
int i;
int j;
for(i<4;j=5;j=0)
printf("%d",i);
return 0;
}
I have got the following output.
OUTPUT:
1616161616161616161616161616161616161616.........indefinitely
I understood why this is an indefinite loop but i am unable to understand why my PC is printing this specific output? Is there any way to understand in these above kind of programs what the system will provide us as output?
This is a case of undefined behaviour.
You have declared i so it has a memory address but haven't set the value so its value is just whatever was already in that memory address (in this case 16)
I'd guess if you ran the code multiple times (maybe with restarts between) the outputs would change.
Some more information on uninitialized variables and undefined behaviour: https://www.learncpp.com/cpp-tutorial/uninitialized-variables-and-undefined-behavior/
Looks like i was never initialized, and happens to contain 16, which the for loop is printing continuously. Note that printf does not add a new line automatically. Probably want to read the manual on how to use for.
To better understand what is going on, you can add additional debugging output to see what other variables are set to (don't forget the \n newline!) but really problem is the for loop doesn't seem right. Unless there's something going on with j that you aren't showing us, it really doesn't belong there at all.
You are defining the variable i but never initializing it, instead, you are defining the value for j but never using it.
On a for loop, first you initialize the control variable, if you haven't already done it. Then you specify the condition you use to know if you should run another iteration or not. And last but not least, change the value of the control variable so you won't have infinite loops.
An example:
#include <stdio.h>
int main()
{
// 1. we declare i as an integer starting from 1
// 2. we will keep iterating as long as i is smaller than 10
// 3. at the end of each iteration, we increment it's value by 1
for (int i = 1; i < 10; i++) {
printf("%d\n", i);
}
return 0;
}
I am trying this following C program.
add (int a , int b)
{ // Do nothing
}
void main()
{
printf("%x\n", add(1,1));
printf("%x\n", add(1,1));
}
GCC gives me output as below:
Output: 1
2
Can someone explain why these values are getting printed. Even though I assume it is garbage, sequence of 1 and 2 is confusing for me.
This is undefined behaviour. Try to run your program with different compiler, you will get different value.
Try with a functions which uses different stack variables on the basis of some input. You will get different value as variable stack will change.
Compiler dependent behaviour. DO NOT IGNORE COMPILER WARNINGS
I came across an interesting output and I'd to know how the computer is working to produce this. I know that whenever you have %d in a string, you should have a variable to accompany it. When I wrote two %d's and only one variable, I expected that the computer would churn out the same value for the %d's, since it had only one variable to draw on, but for some reason, the %d's returned the value for x and the value for the variable xCubed. I want to know why the program returns xCubed without my writing xCubed at the end of the string. Here's the code:
#include <stdio.h>
int cube(int x);
int main(void){
int x = 5;
int xCubed = cube(x);
printf("Why does this number, %d, equal this number %d?", x);
return 0;
}
int cube(int x){
return x * x * x;
}
Thank you!
Your program invokes undefined behaviour. Anything could happen. Possibly the valued returned from the call to cube happens to lie next to the value of x on the stack. Of course, this behaviour being undefined means that any change to your program, or your compiler options, could result in different behaviour.
In any case, you are expected to supply two values. Do so.
printf("Why does this number, %d, equal this number %d?", x, x);
If you compiled your program with full warnings then the compiler would have warned you of your error. And you could even ask your compiler to treat warnings as errors to stop you committing the mistake.
Your program causes undefined behaviour, so anything is possible. It's some quirk of stack/register layout and calling convention for your platform that gives you the results you see.
That is because xCubed happens to be allocated just after x, which means closer to the printf part of the stack (activation frame).
printf is a vararg function, it has no implicit way of knowing how many arguments it was passed. So, when you call printf with two placeholders but just one value supplied, it will read past the first argument expecting a second and "fall" into the stack of the caller, whose nearest content is exactly xCubed.
Just to be clear: this is the reason why your code exhibits that particular behaviour, not the way it is expected to work. You have a serious bug in your code.
This was by good luck. In effect, it is undefined behaviour.
Obviously, in your case the variable xCubed was put onto stack immediately after the free space. Upon doing the printf() call, x was put immediately before that, and then the address of the format string.
If you compile this program with other optimization settings, your compiler might decide to put xCubed somwhere else, or in a register, or omit it altogether, as its value is never used.
I have a recursive program. When the printf is used in the function, it outputs 123 and when used outside, it outputs 0123 .
#include <stdio.h>
fact(int);
int main()
{
int x=3;
fact(x);
printf("\n");
system("PAUSE");
}
int fact(int y)
{
if (y > 0)
{
fact(y-1);
printf("%d",y);
}
//printf("%d",y);
}
I am not using both the printf at the same time . What difference does the location of this printf statement create?
Since your if condition looks for the values greater than zero, it is working as expected.
When the printf outside that IF block is used, it gets executed even when y is 0, which is not the case for the printf inside the IF block.
fact(int) is called by following sequence,
fact(3)-->fact(2)--->fact(1)--->fact(0)
The last call is fact(0). According to the implementation of fact(int), when 0 is passed in, 0 is printed if printf() is used outsite. 0 is not printed if printf() is used inside.
In fact, all the values passed into fact(int) is printed when printf() is used outsite.
I'd say one reason you didn't see the answer yourself is because your code is sloppy. Here are some complaints:
Your functions have no explicit return statements which are
especially important for understanding recursive code.
system() requires stdlib, but stdlib.h isn't included.
system("PAUSE") is unportable and unnecessary. Actually your code
would not run on my system because of this. See:
http://www.gidnetwork.com/b-61.html
Your question looks like homework, so this one is the homework's fault and not yours: because n! grows so quickly, factorial functions using 'int' for the return type can only calculate n! for 1<=n<=12, which is useless.
Try this exercise: write a one-line factorial function using a single return and a conditional assignment.
I have a pretty simple function in my program using the secant method to find a root of a function. It works fine with the printf() that's indicated below. But if I comment it out, the loop endlessly repeats. I have no idea why...
I've read about printf changing a variable, but I don't see anything on it changing the storage of the variable. Am I missing something? It's not a great solution to have it print, since the iterates are not important and the function is called millions of times.
double guess1=500.;
double y1=estimater(r,volume,guess1,adm,tm,rr[r]);
double guess2=adm/30.;
double y2=estimater(r,volume,guess2,adm,tm,rr[r]);
int i;
double guess3=0.;
double y3;
double tol =heightTOL;
int secmax=SECANTMAX;
for(i=1;i<=secmax;i++){
guess3=guess2-y2*(guess2-guess1)/(y2-y1);
if(guess3>adm/2.){
guess3=adm/2.;
}
if(guess3<=0.){
guess3=0.;
}
y3=estimater(r,volume,guess3,adm,tm,rr[r]);
y1=y2;
y2=y3;
guess1=guess2;
guess2=guess3;
if(fabs(guess2-guess1)<tol){
break;
}
if(i==secmax){
printf("\nRan out of iterations in height finder\n");
}
printf("%d %f",i,guess3); //THIS ONE HERE!!!!!!!!
}
return guess3;
printf is not changing your data at all. The only way the printf family of functions can have any effect on your data is if you use the %n format specifier (which writes into the variable whose address you pass) or if you're doing something which invokes undefined behavior, like passing the wrong format arguments. You're not doing either of those things here (your format strings are correct), so your bug lies elsewhere.
Check that all your array accesses are in bounds. Try running your code in Valgrind or other validators to try to find memory errors.