e******d 发帖数: 310 | 1 I want to sort strings using qsort(), and try the following code, but doesn
't work. Could you please help me out. Thank you.
=============================================
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
int mystrcmp(const void * str1, const void * str2)
{
return strcmp( (const char*)str1, (const char*)str2 ) ;
}
int main()
{
int n = 4;
char *pstr[] = {"God", "Bed", "Act", "Cup"};
qsort(pstr, n, sizeof(char*), mystrcmp);
int i = 0;
for(i = 0; |
|
s*******e 发帖数: 664 | 2 ☆─────────────────────────────────────☆
petersam (google) 于 (Fri Oct 2 16:06:00 2009, 美东) 提到:
我尝试用intel 9 编译器在vc 6.0的环境里编译openmp, 但其中一个线程老是被重复
执行, 不知道为什么? 有谁遇到过类似的问题吗?
☆─────────────────────────────────────☆
petersam (google) 于 (Fri Oct 2 16:36:24 2009, 美东) 提到:
以下是我的测试代码:
#include "stdio.h"
#include "omp.h"
int main(){
int i;
omp_set_num_threads(2);
#pragma omp parallel for
for(i = 0; i < 6; i++ )
printf("i = %d\n", i);
return 0;
}
☆───────────────────────────────────── |
|
s*******e 发帖数: 664 | 3 ☆─────────────────────────────────────☆
flytomar (撒旦归来) 于 (Mon Nov 23 19:31:10 2009, 美东) 提到:
一直没搞定这个。
另外一个程序,假设名字abc,在同一个路径,有四个参数。
我应该怎么调用它呢?查了google,用execl但是不工作啊
execl(pathname, "abc", arg1, arg2, arg3, arg4, NULL);
是这样用吗?
谢谢
☆─────────────────────────────────────☆
pptwo (pp) 于 (Mon Nov 23 19:34:26 2009, 美东) 提到:
just use system, or popen if you want to write/read stdio
☆─────────────────────────────────────☆
flytomar (撒旦归来) 于 (Mon Nov 23 19:39:09 2009, 美东) 提到:
system 可以调用我自己的pro |
|
R****a 发帖数: 199 | 4 I think the major part is no problem. The following is my code and it works
by g++.
<<>
#include
class A
{
public:
void DoSomething() {printf("Inside! \n");}
};
void foo(A *a)
{
int i;
for(i=0;i<10;i++)
a[i].DoSomething();
}
int main()
{
A a[10];
foo(a);
}
build by command:
g++ try1.cc -o try1.out
run try1.out
<>
# ./try1.out
Inside!
Inside!
Inside!
Inside!
Inside!
Inside!
Inside!
Inside!
Inside!
Inside!
Do you have the compile error? You m |
|
w***g 发帖数: 5958 | 5 原来C++的main是特殊编译的!
试了一下发现真是的。下面一段程序用gcc编译返回值为20,用g++编译返回值为0。太
牛了!
#include
int foo () {
return 20;
}
int main (int argc, char *argv[]) {
foo();
} |
|
x****u 发帖数: 44466 | 6 测试用的VC版程序:
#include
#include
/*volatile */unsigned int global_var;
DWORD WINAPI func(void* arg) {
unsigned int x=0;
long long i;
global_var = 1;
for (i=0; i<10000000000L; i++) {
x=x*2;
x+= global_var;
}
printf("%d\n", x);
return 0;
}
DWORD WINAPI func1(void* arg) {
global_var=2;
return 0;
}
int main()
{
HANDLE t1, t2;
t1 = C |
|
g****y 发帖数: 436 | 7 heihei, in my test:
gcc: -O1
total loops:2000000000
Time for threaded call:00:00:02
Time for non-threaded call:00:00:03
codes:
#include "threading.h"
#include "timer.h"
#include
#include
#include
int M;
void* cal1(void* vp) {
int flag1 = 0;
for (int n = 0; n < M; n++) {
flag1 = 1;
}
}
void* cal2(void* vp) {
int flag2 = 0;
for (int n = 0; n < M; n++) {
flag2 = 1;
}
}
void* cal0(void* vp) {
int flag2 = 0;
for (int n = 0; n < M; n++) {
fla |
|
y***d 发帖数: 2330 | 8 发现下面这个程序 gcc 在不同的优化条件下给出不同的结果,看起来是个 gcc 的 bug;
gcc -v
Using built-in specs.
Target: x86_64-linux-gnu
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
#include
int f(unsigned int x)
{
return ((x>>31)||((0-x)>>31));
//*(2*(x>>31) + 1);
}
int main()
{
printf("%d\n", f(1));
printf("%d\n", f(0));
printf("%d\n", f(-1));
return 0;
}
不优化时,
gcc 1.c -Wall
./a.out
1
0
1
优化时,
gcc 1.c -Wall -O
./a.out
1
1
1 |
|
d**e 发帖数: 6098 | 9 【 以下文字转载自 JobHunting 讨论区 】
发信人: done (not done yet), 信区: JobHunting
标 题: C的argc问题
发信站: BBS 未名空间站 (Sat Oct 2 22:21:08 2010, 美东)
我用的是ubuntu, gcc 4.2.4
------------------------
#include
int main(int argc, char * argv[])
{
printf("argc = %d\n", argc);
return 0;
}
------------------------
比如我传入一个参数,输出是 2,正确
但如果我传入的是"*"符号,结果是 13 ... 这是为什么呢?
似乎只要有"*",argc至少就是13了。
谢谢
$ ./test 12 * 3
argc = 15
$ ./test *
argc = 13
$./test 12
argc = 2 |
|
p*****u 发帖数: 711 | 10 但如果不定义B::print.
B obj;
obj.print就会调用A::print, 是否可以这样理解,调用一个obj.print()时,先在
class
B中找print(),如果没有定义,再在class B的supclass 即class A中找? 函数调用时
这个查
找是如何实现的?
#include
class A{
public:
void print();
};
void A::print() {
cout << "A.print" << endl;
}
class B : public A {
public:
void print();
};
int main() {
B obj;
obj.print();
return 0;
}
给出的error msg是
In function `main':
undefined reference to `B::print()'
说明在B中定义print()之后,A的print就被屏蔽了?compiler在定义B::print()时,做
了什么
事情?
望大侠们解惑。。。 |
|
a****o 发帖数: 686 | 11 tolower(char c)
C++ and C both have a version of tolower, do not mix them.
most likely, you will need a template to wrap it if you have to include
stdio.h in C++. |
|
|
S******n 发帖数: 489 | 13 如下代码,为什么会显示segmentation fault呢。我是用g++编译的。请问这个是为什么
呢?资源不够么?
#include
#include
#define NODE 2000
int main()
{
int graph[NODE + 1][NODE + 1];
int node[NODE + 1];
int nodeCheck[NODE + 1];
for (int i = 1; i <= NODE; i ++)
{
for (int j = 1; j <= NODE; j ++)
{
graph[i][j] = 2;
}
}
return 1;
} |
|
X****r 发帖数: 3557 | 14 右边第一行应该是1->2,不是1->1吧。
我不懂perl,不过这个程序实在简单,用C写一个也就几行:
int expand(int input[], int output[][2], int start) {
int branch, end = start + 1;
for (branch = 0; branch < input[start]; ++branch) {
output[start][branch] = end;
end = expand(input, output, end);
}
return end;
}
验证程序:
#include
#include
#define MAX 100
int main(int argc, char *argv[]) {
int input[MAX], output[MAX][2] = {0};
int i;
for (i = 1; i < argc; ++i) {
input[i] = atoi(argv[i]);
}
printf... 阅读全帖 |
|
n****u 发帖数: 467 | 15 来自主题: Programming版 - 帮忙找个错 #include
int main(){
char *s="test";
sprintf(s,"t");
printf("%s\n",s);
}
Bus error。C 的小例子,是先初始化,然后想用sprintf重新赋值。 |
|
r****o 发帖数: 1950 | 16 【 以下文字转载自 InterviewHackers 俱乐部 】
发信人: roufoo (五经勤向窗前读), 信区: InterviewHackers
标 题: 请教如何修正这个C程序的bug。
发信站: BBS 未名空间站 (Fri Dec 17 03:28:43 2010, 美东)
这个程序是网上直接copy下来的。程序很简单,是将输入的字符串变成integer。
#include
int main(void)
{
int sum = 0, i = 0;
char input[5];
while (1) {
sum = 0;
scanf("%s", input);
for (i = 0; input[i] != '\0'; i++)
sum = sum*10 + input[i] - '0';
printf("input=%d\n", s... 阅读全帖 |
|
b********e 发帖数: 58 | 17 The following code (I know it's ugly) seems to do the job.
Tested with gcc under cygwin.
#include
#include
void add(int);
void finish(int);
void (*p[2])(int x);
int sum = 0;
int main(int argc, char **argv)
{
p[0] = add;
p[1] = finish;
doit(atoi(argv[1]));
return(0);
}
int doit(int n)
{
int next;
sum += n;
next = n-1;
(*p[next==0])(next);
}
void add(int x)
{
doit(x);
}
void finish(int x)
{
printf("sum = %d\n", sum);
exit(0);
} |
|
y**********0 发帖数: 425 | 18 编写函数int index(char *s, char *t),返回字符串t 在字符串s中出现的最左边的
位置,
如果在s中没有与t匹配的子串,就返回-1。
#include
#include
int index(char *s,char *t)
{
int i,j,k;
for(i=0;s[i]!='\0';i++)
for(j=i,k=0;t[k]!='\0' && s[j]==t[k];j++,k++);
if(t[k]=='\0')
return i;
return -1;
}
void main()
{
int n;
char str1[20],str2[20];
gets(str1);
gets(str2);
n=index(str1,str2);
cout<
}
始终得不到正确答案,是Index的问题。
答案的Index程序是这样的:得到了正确的答案。不知道为什么?
int ... 阅读全帖 |
|
c**b 发帖数: 2999 | 19 都用到c++了,怎么还用一些c的东西? 比如那个index()2个参数,直接用array不就行了,
为什么要用指针呢.尽管这样也对,但是太复杂.简单点,干脆用pass by reference,连指针也不用了.答案确实是对的而且构思很简单.我能想到的算法比答案差点,但是逻辑更清晰些:
int index(char s[],char t[])
{
int i,j,k,foundIt=-1;
for(i=0;s[i] != '\0';i++)
for (j=i,k=0;t[k] != '\0';j++,k++)
if (s[j] != t[k])
foundIt = -1;
else foundIt = i;
return foundIt;
}
编写函数int index(char *s, char *t),返回字符串t 在字符串s中出现的最左边的
位置,
如果在s中没有与t匹配的子串,就返回-1。
#inc... 阅读全帖 |
|
r***e 发帖数: 2000 | 20 为什么数组传不出来?谢谢!
1 #include
2 #include
3 #include
4
5 void pastr(char * comm)
6 {
7 comm = (char *)malloc(3);
8 strcpy(comm, "hw");
9 }
10
11 int main() {
12
13 char *comm;
14
15 pastr(comm);
16 printf("..comm..%s\n", comm);
17
18 return 0;
19 } |
|
r***e 发帖数: 2000 | 21
请看这段程序(运行结果在下面)。为什么hello没出来但是world可以出来?
#include
#include
#include
void pastr(char * comm, char ** para)
{
comm = (char *)malloc(9);
para[0] = (char *)malloc(9);
strcpy(comm, "hello");
strcpy(para[0], "world");
}
int main() {
char *comm, *para[]={NULL};
pastr(comm, para);
printf("..comm..%s.. para[0].. %s\n", comm, para[0]);
return 0;
}
$ ./a.out
..comm..(null).. para[0].. world |
|
c**y 发帖数: 172 | 22 Anyone can explain why the second output is 0, rather than 1.
========================================
#include
#include
#include
using namespace std;
int main() {
const int x = 0;
int const *py = &x;
int *px = const_cast(&x);
cout << *px << endl; // output 0
*px = 1;
cout << x << endl; // output 0, I don't understand
cout << *px << endl; // output 1
cout << *py << endl; ... 阅读全帖 |
|
X****r 发帖数: 3557 | 23 Try this yourself:
sizeof_long.c:
#include
int main() {
printf("sizeof(long) = %d\n", (int)sizeof(long));
}
gcc -m32 sizeof_long.c
./a.out
gcc -m64 sizeof_long.c
./a.out |
|
b********f 发帖数: 3 | 24 GCC编译无错,运行提示
"SEGMENTATION FAULT(CORE DUMPED)"
#include
int main()
{
char *buf;
unsigned int vel_h = 0x01;
unsigned int vel_l = 0xB159;
unsigned int temp = 0x01FD;
unsigned int pres = 0x004C;
unsigned int cal1 = 0x3340;
unsigned int cal2 = 0x1030;
unsigned int range = 0x01;
sprintf(buf,"%02X,%04X,%04X,%04X,%04X,%04X,%02X",vel_h,vel_l,temp,pres,
cal1,cal2,range);
printf("%s\r\n",buf);
return 0;
} |
|
h********8 发帖数: 7355 | 25 C里,得自己declare 一个global mythis pointer指向global foo。
#include
int foo = 100;
int * mythis=&foo;
int bar()
{
int foo;
/* local foo = global foo, how to implemented? */
foo=*mythis;
return 0;
} |
|
B**F 发帖数: 38 | 26 太初级了只好披马甲来问。
如下程序,如何把example文件中的数字位数一位不少地读入?
试了lf不管用:
0, -0.999714 0.000734634
1, -0.998492 0.00170939
2, -0.996295 0.00268393
多谢指教了!(以下代码中“n"显示不出来)
gfortran, Mac OS X 10.6.7
#include
void main(void)
{
double Z1[3], Z2[3];
int i;
FILE *input= fopen ("example", "r");
for (i=0; i<3; ++i) {
fscanf(input, "%lf %lfn", &Z1[i], &Z2[i]);
printf("%i, %g %gn", i, Z1[i], Z2[i]);}
fclose(input);
return 0;
}
文件"example"内容:
-0.99971372677344128 ... 阅读全帖 |
|
d**d 发帖数: 389 | 27 我用linux下面的POSIX timer, timer_create(),timer_settime(),
为什么在调用了timer_settime()以后,立马就有一个time-out callback? 然后再每过
5秒后有一个time out?
难道不是我调用timer_settime()以后,timer开始计时, 等到5秒以后再出现第一
time out callback 吗?
非常感谢!
代码如下:
#include
#include
#include
#include
#include
void
handle (sigval_t v)
{
time_t t;
char p[32];
time (&t);
strftime(p,sizeof(p),"%T ",localtime(&t));
printf("%s thread 0x%x,val=%d,signal captured.... 阅读全帖 |
|
j*******e 发帖数: 674 | 28 下面code在Linux2.6,gcc下运行:
$ ./a.out
Floating point exception (core dumped)
问题:
1. 已经设了process signal mask, 40行产生的SIGFPE应该被屏蔽
2. 32行安装了signal handler, 40行产生的SIGFPE应该被capture
为什么运行结果是直接core dump?
如果注释掉 “z=x/y", 改用“raise(SIGFPE)", 运行结果就符合预期。
难道“z=x/y"在这里不是rasie SIGFPE 吗?
=============================
#include
#include
#include
void sig_handler(int signum)
{
printf("sig_handler() received signal %d\n", signum);
}
int main(int argc, char * argv[])
{
// setup signal... 阅读全帖 |
|
f******5 发帖数: 11 | 29 Thanks so much.
The compiler I am using is Dev c++ 4.9.9.2
Can I use New operator inside LCS like follows? It shows more compiling time
error?
Or should I declare c[][] outside the helping function LCS or inside Main()
function?
I want to pass the arrayc[][] and b[][] at the end of LCS to another Helping
function called
Print_LCS.
From your point of view, where should I declare the c[][] and b[][]
If i use New operator as follows to declare and initialize c[][], c[][],
both c[][] and b[][] go ou... 阅读全帖 |
|
b********r 发帖数: 1080 | 30 【 以下文字转载自 Computation 讨论区 】
发信人: bankbuster (恭喜发财), 信区: Computation
标 题: C++里用Blas/Lapack的问题
发信站: BBS 未名空间站 (Tue Aug 9 14:57:49 2011, 美东)
我用atlas,程序里简单调用zgeev函数。编译没有错,没有警告。运行也不出错,但结
果完全不对。似乎函数根本没有被调用。
另外哪里能找到在C或者C++下调用Blas/Lapack函数的具体格式?我这里函数参数还是
在网上搜的。完全没有相关的手册。难道Blas/Lapack只是给fortran用的?
程序如下
#include
#include
#include
#include
#include
using namespace std;
typedef complex dcomplex;
extern "C" void zgeev_( char* jobvl, char* jobvr, int* n, ... 阅读全帖 |
|
s******s 发帖数: 505 | 31 The C code blow can be used to read a floating point number
representation. However, line 8 will not work with g++
(invalid conversion from ‘void*’ to ‘float*’ [-fpermissive]).
What is the proper way to do this in C++?
Thank you.
========
1 #include
2
3 int main() {
4 float *numfp;
5 int numi;
6
7 numi = 1082130432;
8 numfp = (void *)&numi;
9 printf("%f\n", *numfp);
10 return 0;
11
12 } |
|
t****t 发帖数: 6806 | 32 let me show you an example:
/* 1.c */
#include
void bar(int* a, float* b)
{
a[5]=200;
b[5]=1.0;
printf("%d\n", a[5]);
printf("&a[5]=%p &b[5]=%p\n", a+5, b+5);
}
/* 2.c */
void bar(int*, float*);
int main()
{
int a[10];
float* b=(float*)a;
bar(a, b);
}
What do you think the result is?
compile with -O2 and -O0, you get different results:
$ gcc -O0 1.c 2.c
$ a.out
1065353216
&a[5]=0x7fffb3828964 &b[5]=0x7fffb3828964
$ gcc -O2 1.c 2.c
$ a.out
200
&a[5]=0x7fff2e7... 阅读全帖 |
|
d****n 发帖数: 1637 | 33 Sorry being a noobie.
this should be what I was talking about.
the 1st version was wrong.
#######################################
#include
void swap(void **firstPtr, void **secondPtr)
{
void *temp = *firstPtr;
*firstPtr = *secondPtr;
*secondPtr = temp;
return;
}
int main()
{
int a = 10;
int b = 20;
int *aPtr = &a;
int *bPtr = &b;
printf("Before Swapping, Address : %x %x, Value : %d %d\n", aPtr, bPtr,
*
aPtr, *bPtr);
swap(&aPtr, &bPtr);
printf("After Swapping, Address : %x %x, Value : %... 阅读全帖 |
|
d****n 发帖数: 1637 | 34 没那么恐怖。
给你一个例子。再屏幕中间打印时间。
#include
#include
#include
#include
main( )
{
initscr( );
int i=0;
char buffer [80];
while(1){
clear();
move( LINES/2 - 1, COLS/2 - 4 );
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo=localtime(&rawtime);
strftime(buffer, 80, "%I:%M:%S%p.", t... 阅读全帖 |
|
d****n 发帖数: 1637 | 35 狗来的:
#include
int add(int a, int b)
{
if (!a)
return b;
else
return add((a & b) << 1, a ^ b);
}
int main()
{
unsigned int a,b;
printf("Enter the two numbers: \n");
scanf("%d",&a);
scanf("%d",&b);
printf("Sum is: %d",add(a,b));
}
|
|
d****n 发帖数: 1637 | 36 char *p="abc"; 在代码段,immutable.
any write operation will cause segfault.
char a[]="abc"; stack, read/write okay
#include
#include
int main(){
char *p="abc";
printf("%p\n",p );
*(p+1)='w'; //segfault
free(p); //segfault or
glibc fault
p =(char *) malloc (sizeof(char)*4) ; //okay, but previous "abc" lost
printf("%p\n",p );
char a[]="def";
printf("%p\t%s\n",a,a); ... 阅读全帖 |
|
t*****n 发帖数: 4908 | 37 照楼主的测试例子我用dgemv()和dsum()改写了一下。没有减少循环层次。另外用
gotoblas做blas库。在我的athlon双核机器上速度是gcc -03两倍。
那位在linux上跑一下看看。多谢
#include
#include
#include
#include
#define size 2000
int main(void)
{
double *m;
double x[size], k[size];
long begin, end;
double sum = 0.0;
int i, j;
begin = time(0);
m = malloc(size * size * sizeof(*m));
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
m[i * size + j] = i + j;
... 阅读全帖 |
|
z*y 发帖数: 1311 | 38 我有一个C语言程序
在Linux上可以编译,正常执行(只用stdio和stdlib)
在Windows编译,执行出错
先用MinGW的gcc
然后为了debug,又放到VC Studio里
报错:stack overflow
会是什么问题?
谢谢 |
|
d****n 发帖数: 1637 | 39 /*only works for small number */
/*die when integer overflow*/
#include
#include
int bico(int n, int k){
int t, i ,c ;
if (k>n) t=n, n=k, k=t;
if (k<0 ) return 0 ;
if (k>(n-k)) k=n-k;
c=1;
for(i=0;i
c*=(n-(k-(i+1))) , c/=(i+1);
return c ;
}
int main(int argc, char * argv[] ){
if(argc!=3) return -1;
int c;
c=bico(atoi(argv[1]) , atoi(argv[2]) ) ;
printf("bionomial %d\n", c);
} |
|
d****n 发帖数: 1637 | 40 /*working with larger numbers but still die */
/**C(80, 20) is okay, which was not ok in previous version**/
#include
#include
#include
float
gammln (float xx)
//Returns the value ln[Ãxx)] for xx > 0.
{
//Internal arithmetic will be done in double precision, a nicety that
//you can omit if five-figure
//accuracy is good enough.
double x, y, tmp, ser;
static double cof[6] = { 76.18009172947146, -86.50532032941677,
24.01409824083091, -1.231739572450155,
... 阅读全帖 |
|
d****n 发帖数: 1637 | 41 /*only works for small number */
/*die when integer overflow*/
#include
#include
int bico(int n, int k){
int t, i ,c ;
if (k>n) t=n, n=k, k=t;
if (k<0 ) return 0 ;
if (k>(n-k)) k=n-k;
c=1;
for(i=0;i
c*=(n-(k-(i+1))) , c/=(i+1);
return c ;
}
int main(int argc, char * argv[] ){
if(argc!=3) return -1;
int c;
c=bico(atoi(argv[1]) , atoi(argv[2]) ) ;
printf("bionomial %d\n", c);
} |
|
d****n 发帖数: 1637 | 42 /*working with larger numbers but still die */
/**C(80, 20) is okay, which was not ok in previous version**/
#include
#include
#include
float
gammln (float xx)
//Returns the value ln[Ãxx)] for xx > 0.
{
//Internal arithmetic will be done in double precision, a nicety that
//you can omit if five-figure
//accuracy is good enough.
double x, y, tmp, ser;
static double cof[6] = { 76.18009172947146, -86.50532032941677,
24.01409824083091, -1.231739572450155,
... 阅读全帖 |
|
d****n 发帖数: 1637 | 43 Sorry something was wrong from googled.
here is my test.
#include
#include
int main(){
char const * p ="Hello";
char q[]="York";
printf("%p\n",p);
p=q; //okay
printf("%p\n",p);
const char *k="two";
printf("%p\n",k);
k=q; // okay
printf("%p\n",k);
char * const s="one";
printf("%p\n",s);
s=q; //segfault
printf("%p\n",s);
} |
|
d****n 发帖数: 1637 | 44 my shabby code for questions 4.
//File resovle.c
#include
#include
#include
#define MAXLINECHARSIZE 1000
inline int fsplit(char *line, char **items, char delim);
int main(int argc, char * argv[]){
/***prepare && allocate buffer/space***/
int n,nbytes=100, maxcol=10, maxchar=1000;
char **items;
{
items=(char **) malloc(maxcol*sizeof(char *));
int i;
for(i=0; i
items[i]=(char*)malloc(maxchar*sizeo... 阅读全帖 |
|
d****n 发帖数: 1637 | 45 C面试题
~~~~~~~~~~~
1.使用 #define 定义一个值为一年的秒数的常量,不考虑润年。
~~~~~~~~~~~
2.使用 #define 定义一个返回两个数中较小的一个的宏。
~~~~~~~~~~~~
3.将变量a定义成如下类型:
1. 有符号整数
2. 双精度浮点数
3. 指向一个有符号整数的指针
4. 一个十个成员的有符号整数数组
5. 一个函数指针,指向的函数返回类型为有符号整数,有一个有符号整数类型的参数
~~~~~~~~~~~~
4.C语言中的static的用处是?
~~~~~~~~~~~~
5. 写出下面函数被调用时的输出。
void foo(void)
{
unsigned int a = 6;
int b = -20;
(a+b > 6) ? puts("> 6") :
puts(" < = 6");
}
~~~~~~~~~~~~
6.写出下面程序的输出
#include
#include
typedef struct
{
char flag;
int value;
}SampleSt... 阅读全帖 |
|
l***t 发帖数: 10 | 46 let me try:
~~~~~~~~~~~
1.使用 #define 定义一个值为一年的秒数的常量,不考虑润年。
#define sec_per_year (60*60*24*365)UL
~~~~~~~~~~~
2.使用 #define 定义一个返回两个数中较小的一个的宏。
#define MIN(a,b) ((a)<=(b)?(a):(b))
~~~~~~~~~~~~
3.将变量a定义成如下类型:
1. 有符号整数
int a;
2. 双精度浮点数
double a;
3. 指向一个有符号整数的指针
int *a;
4. 一个十个成员的有符号整数数组
int a[10];
5. 一个函数指针,指向的函数返回类型为有符号整数,有一个有符号整数类型的参数
int (*a)(int);
~~~~~~~~~~~~
4.C语言中的static的用处是?
~~~~~~~~~~~~
5. 写出下面函数被调用时的输出。
void foo(void)
{
unsigned int a = 6;
int b = -20;
(a+b > 6) ? puts("> 6") :
puts... 阅读全帖 |
|
d****n 发帖数: 1637 | 47 okay,没代码没真相。
here is my shabby codes and I want to share with you all.
output and analysis will be post soon
//file name matrix_op.c
#include
#include
#include
#include
#include
#include
#define _MX_INIT(type_t, row, col) ({ int i;\
type_t ** ret;\
ret=(type_t ** )malloc( row *sizeof(type_t*));\
for(i=0;i
(ret);\
})
#defin... 阅读全帖 |
|
d****n 发帖数: 1637 | 48 use macro , no need to calculate the constant
~~~~~~
#include
#define SETN( x ,n) ((x)|=(1<<(n)))
#define UNSETN(x, n) ((x)&=~(1<<(n)))
int main(){
unsigned short int i=1;
SETN(i, 9) ;
printf("%d\n",i);
UNSETN(i, 9) ;
printf("%d\n",i);
}
///output
513
1 |
|
z*****n 发帖数: 447 | 49 比如:
#include
class f{
public:
virtual void p(){printf("father\n");}
};
class c : public f{
public:
void p(){
printf("child\n");
}
};
--------------------------------------
Derived Class 调用Base Class可以这样:
f *a = new c;
a->f::p(); // 这里Print的是"father"
---------------------------------------
如果
f *b = new f;
b怎样才可以调用到 c的method p()
多谢! |
|
z*****n 发帖数: 447 | 50 比如:
#include
class f{
public:
virtual void p(){printf("father\n");}
};
class c : public f{
public:
void p(){
printf("child\n");
}
};
--------------------------------------
Derived Class 调用Base Class可以这样:
f *a = new c;
a->f::p(); // 这里Print的是"father"
---------------------------------------
如果
f *b = new f;
b怎样才可以调用到 c的method p()
多谢! |
|