提交 e2b25f3a 编写于 作者: B Ben

Tutorial appendix

上级 f82df1da
#include <stdlib.h>
#include <stdio.h>
void double_in(int *in){
*in *= 2;
}
int main(){
int x= 10;
double_in(&x);
printf("x is now %i.\n", x);
}
#include <assert.h>
int main(){
int list[100];
int *list2 = list; //Declares list2 as a pointer-to-int,
//pointing to the same block of memory list points to.
*list2 = 7; //list2 is a pointer-to-int, so *list2 is an int.
assert(list[0] == 7);
}
#include <stdio.h>
int main(){
printf("3./5=%g\n", 3./5);
printf("3/5=%i\n", 3/5);
printf("3%%5=%i\n", 3%5);
}
#include <stdio.h>
void loops(int max){
int i=0;
do {
printf("Hello #%i\n", i);
i++;
} while (i < max); //Note the semicolon.
}
int main(){
loops(3); //prints three greetings
loops(0); //prints one greeting
}
#include <stdio.h>
void error_print(FILE *ef, int error_code, char *msg){
fprintf(ef, "Error #%i occurred: %s.\n", error_code, msg);
}
int main(){
FILE *error_file = fopen("example_error_file", "w"); //open for writing
error_print(error_file, 37, "Out of karma");
}
#include <stdio.h>
int main(){
for (int i=0; i < 10; i++){
printf("Hello #%i\n", i);
}
}
#include <stdio.h>
int main(){
for (int i=0; i < 10; i++) printf("Hello #%i\n", i);
}
#include <stdio.h>
int main(){
printf("Hello, world.\n");
}
#include <stdio.h>
int main(){
if (6 == 9)
printf("Six is nine.\n");
int x=3;
if (x==1)
printf("I found x; it is one.\n");
else if (x==2)
printf("x is definitely two.\n");
else
printf("x is neither one nor two.\n");
}
#include <stdio.h>
int intlist[10];
int main(){
int len=20;
char string[len];
intlist[7] = 7;
snprintf(string, 20, "Item seven is %i.", intlist[7]);
printf("string says: <<%s>>\n", string);
}
CFLAGS=-g -Wall -std=gnu11 -O3
LDLIBS=-lm
#include <stdlib.h> //malloc and free
#include <stdio.h>
int main(){
int *intspace = malloc(3000*sizeof(int));
for (int i=0; i < 3000; i++)
intspace[i] = i;
FILE *cf = fopen("counter_file", "w");
for (int i=0; i < 3000; i++)
fprintf(cf, "%i\n", intspace[i]);
free(intspace);
fclose(cf);
}
#include <stdlib.h>
#include <stdio.h>
void double_in(int *in){
*in *= 2;
}
int main(){
int *x= malloc(sizeof(int));
*x= 10;
double_in(x);
printf("x now points to %i.\n", *x);
}
#include <stdio.h>
typedef struct {
int numerator, denominator;
double value;
} ratio_s;
ratio_s new_ratio(int num, int den){
return (ratio_s){.numerator=num, .denominator=den, .value=num/(double)den};
}
void print_ratio(ratio_s r){
printf("%i/%i = %g\n", r.numerator, r.denominator, r.value);
}
ratio_s ratio_add(ratio_s left, ratio_s right){
return (ratio_s){
.numerator=left.numerator*right.denominator
+ right.numerator*left.denominator,
.denominator=left.denominator * right.denominator,
.value=left.value + right.value
};
}
int main(){
ratio_s twothirds= new_ratio(2, 3);
ratio_s aquarter= new_ratio(1, 4);
print_ratio(twothirds);
print_ratio(aquarter);
print_ratio(ratio_add(twothirds, aquarter));
}
#include <stdio.h>
typedef struct {
int numerator, denominator;
double value;
} ratio_s;
int main(){
printf("size of two ints: %zu\n", 2*sizeof(int));
printf("size of two ints: %zu\n", sizeof(int[2]));
printf("size of a double: %zu\n", sizeof(double));
printf("size of a ratio_s struct: %zu\n", sizeof(ratio_s));
}
#include <math.h> //The square root function is declared here.
#include <stdio.h>
int main(){
double x = 49;
printf("The truncated square root of x is %g.\n",
x > 0 ? sqrt(x) : 0);
}
#include <stdio.h>
int main(){
double pi= 3.14159265; //POSIX defines the constant M_PI in math.h, by the way.
int count= 10;
printf("%g times %i = %g.\n", pi, count, pi*count);
}
#include <stdio.h>
int main(){
int i=0;
while (i < 10){
printf("Hello #%i\n", i);
i++;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册