C语言实例

  对称整数的实例:

运行下面代码

复制代码
#include <stdio.h>
 
int main()
{
     int num, temp, sum=0, N;
     printf("输入一个数: ");
     scanf("%d",&num);
     N = num;
 
     /*对称整数倒排序与原数相等
     */
     while(num > 0)
     {
          temp = num % 10;        /*取最后一位*/
          printf( "____%d____", sum );
          sum = sum * 10 + temp; /*低位与高位互换位置*/
          num /= 10;                   /*移除最后一位*/
     }   
         
     if(N == sum)
          printf("是对称整数\n");
     else
          printf("不是对称整数\n");
}
复制代码

  

  打印三角形1:

运行下面代码

复制代码
#include <stdio.h>
#include <cstdlib>
int main()
{
    int num = 0, temp = 0, temp2 = 0;
    printf("输入一个数 : ");
    scanf("%d", &num);
    temp2 = temp = num%2 == 0 ? num/2 : int(num/2)+1;
    for(int i=0; i<num; i++) {
        if(i<temp) {
            int line = i;
            while( line!=0 ) {
                printf("*");
                line--;
            }
        }else{
            int line = temp2;
            while( line!=0 ) {
                printf("*");
                line--;
            }
            temp2--;
        }
        printf("\n");
    }
}
复制代码

  打印三角形2:

运行下面代码

复制代码
#include <stdio.h>
 
int main()
{
    int num,line;
    int i,temp;
 
    printf("输入一个数:");
    scanf("%d",&num);
 
    for(i = 1; i <= num; i++)
    {
        line = i;      
        while(line--)  
            printf("*");       
        printf("\n");  
    }
 
    for(i = num; i > 0; i--)
    {
        line = i;
        while(line--)
            printf("*");
        printf("\n");  
    }
}
 
复制代码

  用户的输入字符串修改为大写:

运行下面代码

复制代码
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
    char str[20];
    char* p; 
    printf("input chars:  ");
    scanf("%s",str);
    for(p = str; *p!='\0'; p++) {
    //for(p = str; *p!="\0"; p++) { 注意单引号和双引号的意思是不一样的 , 单引号是单字, 双引号代表了多了字符串 
        printf("%c", toupper(*p));
    } 
}
 
复制代码

  把字符串转化为数字:

运行下面代码

复制代码
#include <stdio.h>
#include <stdlib.h>

int main() {
    char str[] = "111";
    int c =  atoi(str);
    printf("%d", c);
    return 0;
}
复制代码

  使用malloc申请内存 , 并修改内存中的数据 :

运行下面代码

复制代码
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10

int main() {
    int sum = 0;
    int* p = (int*) malloc(SIZE*10);
    for(int i=0; i<10; i++) {
        *p = i;
        printf("%d",*p);
        *p = *p+1;
    }
}
 
复制代码

  指针练习:

  数字指针 :

运行下面代码

复制代码
#include <stdio.h>
#include <stdlib.h>
 
void run(char *p) {
    *(p + 2) = '_';         /*错误右边指向的是常量*/
    printf("%s\n",p);  
    //printf的使用上, 如果输出的是数字, 对应的指针要加*, 如果是字符串就不用了;
    int* pp;
    int val = 10;
    pp = &val;
    printf("%d", *pp);
}
int main()
{  
    char ptr[] = "I Love hehe!";
    run(ptr);
    return 0;
}
复制代码

   字符串指针, 字符串的复制 : 

运行下面代码

复制代码
#include <stdio.h>
#include <stdlib.h>

void copy ( char *dest, char *str2 ) {
    printf("%s\n", dest);
    printf("%s\n", str2);
    while(*str2) {
        *dest=*str2;
        dest++;
        str2++;
    };
}
int main()
{
    char str[] = "hehe";
    char str2[] = "result";
    copy(str, str2);
    printf("result : %s\n",str);
    return 0 ;
}
复制代码

运行下面代码

复制代码
#include <stdio.h>
#include <stdlib.h>

void copy ( int *dest, int *num2 ) {
    *dest = *num2;
}
int main()
{
    int num = 1111;
    int num2 = 2222;
    //如果是数字,要手动添加&, 让参数变成指针, 而字符串不需要添加&;
    copy(&num, &num2);
    printf("result : %d\n",num);
    return 0 ;
}
复制代码

   函数指针 :

运行下面代码

复制代码
#include <stdio.h>
#include <stdlib.h>
 
void fn(int);
int main() {
    int x = 10;
    fn(x);
    void (*p)(int);
    p = &fn;
    p(x);
    return 1;
}
void fn(int x) {
    printf("outpub number : %x \n",x);
}
复制代码

 

 

  form : http://blog.chinaunix.net/uid-24219701-id-2912133.html

本文作者:方方和圆圆

本文链接:https://www.cnblogs.com/diligenceday/p/5734847.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   方方和圆圆  阅读(269)  评论(0编辑  收藏  举报
历史上的今天:
2013-08-04 我的防Q+

再过一百年, 我会在哪里?

💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
点击右上角即可分享
微信分享提示