[Courses] C Programming For Absolute Beginners, Lesson 2: Fun With Printf, Scanf, Puts, and Variables

Christopher Howard christopher.howard at frigidcode.com
Tue Feb 21 18:21:25 UTC 2012


On 02/21/2012 07:45 AM, Leslie wrote:

> 
> #include <stdio.h> 
> 
> int main() 
> { 
> 	char name; 
> 
> 	puts( "Please enter your name: " ); 
> 	scanf( "%s", &name ); 
> 	printf( "Hi, %s.\n", name); 
> 
> 	return 0; 
> }
> 
> But when I went to compile, as follows, I got the errors below --
> although I thought I gave it the right type, namely char.
> 
> leslie:~/cprog$ gcc -o name name.c 
> name.c: In function ‘main’: 
> name.c:14: warning: format ‘%s’ expects type ‘char *’, but argument 2
> has type ‘int’ 
> 
> 

Carla did not use %s in her example because using strings requires the
allocation of blocks of memory, which she has not had the chance to
explain yet.

Your "char name" variable actually only has storage space for one single
character. However, the %s format expects you to pass in a pointer to a
memory block where a full string could be stored.

Since Carla will doubtlessly have a whole lesson on this, I will expound
no more on it.

-- 
frigidcode.com
indicium.us



More information about the Courses mailing list