#include<stdio.h>
#define scanf "%s is char"
main()
{
printf(scanf,scanf);
}

the output is
%s is char is char
y it is so..

Recommended Answers

All 11 Replies

Think about it. What's your format specifier? What is being replaced in the format specifier? What is it being replaced with?

i understand. but check wat i am saying is right..
printf("%s is char","%s is char");
2nd one act as a string tat replaces the format specifier of the first..?

now only i tried the following statements they also give same output

printf(scanf,scanf,scanf);  1
printf(scanf,scanf,scanf,scanf);    2

the o/p of 1 & 2 r
%s is char is char
they r same
a.y they r same
b.wat is the reason for such an o/p

>> i tried the following statements they also give same output

printf() discards the excess arguments, hence the output is identical in all the three cases.

By the way, please write in full English to make it easier to understand what you are saying.


[EDIT]

I think your code snippet would be better written as e.g.

#include <stdio.h>

/* Instead of 'scanf' use e.g. my_string */
#define my_string "%s is char"

/* main() returns an 'int', explicitly */
int main()
{
  printf(my_string, my_string);
  return 0;
}

ok.but i got another doubt,
then why there is no error message like

"too many parameters are not allowed"
i understand now.but check what i understood is right...

printf("%s is char","%s is char","%s is char");
/*            1              2          3         */

do you mean that 1 act as parameter having format specifier and 2 and 3 are acting as string values that will replace the %s in 1st parameter?
any way thanks..for your help

>then why there is no error message like
>"too many parameters are not allowed"

While a compiler could warn you at build time about a different number of actual arguments than the format string suggests, it's more complicated than you might think.

>do you mean that 1 act as parameter having format specifier and 2 and 3
>are acting as string values that will replace the %s in 1st parameter?

Correct. Only the first parameter to scanf acts as a format string.

please understand the macro

according to the problem scanf is replaced by the statement "%s is char" before compilation. so finaly statement looks like printf("%s is char","%s is char"); so the Format specifier %s in the first statement takes the second string as a constant string so print that string as it is...so finaly program will print %s is char is char.

hi narue i am unable to understand
"While a compiler could warn you at build time about a different number of actual arguments than the format string suggests, it's more complicated than you might think."
plz help...

>hi narue i am unable to understand
What's to understand? It's possible to get an error message, but unlikely. Don't expect your compiler to warn you about doing obviously stupid things, you need to use a modicum of gray matter to program in C.

UINT8 *command = "21,451,30,50,.......last parameter" (like this i will have variable no of numerical parameters. but i will knowing the no of parameters each command has.)

using sscanf i need to store this in a array array [20] like
array[1] =21;
array[2] =451;
array[3] = 30;
array[4]=50;

..
..
array[n] = last parameter;

Can anybody provide me a solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.