#include<stdio.h> int main() { char str[] = "Nagpur"; str[0]='K'; printf("%s, ", str); str = "Kanpur"; printf("%s", str+1); return 0; }
To remove error we have to change this statement str = "Kanpur"; to strcpy(str, "Kanpur");
The program prints the string "anpur"
#include<stdio.h> int main() { char *str[] = {"Frogs", "Do", "Not", "Die", "They", "Croak!"}; printf("%d, %d", sizeof(str), strlen(str[0])); return 0; }
Step 2: printf("%d, %d", sizeof(str), strlen(str[0]));
sizeof(str) denotes 6 * 4 bytes = 24 bytes. Hence it prints '24'
strlen(str[0])); becomes strlen(Frogs)). Hence it prints '5';
Hence the output of the program is 24, 5
Hint: If you run the above code in 16 bit platform (Turbo C under DOS) the output will be 12, 5. Because the pointer occupies only 2 bytes. If you run the above code in Linux (32 bit platform), the output will be 24, 5 (because the size of pointer is 4 bytes).
#include<stdio.h> int main() { printf("%u %s\n", &"Hello1", &"Hello2"); return 0; }
The %u format specifier tells the compiler to print the memory address of the "Hello1".
The %s format specifier tells the compiler to print the string "Hello2".
Hence the output of the program is "1022 Hello2".
#include<stdio.h> int main() { int i; char a[] = "\0"; if(printf("%s", a)) printf("The string is not empty\n"); else printf("The string is empty\n"); return 0; }
Step 1: char a[] = '\0'; The variable a is declared as an array of characters and it initialized with "\0". It denotes that the string is empty.
Step 2: if(printf("%s", a)) The printf() statement does not print anything, so it returns '0'(zero). Hence the if condition is failed.
In the else part it prints "The string is empty".
#include<stdio.h> #include<string.h> int main() { char *str1 = "India"; char *str2 = "CURIOUSTAB"; char *str3; str3 = strcat(str1, str2); printf("%s %s\n", str3, str1); return 0; }
It may cause a 'segmentation fault error' in GCC (32 bit platform).
#include<stdio.h> int main() { char str[25] = "CuriousTab"; printf("%s\n", &str+2); return 0; }
Step 2: printf("%s\n", &str+2);
=> In the printf statement %s is string format specifier tells the compiler to print the string in the memory of &str+2
=> &str is a location of string "CuriousTab". Therefore &str+2 is another memory location.
Hence it prints the Garbage value.
#include<stdio.h> int main() { char p[] = "%d\n"; p[1] = 'c'; printf(p, 65); return 0; }
Step 2: p[1] = 'c'; Here, we overwrite the second element of array p by 'c'. So array p becomes "%c".
Step 3: printf(p, 65); becomes printf("%c", 65);
Therefore it prints the ASCII value of 65. The output is 'A'.
#include<stdio.h> int main() { printf(5+"Good Morning\n"); return 0; }
Hence the output is "Morning"
#include<stdio.h> int main() { char str1[] = "Hello"; char str2[10]; char *t, *s; s = str1; t = str2; while(*t=*s) *t++ = *s++; printf("%s\n", str2); return 0; }
#include<stdio.h> int main() { char str = "CuriousTab"; printf("%s\n", str); return 0; }
To eliminate the error, we have to change the above line to
char *str = "CuriousTab"; (or) char str[] = "CuriousTab";
Then it prints "CuriousTab".
#include<stdio.h> int main() { printf(5+"CuriousTab\n"); return 0; }
Comments
There are no comments.Copyright ©CuriousTab. All rights reserved.