top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

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

+4 votes
765 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 LinkedIn 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?

...