Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > segmentation fault

Reply
Thread Tools

segmentation fault

 
 
ramu
Guest
Posts: n/a
 
      02-06-2006
Hi,
int main(void)
{
char ch='a';
printf("%s",ch);
return 0;
}

Why does this give segmentation fault?

Regards,

 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      02-06-2006
ramu wrote:
> Hi,
> int main(void)
> {
> char ch='a';
> printf("%s",ch);
> return 0;
> }
>
> Why does this give segmentation fault?


Because ch is a char (containing 'a'), not a string.
If you want to print a char, tell printf so:

#include <stdio.h> /* necessary for printf */
int main(void)
{
char ch='a';
printf("%c\n",ch); /* note '%c' and the '\n' */
return 0;
}

If you want to print a string, provide one:

#include <stdio.h> /* necessary for printf */
int main(void)
{
char ch[]="a"; /* a string, equivalent to
char ch[] = {'a', 0};
*/
printf("%s\n",ch); /* note the '\n' */
return 0;
}
 
Reply With Quote
 
 
 
 
ramu
Guest
Posts: n/a
 
      02-06-2006
Hi,
Thanks for you answer.

I want to know exactly how does it give segmentation fault? And can you
explain the significance of ' \n' in the above code?

Regards

 
Reply With Quote
 
Steve Summit
Guest
Posts: n/a
 
      02-06-2006
ramu wrote:
> char ch='a';
> printf("%s",ch);
>
> Why does this give segmentation fault?


There is a clue in question 8.1 of the FAQ list at
http://c-faq.com/charstring/q8.1.html .
 
Reply With Quote
 
Vladimir S. Oka
Guest
Posts: n/a
 
      02-06-2006
ramu wrote:

> Hi,
> Thanks for you answer.


Please provide context. How to:

"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>

> I want to know exactly how does it give segmentation fault?


You'll have to study your compiler/OS to get an answer to this, and that
is off topic here (even if you said what they were).

> And can you explain the significance of ' \n' in the above code?


If printf() is not terminated by a '\n' it's not guaranteed to output
anything.


--
BR, Vladimir

There's an old proverb that says just about whatever you want it to.

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      02-06-2006
ramu wrote:
> Hi,
> Thanks for you answer.
>
> I want to know exactly how does it give segmentation fault?


Because the "%s" specifier tells printf that a string will be printed,
and a string is terminated with a 0. But 'a' is a single char with no 0;
printf will search into memory not allocated by your program for that 0,
and that is not allowed.

> And can you
> explain the significance of ' \n' in the above code?


Without an end-of-line character '\n' at the end of the last output
line, there are no guarantees about what will happen, including whether
you will ever see that line, whether (if it appears) it will have any
prompt on the same or next line or even be overwritten by that prompt.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-06-2006
Martin Ambuhl <(E-Mail Removed)> writes:
> ramu wrote:
>> Hi,
>> Thanks for you answer.
>> I want to know exactly how does it give segmentation fault?

>
> Because the "%s" specifier tells printf that a string will be printed,
> and a string is terminated with a 0. But 'a' is a single char with no
> 0; printf will search into memory not allocated by your program for
> that 0, and that is not allowed.


The code in question was:

char ch='a';
printf("%s",ch);

The fact that the 'a' isn't followed by a '\0' isn't the issue.
Given some plausible assumptions about how parameters are passed,
the value of 'a' (97 on an ASCII-based system) will be interpreted by
printf() as if it were a pointer value. So it *might* print characters
starting at memory address 97 until it happens to run into a '\0'.
Or it might die immediately if that address doesn't exist or is
write-protected.

And that's just assuming that a character (almost certainly promoted
to int) and a char* are passed to printf() in the same location. The
"plausible assumptions" I mentioned above are absolutely *not*
guaranteed by the standard, and there are real-world implementations
where they don't apply. The *only* thing guaranteed is that the code
invokes undefined behavior, defined as "behavior, upon use of a
nonportable or erroneous program construct or of erroneous data, for
which this International Standard imposes no requirements". The
standard joke here is that it can make demons fly out your nose;
that's obviously unrealistic, but an implementation that actually did
that wouldn't violate the standard (just the laws of physics).

If you want to understand why the code behaves in a particular way on
a particular implementation, the C standard is silent on the subject;
you might ask in a newsgroup specific to your compiler or system.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      02-06-2006
ramu wrote:
>
> Hi,
> int main(void)
> {
> char ch='a';
> printf("%s",ch);
> return 0;
> }
>
> Why does this give segmentation fault?


Because you lied to printf(). You told printf() to expect a pointer to
a nul-terminated string, and instead passed a char.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <(E-Mail Removed)>

 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      02-06-2006
On 5 Feb 2006 21:46:21 -0800, in comp.lang.c , "ramu"
<(E-Mail Removed)> wrote:

>Hi,
>Thanks for you answer.



Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header. For more informatio, please go here
<http://cfaj.freeshell.org/google/>

>I want to know exactly how does it give segmentation fault?


If you mean "why precisely does my computer decide this is a
segmentation fault", then you'd need to ask in a group specialising in
your OS. As far as C is concerned, its merely a bug.

>And can you
>explain the significance of ' \n' in the above code?


'\n' means "print a newline".

You should buy a decent C book, it will explain all this stuff. Usenet
is NOT a good place to learn C.
#

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      02-06-2006

ramu wrote:
> Hi,
> int main(void)
> {
> char ch='a';
> printf("%s",ch);
> return 0;
> }
>
> Why does this give segmentation fault?
>
> Regards,


The %s conversion specifier expects its corresponding argument to be
the starting address of a zero-terminated array of char. printf() is
taking the value of 'a' (ASCII 97) and intepreting that as an address.
The problem is that 97 is an invalid address, hence the segfault.

You want to use the %c conversion specifier to print single characters.


int main(void)
{
char ch = 'a';
char str[] = "a"; /* {'a', 0} */

printf ("ch=%c, str=%s\n", ch, str);
return 0;
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation fault using Firefox 15.0.2 Keith Lee Firefox 3 04-29-2006 05:45 PM
Xerces on Solaris - Segmentation fault ldvmbs@gmail.com XML 0 05-16-2005 07:21 AM
Xerces XML Parser Segmentation fault with Java Pud XML 0 11-06-2003 05:07 PM
Intel Xeon + Linux + IBM sdk 1.3.1 - getting Segmentation fault Alex Hunsley Java 17 11-06-2003 12:12 AM
Re: segmentation fault exception handling Ivan Vecerina C++ 0 06-29-2003 10:56 PM



Advertisments