#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[20] = "World";
char str3[20];
printf("Length of str1: %lu\n", strlen(str1));
printf("str1 in lowercase: %s\n", str1);
printf("str1 in uppercase: %s\n", str1);
strcpy(str3, str1);
printf("str3 after strcpy: %s\n", str3);
if (strcmp(str1, str2) == 0) {
printf("str1 and str2 are equal.\n");
} else {
printf("str1 and str2 are not equal.\n");
}
strcat(str1, str2);
printf("str1 after strcat: %s\n", str1);
strncpy(str3, str2, 3);
str3[3] = '\0';
printf("str3 after strncpy: %s\n", str3);
return 0;
}
Length of str1: 5
str1 in lowercase: hello
str1 in uppercase: HELLO
str3 after strcpy: HELLO
str1 and str2 are not equal.
str1 after strcat: HELLOWORLD
str3 after strncpy: Wor
Note: strlwr and strupr are not part of the standard C library. You may need to implement these functions manually or use a different approach for case conversion.
#include <stdio.h>
#define PI 3.14
#define SQUARE(x) (x * x)
#ifdef PI
#define AREA(r) (PI * SQUARE(r))
#endif
int main() {
int radius = 5;
printf("Area of circle with radius %d: %.2f\n", radius, AREA(radius));
#ifdef PI
printf("PI is defined.\n");
#else
printf("PI is not defined.\n");
#endif
return 0;
}
Area of circle with radius 5: 78.50
PI is defined.
#include <stdio.h>
int main() {
char name[20];
char ch;
printf("Enter your name: ");
// Note: gets is unsafe; use fgets instead
fgets(name, sizeof(name), stdin);
name[strcspn(name, "\n")] = 0;
printf("Hello, ");
puts(name);
printf("Enter a character: ");
ch = getchar();
printf("You entered: ");
putchar(ch);
printf("\n");
return 0;
}
Enter your name: John
Hello, John
Enter a character: A
You entered: A
Note: gets is deprecated and unsafe due to buffer overflow risks. Use fgets instead, as shown in the corrected code above.
#include <stdio.h>
int main() {
int choice;
printf("Enter a number (1-3): ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("You chose 1\n");
break;
case 2:
printf("You chose 2\n");
break;
case 3:
goto label;
default:
printf("Invalid choice\n");
}
label:
printf("You reached the label.\n");
return 0;
}
Enter a number (1-3): 3
You reached the label.
#include <stdio.h>
int main() {
int i, j;
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
#include <stdio.h>
int main() {
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 7; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
* * * * * * *
* * * * * * *
* * * * * * *
#include <stdio.h>
int main() {
int i, j;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
*
* *
* * *
* * * *
* * * * *
#include <stdio.h>
int main() {
int i, j;
for (i = 5; i >= 1; i--) {
for (j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
* * * * *
* * * *
* * *
* *
*