LearnFrenzy
Sign in Take a free test
C

C Programming · Topic 6 of 23

Strings Questions and Concepts

  • 4 exercises
  • 40 questions
  • ~60 min
  • Answers explained

Interview Questions

Strings · Study material

C Strings Interview Questions and Answers

1. How is a string stored in C?

As a char array ending with the null character '\0'. There is no length field, so functions find the end by scanning for the zero byte.

2. What is the difference between char s[] = "hi" and char *s = "hi"?

The first creates a writable 3-byte array initialised from the literal. The second points to the literal itself, which may be in read-only memory; modifying it is undefined behaviour, so write const char *s.

3. Why was gets() removed?

It cannot know the size of the buffer, so any long input overflows it. It was deprecated in C99 and removed in C11; use fgets().

4. What does strcmp() return?

A negative value, zero or a positive value depending on the first differing character (compared as unsigned char). The exact value is not specified.

5. Why is strncpy() considered tricky?

If the source has n or more characters, it does not write a terminator, and if it is shorter it pads the rest with zeros. You usually need d[n - 1] = '\0'.

6. What does snprintf() return?

The number of characters the complete output would need (without '\0'), or a negative value on error. A value ≥ the buffer size means the result was truncated.

7. Why should you not use == to compare strings?

It compares two addresses, not the characters. Two equal strings at different addresses compare unequal.

8. Is strtok() thread-safe?

No. It keeps hidden state between calls. Use strtok_r() (POSIX) or strtok_s() where available, or parse with strcspn()/strspn().

9. Why cast to unsigned char before toupper()?

The ctype functions accept an int that is EOF or representable as unsigned char. A plain char can be negative for bytes above 127, which is undefined behaviour.

10. What does strlen() return for a UTF-8 string like "héllo"?

6 – it counts bytes, and é takes 2 bytes in UTF-8.

11. What is the difference between atoi() and strtol()?

atoi() has no error reporting (and overflow is undefined). strtol() returns where parsing stopped and sets errno to ERANGE on overflow.

12. What changed for strings in C23?

strdup() and strndup() became standard, memccpy() was added, and u8"" literals now have type char8_t arrays (an unsigned char type).

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About C Programming

C programming is the language most computer science courses start with and the foundation of operating systems, embedded devices and compilers. This section helps you learn C properly – data types, operators, control statements, functions, arrays, strings, pointers, structures, memory allocation and files – and then prove it with hundreds of practice questions explained line by line.

Why it matters

  • C teaches how a computer really works: memory, addresses and how data is stored
  • Pointers and memory questions are a favourite in written tests and technical interviews
  • Once you know C, learning C++, Java and Python becomes much easier
  • Embedded systems, operating systems and many companies' coding rounds still use C

Who it is for

  • School students (Class 11–12 computer science) building a strong programming base
  • College students (B.Tech, BCA, MCA, B.Sc CS) preparing for semester exams, lab work and viva
  • Freshers preparing for campus placements, aptitude-cum-coding tests and technical interviews
  • Anyone preparing for GATE CS or government IT exams

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs that train you to spot bugs quickly
  • Complete programs with solutions and real output, verified on a compiler

Asked in

  • Campus placements
  • TCS, Infosys, Wipro tests
  • Technical interviews
  • GATE CS
  • University exams
  • Government IT exams

How to use this section

  1. Read the study material of a topic and run the examples yourself.
  2. Practise the four exercises – MCQs, output, errors, programs – and read every explanation.
  3. Test yourself with the timed online test, then revise the topics you got wrong.

Search LearnFrenzy

Esc

Quick links