简体   繁体   English

f1()函数将如何执行?

[英]How this function f1() will execute?

#include <stdio.h>
void f1(char* str, int index)
{
   *(str + index) &= ~32;
}
int main()
{
   char arr[] = "gatecsit";
   f1(arr, 0);
   printf("%s", arr);
   return 0;
}

How is function f1() working? 函数f1()如何工作?
Being specific *(str + index) &= ~32; 是特定的*(str + index)&=〜32; this one.... thanks 这个...。谢谢

I think f1() capitalizes the first letter of the string by exploiting a property of ASCII that means that corresponding lower and upper-case letters differ by 32. For example the code for 'A' is 65, while that for 'a' is 97. The '&= ~32' bit of the code will turn of bit-5 of the ASCII representation of the character str[index], which should turn the 'g' into 'G'. 我认为f1()通过利用ASCII的属性将字符串的第一个字母大写,这意味着相应的小写和大写字母相差32。例如,“ A”的代码为65,而“ a”的代码为97.该代码的“&=〜32”位将把字符str [index]的ASCII表示的第5位变为“ g”。 This should be fine for strings that contain only ordinary letters, but will have strange effects on digits and punctuation characters. 这对于只包含普通字母的字符串应该是正确的,但是会对数字和标点符号产生奇怪的影响。

The expression 表达方式

*(str + index)

is equivalent to 相当于

str[index]

So the character at position index is changed the following way 因此,位置index处的字符按以下方式更改

*(str + index) &= ~32;

In the ASCII table lower case letters differ from upper case letters by having one more set bit. 在ASCII表中,小写字母与大写字母的不同之处是多了一个设置位。 For example the lower case letter 'a' has the code in hex 61 while the upper case letter 'A" has the code in hex 41 . So the difference is equal to the value in hex 20 that in decimal is equal to 32 . 例如,小写字母'a'具有十六进制61的代码,而大写字母'A"具有十六进制41的代码,因此,该差等于十六进制20中的值,十进制等于32

So the original expression resets the corresponding bit in the character to 0 converting a lower case letter to the upper case letter. 因此,原始表达式会将字符中的相应位重置为0,从而将小写字母转换为大写字母。

The code removes 1 bit from the character effectively subtracting 32 from the byte or 0x20. 该代码从字符中删除了1位,从字节或0x20有效减去32。

#include <stdio.h>
#include <string.h>

void f1(char* str, int index)
{
 // The code removes 1 bit from the character at the position `str[index]`
 // effectively subtracting 32 from that character
 // Capital letters in ASCII are apart by 32 (0x20) from small letters 

 // Since 'a' = 0x61 and 'A' = 0x41  'a' - 32 = 'A'
 // Since 'b' = 0x62 and 'B' = 0x42  'b' - 32 = 'B'

 // `~` is a binary negation operator 0 -> 1;  1 -> 0
 // `&` is a binary AND
 // x &= y; is equivalent to x = x & y;

 // ~0x20 = 0b11011111    

   *(str + index) &= ~0x20; // 0x20 = 32;
}


int main()
{
   int i;
   char arr[] = "gatecsit";
   size_t len = strlen(arr);


  for(i = 0; i< len; i++)
      printf(" %c " , arr[i]);   

   printf("\n");

  for(i = 0; i< len; i++)
      printf(" %X" , arr[i]);

   printf("\n");

  // convert all letters:  
   for(i = 0; i< len; i++)
      f1(arr, i);

   printf("\n");

  for(i = 0; i< len; i++)
      printf(" %c " , arr[i]);  

   printf("\n");

  for(i = 0; i< len; i++)
      printf(" %X" , arr[i]);

   return 0;
 }

Output: The small and capital are letters apart by 0x20 (or 32 decimal). 输出:小写字母和大写字母相隔0x20(或32个十进制)。 This can be clearly seen from this printout: 从此打印输出可以清楚地看到:

 g  a  t  e  c  s  i  t 
 67 61 74 65 63 73 69 74

 G  A  T  E  C  S  I  T 
 47 41 54 45 43 53 49 54

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何按点和位置对(F1 车手的)链表进行排序? - How to sort a linked list (of F1 drivers) by points and positions? 如何为双指针分配内存,并且它应该在f1,f2和f3中可见? - How to allocate memory for double pointer and it should be visible in f1, f2 and f3? C,变量f1周围的堆栈已损坏 - C, stack around variable f1 was corrupted Ncurses:检测是否按下 F1 键并使用信号 - Ncurses: Detecting if F1 key pressed and using signals 在给定时间/样本量下,频率f1和f2之间指数变化的正弦波 - sine wave that exponentialy changes between frequencies f1 and f2 at given time/amount of samples 在给定时间内将频率从f1缓慢升高到f2的正弦波 - sine wave that slowly ramps up frequency from f1 to f2 for a given time 如何执行C中的第二个function? - How to execute the second function in C? printf函数如何处理%f规范? - How printf function handle %f specification? C程序不允许n个以上的线程同时执行函数f - C program not to allow more than n threads to execute simultaneously a function f 如何在32位MCU上比“ snprintf(mystr,22,``{%+ 0.4f,%+ 0.4f}&#39;&#39;,(double)3.14159265,(double)2.718281828459);”更快地执行) - How to execute faster than “snprintf(mystr, 22, ”{%+0.4f,%+0.4f}“, (double)3.14159265, (double) 2.718281828459);” on a 32 bit mcu
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM