printf wrong output when reversing a string [duplicate]
This question already has an answer here:
Reversing a string literal in C with pointers [duplicate]
1 answer
so this is the code for reversing a string
#include<stdio.h>
char* function(char *);
int main()
{
char a="computer";
printf("%s", function(a));
return 0;
}
char* function(char *p)
{
int l,i;
char t;
for (l=0;*(p+l)!='';l++);
for(i=0; i<(l/2) ; i++)
{
t=*(p+i);
*(p+i)=*(p+l-1-i);
*(p+l-1-i)=t;
}
return (p);
}
but if i change printf("%s", function(a));
in the main body to
printf("%s", function("computer"));
there is no output (the output is blank) in dev c++.... but it gives the desired output in turbo c++ even with this change....why is that?
c string pointers printf reverse
marked as duplicate by WhozCraig
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 20:58
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Reversing a string literal in C with pointers [duplicate]
1 answer
so this is the code for reversing a string
#include<stdio.h>
char* function(char *);
int main()
{
char a="computer";
printf("%s", function(a));
return 0;
}
char* function(char *p)
{
int l,i;
char t;
for (l=0;*(p+l)!='';l++);
for(i=0; i<(l/2) ; i++)
{
t=*(p+i);
*(p+i)=*(p+l-1-i);
*(p+l-1-i)=t;
}
return (p);
}
but if i change printf("%s", function(a));
in the main body to
printf("%s", function("computer"));
there is no output (the output is blank) in dev c++.... but it gives the desired output in turbo c++ even with this change....why is that?
c string pointers printf reverse
marked as duplicate by WhozCraig
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 20:58
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
Your program invokes undefined behavior when you write to a string literal with the second version. There are at least a thousand duplicates of this issue on this site, but the title's rarely reflect the actual issue, and are so divergent they're surprisingly difficult to find. Turbo C++ is probably older than you are, so get a modern compiler and turn up your warnings to pedantic levels.
– WhozCraig
Nov 20 '18 at 20:57
1
Exact duplicate of Reversing a string literal in C with pointers
– usr2564301
Nov 20 '18 at 20:57
1
Tip: Don't usel
and1
andi
in arithmetic expressions.
– Tim Randall
Nov 20 '18 at 20:58
1
@TimRandall because we can get it mixed up right?
– Anas Aziz
Nov 20 '18 at 21:01
@AnasAziz Yes, it's difficult to read. Personally I don't like to use single letter variable names, except infor
loops or cases where the meaning is super-obvious. I like to use one or two word names where possible.
– Tim Randall
Nov 20 '18 at 21:05
add a comment |
This question already has an answer here:
Reversing a string literal in C with pointers [duplicate]
1 answer
so this is the code for reversing a string
#include<stdio.h>
char* function(char *);
int main()
{
char a="computer";
printf("%s", function(a));
return 0;
}
char* function(char *p)
{
int l,i;
char t;
for (l=0;*(p+l)!='';l++);
for(i=0; i<(l/2) ; i++)
{
t=*(p+i);
*(p+i)=*(p+l-1-i);
*(p+l-1-i)=t;
}
return (p);
}
but if i change printf("%s", function(a));
in the main body to
printf("%s", function("computer"));
there is no output (the output is blank) in dev c++.... but it gives the desired output in turbo c++ even with this change....why is that?
c string pointers printf reverse
This question already has an answer here:
Reversing a string literal in C with pointers [duplicate]
1 answer
so this is the code for reversing a string
#include<stdio.h>
char* function(char *);
int main()
{
char a="computer";
printf("%s", function(a));
return 0;
}
char* function(char *p)
{
int l,i;
char t;
for (l=0;*(p+l)!='';l++);
for(i=0; i<(l/2) ; i++)
{
t=*(p+i);
*(p+i)=*(p+l-1-i);
*(p+l-1-i)=t;
}
return (p);
}
but if i change printf("%s", function(a));
in the main body to
printf("%s", function("computer"));
there is no output (the output is blank) in dev c++.... but it gives the desired output in turbo c++ even with this change....why is that?
This question already has an answer here:
Reversing a string literal in C with pointers [duplicate]
1 answer
c string pointers printf reverse
c string pointers printf reverse
asked Nov 20 '18 at 20:51
Anas AzizAnas Aziz
347
347
marked as duplicate by WhozCraig
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 20:58
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by WhozCraig
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 20:58
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
Your program invokes undefined behavior when you write to a string literal with the second version. There are at least a thousand duplicates of this issue on this site, but the title's rarely reflect the actual issue, and are so divergent they're surprisingly difficult to find. Turbo C++ is probably older than you are, so get a modern compiler and turn up your warnings to pedantic levels.
– WhozCraig
Nov 20 '18 at 20:57
1
Exact duplicate of Reversing a string literal in C with pointers
– usr2564301
Nov 20 '18 at 20:57
1
Tip: Don't usel
and1
andi
in arithmetic expressions.
– Tim Randall
Nov 20 '18 at 20:58
1
@TimRandall because we can get it mixed up right?
– Anas Aziz
Nov 20 '18 at 21:01
@AnasAziz Yes, it's difficult to read. Personally I don't like to use single letter variable names, except infor
loops or cases where the meaning is super-obvious. I like to use one or two word names where possible.
– Tim Randall
Nov 20 '18 at 21:05
add a comment |
2
Your program invokes undefined behavior when you write to a string literal with the second version. There are at least a thousand duplicates of this issue on this site, but the title's rarely reflect the actual issue, and are so divergent they're surprisingly difficult to find. Turbo C++ is probably older than you are, so get a modern compiler and turn up your warnings to pedantic levels.
– WhozCraig
Nov 20 '18 at 20:57
1
Exact duplicate of Reversing a string literal in C with pointers
– usr2564301
Nov 20 '18 at 20:57
1
Tip: Don't usel
and1
andi
in arithmetic expressions.
– Tim Randall
Nov 20 '18 at 20:58
1
@TimRandall because we can get it mixed up right?
– Anas Aziz
Nov 20 '18 at 21:01
@AnasAziz Yes, it's difficult to read. Personally I don't like to use single letter variable names, except infor
loops or cases where the meaning is super-obvious. I like to use one or two word names where possible.
– Tim Randall
Nov 20 '18 at 21:05
2
2
Your program invokes undefined behavior when you write to a string literal with the second version. There are at least a thousand duplicates of this issue on this site, but the title's rarely reflect the actual issue, and are so divergent they're surprisingly difficult to find. Turbo C++ is probably older than you are, so get a modern compiler and turn up your warnings to pedantic levels.
– WhozCraig
Nov 20 '18 at 20:57
Your program invokes undefined behavior when you write to a string literal with the second version. There are at least a thousand duplicates of this issue on this site, but the title's rarely reflect the actual issue, and are so divergent they're surprisingly difficult to find. Turbo C++ is probably older than you are, so get a modern compiler and turn up your warnings to pedantic levels.
– WhozCraig
Nov 20 '18 at 20:57
1
1
Exact duplicate of Reversing a string literal in C with pointers
– usr2564301
Nov 20 '18 at 20:57
Exact duplicate of Reversing a string literal in C with pointers
– usr2564301
Nov 20 '18 at 20:57
1
1
Tip: Don't use
l
and 1
and i
in arithmetic expressions.– Tim Randall
Nov 20 '18 at 20:58
Tip: Don't use
l
and 1
and i
in arithmetic expressions.– Tim Randall
Nov 20 '18 at 20:58
1
1
@TimRandall because we can get it mixed up right?
– Anas Aziz
Nov 20 '18 at 21:01
@TimRandall because we can get it mixed up right?
– Anas Aziz
Nov 20 '18 at 21:01
@AnasAziz Yes, it's difficult to read. Personally I don't like to use single letter variable names, except in
for
loops or cases where the meaning is super-obvious. I like to use one or two word names where possible.– Tim Randall
Nov 20 '18 at 21:05
@AnasAziz Yes, it's difficult to read. Personally I don't like to use single letter variable names, except in
for
loops or cases where the meaning is super-obvious. I like to use one or two word names where possible.– Tim Randall
Nov 20 '18 at 21:05
add a comment |
1 Answer
1
active
oldest
votes
Parameter "computer"
, which you pass to function
, is a string literal, and changing/manipulating the contents of a string literal is undefined behaviour. That's what you are experiencing - something undefined.
but then why is it running OK in turboC++ ide?
– Anas Aziz
Nov 21 '18 at 7:34
@Anas: because software from the 1980s did not check if the programmer followed the syntax rules. It just expected them to.
– usr2564301
Nov 21 '18 at 8:32
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Parameter "computer"
, which you pass to function
, is a string literal, and changing/manipulating the contents of a string literal is undefined behaviour. That's what you are experiencing - something undefined.
but then why is it running OK in turboC++ ide?
– Anas Aziz
Nov 21 '18 at 7:34
@Anas: because software from the 1980s did not check if the programmer followed the syntax rules. It just expected them to.
– usr2564301
Nov 21 '18 at 8:32
add a comment |
Parameter "computer"
, which you pass to function
, is a string literal, and changing/manipulating the contents of a string literal is undefined behaviour. That's what you are experiencing - something undefined.
but then why is it running OK in turboC++ ide?
– Anas Aziz
Nov 21 '18 at 7:34
@Anas: because software from the 1980s did not check if the programmer followed the syntax rules. It just expected them to.
– usr2564301
Nov 21 '18 at 8:32
add a comment |
Parameter "computer"
, which you pass to function
, is a string literal, and changing/manipulating the contents of a string literal is undefined behaviour. That's what you are experiencing - something undefined.
Parameter "computer"
, which you pass to function
, is a string literal, and changing/manipulating the contents of a string literal is undefined behaviour. That's what you are experiencing - something undefined.
answered Nov 20 '18 at 20:57
Stephan LechnerStephan Lechner
28.1k32143
28.1k32143
but then why is it running OK in turboC++ ide?
– Anas Aziz
Nov 21 '18 at 7:34
@Anas: because software from the 1980s did not check if the programmer followed the syntax rules. It just expected them to.
– usr2564301
Nov 21 '18 at 8:32
add a comment |
but then why is it running OK in turboC++ ide?
– Anas Aziz
Nov 21 '18 at 7:34
@Anas: because software from the 1980s did not check if the programmer followed the syntax rules. It just expected them to.
– usr2564301
Nov 21 '18 at 8:32
but then why is it running OK in turboC++ ide?
– Anas Aziz
Nov 21 '18 at 7:34
but then why is it running OK in turboC++ ide?
– Anas Aziz
Nov 21 '18 at 7:34
@Anas: because software from the 1980s did not check if the programmer followed the syntax rules. It just expected them to.
– usr2564301
Nov 21 '18 at 8:32
@Anas: because software from the 1980s did not check if the programmer followed the syntax rules. It just expected them to.
– usr2564301
Nov 21 '18 at 8:32
add a comment |
2
Your program invokes undefined behavior when you write to a string literal with the second version. There are at least a thousand duplicates of this issue on this site, but the title's rarely reflect the actual issue, and are so divergent they're surprisingly difficult to find. Turbo C++ is probably older than you are, so get a modern compiler and turn up your warnings to pedantic levels.
– WhozCraig
Nov 20 '18 at 20:57
1
Exact duplicate of Reversing a string literal in C with pointers
– usr2564301
Nov 20 '18 at 20:57
1
Tip: Don't use
l
and1
andi
in arithmetic expressions.– Tim Randall
Nov 20 '18 at 20:58
1
@TimRandall because we can get it mixed up right?
– Anas Aziz
Nov 20 '18 at 21:01
@AnasAziz Yes, it's difficult to read. Personally I don't like to use single letter variable names, except in
for
loops or cases where the meaning is super-obvious. I like to use one or two word names where possible.– Tim Randall
Nov 20 '18 at 21:05