Help with printf %s

This is a discussion on Help with printf %s within the C Programming forums, part of the General Programming Boards category; Hi can anyone help with why my program will not print out a full "name". I tried the %s format ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    2

    Help with printf %s

    Hi can anyone help with why my program will not print out a full "name". I tried the
    %s format identifier but it will not compile. Thanks

    Code:
    #include	<stdio.h>
    #include	<string.h>
    
    int main (void)
    {
    
    	struct book
    	{
    		char name  ;
    		float price;
    		int pages;
    	};
    
    	struct book b [ 3 ];
    		for ( int i = 0; i < 3; i++ )
    	{
    		printf("Enter name,price,pages\n");
    		scanf("%s      %f       %d" , &b [ i ] .name,     &b [ i ] .price,     &b [ i ] .pages );
    	}
    
    	for (int i = 0; i < 3; i++ )
    
    	{
    		printf("%c     %f       %d\n" ,b [ i ] .name , b [ i ] .price, b [ i ] .pages );
    	}
    
    	return (0);
    }

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    Quote Originally Posted by tonyjohn View Post
    Hi can anyone help with why my program will not print out a full "name". I tried the
    %s format identifier but it will not compile. Thanks

    Code:
    #include	<stdio.h>
    #include	<string.h>
    
    int main (void)
    {
    
    	struct book
    	{
    		char name  ;
    		float price;
    		int pages;
    	};
    
    	struct book b [ 3 ];
    		for ( int i = 0; i < 3; i++ )
    	{
    		printf("Enter name,price,pages\n");
    		scanf("%s      %f       %d" , &b [ i ] .name,     &b [ i ] .price,     &b [ i ] .pages );
    	}
    
    	for (int i = 0; i < 3; i++ )
    
    	{
    		printf("%c     %f       %d\n" ,b [ i ] .name , b [ i ] .price, b [ i ] .pages );
    	}
    
    	return (0);
    }
    The type of the variable 'name' is 'char'. The %s modifier expects a 'char*'. You need to either make 'name' a character array
    Code:
    char name[SOME_LIMIT];
    or make it a character pointer
    Code:
    char* name
    and allocate the required memory before you use it.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    	struct book
    	{
    		char name  ;
    Your "name" here is only 1 character. Try compiling with warnings on, because your compiler should be screaming at you right now. Also, %s on your scanf is going to stop on a space, so unless "name" is only going to be a single word, then you are going to have problems there too.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    2
    Thanks for the help I appreciate it !!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printf.c and C
    By jordanguyoflove in forum C Programming
    Replies: 13
    Last Post: 10-15-2008, 12:33 PM
  2. printf hex
    By bazzano in forum C Programming
    Replies: 6
    Last Post: 01-04-2006, 01:54 PM
  3. about printf
    By cheesestick in forum C Programming
    Replies: 9
    Last Post: 03-25-2005, 12:32 PM
  4. make printf using printf?
    By germaneater in forum C Programming
    Replies: 9
    Last Post: 11-10-2004, 09:58 PM
  5. about printf
    By unregistered in forum C Programming
    Replies: 2
    Last Post: 04-11-2002, 12:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21