C how to program部分代码摘录
1.Fig2.1 Text printing program
/*Fig.2.1:fig02_01.c
A first program in C*/
#include <stdio.h>
/*function main begins program execution*/
int main(void)
{
printf("Welcome to C!\n");
return 0;/*indicate that program ended successfully*/
}/*end function main*/
2.Fig2.3 Printing on one line with seperate printf statements.
/*Fig.2.3:fig02_03.c
printing on one line with two printf statements*/
#include <stdio.h>
/*function main begins program execution*/
int main(void)
{
printf("Welcome");
printf("to C!\n");
return 0;/*indicate that program ended successfully*/
}/*end fuction main*/
3.Fig2.4 Printing on multiple lines with a single printf.
/*Fig.2.4: fig02_04.c
Printf multiple lines with a single printf */
#include <stdio.h>
/*function main begins program excution*/
int main(void)
{
printf("Welcome\n to\n C!");
return 0;/*indicate that program ended successfully*/
} /*end fuction main*/
4.Fig2.5 Addition program
/*Fig.2.5:fig02_05.c
Addition program*/
#include <stdio.h>
/*function main begins program execution*/
int main(void)
{
int integer1;/*first number to be input by user*/
int integer2;/*second number to be input by user*/
int sum; /*variable in which sum will be stored*/
printf("Enter first integer\n");/*prompt*/
scanf("%d",&integer1); /*read an integer*/
printf("Enter second integer\n");/*prompt*/
scanf("%d",&integer2); /*read an integer*/
sum = integer1 + integer2 ; /*assign total to sum*/
printf("sum is %d\n",sum); /*print sum*/
return 0; /*indicate that program ended successfully*/
} /*end function main*/
5.Fig2.13 Using equality and relational operators.
/*Fig.2.13:fig02_13.c
Using if statements, relational
operators,and equlity operators*/
#include <stdio.h>
/*function main begins program execution*/
int main(void)
{
int num1;/*first number to be read from user*/
int num2;/*second number to be read from user*/
printf("Enter two integer,and I will tell you\n");
printf("the relationship they satisfy:");
scanf("%d%d",&num1,&num2);/*read two integers*/
if(num1==num2){
printf("%d is equal to %d\n",num1,num2);
}/*end if*/
if (num1!=num2)
{
printf("%d is not equal to %d\n",num1,num2);
}/*end if*/
if(num1<num2){
printf("%d is less than %d\n",num1,num2);
}/*end if*/
if (num1>num2)
{
printf ("%d is greater than %d\n",num1,num2);
}/*end if*/
if(num1<=num2){
printf("%d less than or equal to %d\n",num1,num2);
} /*end if*/
if(num1>=num2){
printf("%d is greater than or equal to%d\n",num1,num2);
}/*end if*/
return 0;
}/*end function main*/