How does the putchar and whole of the code gets executed? [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want the dry run of the code for the first 3 iterations to understand.
THe output of the code is : abcdbcdbcdbcdbc........(infinte times)
I know how for loop works and put char also.I did not dry run as i was not understand will the third argument in the for loop will increment or not.
#include <stdio.h>
int main()
{
for (putchar('a');putchar('b');putchar('d'))
putchar('c');
return 0;
}

For your example:
Initial statement : putchar('a')
Condition expression: putchar('b')
Repeat step: putchar('d')
Loop statement : putchar('c')
Now map you code with flow chart above.
Since putchar returns the character it has printed which is b also satisfies the true condition, thus your for loops run infinite time.
Attribution :
http://www.equestionanswers.com/c/for-while-do-while-loop-syntax.php

putchar always returns the char that you put. So for instance, putchar('a') returns 'a'. With that in mind, let's have a look at how the for loop works:
for ( init_clause ; cond_expression ; iteration_expression )
loop_statement
The init_clause is putchar('a'). This prints a once, because the init_clause is evaluated once at the beginning of the for loop.
The cond_expression is putchar('b'). This is checked every run through the loop, which is why it always prints b. And as it returns 'b' every time, the loop never stops. The loop would only stop if the cond_expression returned 0 or the loop is exited otherwise, for instance through break.
The iteration_expression is putchar('d'), hence d is printed every time. The loop_statement is putchar('c'), printing c.
The result is printing a once, followed by an infinite amount of bcd. The reason why you get them in this order is because in every run throught the loop, it first checks the cond_expression, executes the loop_statement and then the iteration_expression.

Related

Why I am getting this output?output = 012345 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for ( ;i<=5;i++)
{
printf ("%d",i);
}
}
Why I am getting this output? output = 012345
I did not expecting any output. because as i didnt intilise i, so from where he is going to start?
if i will be intilise by a undefined value, why I am getting the same output as many times i run the code?
int i;
i is uninitialized and holds an indeterminate value. 0 is one of the many values it can contain.
for ( ;i<=5;i++)
In both i<=5 and i++ you read i that has an indeterminate value. The compiler is allowed to assume that you never do that and may produce assembler code that does anything. The program therefore has what is called undefined behavior.
printf ("%d",i);
Here you print i whatever it may be. If i happened to contain 0 when you left it uninitialized - and if the compiler was "kind" enough to let your failure to initialize i through without producing an executable that crashes (or worse), then the loop will step i from 0 to 6. When it reaches 6, the condition, i<=5, will be false and the loop will end. Only for the values when i is less than, or equal to, 5 will printf ("%d",i); be reached.
Note that the program may do different things when you run it multiple times because it has undefined behavior. i may suddenly be -98723849 when you run the program and then your loop will print the values from -98723849 up to 5.
I did not expecting any output. because as i didnt intilise i, so from where he is going to start?
As mentioned - the program could do just about anything because you didn't initialize i and then read from it. In your case, it seems i contained a 0 and that the program appears to behave as-if you initialized it to 0. Again, this is "pure luck" - no matter how many times in a row it displays the same behavior.
if i will be intilise by a undefined value, why I am getting the same output as many times i run the code?
Because the program has undefined behavior. It may repeat the same thing over and over again for many years. It's a ticking bomb that may suddenly explode one day.

about the break statement [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm kind of confused about the break statement
If I have for example a for loop inside a while loop and there is a break inside the for loop:
the break statement will break out of the while loop right?
No, the break statement will break out of the for loop. The break statement is used to exit the loop it is written in
The break statement in C programming has the following two usages:
When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.
It can be used to terminate a case in the switch statement
If you are using nested loops, the break statement will stop the execution of the innermost loop and start executing the next line of code after the block.
Have a look here
The keyword break will exit the loop it's written in for example:
int main(void) {
int i=0;
int for_called_count=0;
while(i<10){
for_called_count++;
for(int j=1; j<=10; j++){
i++;
if(j==5){
break;
}
}
}
printf("i: %i", i);
printf("\nfor_called_count: %i", for_called_count);
}
Output:
i: 10
for_called_count: 2
Here you see, for is called twice because once the for loop exits with break, the while loop is not done as i is still smaller than 10 so it calls for again. That shows that break only exits the loop it's written in.
The break will only exit the for loop, and will proceed again in while loop if the condition in while loop is still not met

In C, what happens when the condition for a for loop aren't met at the beginning? [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 5 years ago.
Improve this question
For example, what happens if I say:
for(i = 2; i < 2; i++)
Obviously, this is a useless for loop, but maybe i = a, and a is set by something else. So what happens in this case?
Neither iteration of the loop will be executed.
In fact this loop (provided that the condition has no side effects)
for(i = 2; i < 2; i++) { /* ... */ }
is equivalent to this statement
i = 2;
The condition of a for loop is checked before every iteration, including the first one; so your loop's body will never be executed.
The way for loop works is it checks for condition (in your case i < 2) and executes whatever is between { } or whatever code on following lines
As you are initializing i to 2 the condition fails immediately and nothing executes.
Essentially whatever code that is inside of for loop never executes.
In a for loop, the condition is evaluated before the first iteration. This means that in your example, the content of the loop would not be executed because i is already greater than or equal to 2.
Example code path:
Set i = 2.
Check if i < 2.
Exit loop because step 2 evaluated to false.
i would still be modified, however, as the variable initialization (i.e. i = 2) still occurs before the condition is checked.

Can someone help me understand how this "for" loop works? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
int main(void) {
long fall, n, k, p, i, j, r;
long long x, y, a[110][110];
for(a[0][0]=scanf("%ld",&fall);fall--;printf("%lld\n",y)) {
for(i=!!scanf("%ld%ld%ld",&n,&k,&p);i<=k+1;i++)
for(j=0;++j<=i;a[i][j]=(a[i-1][j-1]+j*a[i-1][j])%p)
;
for(y=!(j=1);j<=k+1;y=(y+a[k+1][j++]*x)%p)
for(x=!((r=n%j)*!(i=-1));++i<j;x=x*(n-i)/((i==r)?j:1)%p)
;
}
return 0;
}
How does for loop work here? It doesn't follow the syntax as I see.
for loops have the following pattern:
for(initial expression; conditional expr; afterthought)
I'll break down the first loop for you, you should be able to do the rest on your own.
for(a[0][0]=scanf("%ld",&fall);fall--;printf("%lld\n",y))
The initialization part of this loop is a[0][0]=scanf("%ld",&fall).
scanf is used for reading input and returns the number of input values. In this case, it will be 1 and it gets assigned to a[0][0].
fall-- is the conditional expression. In C, positive numbers are evaluated as true. So this loop will run until fall == 0.
printf("%lld\n",y) is the afterthought. It gets run after each loop iteration. In this case, it will simply print the value.
Unraveling obfuscated code can be a good learning exercise though you must obviously never use it in practice.
This code abuses the fact that the first and third conditions of the for loop does not necessarily need to have anything to do with the loop itself. At its core, the for loop simply executes an initial expression, performs the conditional check and executes the afterthought after every iteration.

using goto keyword in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
# include <stdio.h>
int main()
{
int a=5;
begin:
if(a)
printf("%d\n",a);
a--;
goto begin;
return 0;
}
When a becomes 0 then if condition will not execute then why the output is going to be infinty in this code means
output -
5
4
3
2
1
0
-1
-2
and so on endless
If the program really does print 0 for you then there might be a serious problem with your compiler (or even your machine...). My suspicion is that it doesn't print 0, but your question is really why the program loops infinitely.
This is because the if-body only contains the print statement. So when a reaches 0 it isn't printed but the lines
a--;
goto begin;
are still executed. The machine will obey, go back to begin and the loop continues. The quickest fix is to put braces around all the statements you want executed when a != 0:
if(a){
printf("%d\n",a);
a--;
goto begin;
}
return 0;
This will make the program only loop until a is 0, after which it returns.
But the real problem is: don't use goto (for this)! This is a perfect situation to use a while loop:
while(a--){
printf("%d\n", a);
}
return 0;
(The braces around the while-body are not even strictly necessary here, but just good practice)
Its because after the if the goto statement is again executed and then the value of a has already become other than 0 . Now, again you get the goto statement and therefore if goes on executing and printing negative values.
Look at it this way :-
The statement
printf("%d\n",a);
is executed only when condition in if is true. TRUE here refers to anything not equal to 0 so, the printf is not executed when a is 0, while it executes for any other value. Now, a-- and goto are executed outside if so they are executed again and again making the condition in if true always and negative values are printed infinitely.
Nevertheless,
My question is , why are you using goto?
if a==1 -> Evaluates to TRUE
if a==0 -> Evaluates to FALSE
if a==-1 -> Evaluates to TRUE
etc.
Therefore it will display numbers in descending order, except 0 which will not display.

Resources