C语言是一门通用计算机编程语言,应用广泛。下面是小编整理的C语言基础知识大全,希望对大家有帮助!
1、C语言打印一条语句源代码:
/* C Program to print a sentence. */
#include
int main()
{
printf("C Programming"); /* printf() prints the content inside quotation */
return 0;
}
输出:
C Programming
2、C语言打印用户输入的一个整数#include
int main()
{
int num;
printf("Enter a integer: ");
scanf("%d",&num); /* Storing a integer entered by user in variable num */
printf("You entered: %d",num);
return 0;
}
输出:
Enter a integer: 25You entered: 25
3、C语言实现两个整数相加
/*C programming source code to add and display the sum of two integers entered by user */
#include
int main( )
{
int num1, num2, sum;
printf("Enter two integers: ");
scanf("%d %d",&num1,&num2); /* Stores the two integer entered by user in variable num1 and num2 */
sum=num1+num2; /* Performs addition and stores it in variable sum */
printf("Sum: %d",sum); /* Displays sum */
return 0;
}
输出:
Enter two integers: 1211Sum: 23
4、C语言实现两个小数相乘/
*C program to multiply and display the product of two floating point numbers entered by user. */
#include
int main( )
{
float num1, num2, product;
printf("Enter two numbers: ");
scanf("%f %f",&num1,&num2); /* Stores the two floating point numbers entered by user in variable num1 and num2 respectively */
product = num1*num2; /* Performs multiplication and stores it */
printf("Product: %f",product);
return 0;
}
输出:
Enter two numbers: 2.41.1Product: 2.640000
5、C语言查找字符的ASCII值/
* Source code to find ASCII value of a character entered by user */
#include
int main(){
char c;
printf("Enter a character: ");
scanf("%c",&c); /* Takes a character from user */
printf("ASCII value of %c = %d",c,c);
return 0;
}
输出:
Enter a character: GASCII value of G = 71
6、C语言根据用户输入的整数做商和余数
/* C Program to compute remainder and quotient */
#include
int main(){
int dividend, divisor, quotient, remainder;
printf("Enter dividend: ");
scanf("%d",÷nd);
printf("Enter divisor: ");
scanf("%d",&divisor);
quotient=dividend/divisor; /* Computes quotient */
remainder=dividend%divisor; /* Computes remainder */
printf("Quotient = %d",quotient);
printf("Remainder = %d",remainder);
return 0;
}
输出:
Enter dividend: 25Enter divisor: 4Quotient = 6Remainder = 1
7、C语言获取整型、单精度浮点型、双精度浮点型和字符型的长度基本语法:
/* This program computes the size of variable using sizeof operator.*/
#include
int main(){
int a;
float b;
double c;
char d;
printf("Size of int: %d bytes",sizeof(a));
printf("Size of float: %d bytes",sizeof(b));
printf("Size of double: %d bytes",sizeof(c));
printf("Size of char: %d byte",sizeof(d));
return 0;
}
输出:
Size of int: 4 bytesSize of float: 4 bytesSize of double: 8 bytesSize of char: 1 byte
8、C语言获取关键字long的长度范围
#include
int main(){
int a;
long int b; /* int is optional. */
long long int c; /* int is optional. */
printf("Size of int = %d bytes",sizeof(a));
printf("Size of long int = %ld bytes",sizeof(b));
printf("Size of long long int = %ld bytes",sizeof(c));
return 0;
}
输出:
Size of int = 4 bytesSize of long int = 4 bytesSize of long long int = 8 bytes
9、C语言交换数值
#include
int main(){
float a, b, temp;
printf("Enter value of a: ");
scanf("%f",&a);
printf("Enter value of b: ");
scanf("%f",&b);
temp = a; /* Value of a is stored in variable temp */
a = b; /* Value of b is stored in variable a */
b = temp; /* Value of temp(which contains initial value of a) is stored in variable b*/
printf("After swapping, value of a = %.2f", a);
printf("After swapping, value of b = %.2f", b);
return 0;
}
输出:
Enter value of a: 1.20Enter value of b: 2.45After swapping, value of a = 2.45After swapping, value of b = 1.2
10、C语言检查数值是奇数还是偶数
/*C program to check whether a number entered by user is even or odd. */
#include
int main(){
int num;
printf("Enter an integer you want to check: ");
scanf("%d",&num);
if((num%2)==0) /* Checking whether remainder is 0 or not. */
printf("%d is even.",num);
else
printf("%d is odd.",num);
return 0;
}
输出1:
Enter an integer you want to check: 2525 is odd.
输出2:
Enter an integer you want to check: 1212 is even.
也可以用条件运算符解决:
/* C program to check whether an integer is odd or even using conditional operator */
#include
int main(){
int num;
printf("Enter an integer you want to check: ");
scanf("%d",&num);
((num%2)==0) ? printf("%d is even.",num) : printf("%d is odd.",num);
return 0;
}
11、C语言检查是元音还是辅音
#include
int main(){
char c;
printf("Enter an alphabet: ");
scanf("%c",&c);
if(c=='a'||c=='A'||c=='e'||c=='E'||c=='i'||c=='I'||c=='o'||c=='O'||c=='u'||c=='U')
printf("%c is a vowel.",c);
else
printf("%c is a consonant.",c);
return 0;
}
输出1:
Enter an alphabet: ii is a vowel.
输出2:
Enter an alphabet: GG is a consonant.
也可以用条件运算符解决
/* C program to check whether a character is vowel or consonant using conditional operator */
#include
int main(){
char c;
printf("Enter an alphabet: ");
scanf("%c",&c);
(c=='a'||c=='A'||c=='e'||c=='E'||c=='i'||c=='I'||c=='o'||c=='O'||c=='u'||c=='U') ? printf("%c is a vowel.",c) : printf("%c is a consonant.",c);
return 0;
}
输出结果和上面的程序相同。
12、C语言实现从三个数值中查找最大值实现1:
/* C program to find largest number using if statement only */
#include
int main(){
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
if(a>=b && a>=c)
printf("Largest number = %.2f", a);
if(b>=a && b>=c)
printf("Largest number = %.2f", b);
if(c>=a && c>=b)
printf("Largest number = %.2f", c);
return 0;
}
实现2:
/* C program to find largest number using if...else statement */
#include
int main(){
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
if (a>=b)
{
if(a>=c)
printf("Largest number = %.2f",a);
else
printf("Largest number = %.2f",c);
}
else
{
if(b>=c)
printf("Largest number = %.2f",b);
else
printf("Largest number = %.2f",c);
}
return 0;
}
实现3:
/* C Program to find largest number using nested if...else statement */
#include
int main(){
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
if(a>=b && a>=c)
printf("Largest number = %.2f", a);
else if(b>=a && b>=c)
printf("Largest number = %.2f", b);
else
printf("Largest number = %.2f", c);
return 0;
}
输出结果相同:
Enter three numbers: 12.213.45210.193Largest number = 13.45
13、C语言解一元二次方程
/* Program to find roots of a quadratic equation when coefficients are entered by user. */
/* Library function sqrt() computes the square root. */
#include
#include /* This is needed to use sqrt() function.*/
int main()
{
float a, b, c, determinant, r1,r2, real, imag;
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);
determinant=b*b-4*a*c;
if (determinant>0)
{
r1= (-b+sqrt(determinant))/(2*a);
r2= (-b-sqrt(determinant))/(2*a);
printf("Roots are: %.2f and %.2f",r1 , r2);
}
else if (determinant==0)
{
r1 = r2 = -b/(2*a);
printf("Roots are: %.2f and %.2f", r1, r2);
}
else
{
real= -b/(2*a);
imag = sqrt(-determinant)/(2*a);
printf("Roots are: %.2f+%.2fi and %.2f-%.2fi", real, imag, real, imag);
}
return 0;
输出1:
Enter coefficients a, b and c: 2.345.6Roots are: -0.87+1.30i and -0.87-1.30i
输出2:
Enter coefficients a, b and c: 410Roots are: 0.00 and -0.25
14、C语言检查是否是闰年
/* C program to check whether a year is leap year or not using if else statement.*/
#include
int main(){
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 == 0)
{
if( year%100 == 0) /* Checking for a century year */
{
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);
return 0;
}
输出1:
Enter year: 20002000 is a leap year.
输出2:
Enter year: 19001900 is not a leap year.
输出3:
Enter year: 20122012 is a leap year.
1.《c语言编程代码大全 C语言基础知识大全》援引自互联网,旨在传递更多网络信息知识,仅代表作者本人观点,与本网站无关,侵删请联系页脚下方联系方式。
2.《c语言编程代码大全 C语言基础知识大全》仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证。
3.文章转载时请保留本站内容来源地址,https://www.lu-xu.com/jiaoyu/76789.html