C Programming Interview Questions
Join the DZone community and get the full member experience.
Join For FreeMost interview questions are based on algorithms and live coding. Companies know that learning a programming language or switching from one language to another should not be a problem for the candidate. But sometimes, for specific jobs, companies look for the candidate who specializes in a particular language. Here are some of the most frequently asked questions I have compiled that are asked during the interview for C developers.
C is a procedural programming language, used for writing effective programs. C is widely used language for the following reasons:
- It supports ready access to hardware when required.
- It has portable compilers that support all machines ranging from Macintosh to Cray (supercomputers).
- It works on standard library concept.
- Its syntax is elegant.
- It supports powerful operators.
- Programmers can easily optimize the code by using hand-coding isolated procedures.
Let us understand the features of C that are commonly used. In this article, we will understand character set, identifiers, variables, and data types used in C.
Character Set in C
A character set refers to a group of letters, digits and some special characters that are supported and valid in a given language.
Letters |
Uppercase: A B C…..X Y Z |
|||||||||||||
|
Lowercase: a b c…..x y z |
|||||||||||||
Digits |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
||||
Special Characters |
, |
< |
> |
. |
_ |
( |
||||||||
|
) |
; |
$ |
: |
% |
[ |
||||||||
|
] |
# |
? |
' |
& |
{ |
||||||||
|
} |
" |
^ |
! |
* |
/ |
||||||||
|
| |
- |
\ |
~ |
+ |
|
||||||||
Keywords in C
Keywords are the reserved words used in C programming. Each keyword has a standard meaning. Each keyword is recognized by the C compiler when used in a program. They cannot be used by the programmer-defined identifiers. Using the keywords as programmer-defined identifiers will result in a compilation error.
NOTE: All keywords must be written in lowercase.
There are 32 keywords available in C listed below.
auto |
double |
int |
struct |
break |
else |
long |
switch |
case |
enum |
register |
typedef |
char |
extern |
return |
union |
continue |
for |
signed |
void |
do |
if |
static |
while |
default |
goto |
sizeof |
volatile |
const |
float |
short |
unsigned |
Identifiers in C
An identifier is a name assigned to C entities, such as variables, functions, structures, etc. Identifiers are created so that C can uniquely identify them during the execution of program. These are also known as programmer-defined identifiers.
xxxxxxxxxx
Syntax:
int apple_tree;
int mango_tree;
In the example above, apple_tree
is an identifier that denotes a variable of type integer. Similarly, mango_tree
is another identifier, which denotes another variable of type integer.
There is no length restriction of identifier name. But, C compiler considers up to the 32 characters length. If more than 32 characters are used, those characters will be ignored by the compiler.
Variables in C
Variables are referred to as memory location in a computer’s memory. They represent memory location. In general, if we want to search any place in a town or an area, we search the location by its name. The same way, we give names to variables that indicate the memory location. These names are called identifiers. syntax int num;
In the example above, num
is a variable name or identifier of integer type.
There are 3 steps to use variables:
- Name variable: Refers to giving meaningful names to variables
- Declare variable: Refers to passing on some information about the variable to the complier. The declaration statement includes data type of the variable.
- Use variable: Refers to assigning values to variables. The assignment operator is used to assign value.
Data Types in C
Data types represent the type of variable used for declaring variables in a block code. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted.
The data types in C can be classified as follows:
Data Type |
Description |
Basic Types |
These are arithmetic types and categorized into 2 types: a) integer types b) floating-point types |
Enumerated Types |
These are also arithmetic types that are used as user-defined types and that can only be assigned certain discrete integer values throughout the program. |
The type, void |
This type indicates that no value is available for variable |
Derived Types |
These are a) pointer types b) array types c) structure types d) union types e) function types |
Why is C suitable for system programming?
As features of C include low-level access to memory, simple set of keywords, and clean style, it is suitable for system programming like operating system.
How do I decide which integer type to use in C?
Before deciding which integer type to use, it is necessary to understand each type and the value it holds. When you are clear with each integer type, you will be easily able to decide the integer type to be used in your code.
Find the table below that describes integer types in C:
Type |
Storage size |
Value range |
char |
1 byte |
-128 to 127 or 0 to 255 |
unsigned char |
1 byte |
0 to 255 |
signed char |
1 byte |
-128 to 127 |
int |
2 or 4 bytes |
-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 |
unsigned int |
2 or 4 bytes |
0 to 65,535 or 0 to 4,294,967,295 |
short |
2 bytes |
-32,768 to 32,767 |
unsigned short |
2 bytes |
0 to 65,535 |
long |
4 bytes |
-2,147,483,648 to 2,147,483,647 |
unsigned long |
4 bytes |
0 to 4,294,967,295 |
Based on the value range specified in the table above, you can use the integer types to declare variables.
- Use long if you need large values
- Use short if space is very important or if there are large arrays and structures
- Use unsigned types if negative values are not important or you want to be clear of sign-extension problem when manipulating bits or bytes.
The sizeof
operator is used to get exact size of a type or a variable on your system. The example is given below.
Syntax:
int main()
{
printf("Storage size for int : %d \n", sizeof(int));
return 0;
}
Output on Linux after compiling and executing the code:
xxxxxxxxxx
Storage size for int : 4
The relationship of integer types is something like this:
sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
What are pointers in C?
There are two properties of variables: value and address. Many times, we need to deal with the address of memory location rather than its value. A pointer refers to the address of a variable in memory. It allows us indirectly access variables. It can also be referred to as a variable whose value is the address of another variable.
C pointers are explicitly used with arrays, structures and functions.
The following two rules are very important when using pointers:
- A variable name defined with ampersand (&) in front of it defines the address of variable and therefore, points to the variable. Think of ampersand (&) as an address.
- A variable name with star (*) in front of it refers to the value of variable pointed to by the pointer. Think of star (*) as stored.
These are called reference operators.
Declaring a Pointer
A pointer is declared as:
<pointer type> *<pointer-name>
In the above declaration:
<pointer type>
: refers to the type of pointer such as int, char, float etc. The type specifies a variable type whose address this pointer can store.<pointer-name>
: refers to a name of the pointer specified by the user. The pointer names commonly starts with the character ‘p’ or end with ‘ptr’.
For Example:
int *iptr;
In the above declaration, ‘int’ signifies the pointer type, iptr is the name of the pointer while the asterisk ‘*’ signifies that ‘iptr’ is a pointer variable.
Initializing a Pointer
A pointer is initialized as:
<pointer declaration(except semicolon)> = <address of a variable>
for example:
int i = 5, *iptr = &x;
In the code above, we declared an integer variable ‘x’ which stores the value ‘5’. Now, we declared an integer pointer ‘iptr’ and initialized it with the address of variable ‘x’.
Note that the ‘&’ operator is used to access the address of any type of variable.
Opinions expressed by DZone contributors are their own.
Trending
-
A Data-Driven Approach to Application Modernization
-
How Web3 Is Driving Social and Financial Empowerment
-
13 Impressive Ways To Improve the Developer’s Experience by Using AI
-
Turbocharge Ab Initio ETL Pipelines: Simple Tweaks for Maximum Performance Boost
Comments