the introduction of c language
- the first C standard called ANSI C was born in 1983 for the strongly demand of C community,was adopted in 1989,usualy intituled as C89.the C99 standard was constructed starting in 1994 which adds a lot of important and pressing functionalities such as supporting international character sets,allowing to single line notation,new data types,Variable-length array and so on.the standard c00 ommitee’s developing of new C standard began in 2007,released in 2011.however,the C standard always abide by a pricinple that C language is supposed to keep simple and small.
- the source code of C will be compilied to an executable which can run on several OS such as windows,linux,unix,macos and so on .
- outputing to screen(terminal) is so easy, immediately use the printf function.
#include <stdio.h>
int main(void) {
int num = 88;
printf("the number is:");
printf("%d",num);
}
the first printf
fucntion take out the string “the number is:” for outputing to terminal,without line break.the second use two parameters which has one more parameter than the first printf
,the newly added parameter is character format and in this example %d
represents integer value.there are few formats as similar as %d
as follows.
%d decimal integer
%f floating number
%c single char
%s string
%x Hexadecimal integer
%p pointer address
%% Output percentage sign
- a c program starts its running with main() function,means the main function is the entry of all program .
- all variables in a C program must be declared earlier than using them.
- all variables must belong to a specific data type.
- basic data types are shown as follows.
int: Integer type, typically 4 bytes (platform-dependent). Range: -2,147,483,648 to 2,147,483,647 (32-bit).
char: Character type, 1 byte. Range: -128 to 127 or 0 to 255 (unsigned).
float: Single-precision floating-point, 4 bytes. Range: ~1.2E-38 to 3.4E+38.
double: Double-precision floating-point, 8 bytes. Range: ~2.3E-308 to 1.7E+308.
void: Represents "no type" (used for functions with no return value or pointers to generic data).
Type Modifiers
signed: Allows positive and negative values (default for int and char).
unsigned: Only non-negative values (doubles the positive range).
short: Smaller integer (usually 2 bytes). Range: -32,768 to 32,767.
long: Larger integer (4 or 8 bytes). Range: -2³¹ to 2³¹-1 (32-bit) or -2⁶³ to 2⁶³-1 (64-bit).
long long: Extended integer (at least 8 bytes). Range: -2⁶³ to 2⁶³-1.
Derived Data Types
Arrays: Contiguous collection of elements of the same type (e.g.,
int arr[10]
).Pointers: Stores memory addresses (e.g.,
int *ptr
).Structures (struct): Groups variables of different types under one name.
Unions (union): Similar to struct, but members share memory.
User-Defined Types
typedef: Creates aliases for existing types (e.g.,
typedef int myInt;
).Enums (enum): Defines named integer constants (e.g.,
enum {RED, GREEN, BLUE};
).
#include <stdio.h>
int main() {
int a = 10;
float b = 3.14;
char c = 'A';
double d = 123.456e-10;
unsigned long e = 1000000UL;
printf("int: %d, float: %f, char: %c, double: %lf, ulong: %lu\n", a, b, c, d, e);
return 0;
}
- The four operations can be accomplished by mathematical operator
#include <stdio.h>
int main() {
float num1, num2;
char op;
printf("请输入两个数字和运算符(如:3 + 5):");
scanf("%f %c %f", &num1, &op, &num2);
switch (op) {
case '+':
printf("结果: %.2f\n", num1 + num2);
break;
case '-':
printf("结果: %.2f\n", num1 - num2);
break;
case '*':
printf("结果: %.2f\n", num1 * num2);
break;
case '/':
if (num2 != 0) {
printf("结果: %.2f\n", num1 / num2);
} else {
printf("错误:除数不能为 0!\n");
}
break;
default:
printf("错误:无效的运算符!\n");
}
return 0;
}
of couse,C language already provide a little more complicated computation through math library.
#include <stdio.h>
#include <math.h>
int main() {
double x = 2.0;
printf("e^%.2f = %.2f\n", x, exp(x)); // e^x
printf("2^%.2f = %.2f\n", x, exp2(x)); // 2^x
printf("ln(%.2f) = %.2f\n", x, log(x)); // 自然对数
printf("log2(%.2f) = %.2f\n", x, log2(x)); // 以2为底的对数
return 0;
}
if you handle more advanced computer knowledge ,so much so that Reverse Polish Expression can be written with C to achieve more tasks.
references
- deepseek