TwitterSharebutton

Learn Through Suffering C language Learn Through Suffering 
C language

Words and symbols

Reserved words

The basic keywords in C language are as follows:

C keywords
auto break case char const continue default do
double else enum extern float for goto if
int long register return short signed sizeof static
struct switch typedef union unsigned void volatile while

Some compilers may define additional reserved words as well.
In addition, C++ compilers also define C++ keywords.
Often, they're words we use regularly, so we understand them without even realizing it.
I think you'll quickly notice it's a compiler error if you happen to use it.

However, reserved words can also be used as macro names.
It's best to avoid it to prevent confusion.

Actually, 'main' is not a reserved word, so you can use it for variable names without any issues.
It would be better not to use it because it feels off.

Output format specifiers

This is documentation regarding the format and specifiers for output conversion specifiers.
It's commonly used with functions like printf.

Output format specifiers
% option length.precision type

Specify these connected.Non-type specifiers can be omitted.
Typically, only the length is specified for alignment.
Note that the point between length and precision is a decimal point.It's not a comma.
Incidentally, some systems also maintain size information relating to precision and type.
This is for far/near pointers, and it's clearly outdated, so I won't deal with it.

Option List
symbol Effect
- Left-align output
+ Right-align output
# Do not suppress trailing zeros or the decimal point.

Length and Precision Format
symbol Effect
L Output at least L digits, padding with spaces.
0L Output at least L digits, padding with zeros.
L.P Output at least L digits total, with P digits after the decimal point.
L.0 Output at least L digits total, with no decimal places.
Let L represent the length value and P represent the accuracy value.
※0 is technically optional, but we include it here for the sake of length and accuracy.

Type List
symbol Meaning
d,i Signed decimal integer.
o Unsigned octal integer.
x,X Unsigned hexadecimal integer (x: lowercase, X: uppercase).
u Unsigned decimal integer.
f Real Number。
e,E Scientific notation (e: lowercase, E: uppercase).
g,G Uses normal or scientific notation depending on the value.
c 1Character。
s String. Outputs up to the null character.
p Pointer address in uppercase hexadecimal.
n Stores the number of characters written so far.
% Outputs a literal % character.

The correct format specifier to use for displaying doubles is %f.
Originally, %lf did not exist, but now %lf can be used as well.

Input Conversion Specifiers

This is documentation on the format and specifiers for input conversion specifiers.
It is primarily used with functions like scanf.

Input Conversion Specifiers
% * Length Size Type

Specify these connected.Non-type specifiers can be omitted.
Often, to specify input limitations, one will typically specify length alone.
Incidentally, some systems hold information about the size of pointers regarding the length and size.
This is for far/near pointers, and it's clearly outdated, so I won't deal with it.

If an asterisk is present, the data is skipped.(Not assigned to a variable)
It's used to skip unnecessary data when loading file data, for example.

When a length is specified, it reads a number (or string) with that many digits.
It is used to prevent exceeding digit length limits or string buffer boundaries.

Size List
symbol Effect
h Converts input data to short.
l Converts input data to long or double according to the type specifier.
L Converts input data to long double.

Type List
symbol Meaning
d Decimal int.
o Octal int.
x Hexadecimal int.
i int (base is determined by the input format).
u Unsigned decimal int.
f float
e,E Scientific notation float (e: lowercase, E: uppercase).
g,G Normal or scientific notation.
c Single char.
s Non-whitespace char string.
p Pointer value.
n Stores the number of characters read so far.
% Does nothing.

Input conversion specifiers enforce stricter type checking because they perform assignments.
There's no double type in the list above, but...
This is specified in conjunction with the size specifier l.

Operators and Precedence

The operator precedence in C is very well designed,
In typical usage, it's not necessary to explicitly include ().
It usually works if you write it according to the formula.
When you're unsure about your priorities, it's better to compare them by looking at this table than...
It's easy and reliable to attach ().

C Language Operators and Precedence
The higher up, the higher the priority.
The symbols in each delimited section will have the same precedence.

Type List
name symbol Function Associativity Arity
Array Subscript [] Access array element Left to right Unary
functionCall () Call a function Left to right Unary
Member Access . Access struct member Left to right Unary
-----
Arrow -> Access struct member via pointer Left to right Unary
Post-increment ++ Increment variable by 1 None Unary
Post-decrement -- Decrement variable by 1 None Unary
Pre-increment ++ Increment variable by 1 None Unary
Pre-decrement -- Decrement variable by 1 None Unary
Dereference * Access value pointed to by pointer None Unary
address & Get address of variable None Unary
Unary Plus + Positive value None Unary
Unary Minus - Negate value None Unary
Logical NOT ! Invert boolean value None Unary
sizeof sizeof Get size of variable, array, or type None Unary
Cast (type) Convert to specified type Right to left Unary
-----
Multiplication * Multiply Left to right Binary
Division / Divide Left to right Binary
Modulo % Remainder Left to right Binary
-----
Addition + Add Left to right Binary
Subtraction - Subtract Left to right Binary
-----
Left Shift << Shift bits left (multiply by 2) Left to right Unary
Right Shift >> Shift bits right (divide by 2) Left to right Unary
-----
Less Than < True if left is less than right Left to right Binary
Greater Than > True if left is greater than right Left to right Binary
Less Than or Equal <= True if left is less than or equal to right Left to right Binary
Greater Than or Equal >= True if left is greater than or equal to right Left to right Binary
-----
Equality == True if left and right are equal Left to right Binary
Inequality != True if left and right are not equal Left to right Binary
-----
Bitwise AND & Bitwise AND Left to right Binary
-----
Bitwise XOR ^ Bitwise XOR Left to right Binary
-----
Bitwise OR | Bitwise OR Left to right Binary
-----
Logical AND && True if both left and right are true Left to right Binary
-----
Logical OR || True if either left or right is true Left to right Binary
-----
Conditional ? and : Evaluate first expression if true, second if false Right to left Ternary
-----
Assignment = Assign right value to left variable Right to left Binary
Multiply Assignment *= Multiply and assign Right to left Binary
Divide Assignment /= Divide and assign Right to left Binary
Modulo Assignment %= Modulo and assign Right to left Binary
Add Assignment += Add and assign Right to left Binary
Subtract Assignment -= Subtract and assign Right to left Binary
Left Shift Assignment <<= Left-shift and assign Right to left Binary
Right Shift Assignment >>= Right-shift and assign Right to left Binary
Bitwise AND Assignment &= Bitwise AND and assign Right to left Binary
Bitwise XOR Assignment ^= Bitwise XOR and assign Right to left Binary
Bitwise OR Assignment = Bitwise OR and assign Right to left Binary
-----
Comma , Combine expressions Left to right Binary

There are no specific rules for the name, so I used a commonly used designation.
Below, you'll find an explanation of some operators.

The reference operator is an operator that switches a pointer variable to normal variable mode.
The asterisk used when declaring pointer variables has no grammatical relevance.

The array subscript operator refers to the brackets [] that are attached to an array.
That function adds the byte size of a variable to an address.
Note that the brackets used when declaring an array have no grammatical relationship.

The conditional operator is a simplified way to create conditional expressions, and is used as follows:

Conditional operator
variable = (condition) ? expression1 : expression2;

If the condition is true, expression 1 is executed, and if it's false, expression 2 is executed.
And the result is assigned to a variable, but it can be used without specifying a variable.
Some people, including myself, prefer to use it because it allows for simpler conditional judgments than if statements.
Furthermore, this operator uses three operands, which is why it's also known as the ternary operator.

The sequential operator is a way to combine two expressions into one.

Sequential operator
j = (i = 3 , i + 2 );

In this example, i = 3 is calculated first, and then i + 2 is calculated.
Ultimately, j will be assigned the value of 5.
However, this operator doesn't really have any practical use.

Memory class specifier

C offers the following storage class specifiers.
However, it seems many of them are already obsolete.

Memory class specifier
auto
register
static
extern
typedef


auto specifier
It means the variable is an automatic variable.
However, inside a function, variables automatically become automatic variables,
It's a meaningless specifier because it results in an error outside of a function.


register specifier
It means that variable is frequently used.
In the past, allocating this variable to a register
was used to speed up processing,
Compiler optimizations are prioritized nowadays.

Nowadays, in multithreaded programming,
It can be used as a designation for a variable undergoing exclusive processing.


static specifier
It means that variable will persist until the program ends.
Within a function, variable values are maintained within the function's scope.
Outside of a function, a variable is only accessible within the file where it is defined.


`extern` specifier
It means that definitions, such as variables, are done in other source files.
It is used to declare common variables within a header file.


`typedef` specifier
I declare a new type.
It allows for easy type declarations and compiler checking.
Note: This is not originally related to the Memory class, but is included here for grammatical convenience.

escape character

It's used for manipulating string display and other operations, even though it can't be displayed on the screen.
Line breaks are well-known, but there are also others like these.

escape character
escape character hexadecimal Function
\a 0x07 Play a beep sound
\b 0x08 Move cursor back one position
\t 0x09 Move cursor to the next tab stop
\n 0x0A Move cursor to the next line (newline)
\f 0x0C Form feed (printers only)
\r 0x0D Move cursor to the beginning of the line
\\ 0x5C Display \
\' 0x2C Display '
\" 0x22 Display "
\? 0x3F Display ?
\digit Same in octal Display character for octal code
\xdigit Same in hex Display character for hex code

These are commonly used.
The form feed character (\f) can only be used for printer output and is not commonly used.
\r is a line ending in Mac, and now it's a line ending on other operating systems as well.

Escape characters require two or more characters in their notation.
It's a display issue, but internally it's treated as a single character.

Defined constants

In C, information about the compiler and debugger, among other things,
It's provided as a defined constant.

Debugging Constants
Constant Meaning
__LINE__ Current line number
__FILE__ Current source file name
__DATE__ Compilation date
__TIME__ Compilation time

By using `__FILE__` and `__LINE__`, you can determine the file and line number at runtime.
We can pinpoint the line number where the error occurred.
It's very useful for dumping to log files or using in a simple debugger.
I wish other programming languages had this feature too...

Predefined Integer Limit Constants
Constant Meaning
CHAR_BIT Size of char in bits.
CHAR_MAX Maximum value of char.
CHAR_MIN Minimum value of char.
INT_MAX Maximum value of int.
INT_MIN Minimum value of int.
LONG_MAX Maximum value of long.
LONG_MIN Minimum value of long.
SCHAR_MAX Maximum value of signed char.
SCHAR_MIN Minimum value of signed char.
SHART_MAX Maximum value of short.
SHART_MIN Minimum value of short.
UCHAR_MAX Maximum value of unsigned char.
UINT_MAX Maximum value of unsigned int.
ULONG_MAX Maximum value of unsigned long.
USHRT_MAX Maximum value of unsigned short.
These constants require the inclusion of `#define `.

Whether a `char` type is signed depends on the compiler.
Naturally, both are designed to ensure no impact on character storage.


About This Site

Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.

Part 0: Program Overview

  1. What is a program?



Chapter 3: Displaying on the Screen

  1. String Display
  2. line break
  3. Practice Problem 3

Chapter 4: Displaying and Calculating Numbers

  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4


Chapter 6: Input from the Keyboard

  1. input function
  2. The fear of input
  3. Practice Problem 6



Chapter 9: Repeating a Fixed Number of Times

  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9

Chapter 10: Repeating Without Knowing the Number of Times

  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10



Chapter 13: Handling Multiple Variables at Once

  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13






Chapter 19: Dynamic Arrays

  1. Create arrays freely.
  2. Practice Problem 19