#include <stdio.h>
#define TWO 2
#define OW "Consistency is the last refuge of the unimagin\ tive. -Oscar Wilde"
#define FOUR TWO * TWO
#define PX printf("X is %d.\n",x)
#define FMT "X is %d.\n"
int main(void )
{
int x = TWO;
PX;
x =FOUR;
printf(FMT ,x);
printf("%s \n", OW);
printf("TWO : OW\n");
return 0;
}
#include <stdio.h>
#define SQUARE(X) X*X
#define PR(X) printf("the result is %d \n",X)
void main()
{
int x = 4;
int z ;
printf("x = %d \n ",x );
z =SQUARE(x);
printf("evaluating square(2) : ");
PR(z);
z =SQUARE(2);
printf("evaluating square(X+2) : ");
PR(SQUARE(x+2));
PR(z);
printf("evaluating 100/square(2) : ");
PR(100/SQUARE(x+2));
printf("x is %d \n",x);
printf("EValuating square(++x)");
PR( SQUARE(++x));
printf("%d\n",x);
}
##运算符
#include <stdio.h>
#define XNAME(n) x ## n
#define PRINT_XN(n) printf("x" #n " = %d \n " ,x ## n);
void main(void)
{
int XNAME(1) = 14;
int XNAME(2) = 20;
PRINT_XN(1);
PRINT_XN(2);
}
//
// names.c
// 10以内的四则运算
//
// Created by xiaofeng on 18/12/16.
// Copyright © 2018年 xiaofeng. All rights reserved.
//
#define SLEN 32
struct names_st
{
char first[SLEN];
char last[SLEN];
};
#include <stdio.h>
#include "names.h"
void get_name(names *pn)
{
int i ;
printf("please enter your first name");
fgets(pn -> first, SLEN, stdin);
i =0;
while (pn -> first[i] != '\n' && pn -> first[i] != '\0') {
i ++;
}
if (pn -> first[i] == '\n') {
pn->first[i];
}
else
while (getchar() != '\n') {
continue;
}
printf("please enter your last name");
fgets(pn -> last, SLEN, stdin);
while (pn -> last[i] != '\n' && pn -> last[i] != '\0') {
i++;
}
if (pn -> last[i] == '\n') {
pn->last[i] = '\0';
}
else
while (getchar()!= '\n') {
continue;
}
}
void show_names(const names *pn)
{
printf("%s %s \n ",pn ->first,pn -> last);
}
#include"names.h"
#include"names.c"
#include <stdio.h>
void main()
{
names candidat;
get_names(&candidat);
printf("Let 's welcome ");
show_names(&candidat);
printf("to this program !\n");
}
//
// names.h
// 10以内的四则运算
//
// Created by xiaofeng on 18/12/16.
// Copyright © 2018年 xiaofeng. All rights reserved.
//
struct names_st;
typedef struct names_st names;
void get_names(names *);
void show_names(const names *);
//void get_name(names *pn);
使用条件编译
#include <stdio.h>
#define JUST_CHECKING 1
#define LIMIT 4
void main()
{
int i ;
int total = 0;
for (i = 1 ; i <= LIMIT; i ++) {
total +=2*i*i +1 ;
#if JUST_CHECKING
printf("i = %d ,running total = %d \n ", i, total);
#endif
}
printf("Grand total = %d \n ", total);
}
#include<stdio.h>
#include<stdlib.h>
void why_me();
void main()
{
printf( " THe file is %s \n",__FILE__);
printf( " THe date is %s \n",__DATE__);
printf( " THe file is %s \n",__FILE__);
printf( " THe file is %s \n",__FILE__);
printf( " THe TIME is %s \n",__TIME__);
//printf( " THe VERSION is %d \n",__STDC_VERSION__);
printf( " THe line is %d \n",__LINE__);
//printf( " THe funciton is %s \n",__func__);
why_me();
system("pause");
}
void why_me()
{
printf( " THe line is %d \n",__LINE__);
}
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define RAD_TO_DEG (180/(4*atan(1)))
typedef struct polar_v
{
double magitude;
double angle;
}POLAR_V;
typedef struct rect_v
{
double x;
double y;
}RECT_V;
POLAR_V rect_to_polar(RECT_V rv);
void main()
{
RECT_V input;
POLAR_V result;
puts("enter x,y coordinates enter q to quit ");
while (scanf("%lf %lf",&input.x,&input.y)==2)
{
result = rect_to_polar( input);
printf("magnitude = %0.2f,angle = %0.2f\n",result.magitude,result.angle);
}
puts("bye");
}
POLAR_V rect_to_polar(RECT_V rv)
{
POLAR_V pv;
pv.magitude = sqrt(rv.x * rv.x + rv.y*rv.y);
if(pv.magitude == 0)
pv.angle = 0.0;
else
{
pv.angle = RAD_TO_DEG * atan2(rv.y, rv.x);
}
return pv;
}
#include <stdio.h>
#include <stdlib.h>
void sign_off (void );
void too_bad (void );
void main()
{
int n ;
atexit (sign_off);
puts("enter an integer");
if (scanf ("%d",&n) != 1)
{
puts("THAT NO integer");
atexit(too_bad);
exit(EXIT_FAILURE);
}
printf("%d is %s \n" ,n ,( n % 2 == 0) ? "even" : "odd");
system("pause");
}
void sign_off()
{
puts (" thus terminates another agnificetnt program from");
puts ("seesaw software ");
}
void too_bad()
{
puts (" seesaw software extends its heartfelt condolences ");
puts (" to you upon the failure of your program ");
}
#include <stdio.h>
#include <stdlib.h>
#define NUM 40
void fillarray (double ar[],int n);
void showarray (const double ar[],int n);
int mycomp (const void *p1 ,const void *p2);
void main()
{
double vals[NUM];
fillarray (vals,NUM);
puts("random list ");
showarray(vals,NUM);
qsort(vals,NUM,sizeof(double),mycomp);
puts("\n SOrted list" );
showarray(vals,NUM);
system("pause");
}
void fillarray (double ar[],int n)
{
int index;
for ( index = 0; index < n; index++)
{
ar[index] = (double)rand()/((double)rand()+0.1);
}
}
void showarray (const double ar[],int n)
{
int index;
for ( index = 0; index < n; index++)
{
printf("%9.4f",ar[index]);
if (index % 6 ==5)
{
putchar('\n');
}
}
if (index %6 != 0)
{
putchar('\n');
}
}
int mycomp(const void *p1,const void * p2)
{
const double *a1 = (const double *) p1;
const double *a2 = (const double *) p2;
if(*a1 < *a2)
{
return -1;
}
else if (*a1 == *a2)
{
return 0;
}
else
{
return 1;
}
}
使用 assert()
#include <stdio.h>
#include <math.h>
#include <assert.h>
void main()
{
double x, y,z;
puts("enter a pair of numbers (0 0 to quit)");
while (scanf("%lf%lf",&x,&y)== 2 && (x != 0 || y != 0)) {
z = x * x -y *y ;
assert(z >= 0);
printf("answer is %f \n",sqrt(z));
puts("Next pair of numbers ");
}
puts("Done");
}
使用memcpy 和 memove 函数
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 10
void show_array(const int ar[] ,int n);
void main()
{
int values[SIZE] = { 1,2,3,4,5,6,7,8,9,10};
int target[SIZE];
double curious[SIZE/2] = {1.0,2.0,3.0,4.0,5.0};
puts("memcpy() used: ");
puts("values( original data ) ");
show_array(values, SIZE);
memcpy(target, values, SIZE * sizeof(int));
puts("target (copy of values ");
show_array(target, SIZE);
puts("\n using memmove () with verlapping ranges :");
memmove(values +2 , values, 5*sizeof(int));
puts("alues elements 0-5 copied to 2-7 ");
show_array(values, SIZE);
puts(" U sing memcpy () to copy double to int :");
memcpy(target, curious , (SIZE/2) * sizeof(double));
show_array(target, SIZE);
}
void show_array(const int ar[] ,int n)
{
int i;
for (i = 0; i < n ; i ++) {
printf("%d",ar[i]);
}
putchar('\n');
}
#include <stdio.h>
#include <stdarg.h>
double sum (int , ...);
int main(void)
{
double s, t;
s =sum (3,1.1,2.5,13.3);
t =sum(6,1.1,2.1,13.1,4.1,5.1,6.1);
printf("return value for " "sum (3,1.1,2.5 ,13.3)\n %g\n",s);
printf("return value for " "sum (6,1.1,2.1,13.1,4.1,5.1,6.1)\n%g\n",t);
}
double sum(int lim,...)
{
va_list ap;
double tot = 0;
int i;
va_start(ap, lim);
for (i = 0 ; i < lim; i ++) {
tot +=va_arg(ap, double);
}
va_end(ap);
return tot;
}
网友评论