#include <stdio.h>
int a = 3;
void f()
{
int a = 1, i;
printf("%d\\n",a);
for(i=0; i<1; i++)
{
int a = 2;
printf("%d\\n", a);
}
}
int main()
{
f();
printf("%d\\n",a);
}
#include <stdio.h>
int inc(int a)
{
return (++a);
}
int multi(int *a, int *b, int *c)
{
return ( *c = *a * *b );
}
int (*p)(int);
void show( int(*fun)(int*, int*, int*), int arg1, int *arg2)
{
p = inc;
int temp = p(arg1);
fun(&temp, &arg1, arg2);
printf("%d\\n", *arg2);
}
int main()
{
int a;
show(multi, 10, &a);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char *src = "hello world";
char *dest = NULL;
int len = strlen(src);
dest = (char *)malloc(len);
char *d = dest;
char *s = &src[len];
while(len-- != 0)
*d++ = *s--;
printf("%s\\n",dest);
free(dest);
return 0;
}
#include <stdio.h> |
| #include <string.h> |
| #include <stdlib.h> |
| void fun(char str[100]) |
| { |
| printf("%d\ ", sizeof(str)); |
| } |
| int main() |
| { |
| char str[] = "Hello"; |
| char *p1 = str; |
| int n = 10; |
| char *p2 = (char *)malloc(100); |
| printf("%d\ ", sizeof(str)); /*求数组长度,包括'\\0'*/ |
| printf("%d\ ", strlen(str)); /*求字符串长度,不包括'\\0'*/ |
| printf("%d\ ", sizeof(p1)); /*求char型指针长度*/ |
| printf("%d\ ", strlen(p1)); /*求字符串长度,不包括'\\0'*/ |
| printf("%d\ ", sizeof(n)); /*求 n 这个整型变量的长度*/ |
| printf("%d\ ", sizeof(p2)); /*求 p2 指针类型长度*/ |
| fun(p2); |
| return 0; |
| } |
| 运行及输出: |
beyes@linux-beyes:~/C> ./sizeof_and_strlen.exe |
| 6 |
| 5 |
| 4 |
| 5 |
| 4 |
| 4 |
| 4 |
| 说明: |
| 此种题在招聘 C 语言软件开发工程师时很常见。主要测试对 sizeof() 和 strlen() 的理解。 |
| 第一个输出为 6,测的是 str 这个数组的长度,包括末尾的 '\\0'. |
| 第二个输出为 5,测的是字符串自身,不包括结尾的 '\\0'. |
| 第三个输出为 4,由于 p1 是一个指针,sizeof() 求的是变量类型的长度。一个指针变量存放的是一个地址,而 32 位机器的一个地址都是 32 位即 4 个字节。 |
| 第四个输出为 5,测的是 p1 指向的字符串的长度,和第二个一样。 |
| 第五个输出为 4,一个整型变量的存储长度一般一般是 4 个字节,早期的机器使用 2 个字节来保存 int 型变量。 |
| 第六个输出为 4, 这里同样求的是指针变量 p2 的长度,和第三个输出一样。 |
| 第七个输出为 4,这里调用了 fun 函数。因为数组作为函数的参数时,对系统来说,char str[100] 和 char *str 是一样的。 |
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char a;
char *str = &a;
strcpy(str, "hello");
printf("%s\\n", str);
return 0;
}
| str = (char *) malloc( strlen("hello") + 1 ); |
| free(str); |
| int length = sizeof( array ) / sizeof( array[0] ); |
| char a[10] = "hello"; int len = strlen(a); |
#include <stdio.h>
int main(void)
{
int **p;
int array[ 100 ];
p = &array;
return 0;
}
beyes@linux-beyes:~/C> gcc -c temp.c
temp.c: In function ‘main’:
temp.c:8: warning: assignment from incompatible pointer type
#include <stdio.h>
int main(void)
{
int **p, *q;
int array[ 100 ];
q = array;
p = &q;
return 0;
}
#include <stdio.h> |
| #include <string.h> |
| void reverse(char *p) |
| { |
| if(*p == '\\0' ) |
| return; |
| reverse(p + 1); |
| printf("%c", *p); |
| } |
| int main() |
| { |
| char str[100]; |
| int len; |
| char *p; |
| printf("请输入字符串:"); |
| fgets(str, 100, stdin); |
| len = strlen(str); |
| if(str[len - 1] == '\ ') |
| str[len-1] = '\\0'; |
| p = str; |
| reverse(p); |
| printf("\ "); |
| return 0; |
| } |
| 说明: |
| 程序中,之所以用 fgets() 而不用 gets() 是因为前者是个安全的函数,而后者因为不检查边界,所以很危险,不使用。 |
#include <stdio.h>
#define MAX 255
int main()
{
unsigned char a[MAX], i;
for(i=0; i<=MAX; i++)
printf("%d ", a[i]);
printf("\\n");
return 0;
}
| char *strcpy(char *dest, char *src) |
char *strcpy(char *dest, char *src)
{
char *ret_string;
if ( ( dest == NULL ) || ( src == NULL ) ) {
printf("arg wrong");
return NULL;
}
ret_string = dest;
while( ( *dest++ = *src++) ) != '\\0' );
return ret_string;
}
| 欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |