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;
}
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.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development