warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’

S

Seebs

A simple code won't work:
#include <stdio.h>

int main(void)
{
char a;

printf("Enter your name: ");
scanf("%s",&a);
printf("Your name is: %s\n",a);

"a" is a char, but printf wants a pointer-to-char.

Also, you're asking for a single character, and then scanning an
arbitrary-length string into it.

Try:

char a[256];

this will still have bugs, but they'll be more subtle. (Hint: How do you
know the user won't type 257 characters?)

-s
 
A

alien

A simple code won't work:
#include <stdio.h>

int main(void)
{
char a;

printf("Enter your name: ");
scanf("%s",&a);
printf("Your name is: %s\n",a);

return 0;
}

it says:
warning: format ‘%s’ expects type ‘char *’, but argument 2 has type
‘int’
 
N

Nick Keighley

A simple code won't work:
#include <stdio.h>

int main(void)
{
char a;

printf("Enter your name: ");

on some implementations this prompt will not appear until after
thae scanf(). You need a newline is string or you need to call
fflush(stdout)
 
K

kr0wie

In
A simple code won't work:
#include <stdio.h>
int main(void)
{
char a;

This reserves storage for one byte of memory - i.e. sufficient to
store a single character. If you want to store a string there, bear
in mind that you will need one byte for a null terminator. That
doesn't leave any room at all for actual data.
printf("Enter your name: ");

How many people do you know whose name is 0 characters long?
scanf("%s",&a);

scanf doesn't know that you've only got one byte available. It will
accept the character pointer *as if* it pointed to an area of memory
sufficiently large to accept any amount of data the user cares to
provide.
printf("Your name is: %s\n",a);
return 0;
}
it says:
warning: format ?%s? expects type ?char *?, but argument 2 has type
?int?

a is a char. C has rules for promoting data types, and in this
instance the char is being promoted to int, which is why the compiler
says what it says.

What you actually need is an *array* in which to store the name:

#include <stdio.h>
#include <stdlib.h>

#define MAX_NAME_LEN 256

int main(void)
{
  int rc = EXIT_SUCCESS;

  char name[MAX_NAME_LEN + 1] = {0};

  printf("Enter your name (max %d characters).\n", MAX_NAME_LEN);
  if(fgets(name, sizeof name, stdin) != NULL)
  {
    printf("Your name is %s\n", name);
  }
  else
  {
    fprintf(stderr, "Error reading name.\n");
    rc = EXIT_FAILURE;
  }

  return rc;

}

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within

Thanks! This really helped me alot.
 
M

Michael Tsang

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
In
A simple code won't work:
#include <stdio.h>
int main(void)
{
char a;

This reserves storage for one byte of memory - i.e. sufficient to
store a single character. If you want to store a string there, bear
in mind that you will need one byte for a null terminator. That
doesn't leave any room at all for actual data.
printf("Enter your name: ");

How many people do you know whose name is 0 characters long?
scanf("%s",&a);

scanf doesn't know that you've only got one byte available. It will
accept the character pointer *as if* it pointed to an area of memory
sufficiently large to accept any amount of data the user cares to
provide.
printf("Your name is: %s\n",a);
return 0;
}
it says:
warning: format ?%s? expects type ?char *?, but argument 2 has type
?int?

a is a char. C has rules for promoting data types, and in this
instance the char is being promoted to int, which is why the compiler
says what it says.

What you actually need is an *array* in which to store the name:

#include <stdio.h>
#include <stdlib.h>

#define MAX_NAME_LEN 256

int main(void)
{
int rc = EXIT_SUCCESS;

char name[MAX_NAME_LEN + 1] = {0};

printf("Enter your name (max %d characters).\n", MAX_NAME_LEN);
if(fgets(name, sizeof name, stdin) != NULL)
{
printf("Your name is %s\n", name);
}
else
{
fprintf(stderr, "Error reading name.\n");
rc = EXIT_FAILURE;
}

return rc;

}

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within

Thanks! This really helped me alot.

#define is bad. Avoid it. It is better to use a const size_t declaration for
MAX_NAME_LEN . Better code:

#include <stdio.h>
#include <stdlib.h>

const size_t MAX_NAME_LEN=256;

int main(void)
{
int rc = EXIT_SUCCESS;

char name[MAX_NAME_LEN + 1];

printf("Enter your name (max %d characters).\n", MAX_NAME_LEN);
if(fgets(name, sizeof name, stdin) != NULL)
{
printf("Your name is %s\n", name);
}
else
{
fprintf(stderr, "Error reading name.\n");
rc = EXIT_FAILURE;
}

return rc;
}
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkquKosACgkQG6NzcAXitM9t5gCdHdYNWrGUel4aozfuh45LNqcT
bMAAniJM8BQEBN39KoegkVBCj5SjC7ls
=bZIS
-----END PGP SIGNATURE-----
 
C

Chris M. Thomasson

In
<599305f3-c68c-481b-81cd-a4ab009c6...@j19g2000yqk.googlegroups.com>,
[...]

Thanks! This really helped me alot.

FWIW, you should probably keep in mind that `fgets()' will store a newline
character in the string if it reads one and has room to store it. With the
program that Richard posted as-is, if I enter the name `Chris', the string
`name' will look like:

"Chris\n"
 
B

Barry Schwarz

A simple code won't work:
#include <stdio.h>

int main(void)
{
char a;

printf("Enter your name: ");
scanf("%s",&a);

Do you know many people with zero length names?
printf("Your name is: %s\n",a);

What is the type of a? What type is it promoted to for this function
call? What part of the error message don't you understand.
 
P

Phil Carmody

Richard Heathfield said:
Michael Tsang wrote: said:
#define is bad.

No, it isn't. It can be misused, but it is not "bad".

Avoid it. It is better to use a const size_t
#declaration for
MAX_NAME_LEN . Better code:

#include <stdio.h>
#include <stdlib.h>

const size_t MAX_NAME_LEN=256;

int main(void)
{
int rc = EXIT_SUCCESS;

char name[MAX_NAME_LEN + 1];

Fine in C99, but how many people here have a conforming C99 compiler?

One doesn't need a fully conforming C99 compiler to support that.
In fact, you only need one single C99 feature to be implemented in
order to support that single requirement.
I doubt very much whether the OP does. And if he doesn't, he violates
a C90 constraint.

Yes, but we're not all stuck back in the 90s like you, Richard.

Phil
 
P

Phil Carmody

Richard Heathfield said:
Richard Heathfield said:
<snip>

#define is bad.

No, it isn't. It can be misused, but it is not "bad".


Avoid it. It is better to use a const size_t
#declaration for
MAX_NAME_LEN . Better code:

#include <stdio.h>
#include <stdlib.h>

const size_t MAX_NAME_LEN=256;

int main(void)
{
int rc = EXIT_SUCCESS;

char name[MAX_NAME_LEN + 1];

Fine in C99, but how many people here have a conforming C99
compiler?

One doesn't need a fully conforming C99 compiler to support that.

It depends on who "one" is, and what "one's" requirements are, and
whether "one's" compiler supports that one single feature.

Wrong, wrong, and wrong. Do you speak a different version of English
to the rest of us? Read for comprehension before responding next time.
<shrug> I'd prefer to keep personalities out of it if possible.

No, you prefer to limit to just a single personality.

Phil
 
P

Phil Carmody

Richard Heathfield said:
No, but it seems I speak a different version to you.


I did last time. If you don't understand my reply, I'm sorry about
that, but I'm not about to spend the next 20 minutes babysitting you
through an explanation of the blindingly obvious.

<snip>

Ah, nice to see you remove my original statement, lest you get seen
too often taking a contrary stance?

Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top