Chapter - 10: Strings

What will be the output of the following programs:


A
Sections
1
Exercises

A

B

C

D

E

F

1

(a)

#include<stdio.h>
int main()
{
	char c[2] = "A";
	printf("\n%c", c[0]);
	printf("\n%s", c);
	return 0;
}

Output:
A
A


(b)

#include<stdio.h>
int main()
{
	char s[] = "Get organised! learn C!!";
	printf("\n%s", &s[2]);
	printf("\n%s", s);
	printf("\n%s", &s);
	printf("\n%c", s[2]);
	return 0;
}

Output:
t organized! learn C!!
Get organized! learn C!!
Get organized! learn C!!
t


(c)

#include<stdio.h>
int main()
{
	char s[] = "No two viruses work similarly";
	int i = 0;
	while (s[i] != 0)
	{
		printf("\n%c %c", s[i], *(s + i));
		printf("\n%c %c", i[s], *(i + s));
		i++;
	}
	return 0;
}

Output:

N N
N N
o o
o o

t t
t t
w w
w w
o o
o o


v v
v v
i i
i i
r r
r r
u u
u u
s s
s s
e e
e e
s s
s s


w w
w w
o o
o o
r r
r r
k k
k k

s s
s s
i i
i i
m m
m m
i i
i i
l l
l l
a a
a a
r r
r r
l l
l l
y y
y y


(d)

#include<stdio.h>
int main()
{
	char s[] = "Churchgate: no church no gate";
	char t[25];
	char *ss, *tt;
	ss = s;
	while (*ss != '\0')
		*ss++ = *tt++;
	printf("\n%s", t);
	return 0;
}

Output:
Garbage or error, that t is not initialized.


(e)

#include<stdio.h>
int main()
{
	char str1[] = { 'H', 'e', 'l', 'l', 'o' };
	char str2[] = "Hello";

	printf("\n%s", str1);
	printf("\n%s", str2);
	return 0;
}

Output:
Hello[garbage characters because the NULL character is missing]
Hello


(f)

#include<stdio.h>
int main()
{
	printf(5 + "Good Morning ");
	return 0;
}

Output: Morning


(g)

#include<stdio.h>
int main()
{
	printf("%c", "abcdefgh"[4]);
	return 0;
}

Output: e


(h)

#include<stdio.h>
int main()
{
	printf("%d %d %d\n", sizeof('3'), sizeof("3"), sizeof(3));
	return 0;
}

Output: 1 2 4


© 2020 Garbage Valuegarbage value logo