Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ANSI C program problem with %s crashing program

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
0
36
US
Code:
#include <stdio.h>
#include <stdlib.h>

int main(){
	
	printf("%s",strspn("Cows like to moo.","Ceiklosw "));
	return 0;
}
When I create a simple program to test said line the program crashes.
The problem appears to be %s. if I change it to be %c it runs, but returns nothing to the screen.
If however I run an example from the book which has a lot of %s in the program, it runs just fine. the samples as c files which I copy and paste. I need to know what's going wrong.
I am using C-Free editor and minGW compiler and the program compiles just fine but when running it gives error in a separate popup window
test.exe has encountered a problem and needs to close.
We are sorry for the inconvenience.
Since I also have VS 2005 loaded there is a debug button and a close button.

DougP
[r2d2] < I Built one
 
The strspn function returns integer, not a pointer value. The %s format specification wants a pointer to char. Now printf treats an integer as a pointer and your program crashed.

To print an (unsigned in this case) integer use %u format specification.

And reread strspn specification...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top