#include<stdio.h> #include<string.h> int main() { char sentence[80]; int i; printf("Enter a line of text\n"); gets(sentence); for(i=strlen(sentence)-1; i >=0; i--) putchar(sentence[i]); return 0; }
#include<stdio.h> int main() { void fun(); fun(); printf("\n"); return 0; } void fun() { char c; if((c = getchar())!= '\n') fun(); printf("%c", c); }
Step 2: fun(); The function fun() is called here.
The function fun() gets a character input and the input is terminated by an enter key(New line character). It prints the given character in the reverse order.
The given input characters are "abc"
Output: cba
#include<stdio.h> int main() { static char s[25] = "The cocaine man"; int i=0; char ch; ch = s[++i]; printf("%c", ch); ch = s[i++]; printf("%c", ch); ch = i++[s]; printf("%c", ch); ch = ++i[s]; printf("%c", ch); return 0; }
#include<stdio.h> int main() { char *names[] = { "Suresh", "Siva", "Sona", "Baiju", "Ritu"}; int i; char *t; t = names[3]; names[3] = names[4]; names[4] = t; for(i=0; i<=4; i++) printf("%s,", names[i]); return 0; }
Step 2: int i; The variable i is declared as an integer type.
Step 3: char *t; The variable t is declared as pointer to a string.
Step 4: t = names[3]; names[3] = names[4]; names[4] = t; These statements the swaps the 4 and 5 element of the array names.
Step 5: for(i=0; i<=4; i++) printf("%s,", names[i]); These statement prints the all the value of the array names.
Hence the output of the program is "Suresh, Siva, Sona, Ritu, Baiju".
#include<stdio.h> int main() { printf("India", "CURIOUSTAB\n"); return 0; }
#include<stdio.h> #include<string.h> int main() { char str1[5], str2[5]; int i; gets(str1); gets(str2); i = strcmp(str1, str2); printf("%d\n", i); return 0; }
The gets(str1) read the input string from user and store in variable str1.
The gets(str2) read the input string from user and store in variable str2.
The code i = strcmp(str1, str2); The strcmp not only returns -1, 0 and +1, but also other negative or positive values. So the value of i is "unpredictable integer value".
printf("%d\n", i); It prints the value of variable i.
#include<stdio.h> #include<string.h> int main() { printf("%c\n", "abcdefgh"[4]); return 0; }
Hence the output is 'e'.
#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> #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() { 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() { 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".
Comments
There are no comments.Copyright ©CuriousTab. All rights reserved.