Thread: New to C: I need to put my printf object into a variable and then print it out.

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    7

    New to C: I need to put my printf object into a variable and then print it out.

    I have a problem, I need to store my printf screen into a variable and output it back to the user. I am super new to c. So any help would be greatly appreciated.

    My code is below:

    Code:
    #include <stdio.h>      
    #include <time.h>       /* time_t, struct tm, time, gmtime */
    
    int main (){
        //Create time structure
        time_t temp = time(NULL); 
        struct tm *tick_time = localtime(&temp);
    
        //Store current day# in year 
        static char s_day_text[] = "DAY 000/"; 
        strftime(s_day_text, sizeof(s_day_text), "DAY %j/", tick_time);
      
        //Store current year in variable
        static char s_year[] = "0000";
        strftime(s_year, sizeof(s_year), "%y", tick_time);
        int year = atoi(s_year);
        
        int total_days=000;    
    
        //Determine if current year is a leap year
        if(year%4 == 0 && year%100 != 0 || year%400 == 0){
             total_days = 366; //Year is a leap year
            
        }else{
             total_days = 365; //Year is not a leap year
        }
      
        
        //Need this print out to be stored in variable 
        printf("%s%d\n" ,s_day_text ,total_days);
      
        //My poor attempts to store a complete printf into a variable
        /*strcat(s_day_text,total_days);*/
    
        char *buffer[21];
        snprintf(buffer, "%s%d\n" ,s_day_text ,total_days);
        printf(buffer);
    
        
       return 0;
    }

  2. #2
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Lines 35 to 37 are in the right direction. Try removing the asterisk when you define the buffer, and also pass 21 as the 2nd argument to snprintf().
    "Talk is cheap, show me the code" - Linus Torvalds

  3. #3
    Registered User
    Join Date
    Sep 2015
    Posts
    7
    Wow, thanks it worked . Didn't realize I had to put the size of the character in snprintf also.
    Last edited by Leston Yearwood; 09-13-2015 at 04:40 PM.

  4. #4
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Quote Originally Posted by Leston Yearwood View Post
    Wow, thanks it worked . Didn't realize I had to put the size of the character in snprintf also.
    You are welcome.

    You should always check the documentation of the functions you are using. If you don't have direct access to the docs, then a simple google lookup is usually all it takes. For example, googling for "snprintf()" gives as the 1st result this link.

    You should also compile your code with the increased level of warnings of your compiler enabled. For example, with gcc this is done by adding the options -Wall -Wextra to its command-line. If you had done so, the compiler would have been hinted you about both errors in your code.

    Btw, char *buffer[21] defines an array of char pointers, while char buffer[21] defines an array of chars. snprintf() expects the latter for its 1st argument.
    Last edited by migf1; 09-13-2015 at 09:22 PM.
    "Talk is cheap, show me the code" - Linus Torvalds

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printf doesn't print on screen
    By Roffemuffe in forum C Programming
    Replies: 4
    Last Post: 05-30-2013, 04:21 AM
  2. print % in printf
    By Saurabh Mehta in forum C Programming
    Replies: 3
    Last Post: 11-21-2012, 03:44 PM
  3. Using printf to print out an array
    By slowcoder in forum C Programming
    Replies: 5
    Last Post: 09-11-2006, 02:36 PM
  4. printf wont print without newline
    By rupurt in forum C Programming
    Replies: 4
    Last Post: 10-24-2005, 09:05 AM
  5. how to print .03 as 3% in printf?
    By shiyu in forum C Programming
    Replies: 2
    Last Post: 01-31-2003, 12:02 PM