C Programming - Strings
- Strings - General Questions
- Strings - Find Output of Program
- Strings - Point Out Correct Statements
- Strings - Yes / No Questions
#include<stdio.h>
int main()
{
char ch = 'A';
printf("%d, %d, %d", sizeof(ch), sizeof('A'), sizeof(3.14f));
return 0;
}
Step 1: char ch = 'A'; The variable ch is declared as an character type and initialized with value 'A'.
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
#include<stdio.h>
int main()
{
char a[] = "Visual C++";
char *b = "Visual C++";
printf("%d, %d\n", sizeof(a), sizeof(b));
printf("%d, %d", sizeof(*a), sizeof(*b));
return 0;
}
#include<stdio.h>
int main()
{
static char mess[6][30] = {"Don't walk in front of me...",
"I may not follow;",
"Don't walk behind me...",
"Just walk beside me...",
"And be my friend." };
printf("%c, %c\n", *(mess[2]+9), *(*(mess+2)+9));
return 0;
}
#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[] = "India\0BIX\0";
printf("%d\n", sizeof(str));
return 0;
}
The following examples may help you understand this problem:
1. sizeof("") returns 1 (1*).
2. sizeof("India") returns 6 (5 + 1*).
3. sizeof("BIX") returns 4 (3 + 1*).
4. sizeof("India\0BIX") returns 10 (5 + 1 + 3 + 1*).
Here '\0' is considered as 1 char by sizeof() function.
5. sizeof("India\0BIX\0") returns 11 (5 + 1 + 3 + 1 + 1*).
Here '\0' is considered as 1 char by sizeof() function.