9 Essential C Interview Questions *

最好的C开发人员和工程师可以回答的全面来源的基本问题. 在我们社区的推动下,我们鼓励专家提交问题并提供反馈.

Hire a Top C Developer Now
Toptal logo是顶级自由软件开发人员的专属网络吗, designers, finance experts, product managers, and project managers in the world. 顶级公司雇佣Toptal自由职业者来完成他们最重要的项目.

Interview Questions

1.

What is a void pointer? 你能在不知道void指针类型的情况下解引用它吗?

View answer

void指针是一种可用于指向任意类型的任何数据的指针. void指针只有在显式强制转换之后才能解引用. For example:

int a = 5;
void *b = &a;
printf(“%d\n”, *((int*)b));
2.

What is the difference between #include "..." and #include <...>?

View answer

不同之处在于预处理器在哪里查找要包含的文件. For the include directive with a double quoted filename, 预处理器将其对文件的搜索限制在当前源文件所在的同一目录中, 然后转到编译器预先指定的标准目录. On the other hand, when the directive uses angle brackets, 预处理器在编译器预先指定的目录中搜索文件——通常是标准库头文件所在的目录.

3.

What are dangling pointers? How are dangling pointers different from memory leaks?

View answer

悬空指针指向已经释放的内存位置. For example:

int *a = malloc(sizeof(int));
free(a);
// a is now a dangling pointer

内存泄漏与悬空指针完全相反. 当内存位置没有被释放时,就会发生内存泄漏,但是没有办法引用它们(例如.e., no pointers are pointing to them).

int *a = malloc(sizeof(int));
a = 0;
//现在a不再指向我们刚刚分配的内存,导致内存泄漏

Unlike higher-level languages with garbage collectors, 在用C语言编程时,始终跟踪已分配的内存是至关重要的.

Apply to Join Toptal's Development Network

and enjoy reliable, steady, remote Freelance C Developer Jobs

Apply as a Freelancer
4.

What is being declared in the following statement?

char (*x) (char*);
View answer

The statement above declares x 作为指向函数的指针,该函数接受单个字符指针参数,并返回一个字符.

5.

当执行以下代码时,输出将是什么? Explain.

#include 
#define SQUARE(a) (a)*(a)

int main() {
    printf("%d\n", SQUARE(4));
    int x = 3;
    printf("%d\n", SQUARE(++x));
}
View answer

The answer is infact undefined, and depends on the compiler being used. Some compilers will result in 16 and 20, while others will produce 16 and 25.

One might expect the second use of the SQUARE macro to yield 16, just like the first use of the SQUARE macro. However, macros are processed by the preprocessor, 在实际编译开始之前进行的步骤. 展开第二个宏将显示实际编译的内容:

(++x)*(++x)

The evaluation of the pre-increment operation ++x is where the undefined behavior in C comes in. With some compilers, the macro will reduce to (4)*(5), while in other cases, it will be evaluated as (5)*(5).

This article discusses this behavior further.

6.

Why is it usually a bad idea to use gets()? Suggest a workaround.

View answer

The function gets() reads characters from the stdin and stores them at the provided input buffer. However, gets() 会继续阅读直到遇到换行符. 除非缓冲区足够大,或者被读取的行长度事先已知, gets() 可能溢出输入缓冲区,并开始覆盖内存,它不应该, wreaking havoc or opening security vulnerabilities.

One way to work around this issue is to use fgets(). 它允许你对读取的最大字符数进行限制:

fgets(b, 124, stdin);
7.

What is the difference between structs and unions?

View answer

结构体是一种复杂的数据类型,它允许将多个变量存储在指定内存块的组中. 结构体的每个成员变量可以存储不同的数据,并且它们都可以同时使用.

struct a {
    int x;
    char y;
} a;

For example, you may store an integer in x, and and a character in y above, independent of each other.

A union, on the other hand, 将任何成员变量的内容存储在完全相同的内存位置. 这允许在相同的内存位置存储不同类型的数据. 结果是,给一个成员赋值将改变所有其他成员的值. 与struct不同,在任何给定时间,联合类型中可能只有一个成员是有用的.

union b {
    int x;
    char y;
} b;

For example, storing a character in y may automatically change the integer you read from x to something meaningless or unpredictable.

8.

下面的代码片段使用强制类型转换将浮点数转换为整数:

float f = 1.0;

int i1 = (int) f;
int i2 = * (int *) &f;

printf("%d\n, i1);
printf("%d\n, i2);

The following output is produced:

1
1065353216

Can you explain why results differ?

View answer

第一个强制转换操作正确地将浮点数转换为整数, as specified by the C standard. The second conversion, however, 是否首先将浮点指针转换为整数指针,然后解引用以获得最终结果. 这样,编译器就可以有效地处理浮点数(通常以IEEE浮点格式存储)中的原始位,就好像它们是整数的位一样. 除了得到错误的结果之外,您还可能执行“错误读取”操作,在以下情况下 sizeof(int) is greater than sizeof(float) (e.g. on some 64-bit architectures).

Although this particular code is unlikely, 它演示了当只有指向要转换的变量的指针可用时,类型转换所涉及的风险之一. In practice, the pointer must be dereferenced before it is cast.

9.

如果在32位操作系统上运行下面的程序,输出结果是什么?

#include 
int  main()
{
  int a=-2;
  printf("%x",a>>3);
}
View answer

在32位操作系统中,整数被存储为4字节.

Since a is negative, it will be stored in 2’s complement. 当一个整数为负时我们想右移n位, we need to prepend ones (not zeros!) to the left hand side. The answer would therefore be 0xFFFF (%x prints out value in hex).

面试不仅仅是棘手的技术问题, so these are intended merely as a guide. 并不是每一个值得雇佣的“A”候选人都能回答所有的问题, nor does answering them all guarantee an “A” candidate. At the end of the day, hiring remains an art, a science — and a lot of work.

Why Toptal

Tired of interviewing candidates? Not sure what to ask to get you a top hire?

Let Toptal find the best people for you.

Hire a Top C Developer Now

Our Exclusive Network of C Developers

Looking to land a job as a C Developer?

Let Toptal find the right job for you.

Apply as a C Developer

Job Opportunities From Our Network

Submit an interview question

提交的问题和答案将被审查和编辑, and may or may not be selected for posting, at the sole discretion of Toptal, LLC.

* All fields are required

Looking for C Developers?

Looking for C Developers? Check out Toptal’s C developers.

David Marín

Freelance C Developer

SpainToptal Member Since July 2, 2015

David是一名开源和开放数据爱好者,拥有超过23年的专业开发经验. He has acquired various skills, including web programming (PHP and JavaScript), C, C++ (under Linux and Windows), and systems management. Of these skills, David擅长web编程,并拥有基于Symfony php的后端全栈经验, jQuery front ends, and WordPress/WooCommerce-based sites.

Show More

Stephen Villee

Freelance C Developer

United StatesToptal Member Since January 8, 2016

Stephen已经做了30多年的软件工程师. 从20世纪80年代为Unix平台开发软件到用PHP开发企业级金融和电子商务软件, JS, and the C language family, 他以一丝不苟和开发高质量的软件而自豪. He is a dedicated, efficient individual and a great communicator, 并期待为您的项目贡献有价值的技能!

Show More

Guri Labartyava

Freelance C Developer

GeorgiaToptal Member Since July 6, 2018

拥有计算机科学和数学学士学位以及计算机工程硕士学位, 古里拥有扎实的学术背景和多年的工作经验, working on complex projects on embedded devices. 此外,他对人工智能和图像处理的基础知识有很强的理解. Guri加入Toptal是因为他热衷于从事影响现实生活的重大项目.

Show More

Toptal Connects the Top 3% of Freelance Talent All Over The World.

Join the Toptal community.

Learn more