Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does pointer increment compare with index increment in C

consider the following two code:

void PrintLetter(char *src)
{
 while(*src != '\0')
 {
   printf("%c",*src);
   src++;
 }
}

and

void PrintLetter(char *src)
{
 int i;
 for(i=0;src[i];i++)
  printf("%c",src[i]);
}

Is there any performance difference between the two?

like image 605
Shamim Hafiz - MSFT Avatar asked Jun 01 '11 14:06

Shamim Hafiz - MSFT


1 Answers

None whatsoever. The compiler will perform its optimizations regardless of the form you are writing. The underlying assembly code is the same.

like image 195
Eli Iser Avatar answered Sep 22 '22 10:09

Eli Iser