Machine-translated by Google. Questions are authored in English.
Top

Discussion

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

 

  • A.GeeksQuiz
  • B.geeksquiz
  • C.Compiler dependent

Answer: C

Correct answer: Compiler dependent

arr points to a string literal, and modifying one is undefined behaviour. Most compilers place literals in read-only memory, so the write faults at run time; others allow it, and the program prints GeeksQuiz. The fix is char arr[] = "geeksquiz";, which gives a modifiable copy. (The &= ~32 trick works because upper and lower case differ by exactly bit 5.)

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.