printf wrong output when reversing a string [duplicate]












-1
















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?










share|improve this question













marked as duplicate by WhozCraig c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

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 use l and 1 and i 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
















-1
















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?










share|improve this question













marked as duplicate by WhozCraig c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

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 use l and 1 and i 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














-1












-1








-1









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?










share|improve this question















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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 20:51









Anas AzizAnas Aziz

347




347




marked as duplicate by WhozCraig c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

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 c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

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 use l and 1 and i 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














  • 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 and 1 and i 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








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












1 Answer
1






active

oldest

votes


















3














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.






share|improve this answer
























  • 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




















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














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.






share|improve this answer
























  • 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


















3














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.






share|improve this answer
























  • 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
















3












3








3







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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





















  • 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





Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]