top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

Got segmentation fault while printing line number of file with %s and __LINE__ in c ?

+4 votes
137 views

I have written print("%s", __LINE__); I get crashed. Can someone please explain why it is happened ?

posted Sep 17, 2013 by Vikram Singh

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button
I think it should be a printf() function.

2 Answers

+1 vote

Your error is for the in printf() function.
___LINE___ isn't a C style string, its an integral type (the exact flavor escapes me at the moment). %s is the wrong format. This is why, in C++, it is recommended that you avoid printf() and friends, and use iostreams instead.

answer Sep 17, 2013 by Satyabrata Mahapatra
+1 vote

%s is used for print string.

example= printf("%s", "_LINE_");

or char *p = "_LINE_" ;
    printf("%s",p);
answer Oct 3, 2013 by Dheerendra Dwivedi
Dheerandra: it may not work as __LINE__ prints the line number, so the intention here is different.
Similar Questions
0 votes

Following is my code and getting segmentation fault. Please help

#include<stdio.h>
#include<string.h>
main()
{
    char *p,str[100]="Hi, How Are You, Fine, Thank You";
    p=strtok(str,",");
    printf("%s\n",p);
    while(p != NULL)
    {
        p=strtok(NULL,",");
        printf("%s\n",p);
    }
}

O/p is:
Hi
How Are You
Fine
Thank You
and then Segmentation fault.

0 votes

How can we debug the segmentation fault in a program which has been compiled without "-g" option?

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...