#include<stdio.h> int main() { printf(5+"Good Morning\n"); return 0; }
Hence the output is "Morning"
#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() { 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> 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; }
#include<stdio.h> #include<string.h> int main() { static char str1[] = "dills"; static char str2[20]; static char str3[] = "Daffo"; int i; i = strcmp(strcat(str3, strcpy(str2, str1)), "Daffodills"); printf("%d\n", i); return 0; }
#include<stdio.h> int main() { char ch = 'A'; printf("%d, %d, %d", sizeof(ch), sizeof('A'), sizeof(3.14f)); return 0; }
Step 2:
printf("%d, %d, %d", sizeof(ch), sizeof('A'), sizeof(3.14));
The sizeof function returns the size of the given expression.
sizeof(ch) becomes sizeof(char). The size of char is 1 byte.
sizeof('A') becomes sizeof(65). The size of int is 4 bytes (as mentioned in the question).
sizeof(3.14f). The size of float is 4 bytes.
Hence the output of the program is 1, 4, 4
Comments
There are no comments.Copyright ©CuriousTab. All rights reserved.