码迷,mamicode.com
首页 > 其他好文 > 详细

Learn C

时间:2019-04-24 22:07:46      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:minutes   init   erb   object   pes   -o   meta   func   2.0   

Introduce C

A Simple Example of C

#include<stdio.h>
int main(void)  /*a simple program */
{
    int num;    /* define a variable called num */
    num = 1;    // assign a value to num
    
    printf("I am a simple ");   // use the printf() function
    printf("computer.\n");
    printf("My favorite number is %d because it is the first.\n", num);
    
    return 0;
}

Declarations

#include<stdio.h>
int main(void)  // traditional rules
{
    int doors;
    int dogs;
    doors = 5;
    dogs = 3;
    // other statements
}

#include<stdio.h>
int main(void)  // current C rules
{
    // some statements
    int doors;
    doors = 5;  // first use of doors
    // more statements
    int dogs;
    dogs = 3;   // first use of dogs
    // other statements
}

The Structure of a Simple Program

A function has a header and a body

#include<stdio.h>
int main(void) // function name with arguements
{
int q; // declaration statement
q = 1; // assignment statement
printf("%d is neat.\n", q);    // function statement

return 0;
}

Multiple Functions

/* a program using two functions in one file */
#include<stdio.h>
void bulter(void);    /* ANSI/ISO C function prototyping */
int main(void)
{
printf("I will smmon the butler function.\n");
bulter();
printf("Yes. Bring me some tea and written DVDs.\n");

return 0;
}
?
void bulter(void)    // start of funtion definition
{
printf("You rang, sir?\n");
}

Data and C

A Simple Program

#include<stdio.h>
int main(void)
{
float weight; // user weight
float value; // platinum equivalent

printf("Are you worth your weight in platinum?\n");
printf("Let‘s check it out.\n‘");
printf("Please enter your weight in pounds:");

// get input from the user
scanf("%f", &weight);
// assume platinum is $1700 per ounce
// 14.5833 converts pounds avd to ounces troy
value = 1700.0 * weight * 14.5833;
printf("Your weight in platinum is worth $%.2f.\n", value);
printf("You are easily worth that! If platinum prices drop,\n");
printf("eat more to maintain your value.\n");

return 0;
}

Declaring an integer Variable

#include<stdio.h>
int main(void)
{
int erns;
int hogs, cows, goats;

erns = 1;
hogs = 2;
cows = hogs;
goats = erns;
}

Initializing a Variable

#include<stdio.h>
int main(void)
{
int hogs = 21;
int cows = 32, goats = 14;    // well
int dogs, cat = 99;    // valid, but poor, form
}

Printing integer Values

/* displays some properties of printf() */
#include<stdio.h>
int main(void)
{
	int ten = 10;
	int two = 2;
	
	printf("Doing it right: ");
	printf("%d minus %d is %d\n", ten, 2, ten - two);
	
	printf("Doing it wrong:");
	printf("%d minus %d is %d\n", ten);    // forget 2 arguments
	
	return 0;
}

/*
Doing it right: 10 minus 2 is 8
Doing it wrong:10 minus -1422108384 is 6480512
*/

Displaying Octal and Hexadecimal

/* prints 100 in decimal, octal, and hex */
#include<stdio.h>
int main(void)
{
	int x = 100;
	
	printf("dec = %d; octal = %o; hex = %x\n", x, x, x);
	printf("dec = %d; octal = %#o; hex = %#x\n", x, x, x);
	
	return 0;
}
/*
dec = 100; octal = 144; hex = 64
dec = 100; octal = 0144; hex = 0x64
*/

Integer Overflow

/* maximum int size on our system */
#include<stdio.h>
int main(void)
{
	int i = 2147483647;
	unsigned int j =  4294967295;
	
	printf("%d %d %d\n", i, i+1, i+2);
	printf("%u %u %u\n", j, j+1, j+2);
	
	return 0;
}

/*
2147483647 -2147483648 -2147483647
4294967295 0 1
*/

Printing short, long, long long and unsigned types

/* more printf() properties */
#include <stdio.h>
int main(void)
{
	unsigned int un = 3000000000; /* system with 32-bit int */
	short end = 200; /* and 16-bit short */
	long big = 65537;
	long long verybig = 12345678908642;
	
	printf("un = %u and not %d\n", un, un);
	printf("end = %hd and %d\n", end, end);
	printf("big = %ld and not %hd\n", big, big);
	printf("verybig= %lld and not %ld\n", verybig, verybig);
	
	return 0;
}

/*
un = 3000000000 and not -1294967296
end = 200 and 200
big = 65537 and not 1
verybig= 12345678908642 and not 1942899938
*/

Printing Characters

/* displays code number for a character */
#include<stdio.h>
int main(void)
{
	char ch;
	
	printf("Please enter a character.\n");
	scanf("%c", &ch);
	printf("The code for %c is %d.\n", ch, ch);
	
	return 0;
}

/*
	Here is a sample run:
	Please enter a character.
	C
	The code for C is 67.
*/

Printing Floating-Point Values

/* displays float value in two ways */
#include<stdio.h>
int main(void)
{
	float aboat = 32000.0;
	double abet = 2.14e9;
	long double dip = 5.32e-5;
	
	printf("%f can be written %e\n", aboat, aboat);
	printf("And it‘s %a in hexadecimal, powers of 2 notation\n", aboat);
	printf("%f can be written %e\n", abet, abet);
	printf("%Lf can be written %Le\n", dip, dip);
	
	return 0;
}

/*
32000.000000 can be written 3.200000e+004
And it‘s 0x1.f40000p+14 in hexadecimal, powers of 2 notation
2140000000.000000 can be written 2.140000e+009
0.000000 can be written 3.205284e-317
*/

Floating-Point Round-off Errors

/* demonstrates round-off error*/
#include<stdio.h>
int main(void)
{
	float a, b;
	
	b = 2.0e20 + 1.0;
	a = b - 2.0e20;
	printf("%f \n", a);
}

/*
4008175468544.000000
*/

Type Sizes

/* prints out type sizes */
#include<stdio.h>
int main(void)
{
	/* c99 provides a %zd specifier for sizes */
	printf("Type int has a size of %d bytes.\n", sizeof(int));
	printf("Type char has a size of %d bytes.\n", sizeof(char));
	printf("Type longh has a size of %d bytes.\n", sizeof(long));
	printf("Type long long has a size of %d bytes.\n", sizeof(long long));
	printf("Type double has a size of %d bytes.\n", sizeof(double));
	printf("Type long double has a size of %d bytes.\n", sizeof(long double));
	
	return 0;
}

/*
Type int has a size of 4 bytes.
Type char has a size of 1 bytes.
Type longh has a size of 4 bytes.
Type long long has a size of 8 bytes.
Type double has a size of 8 bytes.
Type long double has a size of 16 bytes.
*/

One More Example: Escape Sequence

/* uses escape characters */
#include<stdio.h>
int main(void)
{
	float salary;
	
	printf("\aEnter your desired monthly salary:");
	printf(" $_______\b\b\b\b\b\b\b");
	scanf("%f", &salary);
	printf("\n\t$%.2f a month is $%.2f a year.", salary, salary*12.0);
	printf("\rGee!\n");
	
	return 0;
}

Character Strings and Formatted Input/Output

Introductory Program

// nosy, informative program
#include<stdio.h>
#include<string.h>	// for strlen() prototype
#define DENSITY 62.4	// human density in lbs per cu ft
int main(void)
{
	float weight, volume;
	int size, letters;
	char name[40];    // name is an array of 40 chars
	
	printf("Hi! What‘s your first name?\n");
	scanf("%s", name);
	printf("%s, What‘s your weight in pounds?\n", name);
	scanf("%f", &weight);
	size = sizeof name;
	letters = strlen(name);
	volume = weight / 	DENSITY;
	printf("Well, %s, your volume is %2.2f cubic feet.\n", name, volume);
	printf("Also, your first name has %d letters,\n", letters);
	printf("and we have %d bytes to store it.\n", size);
	
	return 0;
	
}

Operators, Expressions, and Statements

Introducing Loops

// converts a shoe size to inches
#include<stdio.h>
#define ADJUST 7.31    // one kind of symbolic constant
int main(void)
{
	const double SCALE = 0.33;    // another kind of symbolic constant
	double shoe, foot;
	
	shoe = 9.0;
	foot = SCALE * shoe + ADJUST;
	printf("Shoe size (men‘s)    foot length\n");
	printf("%10.1f %15.2f inches\n", shoe, foot);
	
	return 0;
}
// calculates foot lengths for several sizes
#include<stdio.h>
#define ADJUST 7.31    // one kind of symbolic constant
int main(void)
{
	const double SCALE = 0.333;    // another kind of symbolic constant
	double shoe, foot;
	
	printf("Shoe size (men‘s)    foot length\n");
	shoe = 3.0;
	while (shoe < 18.5)    /* starting the while loop */
	{                      /* start of block */
		foot = SCALE * shoe + ADJUST;
		printf("%10.1f %15.2f inches\n", shoe, foot);
		shoe = shoe + 1.0;
	}                      /* end of block */
	printf("If the shoe fits, wear it.\n");
	
	return 0;
}

Some Terminology: Data Objects, Lvalues, Rvalues, and Operands

// golf tournament scorecard
#include<stdio.h>
int main(void)
{
	int jane, tarzan, cheeta;
	
	cheeta = tarzan = jane = 68;
	printf("		       cheeta  tarzan    jane\n");
	printf("First round score   %4d  %8d  %8d\n", cheeta, tarzan, jane);
	
	return 0;
}

Multiplication Operator:*

/* produces a table of first 20 squares */
#include<stdio.h>
int main(void)
{
	int num = 1;
	
	while (num < 21)
	{
		printf("%4d %6d\n", num, num * num);
		num = num + 1;
	}
	
	return 0;
}
/* exponential growth */
#include<stdio.h>
#define SQUARES 64    // squares on a checkerboard
int main(void)
{
	const double CROP = 2E16;    // worl wheat production in wheat grains
	double current, total;
	int count = 1;
	
	printf("square    grains    total    ");
	printf("fraction of\n");
	printf("          added      grains  ");
	printf("world total\n");
	total = current = 1.0;    // start with one grain
	printf("%4d %13.2e %12.2e %12.2e\n", count, current, total, total/CROP);
	while (count < SQUARES) {
		count = count + 1;
		current = 2.0 * current ;
		/* double grains on next square */
		total = total + current;  //update total
		printf("%4d %13.2e %12.2e %12.2e\n", count, current, total, total/CROP);
	}
	printf("That is all.\n");
	
	return 0;
}

Division Operator:/

// divisions we have known
#include<stdio.h>
int main(void)
{
	printf("integer division: 5/4 is %d \n", 5/4);
	printf("integer division: 6/3 is %d \n", 6/3);
	printf("integer division: 7/4 is %d \n", 7/4);
	printf("floating division: 7./4. is %1.2f \n", 7./4.);
	printf("mixed division: 7./4 is %1.2f \n", 7./4);
	printf("mixed division: 7/4. is %1.2f \n", 7/4.);
	
	return 0;
}

Precedence and the Order of Evaluation

/* precedence test */
#include<stdio.h>
int main(void)
{
	int top, score;
	top = score = -(2 + 5) * 6 + (4 + 3 * (2 + 3));
	printf("top = %d, score = %d\n", top, score);
	
	return 0;
}

The sizeof Operator and the size_t Type

// uses sizeof operator
// uses C99 %z modifier -- try %u or %lu if you lack %zd
#include<stdio.h>
int main(void)
{
	int n = 0;
	size_t intsize;
	
	intsize = sizeof (int);
	printf("n = %u, n has %u bytes; all this have %u bytes.\n",
			n, sizeof n, intsize);
	
	return 0;
}

Modulus Operator: %

// converts seconds to minutes and seconds
#include<stdio.h>
#define SEC_PER_MIN 60    // seconds in a minute
int main(void)
{
	int sec, min, left;
	
	printf("Convert seconds to minutes and seconds!\n");
	printf("Enter the number of seconds (<=0 to quit):\n");
	scanf("%d", &sec);
	while (sec > 0) {
		min = sec / SEC_PER_MIN;  // truncated number of minutes
		left = sec % SEC_PER_MIN;  // number of seconds left over
		printf("%d seocnds is %d minutes, %d seconds.\n", sec, min, left);
		printf("Enter next value (<=0 to quit):\n");
		scanf("%d", &sec);
	}
	printf("Done!\n");
	
	return 0;
}

Incrementing:++

// incrementing: prefix and postfix
#include<stdio.h>
#define ADJUST 3.0
int main(void)
{
	int ultra = 0, super = 0;
	
	while (super < 5){
		super++;
		++ultra;
		printf("super = %d, ultra = %d\n", super, ultra);
	}
	
	float shoe = 3.0;
	double foot, size = 4.5;
	const double SCALE = 2.0;
	while (++shoe < 18.5) {
		foot = SCALE * size + ADJUST;
		printf("%10.1f %20.2f inches\n", shoe, foot);
	}
	
	return 0;
}
// postfix vs prefix
#include<stdio.h>
int main(void)
{
	int a = 1, b = 1;
	int a_post, pre_b;
	
	a_post = a++;    // value of a++ during assignment phase 
	pre_b = ++b;    // value of ++b during assignment phase
	printf("a  a_post    b  pre_b \n");
	printf("%1d %5d %5d %5d\n", a, a_post, b, pre_b);
	
	return 0;
}
/*
a  a_post    b  pre_b
2     1     2     2
*/

Decrementing: --

#include<stdio.h>
#define MAX 100
int main(void)
{
	int count = MAX + 1;
	
	while (--count > 0) {
		printf("%d bottles of spring water on the wall," 
						"%d bottles of spring water!\n", count, count);
		printf("Take one down and pass it around, \n");
		printf("%d bottles of spring water!\n\n", count-1);
	}
	
	return 0;
}

Expression and Statement

/* five kinds of statements */
#include<stdio.h>
int main(void)
{
	int count, sum;    // declaration statement
	
	count = 0;    // assignment statement
	sum = 0;
	while (count++ < 20){
		sum = sum + count;    // statement
		printf("sum = %d\n", sum);    // function statement	
	}
	
	return 0;    // return statement
}

Type Conversions

/* automatic type conversion */
#include<stdio.h>
int main(void)
{
	char ch;
	int i;
	float fl;
	
	fl = i = ch = ‘C‘;
	printf("ch = %c, i = %d, fl = %2.2f\n", ch, i, fl);
	ch = ch + 1;
	i = fl + 2 * ch;
	fl = 2.0 * ch + i;
	printf("ch = %c, i = %d, fl = %2.2f\n", ch, i, fl);
	ch = 1107;
	printf("Now ch = %c\n", ch);
	ch = 80.89;
	printf("Now ch = %c\n", ch);
	
	return 0;
}
/*
ch = C, i = 67, fl = 67.00
ch = D, i = 203, fl = 339.00
Now ch = S
Now ch = P
*/
/* the cast operator */
#include<stdio.h>
int main(void)
{
	int mice;
	
	mice = (int) (1.6 + 1.7);
	printf("mice = %d\n", mice);
	mice = (int) 1.6 + (int) 1.7;
	printf("mice = %d\n", mice);
	
	return 0;
}

Function with Arguments

/* defines a function with an argument */
#include<stdio.h>
void pound(int n);    // ANSI function prototype declaration
int main(void)
{
	int times = 5;
	char ch = ‘!‘;    // ASCII code is 33
	float f = 6.0f;
	
	pound(times);    // int argument
	pound(ch);      // same as pound((int)ch)
	pound(f);       // same as pound((int)f)
	
	return 0;
}

void pound(int n)    // ANSI-style function header
{                    // says takes one int arguemnt
	while (n-- > 0)
		printf("#");
	printf("\n");
}
/*
#####
#################################
######
*/

 

Learn C

标签:minutes   init   erb   object   pes   -o   meta   func   2.0   

原文地址:https://www.cnblogs.com/pankypan/p/10765241.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!