LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-12-2012, 11:48 PM   #1
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Rep: Reputation: Disabled
why we are using , this in variable


see , i found this code for scanf() in c .

int scanf(const char *format, ...) {
va_list args;
int rv;

va_start(args, format);
rv = scan(0, format, args);
va_end(args);

return rv;
}
static int scan(char **in, const char *fmt, va_list args)
{

// inside this block there is a line
while (EOF != (ch = scanchar(in)) && widht--)

}
static int scanchar(char **str) {
extern int getchar(void);
int ch;
if (str) {
ch = **str;
(*str)++;
return ch;


} else {
if ('\r' == (ch = getchar())) {
return EOF;
}
return ch;
}
}


so this is the whole process , but now can you tell me at start we are sending 0 in "in" , so what is the meaning of "in" in this program , and please clarify this with an example !

thanks , tushar
 
Old 07-13-2012, 01:58 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Always use [code] and [/code] tags when quoting source.
Passing NULL means default value, in this case reading from FILE stdin, instead of character-buffer.

Note: This program seems to implement scanf and sscanf together. In real-life cases scanf simply calls fscanf, and fscanf and sscanf are implemented together.

Last edited by NevemTeve; 07-13-2012 at 02:00 AM.
 
1 members found this post helpful.
Old 07-13-2012, 02:08 AM   #3
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
hello NevemTeve ,

means , please clarify more !
 
Old 07-13-2012, 02:19 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
What is unclear?
 
Old 07-13-2012, 02:54 AM   #5
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
please see this .....

take this simple example

scanf("%d",&a); when this line is executed .... so it need a input !

think about these two :-

1st line ....now if i do not enter any input ..... means we are entering o or null !

2nd line .... now if i enter 2 than it will read by characte buffer ..... according to you !
 
Old 07-13-2012, 03:00 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
scanf calls scan (in=NULL), scan calls scanchar (in=NULL), and scanchar calls getchar; finally each call of getchar fetches a character from stdin.

If you are asking about the working of scanf: yes, a single scanf may result in reading zero or more lines from user, depending on the contents of buffers and the content of lines, which can be confusing; that's why many people prefer fgets+sscanf over scanf.

Last edited by NevemTeve; 07-13-2012 at 03:01 AM.
 
Old 07-13-2012, 03:23 AM   #7
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
if .... in == NULL ... means you will read nothing & if == something than it will be read by getchar() , ok

but when will we get this "in == NULL ... means you will read nothing" condition ... according to me this condition will come when we press "ctrl+z" .... now what about your's ?
 
Old 07-13-2012, 03:48 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> if .... in == NULL ... means you will read nothing

I don't understand what are you trying to say here...

in==NULL means: read from stdin via getchar
in!=NULL means: read from memory (address=*in), then increment the pointer

> according to me this condition will come when we press "ctrl+z"

Ctrl+Z (in WinDos) means EOF, which causes getchar return -1. It has nothing to do with parameter 'in'.

Last edited by NevemTeve; 07-13-2012 at 03:50 AM.
 
Old 07-13-2012, 05:19 AM   #9
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
thanx NevemTeve , you have beautiful mind , wow genius man !
.
i think you can play with "C"
.
hello another query , are you in social network , i want to stay in touch with you !

Last edited by tushar_pandey; 07-13-2012 at 05:21 AM.
 
Old 07-13-2012, 05:26 AM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Off: No, I'm way too old for that
 
Old 07-13-2012, 05:29 AM   #11
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
ha ha , why Mr. genius ! seriously i will take your whole knowledge !
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with script writing: Storing cmd in variable, print variable, then exe cmds Arodef Programming 3 01-17-2012 12:26 AM
problem while comparing awk field variable with input variable entered using keyboard vinay007 Programming 12 08-23-2011 12:44 AM
Script to copy specific directory based on variable to folder with that variable name fluxburn Programming 7 01-07-2010 07:59 PM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration