C语言学习1

这篇博客详细解析了C语言中`scanf`和`printf`函数的使用,包括如何处理空格和分隔符,以及不同类型的数据输入输出格式。此外,还介绍了表达式的值类别(lvalue, non-lvalue object, function designator)及其在函数调用和指针操作中的转换规则。同时,讨论了C语言中的运算符,包括赋值、自增/自减、算术、逻辑、比较和成员访问等。
摘要由CSDN通过智能技术生成
// 在用scanf时,在终端输入a,b的值时要加空格隔开
scanf("%d%d",&a,&b);
// 输入:12 1
scanf("%d%c",&i,&c);// i读取到12,c读取到空格
scanf("%d %c",&i,&c);// 这次scanf遇到空格就会跳过了,i读取到12,c读取到字符1

// 输入hello   world
scanf("%s",string);// 只会读到hello
// 两个scanf时
scanf("%s",string);
scanf("%s",string2);
// string会是hello,string2会是world,空格是不会被读到的,因为是分隔符

// %7s告诉scanf最多只能读入7个字符,超过7个字符就不要了
// 输入12345678,只会读取1234567
scanf("%7s",string);

// printf
%nd // 输出整数宽度为n,整数宽度<n时起作用,空格填充
%0nd // 用0填充
%.nf // 小数点后n位

// 这种%f不能换成%d,10.0也不能换成10
// 两个条件都要满足(%f和.0都要有)
// 输出:3.333333
printf("%f",10.0/3);

double a,b;
// 这种将a,b改为double型,输入的时候格式必须要为%lf
// 输出的时候格式为%f,这样可以不用加.0
scanf("%lf%lf",&a,&b);
printf("%f",a/b);
// a++的值是a,++a的值是a+1(二者运算过后a本身的值都会+1)
// 赋值运算符(优先级很低)
a=6的结果就是6,自右向左结合
//引入#include<stdbool.h>后可以定义和使用bool变量
#include<stdio.h>
#include<stdbool.h>
int main(){
	bool b=6>5;
	bool sh=true;
}

随机数

#include<stdio.h>
//用随机数下面这两个库必须要有 
#include<stdlib.h>
#include<time.h>
int main(){
  srand(time(0));
  int a=rand();//a是一个比较大的随机数 
  printf("%d\n",a%100);//得到一个100以内的随机数
}

Expressions

An expression is a sequence of operators and their operands,that specifies a computation.

Each expression in C(an operator with its arguments, a function call, a constant, a variable name, etc)is characterized
by two independent properties:a type and a value category.

Value categories(lvalue, non-lvalue object, function designator) classify expressions by their values.
Every expression belongs to one of three value categories: lvalue, non-lvalue object(rvalue), and function designator.

lvalue//左值
Lvalue expression is any expression with object type other than the type void, which potentially designates an object.
In other words, lvalue expression evaluates to the object identity.

non-lvalue object//右值
Known as rvalues, non-lvalue object expressions are the expressions of object types that do not designate objects,
but rather values that have no object identity or storage location.
The address of a non-lvalue object expression cannot be taken.
Function designator
A function designator is an expression that has function type.(函数类型的表达式,如函数名等)
anything that designates a function, including a dereferenced function pointer.

Note that the function-call operator is defined for pointers to functions and not for function designators themselves.
Although function call is only defined for pointers to functions, it works with function designators due to the
function-to-pointer implicit conversion.

Any function designator expression, when used in any context other than
as the operand of the address-of operator
as the operand of sizeof
as the operand of typeof and typeof_unqual (since C23)
undergoes a conversion to the non-lvalue pointer to the function designated by the expression.
int f(int);
int (*p)(int) = f; // f conversion to &f
(***p)(1); // repeated dereference to f and conversion back to &f

int f(void) { return 1; }
int (*pf)(void) = f;
int main(void){
    f();    // convert f to pointer, then call
    (&f)(); // create a pointer to function, then call
 
    pf();    // call the function
    (*pf)(); // obtain the function designator, implicit convert to pointer, then calls
 
    (****f)(); // convert to pointer, obtain the function, repeat 4x, then call
    (****pf)(); // also OK
}
//Operators
//赋值,自增/自减,算术,逻辑,比较,成员访问([],*,&,->,.)
assignment,increment/decrement,arithmetic,logical,comparison,member access
//其他运算符
()// function call
,// comma operator
(type)// type cast
?:// conditional operator
sizeof// sizeof operator
//算术运算符(arithmetic)中的位运算
//

https://softwareengineering.stackexchange.com/questions/418126/what-is-a-function-returning-type-in-c
https://en.cppreference.com/w/c/language/conversion
https://en.cppreference.com/w/c/language/operator_other
https://stackoverflow.com/questions/64718604/why-does-it-seem-that-func-is-the-same-as-func-in-c

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值