Problem With C?

R

raashid bhatt

Why does the following program runs and works correctly
=============================================
#include <stdio.h>
#include <string.h>

char a = 'a'; / * One byte*/

int main(int argc, char **argv)
{

printf("%s", &a);
/* Prints a */

/* Undefined behaviour on a*/
strcpy (&a, "HELLOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");

printf("%s", &a);
/* Should not print HELOOOO...... correctly */
/*i Think it must print H */

return 0;
}


===========================================

I have copied a full string into a char and prints that string
correctly ?
 
R

raashid bhatt

and Even more strange

printf("%d", (int)sizeof(a));

Prints 1 (one byte) even after copying the whole string into it
 
I

Ian Collins

raashid said:
Why does the following program runs and works correctly

By some queer definition of correctly!
=============================================
#include <stdio.h>
#include <string.h>

char a = 'a'; / * One byte*/
Typo.

int main(int argc, char **argv)
{

printf("%s", &a);
/* Prints a */
By chance. The memory after a happens to be 0.
/* Undefined behaviour on a*/
strcpy (&a, "HELLOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
Completely undefined, everything depends on what memory you have
overwritten.
printf("%s", &a);
/* Should not print HELOOOO...... correctly */
/*i Think it must print H */

return 0;
}


===========================================

I have copied a full string into a char and prints that string
correctly ?

By chance. The block of memory after a happens to be 0 filled.
 
I

Ian Collins

raashid said:
and Even more strange

printf("%d", (int)sizeof(a));

Prints 1 (one byte) even after copying the whole string into it

a is a char and sizeof is a compiler time operator. What else would you
expect?
 
B

Barry Schwarz

Why does the following program runs and works correctly
=============================================
#include <stdio.h>
#include <string.h>

char a = 'a'; / * One byte*/

int main(int argc, char **argv)
{

printf("%s", &a);

You invoke undefined behavior here.
/* Prints a */

/* Undefined behaviour on a*/
strcpy (&a, "HELLOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");

And here also.

One of the more unlucky manifestations of undefined behavior is to do
what you expect. Murphy's law suggests that it will continue to do so
until the most inopportune time when it will do something completely
unexpected.
printf("%s", &a);
/* Should not print HELOOOO...... correctly */
/*i Think it must print H */

You can think whatever you want but you are just guessing.
return 0;
}


===========================================

I have copied a full string into a char and prints that string
correctly ?

Once you invoke undefined behavior, there is no such thing as
correctly.
 
V

vippstar

[...]
Once you invoke undefined behavior, there is no such thing as
correctly.

Don't you mean "there is no such thing as INcorrect"? Anything the
program does in response undefined behavior is correct
(standard-conforming).

Don't snip context. There's no such thing as incorrect from the
implementations view; there's no such thing as correct from the
programmers view.
 
J

John Bode

Why does the following program runs and works correctly
=============================================
#include <stdio.h>
#include <string.h>

char a = 'a'; / * One byte*/

int main(int argc, char **argv)
{

printf("%s", &a);
/* Prints a */

By sheer luck, the memory immediately following a is set to 0; you
could have had 'a' followed by a stream of junk. You have invoked
undefined behavior here. Had you written

printf("%c\n", a);

then you would have seen the output you expect and not have invoked
UB.
/* Undefined behaviour on a*/
strcpy (&a, "HELLOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");

printf("%s", &a);
/* Should not print HELOOOO...... correctly */
/*i Think it must print H */

I'm curious as to why you would think that. The %s conversion
specifier expects a pointer to a zero-terminated string of char; 'E'
is not zero. If you want to just print 'H', you would again write

printf("%c\n", a);

Of course, you've already invoked UB with the strcpy() call (since
you're writing to an object that was declared char, not char[N]), so
there's no guarantee you'd see the output you want anyway.
return 0;

}

===========================================

I have copied a full string into a char and prints that string
correctly ?

UB is working in your favor. That's usually bad news.
 
J

John Bode

and Even more strange

printf("%d", (int)sizeof(a));

Prints 1 (one byte) even after copying the whole string into it

As it would; sizeof returns the size of the *type* of a, which is
char, which is by definition 1 byte.

Assume the following:

char a;
char ar[10] = "Hello";
char *ap = ar;

sizeof a == sizeof(char) == 1
sizeof ar == sizeof (char [10]) == 10
sizeof *ap == sizeof (char *) == however big a pointer type is (4
bytes on my system)

Note that sizeof ar is always 10, regardless of the length of the
string stored in it. strlen() will return the length of the string in
ar:

strlen(ar) == 5
 
C

CBFalconer

raashid said:
Why does the following program runs and works correctly
=============================================
#include <stdio.h>
#include <string.h>

char a = 'a'; / * One byte*/

int main(int argc, char **argv) {
printf("%s", &a);
/* Prints a */

/* Undefined behaviour on a*/
strcpy (&a, "HELLOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");

printf("%s", &a);
/* Should not print HELOOOO...... correctly */
/*i Think it must print H */

return 0;
}

I have copied a full string into a char and prints that string
correctly ?

Each of your printfs invokes undefined behaviour. So does the
strcpy line. That includes actually printing the value out, on
your machine, at this time.
 
P

Peter Nilsson

raashid bhatt said:
Why does the following program runs and works correctly

It might run, it might even give 'expected' output, but it
is not correct and it seems you already know that.

Analysing deliberately broken code is a waste of time. It's
much more beneficial to study which constructs are correct.
=============================================
#include <stdio.h>
#include <string.h>

char a = 'a'; / * One byte*/

int main(int argc, char **argv)
{
  printf("%s", &a);
  /* Prints a */

printf("%.1s", &a);
  /* Undefined behaviour on a*/
  strcpy (&a, "HELLOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");

strncpy(&a, "HELLOOOOOOOOOOOOOOOOOOOOOOOOOOOOO", 1);
  printf("%s", &a);

printf("%.1s", &a);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top