×

C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

Find output of C programs (strings) | Set 2

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.
Submitted by Amit Shukla, on July 03, 2017

Here you will find C programs with output and explanations based on string.

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;
}

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.


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

Answer

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

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;
}

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.


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;
}

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.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.