C的小程序集合

1. The Hello World Program

/*世界上最流行的程序代码,hello.c*/

#include <stdio.h>

int main(void){

printf(“hello world !\n”);

}

2.计算2^n,注意排列的方式,左边是n的值从1到16,右边是2^n

#include <stdio.h>
#define N 16

int main(void){
    int n;
    int val = 1;

    printf("\tn                  \t2^n\n");
    printf("\t==========================\n");
    for(n = 0 ; n < N ; n++){
        printf("\t%d                  \t%d\n",n , val);
        val = 2*val;
    }
    return 0 ;
}

 

3.利用printf来输出一个大G的形状

#include <stdio.h>

void blockg(void);

int main(void){
    blockg();
    return 0 ;
}

void blockg(void){
    printf("ggggggg\n");
    printf("g     g\n");
    printf("g \n");
    printf("g  gggg\n");
    printf("g     g\n");
    printf("ggggggg\n");
}

输出的效果:G

4.求和,输入两个整数,相加

#include "stdio.h"
int main(void){
    int a, b;
    while(scanf("%d%d", &a, &b)!=EOF)
        printf("%d\n", a+b);
}

5.输入N个数,然后求和

#include <stdio.h>

int main(void){
    int n , sum , lcv , current;
    printf("please enter a positive number : ");
    scanf("%d",&n);
    sum = 0 ;
    printf("please enter an integer:");
    for(lcv = 0 ; lcv < n ; lcv++){
        scanf("%d",&current);
        sum = sum + current;
    }
    printf("%d",sum);
    return 0 ;
}

 

6.零钱硬币变美元

/* FILE: coins.c
* DETERMINES THE VALUE OF A COIN COLLECTION
* A Variation of the Hanly/Koffman book's example
*/

#include <stdio.h>

void main ()
{
   int pennies;              // input: count of pennies
   int nickels;              // input: count of nickels
   int dimes;                // input: count of dimes
   int quarters;             // input: count of quarters
   int temp, left;           // temporaries for various

   printf("Enter the number of quarters, dimes, nickels, and pennies: ");
   scanf("%d %d %d %d", &quarters, &dimes, &nickels, &pennies);

   left = 25 * quarters + 10 * dimes + 5 * nickels + pennies;

   printf("Your collection is worth\n ");
   temp = left / 100;
   printf("\t%d dollar", temp);
   if (temp==1)
      printf(", ");
   else
      printf("s, ");
   left = left % 100;

   temp = left / 25;                 //整除
   printf("%d quarter", temp);
   if (temp==1)
      printf(", ");
   else
      printf("s, ");
   left = left % 25;                 //取余数

   temp = left / 10;
   printf("%d dime", temp);
   printf ((temp==1) ? ", " : "s, ");
   left = left % 10;

   temp = left / 5;
   printf("%d nickel", temp);
   if (temp==1)
      printf(", and ");
   else
      printf("s, and ");           //是否复数
   left = left % 5;

   printf("%d penn", left);
   if (left==1)
      printf("y\n");
   else
      printf("ies\n");
}

7.真为1,假为0

#include <stdio.h>

int main(void){
    printf("the value of 1<2 is %d\n" , (1<2));
    printf("the value of 1>2 is %d\n" , (1>2));
    return 0;
}

8.fibonacci.c

#include <stdio.h>

int main(void) {
    int n;        /* The number of fibonacci numbers we will print */
    int i;        /* The index of fibonacci number to be printed next */
    int current;  /* The value of the (i)th fibonacci number */
    int next;     /* The value of the (i+1)th fibonacci number */
    int midval;  /* The value of the (i+2)th fibonacci number */

    printf("How many Fibonacci numbers do you want to compute? ");
    scanf("%d", &n);
    if (n<=0)
       printf("The number should be positive.\n");
    else {
      printf("\n\n\tI \t Fibonacci(I) \n\t=====================\n");
      next = current = 1;
      for (i=1; i<=n; i++) {
    printf("\t%d \t   %d\n", i, current);
    midval = current+next;
    current = next;
    next    = midval;
      }
    }
}

9.作用域

#include <stdio.h>
int x = 2;
int y = 3;
int z = 4;
void moo(int x, int *y){
  int z;
  x = x+3;
  *y = *y+3;
  z = z+3;  /*z在moo函数内部被重新的定义,所以z的值不能取变量z,但是在我们的日常编程的实践中,函数内部的变量名尽
量不要和全局变量重合, 避免一些不必要的烦恼*/
  printf("moo :  x = %1d, *y = %1d, y = %1d, z = %1d\n", x,*y,y,z);//z = 3,int类型的初始值0
}
int main(void){
  moo(x, &y);
  printf("main: x = %1d1, y = %1d, z = %1d\n", x,y,z);//z为全局变量
}
10.最简单的数组操作
#include <stdio.h>

int main(void) {
  int a[2] = {1,2}; 
  int b[2] = {2,3};
  int i;

  for(i=0;i<2;i++)
    a[i]=b[i];
  if(a==b)
    printf("They are equal\n");
  else
    printf("They are not equal\n");
//a和b不相等,要知道a,b之间的赋值只是指针的指向发生了变化,而不是a数组编程了b数组,质的变化是没有的。两个数组在内
存中的位置是不一样的。虽然他们现在储存的指针式一样的,但是系统相互比较的是他们的位置而不是值
    if(a==a)
    printf("Of course a is equal to a\n");
  else
    printf("No, a is not equal to a\n");
  
  for(i=0;i<2;i++)
    printf("a[%1d] = %3d\n", i, a[i]);
}
 11.预处理变量和变量的长度

#include <stdio.h>

int main(void){
    int answer;
    short s = 1;
    long l = 2;
    float f = 3.0;
    double d = 4.4;
    long double ld = 5.55;
    char c = 'p';
     /*预处理器的威力*/
     printf("Date : %s\n", __DATE__);//系统日期
     printf("Time : %s\n", __TIME__);//系统时间
     printf("File : %s\n", __FILE__);//文件名
     printf("Line : %d\n", __LINE__);//表示此行的行数
     printf("Enter 1 or 0 : ");
     scanf("%d", &answer);

     /* 当你选择1时,为yes,0时,为no */
     printf("%s\n", answer?"You sayd YES":"You said NO");

      /* 系统变量的长度 */
     printf("The size of int %d\n", sizeof(answer));
     printf("The size of short %d\n", sizeof(s));
     printf("The size of long %d\n", sizeof(l));
     printf("The size of float %d\n", sizeof(f));
     printf("The size of double %d\n", sizeof(d));
     printf("The size of long double %d\n", sizeof(ld));
     printf("The size of char %d\n", sizeof(c));
}

 s 

12.变量,值,地址之间的关系

#include <stdio.h>

void moo(int a , int *b);
int main(void){
    int x;
    int *y;

    x = 1;
    y = &x;

    printf("Address of x = %d, value of x = %d\n", &x, x);
    printf("Address of y = %d, value of y = %d, value of *y = %d\n", &y, y, *y);//y中存储的地址是x的地址,*y可以访问到x的值。&y,y本身的地址
    moo(9,y);
    return 0;
}
void moo(int a ,int *b){
    printf("Address of a = %d, value of a = %d\n", &a, a);
    printf("Address of b = %d, value of b = %d, value of *b = %d\n", &b, b, *b);//&b本身的地址
}

/* 输出来的结果:

Address of x = 536869640, value of x = 1

Address of y = 536869632, value of y = 536869640, value of *y = 1
Address of a = 536869608, value of a = 9
Address of b = 536869600, value of b = 536869640, value of *b = 1
*/

13.利用int值和%c来显示acsii码

#include <stdio.h>

int main(void){
    int c ;
    for(c = 32 ; c < 127 ; c++){
    printf("\t %c                  %d\n", c, c);
    }
    return 0;
}

14.求你输入的字符串的长度和内容

#include <stdio.h>
#define BUFFER 128
int getLine(char line[], int nmax);
int main(void){
    int len;
    char buffer[BUFFER];

    while(1){
        len = getLine(buffer,BUFFER );
        if(len ==0){break;}
        printf("len = %d,line = %s\n",len,buffer);
    }
}

int getLine(char line[], int nmax){
    int len;
    char c;

    len =0;
    printf("Enter a string [CR to exit]:");
    while((c = getchar()) !='\n'&& len < nmax-1){
        line[len++] = c;
    }
    line[len] = '\0';
    return len;
};

15.思考一个问题

#include <stdio.h>
#define NPRIMES  1000
#define FALSE 0
#define TRUE 1

int main(void){
    int n ;
    int i;
    int primes[NPRIMES];

    printf("请你输入N值:\n");
    scanf("%d",&n);
    for(i = 0 ; i < n;i++){
        primes[i] = i++;//两段代码的唯一区别
        printf("%d\n",primes[i]);
    }
    return 0;
}

 

#include <stdio.h>
#define NPRIMES  1000
#define FALSE 0
#define TRUE 1

int main(void){
    int n ;
    int i;
    int primes[NPRIMES];

    printf("请你输入N值:\n");
    scanf("%d",&n);
    for(i = 0 ; i < n;i++){
        primes[i] = i;
        printf("%d\n",primes[i]);
    }
    return 0;
}

以上两段代码执行的结果一样吗??

结果是不一样的,下面这个是可以正常的打出我们希望的值,但是上面只打出一堆未被赋值的初始值,原因出在i++身上,用debug跟踪,发现i++的赋值正好是隔行赋值,跳着赋值,但是printf是慢了一个节拍,显示是未被赋值的数组元素。

posted @ 2010-04-05 20:33  云端小飞象cg  阅读(500)  评论(0编辑  收藏  举报