LinuxQuestions.org
Visit Jeremy's Blog.
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 05-13-2005, 09:38 AM   #1
skie_knite007
Member
 
Registered: Dec 2004
Location: India
Distribution: Fedora Core 4
Posts: 145

Rep: Reputation: 15
Can we specify variable field width in a scanf() format string?


is there any way to specify variable field width in scanf().....................???????????

I ve got one more doubt........Are these auto variables stored in registers.?

Is there any diff between auto and register?
 
Old 05-13-2005, 10:23 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
I may not understand what you want -

%s specifies a string (with no spaces) of any size you have
%d specifies an integer with varying numbers of numeric characters.

A register variable means the data is stored in a register,
so the object can be up to 32 bits wide on a 32 bit machine and still fit in the register.

Otherwise a register must contain a 32 bit pointer to a big object.

auto variables are the default variable:
Code:
int foo(void)
{
     char s='Y;
     int i=0;

}
s and i are both auto variables. auto variables are stored on the stack, not in registers
 
Old 05-13-2005, 10:41 AM   #3
skie_knite007
Member
 
Registered: Dec 2004
Location: India
Distribution: Fedora Core 4
Posts: 145

Original Poster
Rep: Reputation: 15
Sir,
I really want to kno, if theres any way to specify something like %kc where 'k' is a variable in the scanf(). (like %3d and 3 is got from a variable)
 
Old 05-13-2005, 12:56 PM   #4
Harmaa Kettu
Member
 
Registered: Apr 2005
Location: Finland
Posts: 196

Rep: Reputation: 30
Quote:
is there any way to specify variable field width in scanf().....................???????????
The format string can be a variable just like any other string.
A quick example (using printf instead, but scanf works the same way):
Code:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  char format[6];
  int length = 10;
  int number = 123456;

  if(argc > 1)
  {
    length = atoi(argv[1]);
    if(length < 1) { length = 1; }
    if(length > 99) { length = 99; }
  }
  
  // Construct the format
  // "%%" becomes "%", "%d" is replaced with length, last "d" remains unchanged
  sprintf(format, "%%%dd\n", length);
  
  printf(format, number);
  return(0); 
}
Quote:
I ve got one more doubt........Are these auto variables stored in registers.?
They can be in registers or in the stack, depending where the compiler decides to put them. You can test this yourself. Write some simple functions, compile them with "gcc -S example.c example.s" and examine the output assembler file to see where gcc puts your variables.
Quote:
Is there any diff between auto and register?
In gcc they are the same thing except that a register variable cannot have an address, so code like this:
Code:
register int foo;
do_something(&foo);
causes a warning. Other compilers, especially old ones, may actually force register variables into registers.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Print a part of variable/string p0tw0r Linux - Newbie 1 04-07-2005 02:49 PM
Huge spaces in all variable width fonts Dare Linux - Software 0 10-21-2004 12:45 PM
Has the format (width) changed here? Whitehat LQ Suggestions & Feedback 0 03-06-2004 04:53 PM
variable to string x2000koh Programming 4 07-30-2003 02:23 AM
Getting a variable name based on a string. jtshaw Programming 7 10-08-2002 02:06 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration