Q:

Find output of C programs (strings) | Set 2

belongs to collection: Find output of programs in C

0

In this article we enhance our knowledge regarding strings in C by solving and finding output of some C programming questions from the topic of strings.

1) In C, what will be the output of the given code?

#include <stdio.h>
#include <string.h>

int main()
{
	char *s1, *s2;
	s1= "abcdef" ;
	s2= "abcdeg" ;
	printf(" %d ", strcmp (s1,s2));
	printf(", ");
	s1="abcdef";
	s2="abcdef";
	printf(" %d ", strcmp (s1,s2));
	printf(", ");
	s1="abcdef";
	s2="abcdee";
	printf(" %d ", strcmp (s1,s2));
	printf(", ");
	return 0;
}

2) Which function is used to find the last occurrence of character in C?

3) In C, what will be the output of following program?

#include <stdio.h>
#include <string.h>

int main()
{
	char a[] = "%d\n";
	a[5] = 'i';
	
	printf(a, 85);
	return 0;
}

4) In C, what will be the output of following program?

#include <stdio.h>
#include <string.h>

int main()
{
	char a[] = "Include Help";
	char *b = "Include Help";

	printf("%d, %d, ", sizeof(a), sizeof(b));
	printf("%d, %d", sizeof(*a), sizeof(*b));

	return 0;
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer 1:

Output

-1 , 0 , 1

Explanation

In C the function strcmp() works by comparing each character of both strings. It compares until a different word is found.

If ASCII value of word of 1st string is greater than ASCII value of word of 2ndstring then the output is 1.

Similarly, If ASCII value of word of 1st string is smaller than ASCII value of word of 2nd string then the output is -1.

Answer 2:

strrchr()

Explanation

In C function strrchr() is used to find the last occurrence of character.

Example:

#include <stdio.h>
#include <string.h>

int main()
{
	char document[] = "I love IncludeHelp.com This is an amazing website ";
	char *s, *p ;
	char character2 = 'x', character1 = 't';
	
	s = strrchr(document, character1 );
	p = strrchr(document, character2 );
	
	if (s)
		printf("The position of '%c' is %d\n", character1, s-document);
	else
		printf("The character was not found in given document\n");
	
	if (p)
		printf("The position of '%c' is %d\n", character2 , p-document);
	else
		printf("The character was not found in given document\n");

	return 0;
}

Output

The position of 't' is 47
The character was not found in given document

Answer 3:

Output

85

Explanation

Here, a is a character array that contains "%d\n", this final statement after execution will be considered like printf("%d\n",85); thus, output will be 85.

Answer 4:

Output

13, 8, 1, 1

Explanation

sizeof(a) will return 13 because total number of characters are 13 (including NULL character).

sizeof(b) will return 8 (depending on the compiler architecture, here b is a pointer and pointer takes 8 bytes in the memory as they hold the memory address, and the memory address size depends on the compiler architecture.

sizeof(*a) and sizeof(*b) will return 1 and 1, because *a and *b are representing the single character and in C language character size is 1 byte.

need an explanation for this answer? contact us directly to get an explanation for this answer
need an explanation for this answer? contact us directly to get an explanation for this answer

Answer 1:

Output

-1 , 0 , 1

Explanation

In C the function strcmp() works by comparing each character of both strings. It compares until a different word is found.

If ASCII value of word of 1st string is greater than ASCII value of word of 2ndstring then the output is 1.

Similarly, If ASCII value of word of 1st string is smaller than ASCII value of word of 2nd string then the output is -1.

Answer 2:

strrchr()

Explanation

In C function strrchr() is used to find the last occurrence of character.

Example:

#include <stdio.h>
#include <string.h>

int main()
{
	char document[] = "I love IncludeHelp.com This is an amazing website ";
	char *s, *p ;
	char character2 = 'x', character1 = 't';
	
	s = strrchr(document, character1 );
	p = strrchr(document, character2 );
	
	if (s)
		printf("The position of '%c' is %d\n", character1, s-document);
	else
		printf("The character was not found in given document\n");
	
	if (p)
		printf("The position of '%c' is %d\n", character2 , p-document);
	else
		printf("The character was not found in given document\n");

	return 0;
}

Output

The position of 't' is 47
The character was not found in given document

Answer 3:

Output

85

Explanation

Here, a is a character array that contains "%d\n", this final statement after execution will be considered like printf("%d\n",85); thus, output will be 85.

Answer 4:

Output

13, 8, 1, 1

Explanation

sizeof(a) will return 13 because total number of characters are 13 (including NULL character).

sizeof(b) will return 8 (depending on the compiler architecture, here b is a pointer and pointer takes 8 bytes in the memory as they hold the memory address, and the memory address size depends on the compiler architecture.

sizeof(*a) and sizeof(*b) will return 1 and 1, because *a and *b are representing the single character and in C language character size is 1 byte.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (2)

Structures - find output programs in C (Set 1)... >>
<< Strings - Find output programs in C (Set 1)...