Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8869559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:30:22+00:00 2026-06-14T17:30:22+00:00

char symbols[16] = ; int index = 0; while (1) { if (index %

  • 0
char symbols[16] = "";
int index = 0;
while (1)
{
     if (index % 2)
         snprintf(symbols, sizeof symbols, "a%s", symbols);
     else
         snprintf(symbols, sizeof symbols, "b%s", symbols);

     index++;

     printf("%s\n", symbols);
}

How output looks: a => bb => aaa => bbbb

I want the output looks: a => ba => aba => baba

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:30:23+00:00Added an answer on June 14, 2026 at 5:30 pm

    This is undefined behaviour. From the C99 standard section 7.19.6.5 The snprintf function:

    The snprintf function is equivalent to fprintf, except that the output is written into
    an array (specified by argument s) rather than to a stream. If n is zero, nothing is written,
    and s may be a null pointer. Otherwise, output characters beyond the n-1st are
    discarded rather than being written to the array, and a null character is written at the end
    of the characters actually written into the array. If copying takes place between objects
    that overlap, the behavior is undefined.

    You will need to make a copy of symbols for use as an argument in the snprintf() calls:

    char symbols[16] = "";
    char symbols_copy[16];
    int index = 0;
    while (index++ < 15)
    {
         memcpy(symbols_copy, symbols, sizeof(symbols));
    
         if (index % 2)
             snprintf(symbols, sizeof symbols, "a%s", symbols_copy);
         else
             snprintf(symbols, sizeof symbols, "b%s", symbols_copy);
    
         printf("%s\n", symbols);
    }
    

    See demo http://ideone.com/GvnW7D .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using this function: int times_on_table(char *search,struct table index[],int wct){ int ct=0,num=0; while(ct<wct){
char label[8] = abcdefgh; char arr[7] = abcdefg; printf(%s\n,label); printf(%s,arr); ====output========== abcdefgh abcdefgÅ Why
char c='c'; int i=10; double d =50; long l=30; String s=Goodbye; Are these statement
{ char *a, *b; printf(%lx\n,(b-a)); } Usually works, in fact, I can't imagine it
Say we have a code: int main() { char a[10]; for(int i = 0;
I got some issues as below while try to build Pocket-OCR Undefined symbols for
The following code is mentioned in this question . int (*my_printk)(const char *fmt, ...);
struct bucket { int nStrings; //No. of Strings in a Bucket. char strings[MAXSTRINGS][MAXWORDLENGTH]; //
I know that first 128 symbols of char type are ASCII symbols , I
System: 64bit Ubuntu Lucid GNUStep clang/LLVM test.m #import <Foundation/Foundation.h> int main(int argc, char *

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.