C Interview Questions for Developers
Question: What is the significance of the const
keyword in C?
Answer:
The const
keyword in C is used to define variables whose values cannot be changed after initialization. It serves as a way to protect data from accidental modification, making the program safer and easier to understand. Here’s how it works:
-
Constant Variables: When a variable is declared with
const
, its value cannot be modified after it is initialized.const int x = 10; x = 20; // This will result in a compile-time error.
-
Pointers and
const
: Theconst
keyword can also be used with pointers to indicate that either the pointer itself or the data it points to should not be modified.-
Pointer to constant data: If the data being pointed to should not be modified, the
const
comes before the type:const int *ptr = &x; *ptr = 20; // Error: Cannot modify the value pointed to by ptr.
-
Constant pointer: If the pointer itself should not be modified (i.e., it should always point to the same memory location),
const
comes after the*
:int *const ptr = &x; ptr = &y; // Error: Cannot change the pointer's address.
-
Constant pointer to constant data: If both the pointer and the data it points to should not be modified, both
const
keywords are used:const int *const ptr = &x; ptr = &y; // Error: Cannot change the pointer's address. *ptr = 20; // Error: Cannot modify the value pointed to by ptr.
-
-
Function Parameters:
const
can be used in function declarations to indicate that a parameter should not be modified inside the function. This is particularly useful for pointers and arrays.void printValue(const int *ptr) { // *ptr = 10; // Error: Cannot modify the value pointed to by ptr. }
-
Readability and Optimization: The use of
const
enhances code readability by making it clear which values are intended to remain unchanged. It can also help the compiler optimize the code better, knowing that certain values will not change during execution.
In summary, the const
keyword in C is used to define constant values and restrict modification of variables, improving program stability, readability, and performance.
Question: How are strings represented in C?
Answer:
In C, strings are represented as arrays of characters terminated by a special character known as the null character ('\0'
). The null character signifies the end of the string, so it allows functions to know where the string stops.
Here’s a breakdown of how strings are represented:
-
Character Array: A string in C is essentially a character array that holds individual characters of the string. For example:
char str[] = "Hello";
This creates an array
str
that contains the characters'H'
,'e'
,'l'
,'l'
,'o'
, followed by the null terminator'\0'
. In memory, it looks like this:['H', 'e', 'l', 'l', 'o', '\0']
-
Null Terminator: The null character
'\0'
is critical for C strings, as it marks the end of the string. Without this terminator, functions that process strings (e.g.,printf
,strlen
) wouldn’t know where the string ends. For example, even thoughstr[]
contains five characters, the total size of the array is six (5 characters + 1 null terminator). -
String Initialization: When you initialize a string using double quotes, C automatically appends the null terminator. Here’s how the initialization works:
char str[] = "Hello"; // Equivalent to: // char str[] = {'H', 'e', 'l', 'l', 'o', '\0'};
-
String Pointers: You can also represent strings using pointers. A string can be stored in a pointer to a
char
:char *str = "Hello";
This is equivalent to pointing to the first character in a string literal stored in memory. It’s important to note that string literals are stored in read-only memory, so attempting to modify the string via this pointer will result in undefined behavior.
-
String Functions: C provides several standard library functions to manipulate strings, which are available in the
<string.h>
library:strlen(str)
– Returns the length of the string (excluding the null terminator).strcpy(dest, src)
– Copies the string fromsrc
todest
.strcmp(str1, str2)
– Compares two strings.strcat(dest, src)
– Appendssrc
todest
.
-
Memory Allocation: In some cases, you may dynamically allocate memory for strings using functions like
malloc
orcalloc
. For example:char *str = (char *)malloc(100 * sizeof(char)); // Allocate space for 100 characters
It’s important to manually handle memory allocation and deallocation to avoid memory leaks.
Key Points:
- String representation: Arrays of characters ending with the null terminator
'\0'
. - Memory layout:
char str[] = "Hello"
is stored as['H', 'e', 'l', 'l', 'o', '\0']
. - Pointer to string: A string can also be represented as a
char *
pointer to the first character of the string literal. - String functions: Functions like
strlen
,strcpy
, andstrcmp
help manipulate strings in C.
In summary, strings in C are arrays of characters terminated by a null character '\0'
, and string handling requires careful attention to memory and string boundaries.
Read More
If you can’t get enough from this article, Aihirely has plenty more related information, such as C interview questions, C interview experiences, and details about various C job positions. Click here to check it out.
Tags
- C Language
- Pointers
- Memory Management
- Data Types
- Structures
- Unions
- Functions
- Function Pointers
- Static Keyword
- C Programming
- C Arrays
- Loops in C
- Memory Allocation
- Malloc
- Calloc
- Segmentation Fault
- Error Handling in C
- Header Files
- Macros in C
- String Handling
- Const Keyword
- C Interview Questions
- C++ vs C
- Pre increment
- Post increment
- C Syntax
- C Debugging
- C Programming Concepts
- Manual Memory Management
- Exit vs exit
- C Programming Techniques