C study notes[2]

文章目录

integer

  1. the format character %d in the printf funtion indicates to output with integer format In decimal form. if the integer bases 8 which is octal or bases 16 which is hexadecimal,you should use the %o or %x.we can create the prefix such as 0 or 0X when adding # to those format character.
c 复制代码
# include <stdio.h>
int main(void) {
	int x = 9;
	printf("%o,%#o,%d,%x,%#x",x,x,x,x,x);
}
  1. the char type is a fantastic data type which take up 8 bits ,to occupy one byte memory of computer for achieving some tasks such as representing as a small integer less than 256,a set of binary explaining a instruction, a character for ASCII,a byte's slice of datas from a file and so on.

  2. In C, non-printing characters (also called control characters) are characters that do not represent a written symbol but instead control formatting, transmission, or other functions.

  3. Common Non-Printing Characters in C

Escape Sequence ASCII Value Description
\a 0x07 Alert (bell)
\b 0x08 Backspace
\t 0x09 Horizontal tab
\n 0x0A Newline (line feed)
\v 0x0B Vertical tab
\f 0x0C Form feed (page break)
\r 0x0D Carriage return
\0 0x00 Null character (string terminator)
\\ 0x5C Backslash (printing, but escaped)
\' 0x27 Single quote
\" 0x22 Double quote
  • Hexadecimal : \x1B (escape character)
  • Octal : \033 (same escape character)
c 复制代码
printf("Hello\tWorld!\n");      // Uses tab and newline
printf("Alert: \a\n");          // Triggers a beep (if supported)
printf("Back\bspace\n");        // Prints "Backspace" after backspace
printf("Null terminator: \0\n"); // Stops string at \0
printf("Hex: \x41\n");  // Prints 'A' (ASCII 0x41)
printf("Octal: \101\n"); // Also prints 'A' (octal 101)
  1. _Bool is a Boolean data type introduced in the C99 standard to represent true/false values. It can store either 1 (true) or 0 (false).
c 复制代码
#include <stdio.h>
#include <stdbool.h> // For bool, true, false

int main() {
    _Bool isTrue = 1;   // Works, but not recommended (use bool instead)
    bool isFalse = false; // Preferred (via <stdbool.h>)
    
    printf("isTrue: %d\n", isTrue);   // Output: 1
    printf("isFalse: %d\n", isFalse); // Output: 0
    
    return 0;
}

input

  1. scanf function used to receive user's input with character format such as %s,%d and so on,as similar as format character parameter of printf function.
  • Common format specifiers for scanf:

    %d - integer

    %f - float

    %lf - double

    %c - single character

    %s - string (whitespace delimited)

    %x - hexadecimal integer

    %o - octal integer

c 复制代码
int age;
float height;
printf("Enter your age and height: ");
scanf("%d %f", &age, &height);

you also declare a char array ,50 bytes in size,for inputing a set of chars maximized 50 used in scanf function.

every char array substantially is a sequence consisted of a lot of chars,can be called as character string in C language,capacity of a char array is its length-1 because of ending with \0(Null character) .

c 复制代码
char name[50];
printf("Enter your name: ");
scanf("%49s", name);  // limit input to prevent buffer overflow

string "a" and char 'a' are different that "a" include Null and 'a' only occupy one byte.

  1. scanf return an integer indenfied the amount of reading chars sucessfully.
  2. The asterisk (*) in scanf has a special meaning - it's called an "assignment suppression character".
c 复制代码
int x;
scanf("%*d %d", &x);  // Reads but discards first integer, stores second in x

the format character flaged as * will be omitted to input.

  1. In printf, * is used for dynamic field width and precision specification.
c 复制代码
int width = 10;
int num = 42;
printf("%*d", width, num);  // Prints num in 10-character width
int precision = 3;
float pi = 3.14159;
printf("%.*f", precision, pi);  // Prints "3.142"
c 复制代码
int width = 8;
int precision = 2;
float value = 123.4567;
printf("%*.*f", width, precision, value);  // Prints "  123.46"

constant

  1. declare a constant can use # define constantname value ,for example, #define x 0.001.
  2. const is a keyword used to declare variables as read-only (constant).
c 复制代码
#include <stdio.h>

int main() {
    const int c = 10;  // 'c' is a constant integer
    // c = 20;  // ERROR: Cannot modify a const variable
    printf("c = %d\n", c);  // Output: c = 10
    return 0;
}
  1. Predefined Symbolic Constants,these come built-in with the C language or standard libraries:

    Standard Library Constants (from headers like <limits.h>, <float.h>, <math.h> etc.):

c 复制代码
  NULL (null pointer)

    EOF (end-of-file)

    BUFSIZ (standard buffer size)

    FLT_MAX (maximum float value)

    INT_MAX (maximum integer value)

references

  1. deepseek
相关推荐
La Pulga7 分钟前
【STM32】USART串口(上)
c语言·stm32·单片机·嵌入式硬件·mcu
AI量化投资实验室13 分钟前
今日策略:年化436%,回撤7%,夏普比5.28, deap因子挖掘重构,附python代码
开发语言·python·重构
O_o3811 小时前
QT多窗口跳转
开发语言·qt
海上Bruce1 小时前
C primer plus (第六版)第十一章 编程练习第8题
c语言
DIY机器人工房1 小时前
关于如何让 Vosk 正确识别中文音频,核心是 使用 Vosk 中文模型 + 确保中文音频格式符合要求
开发语言·python
天將明°1 小时前
错误追踪技术指南:让Bug无处可逃的追踪网
c语言·单片机·嵌入式硬件
铭哥的编程日记3 小时前
C++优选算法精选100道编程题(附有图解和源码)
开发语言·c++·算法
꒰ঌ 安卓开发໒꒱3 小时前
Java 面试 -Java基础
java·开发语言·面试
不枯石6 小时前
Matlab通过GUI实现点云的最远点下采样(Farthest point sampling)
开发语言·图像处理·算法·计算机视觉·matlab
-Aerolite-7 小时前
【C/C++】C/C++状态机实现方法
c语言·c++