Calculating string length not correct when not printing out the return value of a program
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Length is suppose to return how far along the counter went when going trough the string. However, it only returns the correct value when it is printed beforehand. If i comment out the printf
it returns 0.
Does anyone have an explanation for this?
#include<stdio.h>
#include<string.h>
#define MAX 100
int length(char *s) {
int i;
for (i = 0; s[i] != ''; ++i)
printf("%d ", i); //<-- here
return i;
}
int main()
{
char s[MAX];
fgets(s, (char)sizeof(s), stdin);
s[strcspn(s, "n")]='';
printf("Length: %dn", length(s));
return 0;
}
c arrays string loops
add a comment |
Length is suppose to return how far along the counter went when going trough the string. However, it only returns the correct value when it is printed beforehand. If i comment out the printf
it returns 0.
Does anyone have an explanation for this?
#include<stdio.h>
#include<string.h>
#define MAX 100
int length(char *s) {
int i;
for (i = 0; s[i] != ''; ++i)
printf("%d ", i); //<-- here
return i;
}
int main()
{
char s[MAX];
fgets(s, (char)sizeof(s), stdin);
s[strcspn(s, "n")]='';
printf("Length: %dn", length(s));
return 0;
}
c arrays string loops
That's why you always use brackets{}
^^
– M.K
Jan 4 at 9:58
add a comment |
Length is suppose to return how far along the counter went when going trough the string. However, it only returns the correct value when it is printed beforehand. If i comment out the printf
it returns 0.
Does anyone have an explanation for this?
#include<stdio.h>
#include<string.h>
#define MAX 100
int length(char *s) {
int i;
for (i = 0; s[i] != ''; ++i)
printf("%d ", i); //<-- here
return i;
}
int main()
{
char s[MAX];
fgets(s, (char)sizeof(s), stdin);
s[strcspn(s, "n")]='';
printf("Length: %dn", length(s));
return 0;
}
c arrays string loops
Length is suppose to return how far along the counter went when going trough the string. However, it only returns the correct value when it is printed beforehand. If i comment out the printf
it returns 0.
Does anyone have an explanation for this?
#include<stdio.h>
#include<string.h>
#define MAX 100
int length(char *s) {
int i;
for (i = 0; s[i] != ''; ++i)
printf("%d ", i); //<-- here
return i;
}
int main()
{
char s[MAX];
fgets(s, (char)sizeof(s), stdin);
s[strcspn(s, "n")]='';
printf("Length: %dn", length(s));
return 0;
}
c arrays string loops
c arrays string loops
edited Jan 4 at 11:36
PauseUnpause
asked Jan 4 at 9:14
PauseUnpausePauseUnpause
6718
6718
That's why you always use brackets{}
^^
– M.K
Jan 4 at 9:58
add a comment |
That's why you always use brackets{}
^^
– M.K
Jan 4 at 9:58
That's why you always use brackets
{}
^^– M.K
Jan 4 at 9:58
That's why you always use brackets
{}
^^– M.K
Jan 4 at 9:58
add a comment |
3 Answers
3
active
oldest
votes
The problem is, if you comment out the printf()
statement in the length()
function, the return
statement becomes part of the loop body, and the very first iteration returns from the call and you get the the-then value of i
, which is just the entry value for the loop, 0
.
for (i = 0; s[i] != ''; ++i)
//printf("%d ", i); //<-- here
return i; // without brace-enfoced scope, this is the loop body.
is the same as
for (i = 0; s[i] != ''; ++i)
return i;
What You need is the loop to complete the execution, hit the exit criteria and then, execute the return
statement with the latest value of i
.
So, to avoid the problem, you can enforce empty execution of the loop, by something like
for (i = 0; s[i] != ''; ++i) ; // notice the ; here, ends the scope.
return i;
or, even better (for the readers)
for (int i = 0; i < 10; i++)
{/*nothing here*/} //indicates empty loop body
return i;
Note: As an alternative way, to enhance the readability, instead of a for
construct you can also make use of while
loop, which goes like
while (s[i] != '')
{
i++; //increment statement is explicit.
}
So what you are saying, if i don't have a semicolon, regardless of how far, the first statement after the loop will get executed. Correct?
– PauseUnpause
Jan 4 at 9:25
1
@PauseUnpause Yep, that's how the grammar works.
– Sourav Ghosh
Jan 4 at 9:26
Here is one situation where while loop should be used instead of for loop where increment Takes place inside the braces.
– Michi
Jan 4 at 9:32
@Michi You mean to say, make the increment of the index explicit by putting it as the loop body forwhile
construct, right?
– Sourav Ghosh
Jan 4 at 9:34
1
@Michi Done already. :)
– Sourav Ghosh
Jan 4 at 14:37
|
show 1 more comment
In a loop like this:
for (...)
statement1;
statement2;
statement1
will be the only thing executed in the loop. When you comment out the printf
call, return i;
is executed on the very first iteration, immediately returning zero.
Notice, however, that statement1
can be empty, so, to run a loop with no body, do:
for (...)
; // yes, a hanging semicolon
// or like this:
for (...);
add a comment |
If you just commnt out that printf
, you will be left with this:
int length(char *s) {
int i;
for (i = 0; s[i] != ''; ++i)
return i;
}
This is equal to:
int length(char *s) {
int i;
for (i = 0; s[i] != ''; ++i){
return i; // returns in the first iteration, without even incrementing i once
}
}
Surely this isn't what you intended. Instead, put a semicolon after the for
loop:
for (i = 0; s[i] != ''; ++i);
This way, for (i = 0; s[i] != ''; ++i);
won't accidentally affect a following statement.
You can prevent such accidents by adding explicit braces to denote your intention, for instance:
int length(char *s) {
int i;
for (i = 0; s[i] != ''; ++i) {
printf("%d ", i); //<-- here
}
return i;
}
Now if you remove the printf
line, it doesn't affect the program execution in any other way than omitting the print.
4
Even better is to always use explicit braces on loops, conditions etc, then problems like this cannot occur. In perl for example, explicit braces are even mandatory.
– Ctx
Jan 4 at 9:20
1
Good suggestion, I added a version of how the function could look like this way.
– Blaze
Jan 4 at 9:23
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54035943%2fcalculating-string-length-not-correct-when-not-printing-out-the-return-value-of%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is, if you comment out the printf()
statement in the length()
function, the return
statement becomes part of the loop body, and the very first iteration returns from the call and you get the the-then value of i
, which is just the entry value for the loop, 0
.
for (i = 0; s[i] != ''; ++i)
//printf("%d ", i); //<-- here
return i; // without brace-enfoced scope, this is the loop body.
is the same as
for (i = 0; s[i] != ''; ++i)
return i;
What You need is the loop to complete the execution, hit the exit criteria and then, execute the return
statement with the latest value of i
.
So, to avoid the problem, you can enforce empty execution of the loop, by something like
for (i = 0; s[i] != ''; ++i) ; // notice the ; here, ends the scope.
return i;
or, even better (for the readers)
for (int i = 0; i < 10; i++)
{/*nothing here*/} //indicates empty loop body
return i;
Note: As an alternative way, to enhance the readability, instead of a for
construct you can also make use of while
loop, which goes like
while (s[i] != '')
{
i++; //increment statement is explicit.
}
So what you are saying, if i don't have a semicolon, regardless of how far, the first statement after the loop will get executed. Correct?
– PauseUnpause
Jan 4 at 9:25
1
@PauseUnpause Yep, that's how the grammar works.
– Sourav Ghosh
Jan 4 at 9:26
Here is one situation where while loop should be used instead of for loop where increment Takes place inside the braces.
– Michi
Jan 4 at 9:32
@Michi You mean to say, make the increment of the index explicit by putting it as the loop body forwhile
construct, right?
– Sourav Ghosh
Jan 4 at 9:34
1
@Michi Done already. :)
– Sourav Ghosh
Jan 4 at 14:37
|
show 1 more comment
The problem is, if you comment out the printf()
statement in the length()
function, the return
statement becomes part of the loop body, and the very first iteration returns from the call and you get the the-then value of i
, which is just the entry value for the loop, 0
.
for (i = 0; s[i] != ''; ++i)
//printf("%d ", i); //<-- here
return i; // without brace-enfoced scope, this is the loop body.
is the same as
for (i = 0; s[i] != ''; ++i)
return i;
What You need is the loop to complete the execution, hit the exit criteria and then, execute the return
statement with the latest value of i
.
So, to avoid the problem, you can enforce empty execution of the loop, by something like
for (i = 0; s[i] != ''; ++i) ; // notice the ; here, ends the scope.
return i;
or, even better (for the readers)
for (int i = 0; i < 10; i++)
{/*nothing here*/} //indicates empty loop body
return i;
Note: As an alternative way, to enhance the readability, instead of a for
construct you can also make use of while
loop, which goes like
while (s[i] != '')
{
i++; //increment statement is explicit.
}
So what you are saying, if i don't have a semicolon, regardless of how far, the first statement after the loop will get executed. Correct?
– PauseUnpause
Jan 4 at 9:25
1
@PauseUnpause Yep, that's how the grammar works.
– Sourav Ghosh
Jan 4 at 9:26
Here is one situation where while loop should be used instead of for loop where increment Takes place inside the braces.
– Michi
Jan 4 at 9:32
@Michi You mean to say, make the increment of the index explicit by putting it as the loop body forwhile
construct, right?
– Sourav Ghosh
Jan 4 at 9:34
1
@Michi Done already. :)
– Sourav Ghosh
Jan 4 at 14:37
|
show 1 more comment
The problem is, if you comment out the printf()
statement in the length()
function, the return
statement becomes part of the loop body, and the very first iteration returns from the call and you get the the-then value of i
, which is just the entry value for the loop, 0
.
for (i = 0; s[i] != ''; ++i)
//printf("%d ", i); //<-- here
return i; // without brace-enfoced scope, this is the loop body.
is the same as
for (i = 0; s[i] != ''; ++i)
return i;
What You need is the loop to complete the execution, hit the exit criteria and then, execute the return
statement with the latest value of i
.
So, to avoid the problem, you can enforce empty execution of the loop, by something like
for (i = 0; s[i] != ''; ++i) ; // notice the ; here, ends the scope.
return i;
or, even better (for the readers)
for (int i = 0; i < 10; i++)
{/*nothing here*/} //indicates empty loop body
return i;
Note: As an alternative way, to enhance the readability, instead of a for
construct you can also make use of while
loop, which goes like
while (s[i] != '')
{
i++; //increment statement is explicit.
}
The problem is, if you comment out the printf()
statement in the length()
function, the return
statement becomes part of the loop body, and the very first iteration returns from the call and you get the the-then value of i
, which is just the entry value for the loop, 0
.
for (i = 0; s[i] != ''; ++i)
//printf("%d ", i); //<-- here
return i; // without brace-enfoced scope, this is the loop body.
is the same as
for (i = 0; s[i] != ''; ++i)
return i;
What You need is the loop to complete the execution, hit the exit criteria and then, execute the return
statement with the latest value of i
.
So, to avoid the problem, you can enforce empty execution of the loop, by something like
for (i = 0; s[i] != ''; ++i) ; // notice the ; here, ends the scope.
return i;
or, even better (for the readers)
for (int i = 0; i < 10; i++)
{/*nothing here*/} //indicates empty loop body
return i;
Note: As an alternative way, to enhance the readability, instead of a for
construct you can also make use of while
loop, which goes like
while (s[i] != '')
{
i++; //increment statement is explicit.
}
edited Jan 4 at 9:55
answered Jan 4 at 9:17
Sourav GhoshSourav Ghosh
111k15132191
111k15132191
So what you are saying, if i don't have a semicolon, regardless of how far, the first statement after the loop will get executed. Correct?
– PauseUnpause
Jan 4 at 9:25
1
@PauseUnpause Yep, that's how the grammar works.
– Sourav Ghosh
Jan 4 at 9:26
Here is one situation where while loop should be used instead of for loop where increment Takes place inside the braces.
– Michi
Jan 4 at 9:32
@Michi You mean to say, make the increment of the index explicit by putting it as the loop body forwhile
construct, right?
– Sourav Ghosh
Jan 4 at 9:34
1
@Michi Done already. :)
– Sourav Ghosh
Jan 4 at 14:37
|
show 1 more comment
So what you are saying, if i don't have a semicolon, regardless of how far, the first statement after the loop will get executed. Correct?
– PauseUnpause
Jan 4 at 9:25
1
@PauseUnpause Yep, that's how the grammar works.
– Sourav Ghosh
Jan 4 at 9:26
Here is one situation where while loop should be used instead of for loop where increment Takes place inside the braces.
– Michi
Jan 4 at 9:32
@Michi You mean to say, make the increment of the index explicit by putting it as the loop body forwhile
construct, right?
– Sourav Ghosh
Jan 4 at 9:34
1
@Michi Done already. :)
– Sourav Ghosh
Jan 4 at 14:37
So what you are saying, if i don't have a semicolon, regardless of how far, the first statement after the loop will get executed. Correct?
– PauseUnpause
Jan 4 at 9:25
So what you are saying, if i don't have a semicolon, regardless of how far, the first statement after the loop will get executed. Correct?
– PauseUnpause
Jan 4 at 9:25
1
1
@PauseUnpause Yep, that's how the grammar works.
– Sourav Ghosh
Jan 4 at 9:26
@PauseUnpause Yep, that's how the grammar works.
– Sourav Ghosh
Jan 4 at 9:26
Here is one situation where while loop should be used instead of for loop where increment Takes place inside the braces.
– Michi
Jan 4 at 9:32
Here is one situation where while loop should be used instead of for loop where increment Takes place inside the braces.
– Michi
Jan 4 at 9:32
@Michi You mean to say, make the increment of the index explicit by putting it as the loop body for
while
construct, right?– Sourav Ghosh
Jan 4 at 9:34
@Michi You mean to say, make the increment of the index explicit by putting it as the loop body for
while
construct, right?– Sourav Ghosh
Jan 4 at 9:34
1
1
@Michi Done already. :)
– Sourav Ghosh
Jan 4 at 14:37
@Michi Done already. :)
– Sourav Ghosh
Jan 4 at 14:37
|
show 1 more comment
In a loop like this:
for (...)
statement1;
statement2;
statement1
will be the only thing executed in the loop. When you comment out the printf
call, return i;
is executed on the very first iteration, immediately returning zero.
Notice, however, that statement1
can be empty, so, to run a loop with no body, do:
for (...)
; // yes, a hanging semicolon
// or like this:
for (...);
add a comment |
In a loop like this:
for (...)
statement1;
statement2;
statement1
will be the only thing executed in the loop. When you comment out the printf
call, return i;
is executed on the very first iteration, immediately returning zero.
Notice, however, that statement1
can be empty, so, to run a loop with no body, do:
for (...)
; // yes, a hanging semicolon
// or like this:
for (...);
add a comment |
In a loop like this:
for (...)
statement1;
statement2;
statement1
will be the only thing executed in the loop. When you comment out the printf
call, return i;
is executed on the very first iteration, immediately returning zero.
Notice, however, that statement1
can be empty, so, to run a loop with no body, do:
for (...)
; // yes, a hanging semicolon
// or like this:
for (...);
In a loop like this:
for (...)
statement1;
statement2;
statement1
will be the only thing executed in the loop. When you comment out the printf
call, return i;
is executed on the very first iteration, immediately returning zero.
Notice, however, that statement1
can be empty, so, to run a loop with no body, do:
for (...)
; // yes, a hanging semicolon
// or like this:
for (...);
answered Jan 4 at 9:18
ForceBruForceBru
20.7k83455
20.7k83455
add a comment |
add a comment |
If you just commnt out that printf
, you will be left with this:
int length(char *s) {
int i;
for (i = 0; s[i] != ''; ++i)
return i;
}
This is equal to:
int length(char *s) {
int i;
for (i = 0; s[i] != ''; ++i){
return i; // returns in the first iteration, without even incrementing i once
}
}
Surely this isn't what you intended. Instead, put a semicolon after the for
loop:
for (i = 0; s[i] != ''; ++i);
This way, for (i = 0; s[i] != ''; ++i);
won't accidentally affect a following statement.
You can prevent such accidents by adding explicit braces to denote your intention, for instance:
int length(char *s) {
int i;
for (i = 0; s[i] != ''; ++i) {
printf("%d ", i); //<-- here
}
return i;
}
Now if you remove the printf
line, it doesn't affect the program execution in any other way than omitting the print.
4
Even better is to always use explicit braces on loops, conditions etc, then problems like this cannot occur. In perl for example, explicit braces are even mandatory.
– Ctx
Jan 4 at 9:20
1
Good suggestion, I added a version of how the function could look like this way.
– Blaze
Jan 4 at 9:23
add a comment |
If you just commnt out that printf
, you will be left with this:
int length(char *s) {
int i;
for (i = 0; s[i] != ''; ++i)
return i;
}
This is equal to:
int length(char *s) {
int i;
for (i = 0; s[i] != ''; ++i){
return i; // returns in the first iteration, without even incrementing i once
}
}
Surely this isn't what you intended. Instead, put a semicolon after the for
loop:
for (i = 0; s[i] != ''; ++i);
This way, for (i = 0; s[i] != ''; ++i);
won't accidentally affect a following statement.
You can prevent such accidents by adding explicit braces to denote your intention, for instance:
int length(char *s) {
int i;
for (i = 0; s[i] != ''; ++i) {
printf("%d ", i); //<-- here
}
return i;
}
Now if you remove the printf
line, it doesn't affect the program execution in any other way than omitting the print.
4
Even better is to always use explicit braces on loops, conditions etc, then problems like this cannot occur. In perl for example, explicit braces are even mandatory.
– Ctx
Jan 4 at 9:20
1
Good suggestion, I added a version of how the function could look like this way.
– Blaze
Jan 4 at 9:23
add a comment |
If you just commnt out that printf
, you will be left with this:
int length(char *s) {
int i;
for (i = 0; s[i] != ''; ++i)
return i;
}
This is equal to:
int length(char *s) {
int i;
for (i = 0; s[i] != ''; ++i){
return i; // returns in the first iteration, without even incrementing i once
}
}
Surely this isn't what you intended. Instead, put a semicolon after the for
loop:
for (i = 0; s[i] != ''; ++i);
This way, for (i = 0; s[i] != ''; ++i);
won't accidentally affect a following statement.
You can prevent such accidents by adding explicit braces to denote your intention, for instance:
int length(char *s) {
int i;
for (i = 0; s[i] != ''; ++i) {
printf("%d ", i); //<-- here
}
return i;
}
Now if you remove the printf
line, it doesn't affect the program execution in any other way than omitting the print.
If you just commnt out that printf
, you will be left with this:
int length(char *s) {
int i;
for (i = 0; s[i] != ''; ++i)
return i;
}
This is equal to:
int length(char *s) {
int i;
for (i = 0; s[i] != ''; ++i){
return i; // returns in the first iteration, without even incrementing i once
}
}
Surely this isn't what you intended. Instead, put a semicolon after the for
loop:
for (i = 0; s[i] != ''; ++i);
This way, for (i = 0; s[i] != ''; ++i);
won't accidentally affect a following statement.
You can prevent such accidents by adding explicit braces to denote your intention, for instance:
int length(char *s) {
int i;
for (i = 0; s[i] != ''; ++i) {
printf("%d ", i); //<-- here
}
return i;
}
Now if you remove the printf
line, it doesn't affect the program execution in any other way than omitting the print.
edited Jan 4 at 9:22
answered Jan 4 at 9:19
BlazeBlaze
7,6141832
7,6141832
4
Even better is to always use explicit braces on loops, conditions etc, then problems like this cannot occur. In perl for example, explicit braces are even mandatory.
– Ctx
Jan 4 at 9:20
1
Good suggestion, I added a version of how the function could look like this way.
– Blaze
Jan 4 at 9:23
add a comment |
4
Even better is to always use explicit braces on loops, conditions etc, then problems like this cannot occur. In perl for example, explicit braces are even mandatory.
– Ctx
Jan 4 at 9:20
1
Good suggestion, I added a version of how the function could look like this way.
– Blaze
Jan 4 at 9:23
4
4
Even better is to always use explicit braces on loops, conditions etc, then problems like this cannot occur. In perl for example, explicit braces are even mandatory.
– Ctx
Jan 4 at 9:20
Even better is to always use explicit braces on loops, conditions etc, then problems like this cannot occur. In perl for example, explicit braces are even mandatory.
– Ctx
Jan 4 at 9:20
1
1
Good suggestion, I added a version of how the function could look like this way.
– Blaze
Jan 4 at 9:23
Good suggestion, I added a version of how the function could look like this way.
– Blaze
Jan 4 at 9:23
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54035943%2fcalculating-string-length-not-correct-when-not-printing-out-the-return-value-of%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
That's why you always use brackets
{}
^^– M.K
Jan 4 at 9:58