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 8398437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:03:13+00:00 2026-06-09T21:03:13+00:00

Good afternoon! I have some doubts about following code. [1] What does the middle

  • 0

Good afternoon! I have some doubts about following code.

[1] What does the middle condition in the for loop mean? ; *p; and ; *sval; – reaching an end of input string? If yes, how is it determined?

[2] I do not understand that complicated for loop.

Let’s suppose that if condition is not satisfied *p == % so we go into switch right away. Now let’s consider opposite case, we enter every other char despite %, if is satisfied so we use continue and also go to switch after that. What’s the difference between those 2 cases then ?
I must be terribly wrong with sth, but I can’t find my mistake for over 2 hours now…

#include <stdarg.h>
/* minprintf: minimal printf with variable argument list */
void minprintf(char *fmt, ...)
{
    va_list ap; /* points to each unnamed arg in turn */
    char *p, *sval;
    int ival;
    double dval;

    va_start(ap, fmt); /* make ap point to 1st unnamed arg */

    for (p = fmt; *p; p++) {         /* [1] */
        if (*p != '%') {
            putchar(*p);
            continue;                /* [2] */
        }
        switch (*++p) {
        case 'd':
            ival = va_arg(ap, int);
            printf("%d", ival);
            break;
        case 'f':
            dval = va_arg(ap, double);
            printf("%f", dval);
            break;
        case 's':
            for (sval = va_arg(ap, char *); *sval; sval++)
                putchar(*sval);
            break;
        default:
            putchar(*p);
            break;
        }
    }
    va_end(ap); /* clean up when done */
}

Help is greatly appreciated!

  • 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-09T21:03:14+00:00Added an answer on June 9, 2026 at 9:03 pm

    When we continue, we just print the character, and skip the switch altogether. The loop goes through the format line looking for (1) a terminating zero, or (2) a percent sign.

    When the terminating zero is reached, the loop ends:

    for (p = fmt; *p; p++)
    //            ^^^ Here is the condition: *p != 0 is implied
    

    When a percent sign is reached, the following symbol is taken as a format character, and the next vararg is interpreted according to it.


    There is a bug in this program – it produces a read past the end of the format string (an undefined behavior) when the percent symbol is the last character of the format string. Try this:

    minprintf("%\0bug!bug!bug!");
    

    The simulated end-of-line marker \0 is skipped, and the bug!bug!bug! output is produced. The reason for this is an unconditional pre_increment of p in the header of the switch statement. To fix this problem, add the following case to the switch:

     case '\0':
        p--; // reverse the ++p from the header of the switch
        break;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good afternoon, I inherited some C# code from years ago. I have refactored it
Good afternoon, I'm learning about using RegEx's in Ruby, and have hit a point
Good afternoon all, I'm using a java.lang.StringBuilder to store some characters. I have no
Good Afternoon, I have some very basic questions on GUI Event Handling. Firstly with
Good afternoon fellows, I'm having trouble with some jQuery-code and I can't seem to
Good afternoon. I have an array with some keys, and values in them. I
Good Afternoon, everyone. I have an ongoing issue with the code below. I know
Good afternoon, I have an activity that calls another. The code is as follows:
Good Afternoon all , I have downloaded data from a web service and im
Good afternoon, I have a quick 'top level' question regarding the usage of Facebook

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.