Common Bugs in C Programming
There are some Common Bugs in C Programming.
Most of the contents are directly from or modified from Prof. Liu Pangfeng’s blog. Most credits should go to him.
For all the following problems, answer the output message of the code, unless they are specified differently. If there are any exception(s) (or segmentation faults), indicate where it is (or they are). Most importantly, you need to explain the reason.
I don't want to write down the complete answer.
If you have some questions, please leave a message below. Thank you!
Problem 1:
//case 1
#include <string.h>
int main(void)
{
char *start = "this is a string";
char *start2 = strtok(start, " ");
return 0;
}
//case 2
#include <string.h>
int main(void)
{
char start[] = "this is a string";
char *start2 = strtok(start, " ");
start2 = "this is a string";
start2[4] = '\0';
return 0;
}
What happens and why?
Hint 1:
Maybe you can get an answer from here, here and here.
"this is a string" in char *start = "this is a string"
is stored in the code segment。
It can not be modified, so the program will cause a runtime error.
For case 1, modified ``char *start``to ``char start[]``
Problem 2:
#include <stdio.h>
int division(int *a, int *b)
{
return *a/*b /* a simple division */;
}
int main()
{
int a = 6;
int b = 2;
int *aptr = &a;
int *bptr = &b;
printf("%d\n", division(aptr, bptr));
}
What's wrong? How to fix it?
Problem 3:
#include <stdio.h>
#include <string.h>
int main(void)
{
char string[] = "this is a string";
char *start;
start = string;
start = strtok(start, " ");
while (start != NULL) {
printf("%s\n", start);
start = strtok(NULL, " ");
}
start = string;
start = strtok(start, " ");
while (start != NULL) {
printf("%s\n", start);
start = strtok(NULL, " ");
}
return 0;
}
Show output and explain the difference of the two loops.
Hint 3:
See the definition of function strtok()
Problem 4:
#include <stdio.h>
int main(void)
{
FILE *fp;
char c;
int count;
int i;
fp = fopen("file", "wb");
for (i = 0; i < 256; i++)
fputc(i, fp);
fclose(fp);
fp = fopen("file", "rb");
count = 0;
while ((c = fgetc(fp)) != EOF)
count++;
printf("count = %d\n", count);
return 0;
}
Explain why one is missing.
Hint 4:
Check the ASCII number of the EOF
Problem 5:
#include <stdio.h>
int main()
{
float a = 1.134;
float b = 3.402;
if (a * 3 == b)
printf("yes");
else
printf("no");
}
What happens and why?
Problem 6:
#include <stdio.h>
int main()
{
long int lab_tel = 035731603;
printf("my lab’s telephone number is %ld\n", lab_tel);
return 0;
}
Why is our lab number incorrect?
Hint 6:
Octal & Decimal
Problem 7:
#include <stdio.h>
int main()
{
int a[10];
if (a == &a)
printf("yes\n");
else
printf("no\n");
if (a + 1 == &a + 1)
printf("yes\n");
else
printf("no\n");
}
What happens and why?
Problem 8:
#include <stdio.h>
int main(void)
{
FILE *fp;
fp = fopen("file", "w");
fputs("hello\n", fp);
fputs("hello", fp);
fputs("hello\n", fp);
fclose(fp);
return 0;
}
Answer the lengths of “file” in Unix and Windows, and explain.
Hint 8:
Windows: \CR\LF
Unix: \LF
Problem 9:
#include <stdio.h>
int main(void)
{
FILE *fp;
char c;
fp = fopen("file", "wb");
for (c = 0; c < 256; c++) {
fputc(c, fp);
}
fclose(fp);
return 0;
}
NEVER NEVER run this. Otherwise, your hard disk will crash.
Just tell what is wrong with this program.
Problem 10:
#include <stdio.h>
#define inc(x) ((x)++)
#define square(x) (x * x)
int main()
{
int i = 3;
int j = 4;
printf("%d\n", square(i + j));
printf("%d %d\n", square(inc(i)), i);
}
Explain the result, and how to fix it.
Problem 11:
struct csie {
char c;
short s;
int i;
double e;
};
struct ceis {
char c;
double e;
int i;
short s;
};
int main(void)
{
printf("csie = %d\n", sizeof(struct csie));
printf("ceis = %d\n", sizeof(struct ceis));
return 0;
}
Explain the result.
Hint: this is very very important to the program development in our lab.
Problem 12:
#include <stdio.h>
#include <string.h>
int main(void)
{
char source[] = "This is a string.";
char destination[4];
int i = 5;
strcpy(destination, source);
printf("i is %d\n", i);
printf("source is [%s]\n", source);
printf("destination is [%s]\n", destination);
return 0;
}
What happens and why? How to fix it?
Problem 13: (The examples are given by Ting-Fu Liao.)
/// header.h
#include <stdio.h>
static int val = 0;
void set(int x) ;
/// impl.c
#include "header.h"
void set(int x)
{
val = x ;
}
/// main.c
#include "header.h"
int main() {
set(100);
if ( val == 100 )
printf("val == 100\n");
else
printf("val != 100\n");
return 0;
}
Separate them into three files. What happens and how to fix it?
Problem 14:
#include <stdio.h>
int main()
{
char filename[80];
FILE *fp;
printf("input file name: ");
fgets(filename, 79, stdin);
fp = fopen(filename, "r");
// try assert(fp != NULL);
fclose(fp);
}
Why can’t you open the file? How to fix it.
Hint 14:
See the definition of fgets()
Problem 15:
int main()
{
int i = 2147483647;
unsigned int ui = 2147483647;
if (i + 1 < 0)
printf("i + 1 < 0\n");
if (ui + 1 > 0)
printf("ui + 1 > 0\n");
if (ui + 1 > i + 1)
printf("ui + 1 > i + 1\n");
return 0;
}
why?
Problem 16:
int main()
{
unsigned int ui = 2147483647;
if (ui + 1 > 0)
printf("ui + 1 > 0\n");
if (ui + 1 < -1)
printf("ui + 1 < -1\n");
return 0;
}
why?
Problem 17:
#include <stdio.h>
int main()
{
int i = -13;
if ((i / 2) == (i >> 1))
printf("yes\n");
else
printf("no\n");
return 0;
}
why?
Problem 18:
// for qsort, read http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/
/* qsort example */
#include <stdio.h>
#include <stdlib.h>
int values[] = {-2147483640, 50, 100};
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int main ()
{
int n;
qsort (values, 3, sizeof(int), compare);
for (n = 0; n < 3 ; n++)
printf ("%d ",values[n]);
return 0;
}
What happens and why? How to fix it?
Hint 18:
Integer overflow.
Problem 19:
#include <stdio.h>
#include <assert.h>
int main(void)
{
FILE *fp;
int c;
fp = fopen(__FILE__, "r");
assert(fp != NULL);
while ((c = fgetc(fp)) != EOF)
putchar(c);
fclose(fp);
return 0;
}
What is the output?
use "gcc –E test.c" to see what happens.
Problem 20:
#include <stdio.h>
#define SWAP(x, y) x ^= y ^= x ^= y
main()
{
int i = 3;
int j = 5;
printf("%d\n", i);
printf("%d\n", j);
SWAP(i, j);
printf("%d\n", i);
printf("%d\n", j);
SWAP(i, i);
printf("%d\n", i);
}
What happens and why? Is strange skills good?
Hint 20:
Don't zuosi !
Problem 21:
#include <stdio.h>
main()
{
int i = 3;
i = i++ + ++i;
printf("%d\n", i);
}
Try this in both Visual C++ 6.0 and gcc.
Problem 22:
#include <stdio.h>
int main()
{
int type = 10;
int i =10;
switch (type) {
case 1:
i = 0;
printf("i = %d\n", i);
break;
case 2:
i = 4;
printf("i = %d\n", i);
break;
defualt:
i = 5;
printf("i = %d\n", i);
break;
}
return 0;
}
What happens ? why??? amazing?? easy.
Problem 23:
#include <stdio.h>
int *bar(int t)
{
int i = t;
int *temp = &i;
printf("temp is %d, (*temp) is %d\n", temp, *temp);
return temp;
}
void foo(int a, int b)
{
int i;
int *temp = &i;
*temp = a+b;
}
int main()
{
int *a;
a = bar(10);
printf("a is %d, (*a) is %d \n", a, *a);
foo(10, 20);
printf("a is %d, (*a) is %d \n", a, *a);
}
What happens and how to fix it?
Problem 24:
#include <stdio.h>
int main()
{
char i = 1;
char j;
scanf("%d", &j);
if (i & j)
printf("yes.\n");
else
printf("no.\n");
return 0;
}
Input
3
What happens and how to fix it?
Problem 25:
The sub1 below may result in a run-time error. Why?
int& sub1 ( int& a , int& b ){
int c = a − b ;
return c ;
}
The sub2 below does not result in a run-time error, but there may be some other
problem. What is the problem?
int& sub2 ( int& a , int& b ){
int ∗ pc = new int ;
∗pc = a − b ;
return (∗ pc ) ;
}
Hint 25:
stack & heap
Problem 26:
#include <iostream>
#include <cstdio>
#include <map>
#include <string>
using namespace std;
typedef void(*fn)();
map<string, fn> m;
#define FuncDef(cmd) void cmd_##cmd() { printf("cmd: "#cmd"\n"); }
#define RegFunc(cmd) m[#cmd] = cmd_##cmd;
FuncDef(quit);
FuncDef(help);
int main()
{
RegFunc(quit);
RegFunc(help);
string cmd;
while ( getline(cin,cmd) ){
if ( m.count(cmd) ) (*m[cmd])();
else printf("Not support %s\n", cmd.c_str());
}
return 0;
}
Show the output of the translated program, and run it. Also importantly, you need to give a scenario when/where you would use it in this way. Another small case is as below. (The examples are given by Ting-Fu Liao.)
#define test(x) x _x=a; pr##x##f(#x" %d",_x);
#include <stdio.h>
int main(a)
{
test(int);
return 0;
}
Common Bugs in C Programming的更多相关文章
- 【转】Multithreaded Python Tutorial with the “Threadworms” Demo
The code for this tutorial can be downloaded here: threadworms.py or from GitHub. This code works wi ...
- 06 Frequently Asked Questions (FAQ) 常见问题解答 (常见问题)
Frequently Asked Questions (FAQ) Origins 起源 What is the purpose of the project? What is the history ...
- 6.在MVC中使用泛型仓储模式和依赖注入实现增删查改
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...
- hadoop2.2编程:MRUnit测试
引用地址:http://www.cnblogs.com/lucius/p/3442381.html examples: Overview This document explains how to w ...
- Thinking in Java——笔记(10)
Inner Classes It allows you to group classes that logically belong together and to control the visib ...
- Linux 相关基础笔记
html,body { } .CodeMirror { height: auto } .CodeMirror-scroll { } .CodeMirror-lines { padding: 4px 0 ...
- 【JavaScript】Registering JavaScript object methods as callbacks
The registration of callback functions is very common in JavaScript web programming, for example to ...
- Event-based Asynchronous Pattern Overview基于事件的异步模式概览
https://msdn.microsoft.com/zh-cn/library/wewwczdw(v=vs.110).aspx Applications that perform many task ...
- Java Concurrency - invokeAny & invokeAll
Running multiple tasks and processing the first result A common problem in concurrent programming is ...
随机推荐
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(59)-BLL层重构
系列目录 前言: 这应该是本系统最后一次重构,将重构BLL层和Model层.来完全取代代码生成器生成的BLL层和DAL层.完全废掉了代码生成器的DAL,BLL,MODEL层. 全自动生成增,删,改 ...
- 谁是2016年的.NET开发者?
Nora Georgieva (http://www.telerik.com/blogs/infographic-the-dotnet-developer-of-2016) Whether you h ...
- jquery+ajax+struts2
AJAX 是与服务器交换数据的艺术,它在不重载全部页面的情况下,实现了对部分网页的更新.编写常规的 AJAX 代码并不容易,因为不同的浏览器对 AJAX 的实现并不相同.这意味着您必须编写额外的代码对 ...
- Python 基础之四初识Python数据类型
数字 Int,整型 Float,浮点型 Long,长整型 布尔 字符串 列表 元组 字典 1.数字 INT(整型) 在32位系统上,整数的位数为32位,取值范围为-2**31~2**31-1,即-21 ...
- java中关键字this的使用
在团队代码中看到对于当前类中的方法,使用了this关键字.经过测试发现,在此种情况下,this关键字的使用可有可无.因此,对java中this的使用做下总结: package testTHIS; pu ...
- [连载]《C#通讯(串口和网络)框架的设计与实现》- 12.二次开发及应用
目 录 第十二章 二次开发及应用... 2 12.1 项目配制... 3 12.2 引用相关组件... 4 12.3 构建主程序... 5 ...
- 图片的赖加载(lazyLoad)
懒加载的意义(在线demo预览) 尽管很多公司的网页都有一些限制,比如页面的最大的图片大小不得大于50k,也有很多图片优化工具fis3.gulp等等,但是如果图片太多还是会影响页面的加载速度,快则几十 ...
- 使用 Web API 模拟其他用户
模拟的要求 模拟可代表另一个 Microsoft Dynamics CRM 用户,用于执行业务逻辑(代码)以便提供所需功能或服务,它使用模拟用户的相应角色和基于对象的安全性.这项技术很有必要,因为 M ...
- bootstrap杂记
http://v3.bootcss.com/ 这个国内的中文站点资料很全 如下看个页面的大体框架 http://www.cnblogs.com/sunhaoyu/p/4275190.html < ...
- Android Weekly Notes Issue #228
Android Weekly Issue #228 October 23rd, 2016 Android Weekly Issue #228 本期内容包括: Android 7.1的App Short ...