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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:46:24+00:00 2026-06-09T13:46:24+00:00

I’ve been seeing these two parallel phrases since I started C weeks ago, need

  • 0

I’ve been seeing these two parallel phrases since I started C weeks ago, need someone with the knowledge of the C compiler to tell me which leads to better code.

Version1:

char s[]="aString",*sp=&s,c;
while(c=*sp++){
operation(c);
}

Version2:

char s[]="aString",*sp=&s;
for(;*sp;sp++){
 operation(*sp);
}

Alright, I understand that version2 involves some repeating differencing, so is version1 always better than version 2? if not , what are some typical exceptions?

  • 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-09T13:46:26+00:00Added an answer on June 9, 2026 at 1:46 pm

    I’d expect those to be exactly the same at any reasonable optimization level – did you try it?

    Edit:

    I wanted to confirm, so I tried it. Here’s my example program (I fixed your pointer mismatch errors, too):

    void operation(char);
    
    void f1(void)
    {
        char s[]="aString",*sp=s,c;
        while(c=*sp++) {
            operation(c);
        }
    }
    
    void f2(void)
    {
        char s[]="aString",*sp=s;
        for(;*sp;sp++) {
             operation(*sp);
        }
    }
    

    Compiled with clang at -O3 on my Mac, here’s the object file:

    example.o:
    (__TEXT,__text) section
    _f1:
    0000000000000000    pushq   %rbp
    0000000000000001    movq    %rsp,%rbp
    0000000000000004    pushq   %rbx
    0000000000000005    pushq   %rax
    0000000000000006    movq    $0x00676e6972745361,%rax
    0000000000000010    movq    %rax,0xf0(%rbp)
    0000000000000014    movb    $0x61,%al
    0000000000000016    leaq    0xf1(%rbp),%rbx
    000000000000001a    nopw    _f1(%rax,%rax)
    0000000000000020    movsbl  %al,%edi
    0000000000000023    callq   _operation
    0000000000000028    movb    (%rbx),%al
    000000000000002a    incq    %rbx
    000000000000002d    testb   %al,%al
    000000000000002f    jne 0x00000020
    0000000000000031    addq    $0x08,%rsp
    0000000000000035    popq    %rbx
    0000000000000036    popq    %rbp
    0000000000000037    ret
    0000000000000038    nopl    _f1(%rax,%rax)
    _f2:
    0000000000000040    pushq   %rbp
    0000000000000041    movq    %rsp,%rbp
    0000000000000044    pushq   %rbx
    0000000000000045    pushq   %rax
    0000000000000046    movq    $0x00676e6972745361,%rax
    0000000000000050    movq    %rax,0xf0(%rbp)
    0000000000000054    movb    $0x61,%al
    0000000000000056    leaq    0xf1(%rbp),%rbx
    000000000000005a    nopw    _f1(%rax,%rax)
    0000000000000060    movsbl  %al,%edi
    0000000000000063    callq   _operation
    0000000000000068    movb    (%rbx),%al
    000000000000006a    incq    %rbx
    000000000000006d    testb   %al,%al
    000000000000006f    jne 0x00000060
    0000000000000071    addq    $0x08,%rsp
    0000000000000075    popq    %rbx
    0000000000000076    popq    %rbp
    0000000000000077    ret
    

    As you can see, literally identical.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.