Thread: is aligning with printf() via a variable value possible?

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    44

    is aligning with printf() via a variable value possible?

    Code:
    void PrintMatrix(int **matrix, int rij, int kolom)
    {
    	int i, j;
    	
    	printf("Resultaat: \n");
    	
    	for (i = 0; i < rij; i++)
    	{
    		printf("[");
    		for (j = 0; j < kolom - 1; j++)
    			printf("%-10i", *(*(matrix + i)+j));
    		printf("%-10i", *(*(matrix + i)+j));             <-------
    		printf("]\n");
    	}
    	
    	printf("\n");
    }
    Is it possible to have a variable alignment instead of a constant (10 in this example)?
    so something like this:

    printf("%-(%i)i", numbers(*(*(matrix + i)+j)), *(*(matrix + i)+j)); <-------

    [ where numbers is a function that calculates the #amount of digits an integer has.
    numbers(321) = 3. ]

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I don't think so.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    45
    i suppose you could do something like this

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (void) {
    
       int number = 8;
       char output[50] = "start of string %";
       char* str2 = "%d";
       char* str3 = "d end of string";
    
       sprintf(&output[17], str2, number);
       sprintf(&output[18], str3);
       printf("'%s'\n\n", output);
       printf(output, 123);
    
       return 0;
    
    }
    so yes, it is possible
    Last edited by LordPc; 06-13-2010 at 07:27 AM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A couple of work arounds, maybe:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
      int i; 
      int num[4] = { 1000, 100, 10, 1 };
      char spc[4] = {""};
      printf("\n\n");
      for(i=0;i<4;i++) {
        printf("\n%d: %s%d", i, spc, num[i]);
        spc[i] = ' ';
      }
      printf("\n\n\n");
      spc[0]=spc[1]=spc[2]=spc[3]='\0';  //reset the space array
      for(i=0;i<4;i++) {
        if(num[i] > 999) 
          spc[0]='\0';
        else if(num[i] > 99)
          spc[0]=' ';
        else if(num[i] > 9)
          strcpy(spc, "  ");
        else 
          strcpy(spc, "   ");
        printf("\n%d: %s%d", i, spc, num[i]);
        spc[i]='\0';
      }
    
      printf("\n\n\t\t\t     press enter when ready");
    
      i = getchar(); ++i;
      return 0;
    }
    @LordPc - Welcome to the Forum!

    Hey, I think we could use a righteous contributor.
    Last edited by Adak; 06-13-2010 at 07:38 AM.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by LordPc View Post
    so yes, it is possible
    Yeah, good idea! The first arg to printf is actually just a C string, so you could pre-construct the whole thing.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Quote Originally Posted by manual
    The field width

    An optional decimal digit string (with non-zero first digit) specifying a minimum field width. If the converted value has fewer characters than the field width, it will be padded with spaces on the left (or right, if the left-adjustment flag has been given). Instead of a decimal digit string one may write '*' or '*m$' (for some decimal integer m) to specify that the field width is given in the next argument, or in the m-th argument, respectively, which must be of type int. A negative field width is taken as a '-' flag followed by a positive field width. In no case does a non-existent or small field width cause truncation of a field; if the result of a conversion is wider than the field width, the field is expanded to contain the conversion result.
    So for example

    printf("%*d\n", 5, myvalue );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    At least, we now know how many people are not printf expert!

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    unrelatd tip, but

    Code:
    *(*(matrix + i)+j)
    is equivelent to
    Code:
    matrix[i][j]
    A lot of people I see use the first one because they think you can't use subscript notation when the array is passed in as int **.


    Of course, if you prefer the other way, then ignore me ^_^

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    44
    Thanks for all the response, Salem's in particular.

    Quote Originally Posted by KBriggs View Post
    unrelatd tip, but

    Code:
    *(*(matrix + i)+j)
    is equivelent to
    Code:
    matrix[i][j]
    A lot of people I see use the first one because they think you can't use subscript notation when the array is passed in as int **.


    Of course, if you prefer the other way, then ignore me ^_^
    I was acctually doing a matrix project to refresh the pointer notation ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a C Programing Quiz project
    By dilemma in forum C Programming
    Replies: 12
    Last Post: 05-15-2009, 03:35 PM
  2. I have some questions :(
    By geekrockergal in forum C Programming
    Replies: 19
    Last Post: 02-01-2009, 09:44 AM
  3. saying hello and 1st question
    By darksys in forum C Programming
    Replies: 12
    Last Post: 10-31-2008, 02:58 PM
  4. Newb Help: Full Arrays and Functions
    By LycanGalen in forum C Programming
    Replies: 5
    Last Post: 01-31-2008, 08:35 PM
  5. Double to Int conversion warning
    By wiznant in forum C Programming
    Replies: 15
    Last Post: 09-19-2005, 09:25 PM