5 Replies - 1024 Views - Last Post: 08 October 2012 - 03:00 AM Rate Topic: -----

#1 rpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 13-August 07

Simple c program error segmentation core dump issue

Posted 08 October 2012 - 01:39 AM

Hi
This simple c program compiles (but with errors I do not understand)
and creates an executable program file yet it refuses to run
(giving segmentation error core dumped):

#include <stdio.h>

credits()
{
	printf('Welcome to rpdc_engine');
	return 0;
}

int main(int argc, char **argv)
{
	credits();
	return 0;
}


Compiling in Geany gives
gcc -Wall -c "rpdc_engine.c" (in directory: /home/richard/Documents/RPD_Computing_Programming/RPD_Chess_programming/RPD_C_Chess_programming/rpdc_engine)
rpdc_engine.c: In function ‘credits’:
rpdc_engine.c:29:9: warning: character constant too long for its type [enabled by default]
rpdc_engine.c:29:2: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [enabled by default]
In file included from rpdc_engine.c:25:0:
/usr/include/stdio.h:365:12: note: expected ‘const char * __restrict__’ but argument is of type ‘int’
Compilation finished successfully.


[richard@lizzief17 rpdc_engine]$ ./rpdc_engine
Segmentation fault (core dumped)

What is the problem with this simple code? I am most grateful for helpful replies and I hope to learn from them!

Is This A Good Question/Topic? 0
  • +

Replies To: Simple c program error segmentation core dump issue

#2 aresh  Icon User is online

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,165
  • Joined: 08-January 12

Re: Simple c program error segmentation core dump issue

Posted 08 October 2012 - 01:42 AM

Well, in printf(), you use '' instead of "". Since printf requires string as argument, and you are not passing one, in throws an error.

Also, you must specify the return type of your function credits.
Was This Post Helpful? 1
  • +
  • -

#3 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Simple c program error segmentation core dump issue

Posted 08 October 2012 - 01:58 AM

View Postaresh, on 08 October 2012 - 02:12 PM, said:

Well, in printf(), you use '' instead of "". Since printf requires string as argument, and you are not passing one, in throws an error.

Also, you must specify the return type of your function credits.


Absolutely,do as Aresh said. I would like to add a small bit. You could also use int main() here as you are not passing any command line arguments here :)

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#4 rpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 13-August 07

Re: Simple c program error segmentation core dump issue

Posted 08 October 2012 - 02:01 AM

Hi aresh
Many thanks for your quick & helpful reply.
I never knew printf needed " ". I do now!(perhaps I did know but forgot?!)
printf("Welcome to rpdc_engine");

Just changing this fixes the whole thing-it compiles & runs with no errors.

So
int credits()
works as well.
Thank you very much for your help & best wishes to you :-)
Was This Post Helpful? 0
  • +
  • -

#5 rpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 13-August 07

Re: Simple c program error segmentation core dump issue

Posted 08 October 2012 - 02:10 AM

Hi aresh
Many thanks for your quick & helpful reply.
Editing printf
printf("Welcome to rpdc_engine");
& credits function return type int fixes the errors and it compiles & runs with no errors.
All good now.
I am amazed this error was caused by the small difference in printf " " vs ' '.
Hopefully now I won't forget the correct syntax for printf!
Once again many thanks & best wishes for your help :-)
Was This Post Helpful? 0
  • +
  • -

#6 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1678
  • View blog
  • Posts: 3,180
  • Joined: 30-May 10

Re: Simple c program error segmentation core dump issue

Posted 08 October 2012 - 03:00 AM

Consider it also a lesson not to run code when there are warnings present.

"makes pointer from integer" is 99% certain to give you a segfault as soon as you try it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1