Status
Not open for further replies.

prateek

Banned
Banned
266
2009
72
0
Please see the source code
I am amazed
the scanf function has %[^\n] it should be %s
why this is working fine ??? what does this mean ??? ---> %[^\n] in scanf function ??

Source Code
PHP:
#include<stdio.h>
#include<conio.h>
main()
{
	char str[20];
	clrscr();
	scanf("%[^\n]",str);
	printf("\n\n\n%s",str);
	getch();
}


Output
[SLIDE]http://i2.lulzimg.com/10a5df8703.png[/SLIDE]
 
14 comments
If you use a trailing space (a space at the end of the format string), your program appears to hang. Instead of entering a blank line or using space characters, you must enter what the value of str2 will be. The same for str3 and when it asks for str3's value, you would enter a value that won't appear. For example, use "%[^\n] " instead of "%[^\n]"/" %[^\n]". Your newline will be ignored as specified...and eaten by the trailing space in the format string. Since scanf didn't get a '\n' character, it will want more data. str2 will have that value and str3 will have str2's value.
 
PHP:
#include<stdio.h>
#include<conio.h>
void call(int, int, int);
void main()
{
	clrscr();
	int a=10;
	call(a,a++,++a);
	getch();
}
void call(int x,int y, int z)
{
	printf("%d %d %d ",x,y,z);
}


why the output is
12 11 11

???
i know post and pre increment perfectly
but what is happening here ???
 
here is what I think

first x will get the value 10,
then y will get value 10
then a will bcum 11
then again a will increase and bcum 12
then z will get 12

but it's not happening like that
the output is 12 11 11
why ??

:facepalm:


or

it should give 10 11 12
as the output .. :facepalm: whats happning
 
Last edited:
[^\n] If I remeber right than this method is used to recieve a string with space through scanf().Normally you cannot do so directly in c if you do so then the string before space get accepted.

And for that operator one first

while calling first a++ get execute and passed i.e. z=11
then a++ i.e. y=11
then a i.e x=12

Hence output is 12 11 11

You must have a look on c function calling convention and post/pre increment function as Halcyon said.
 
Last edited:
hey
thanks brothers :)

i will surely do revision on post / pre increment & decrements

thanks a lot :) :D


rep added :)
 
@BJ@008

that means
the value is passed from right to left ? :O :-\
can you plz explain a bit more


while calling first a++ get execute and passed i.e. z=11
then a++ i.e. y=11
then a i.e x=12
 
Try this:
Code:
int main(){

    int a=1;
    printf("%d",++a - a++ + ++a + a++ + ++a);

    return 0;
}

Result: 10 (according to codepad: http://codepad.org/xvydthj5)

A few tips:
1. Start using int main() rather than void main() (before it becomes your habit)
2. Return 0 if you're using int main() to point the compiler that the code returns a success flag
3. Conio.h is a terrible header filem ignore including it.
 
In my training I did ask weirdo doubts to my tutor regarding post/pre increment operators. I was told that it varies depending on the system and there's no universal rule for evaluating whether it's right-to-left or left-to-right ;)

For simple questions, you can simply go ahead with left-to-right(coz of the C compiler) evaluation.

In the above example check the exact values at every instance and you'll get to the result easily!

++a - a++ + ++a + a++ + ++a
2 - 2 + 3 + 3 + 4 = 10
 
@BJ@008

that means
the value is passed from right to left ? :O :-\
can you plz explain a bit more

Yes according to standard c calling convention value is passed in right to left manner.

like in this example:

Code:
#include<stdio.h>
int main()
{
int a=10;

printf("%d%d%d",a,++a,a++);

return(0);
}

Output: 12 12 10

In this example first a++ is passed i.e. 10(at this point a=11 after passing a++)then ++a is passed hence i.e 12and at last a is passed i.e. 12.

Once printf( ) collects them it prints them in the order in which we have asked it to get them printed (and not the order in which they were passed). Thus 12 12 10 gets printed.Hope it cleared your doubt :)
 
Status
Not open for further replies.
Back
Top