C language learned by suffering
C language learned by suffering
Words and Symbols
reserved word
The basic reserved words in C are as follows
Some compilers may define other reserved words in addition to these.
C++ compilers also define C++ reserved words.
Many of them are words we use everyday, so we don't have to be particularly conscious of them.
Even if you happen to use it, you will notice it immediately with a compiler error.
Note that reserved words can also be used as macro names, but only if the
It is best not to use it, as it will cause confusion.
Actually, main is not a reserved word and can be used in variable names without any problem, but
It is better not to use it because it is uncomfortable.
Reserved words in C
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
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 other reserved words in addition to these.
C++ compilers also define C++ reserved words.
Many of them are words we use everyday, so we don't have to be particularly conscious of them.
Even if you happen to use it, you will notice it immediately with a compiler error.
Note that reserved words can also be used as macro names, but only if the
It is best not to use it, as it will cause confusion.
Actually, main is not a reserved word and can be used in variable names without any problem, but
It is better not to use it because it is uncomfortable.
output conversion specifier
Documentation on the format and specifier of the output conversion specifier.
It is used by the printf function and others.
These are specified by connecting them together. Specifiers other than type can be omitted.
Generally, only the length is specified for the purpose of headroom alignment.
Note that the point between the length and precision is a decimal point. It is not a comma.
Incidentally, some processors have information about size between precision and type, but
This is for far,near pointers and is obviously outdated, so it will not be handled.
Let L denote the numerical value of length and P denote the numerical value of precision.
The *0 is precisely optional, but is included in the length and precision for the sake of explanation.
The %f is correct to use for displaying the double type.
Originally, %lf did not exist, but now %lf can also be used.
It is used by the printf function and others.
Format for output conversion specifiers
% Optional Length. Precision Type
These are specified by connecting them together. Specifiers other than type can be omitted.
Generally, only the length is specified for the purpose of headroom alignment.
Note that the point between the length and precision is a decimal point. It is not a comma.
Incidentally, some processors have information about size between precision and type, but
This is for far,near pointers and is obviously outdated, so it will not be handled.
List of Options
Symbol | Effect |
---|---|
-1.0 | Left-justified |
+ | Right justified |
# | 0, or no decimal point suppression. |
Length and precision format
Symbol | Effect |
---|---|
L | Output at least L digits. Margins should be filled with spaces. |
0L | Output at least L digits. Margins are filled with zeros. |
L.P | Output with at least L digits for the whole and P digits for the decimal point. |
L.0 | Output at least L digits for the entire text, with no decimal point. |
The *0 is precisely optional, but is included in the length and precision for the sake of explanation.
Type List
Symbol | Meaning |
---|---|
d,i | Integer decimal number. |
o | Integer octal unsigned. |
x,X | Integer unsigned in hexadecimal, with x in lowercase and X in uppercase. |
u | Integer decimal unsigned. |
f | Real number. |
e,E | Exponential form of a real number, where e is lowercase and E is uppercase. |
g,G | Depending on the size of the real number, either normal output or exponential form is selected. |
c | One character. |
s | String. Output up to null character. |
p | Output as pointer address, in hexadecimal uppercase. |
n | Output the number of characters written out so far. No type conversion. |
%. | Output of character %. No type conversion. |
The %f is correct to use for displaying the double type.
Originally, %lf did not exist, but now %lf can also be used.
input-conversion specifier
Documentation on the format and specifier of the input conversion specifier.
It is mainly used in the scanf function and other functions.
These are specified by connecting them together. Specifiers other than type can be omitted.
Generally, only length is specified for input restriction purposes.
Incidentally, some processors have information about the size of the pointer between the length and the size, but
This is for far and near pointers and is obviously outdated, so it is not handled.
If * is added, the data is skipped. (No assignment to variables).
Used to skip unnecessary data when reading data from a file, for example.
If a length is specified, the number of digits (or string) with that number of digits will be read.
It is used to limit the number of digits in a number or to ensure that the string buffer area is not exceeded.
The input conversion specifier is a strict type specification because it performs assignments.
There is no double type in the list above, but the
This is specified in combination with the size specifier l.
It is mainly used in the scanf function and other functions.
input-conversion specifier
% * Length Size Type
These are specified by connecting them together. Specifiers other than type can be omitted.
Generally, only length is specified for input restriction purposes.
Incidentally, some processors have information about the size of the pointer between the length and the size, but
This is for far and near pointers and is obviously outdated, so it is not handled.
If * is added, the data is skipped. (No assignment to variables).
Used to skip unnecessary data when reading data from a file, for example.
If a length is specified, the number of digits (or string) with that number of digits will be read.
It is used to limit the number of digits in a number or to ensure that the string buffer area is not exceeded.
Size List
Symbol | Effect |
---|---|
h | Convert read data to short type. |
l | Convert the read data to long or double type according to the type specification. |
L | Convert read data to long double type. |
Mold List
Symbol | Meaning |
---|---|
d | Decimal number of type int. |
o | Octal number of type int. |
x | Hexadecimal number of type int. |
i | int type. The decimal number is determined according to the data. |
u | Unsigned decimal number of type unsigned int. |
f | float type |
e,E | Exponential form of type float, where e is lowercase and E is uppercase. |
g,G | Normal or exponential form. |
c | Character of type char. |
s | String of type char without whitespace. |
p | Pointer value. |
n | Assigns the number of characters read so far. No data is read. |
%. | Do nothing. Meaningless specification. |
The input conversion specifier is a strict type specification because it performs assignments.
There is no double type in the list above, but the
This is specified in combination with the size specifier l.
Operators and Precedence
The precedence of operators in C is very well thought out, so that
In general use, there is no need to explicitly add () to the
Most of the time, writing the formula as it is written will work.
However, if you're unsure about your priorities, rather than looking at this table and comparing
() is easier and more reliable.
C Language Operators and Precedence
The higher up the list, the higher the precedence.
The symbols in each separated part have the same precedence.
There is no particular rule for how to call them, but we have used names that we believe are common.
Below, we have attached explanations of some of the operators.
The reference operator is the operator that switches a pointer variable to normal variable mode.
Note that there is no grammatical relationship with *, which is attached when declaring a pointer variable.
The array subscript operator means [], which is attached to the array, and
Its function is to add the byte size of the variable to the address.
Note that there is no grammatical relationship with [], which is attached when declaring an array.
Conditional operators are simple conditional expressions and are used as shown below.
When the conditional expression is true, Expression 1 is executed; when it is false, Expression 2 is executed.
The result is then assigned to a variable, but can be used without specifying a variable.
Some people (including the author) prefer to use this because it is easier to write conditional judgments than if statements.
This operator is also called the ternary operator because it is the only one in the C language that uses three terms.
Sequential operators are those that attempt to combine two expressions into one.
In this example, i = 3 is computed first, then i + 2.
Finally, j will be assigned 5.
However, this operator has no such use.
In general use, there is no need to explicitly add () to the
Most of the time, writing the formula as it is written will work.
However, if you're unsure about your priorities, rather than looking at this table and comparing
() is easier and more reliable.
C Language Operators and Precedence
The higher up the list, the higher the precedence.
The symbols in each separated part have the same precedence.
Type List
Name | Symbol | Function | Coupling Rule | Number of terms |
---|---|---|---|---|
Array index | []. | Specification of array element number | Left to right | single term |
Function call | () | Call a function | Left to right | unary |
Element selection | . | Selecting Elements of a Structure | Left to right | unary |
|
||||
Arrow | -> "- | Select element of structure pointer | Left to right | unary |
Postfix increment | ++ | Increase the value of the variable by 1 | None | unary |
Postfix decrement | -- | Decrease the value of the variable by 1 | None | unary |
Preceding increment | ++ | Increases the value of the variable by 1 | None | unary |
Preceding decrement | -- | Decrease the value of the variable by 1 | None | unary |
See also | * | Access to the pointer's wandering variable | None | unary |
Address | & (a) | Access variable addresses | None | unary |
Unary plus | + | Positive value | None | Unary |
Unary minus | -1 | Set to negative value | None | unary |
logical negation | ! | Invert true and false states | None | unary |
Size of | sizeof | Get the size of a variable, array or type | None | unary |
Cast | (type) | Force conversion to specified type | Right-to-left | unary |
|
||||
multiplication | * | multiplication | Left to right | Binomial |
division | / (a) | Division | From left to | Binomial |
Surplus | % (%) | remainder | Left to right | Binomial |
|
||||
addition | +1 | addition | Left to right | Binomial |
subtraction | -1 | subtraction | Left to right | Binomial |
|
||||
Left shift | << | Shifts the value of a variable one bit to the left (2x) | Left to right | unary |
Shift Right | >> | Shift the value of a variable one bit to the right (half) | Left to right | unary |
|
||||
smallest | < | True when the value on the left is less than the value on the right | Left to right | Binomial |
greater than or equal to | > | True when the left value is greater than the right | Left to right | Binomial |
below | <= | True when the value on the left is less than or equal to the value on the right | Left to right | Binomial |
more than or equal to | >= | True when the left value is greater than or equal to the right | Left to right | Binomial |
|
||||
equivalence | == == | True when the left and right values are equal | Left to right | Binomial |
inequivalence | ! = ! | True when left and right values are different | Left to right | Binomial |
|
||||
Bit AND | & (a) | Take AND | Left to right | Binomial |
|
||||
Bitwise Exclusive OR | ^ | Take exclusive disjunction | Left to right | Binomial |
|
||||
Bit OR | | (1) | Take OR | Left to right | Binomial |
|
||||
Logical AND | && | True when both left and right are true | Left to right | Binomial |
|
||||
Logical OR | || | True when either the left or right is true | Left to right | Binomial |
|
||||
Condition | ? and : | Substitute the previous expression when true and the second expression when false | Right to left | Ternary |
|
||||
assignment | = (a) | Assign the value of the expression on the right to the variable on the left | Right to left | Binomial |
assignment and multiplication | *= | Substitute multiplication of the left variable by the value of the right expression | Right to left | Binomial |
Substitution Division | /= | Substitute division of the value with the expression on the right for the variable on the left | Right to left | Binomial |
Assigned surplus | %= | Substitute the remainder of the value of the right expression for the left variable | Right to left | Binomial |
Substitution addition | += | Substitute the addition of the value with the right expression for the left variable | Right to left | Binomial |
Substitution subtraction | -= | Substitute subtraction of the value with the right expression for the left variable | Right to left | Binomial |
Left shift assignment | <<= | Assigns a left shift of the value with the right expression to the left variable | Right to left | Binomial |
Right shift assignment | >>= | Assign the right shift of the value with the right expression to the left variable | Right to left | Binomial |
Bit AND assignment | &= | Assign the AND of the value with the expression on the right to the variable on the left | Right to left | Binomial |
Bitwise exclusive or assignment | ^= | Assigns the exclusive OR of the value of the right expression to the left variable | Right to left | Binomial |
Bit OR assignment | = (1) | Assign OR of the value of the left variable with the right expression | Right to left | Binomial |
|
||||
sequential | , | Combine equation | Left to right | Binomial |
There is no particular rule for how to call them, but we have used names that we believe are common.
Below, we have attached explanations of some of the operators.
The reference operator is the operator that switches a pointer variable to normal variable mode.
Note that there is no grammatical relationship with *, which is attached when declaring a pointer variable.
The array subscript operator means [], which is attached to the array, and
Its function is to add the byte size of the variable to the address.
Note that there is no grammatical relationship with [], which is attached when declaring an array.
Conditional operators are simple conditional expressions and are used as shown below.
conditional operator
Variable = ( Conditional expression ) ? Expression1 : Expression2 ;
When the conditional expression is true, Expression 1 is executed; when it is false, Expression 2 is executed.
The result is then assigned to a variable, but can be used without specifying a variable.
Some people (including the author) prefer to use this because it is easier to write conditional judgments than if statements.
This operator is also called the ternary operator because it is the only one in the C language that uses three terms.
Sequential operators are those that attempt to combine two expressions into one.
sequential operator
j = (i = 3 , i + 2 );
In this example, i = 3 is computed first, then i + 2.
Finally, j will be assigned 5.
However, this operator has no such use.
storage class specifier
The C language provides the following memory class specifiers.
However, many of them are no longer in use.
However, many of them are no longer in use.
storage class specifier
auto
register
static
extern
typedef
register
static
extern
typedef
auto specifier
variable is an automatic variable.
However, it is automatically an automatic variable inside a function, and
It is a meaningless specifier because it is an error outside the function.
However, it is automatically an automatic variable inside a function, and
It is a meaningless specifier because it is an error outside the function.
register specifier
It means that the variable will be used frequently.
In the old days, assigning this variable to a register
Although it used to speed up processing, the
Today, compiler optimizations take precedence.
In the modern era, multi-threaded programming, the
It is sometimes used to specify a variable to be processed exclusively.
In the old days, assigning this variable to a register
Although it used to speed up processing, the
Today, compiler optimizations take precedence.
In the modern era, multi-threaded programming, the
It is sometimes used to specify a variable to be processed exclusively.
static specifier
It means that the variable will remain until the end of the program.
Within a function, the value of the variable will be retained within the function.
Outside of a function, the variable will be valid only within its source file.
Within a function, the value of the variable will be retained within the function.
Outside of a function, the variable will be valid only within its source file.
extern specifier
It means that variables and other definitions are made in other source files.
Used to declare common variables in header files.
Used to declare common variables in header files.
typedef specifier
Declares a new type.
Allows easy type declarations and compiler checking.
This is originally unrelated to the memory class, but is included here for syntactic convenience.
Allows easy type declarations and compiler checking.
This is originally unrelated to the memory class, but is included here for syntactic convenience.
escape character
It cannot be displayed on the screen, but is used for the purpose of manipulating the display of strings, etc.
The most famous is \n, which breaks lines, but there are others such as the following.
The most commonly used of these are \n and \t.
\The ﻟf can be used only when outputting to a printer, but it is not very common.
\ɑr is a line break on the Mac, and is now also a line break on other operating systems.
An escape character uses two or more characters for its notation, but
Internally, it is treated as a single character.
The most famous is \n, which breaks lines, but there are others such as the following.
escape character
escape character | Hexadecimal | Function | |
---|---|---|---|
\a | 0x07 | Beep | |
\b | 0x08 | Shift the cursor position backward by one. | |
\t | 0x09 | Moves the cursor position to the next tab position. | |
\n | 0x0A | Move cursor position to next line (line feed) | |
\f | 0x0C | Eject paper (for printers only) | |
\r}{r}{r}{r}{r}{r}{r}{r | 0x0D | Moves the cursor position to the leftmost position on the line (line break on Mac) | |
\blockquote | 0x5C | Show \. | |
\' | 0x2C | Display ''. | |
\blurbs}" | 0x22 | Display "-". | |
\blurbs}? | 0x3F | ? Displaying | |
\numbers that can be used to determine the number of seconds to wait for a response | Same in octal | Display corresponding octal character code | |
\Јx-numbers | Same as number | Display corresponding hexadecimal character code |
The most commonly used of these are \n and \t.
\The ﻟf can be used only when outputting to a printer, but it is not very common.
\ɑr is a line break on the Mac, and is now also a line break on other operating systems.
An escape character uses two or more characters for its notation, but
Internally, it is treated as a single character.
predefined constant
In C, information about the processing system and information that can be used for debugging is stored in the
Predefined constants are provided.
By using __FILE__ and __LINE__, you can run your program at runtime using the
It is possible to locate the line number where the error occurred.
It is very useful for logging to a log file or for a simple debugger.
I wish this feature existed in other programming languages...
Use of these constants requires #define <limits.h>.
Whether the char type is signed or not is compiler-dependent.
Naturally, either way is designed for character storage.
Predefined constants are provided.
Debugging Constants
constant | Meaning |
---|---|
__LINE___. | Line number being executed |
__FILE___. | Name of the source file being executed |
__DATE__. | Date compiled |
__TIME__. | Compiled time |
By using __FILE__ and __LINE__, you can run your program at runtime using the
It is possible to locate the line number where the error occurred.
It is very useful for logging to a log file or for a simple debugger.
I wish this feature existed in other programming languages...
Predefined constants of integer type size
Constant | Numeric Meaning |
---|---|
CHAR_BIT | Size in bits of type char. |
CHAR_MAX | Maximum value of type char. |
CHAR_MIN | Minimum value of type char. |
INT_MAX | Maximum value of type int. |
INT_MIN | Minimum value of type int. |
LONG_MAX | Maximum value of type long. |
LONG_MIN | Minimum value of type long. |
SCHAR_MAX | Maximum value of signed char type. |
SCHAR_MIN | Minimum value of signed char type. |
SHART_MAX | Maximum value of type short. |
SHART_MIN | Minimum value of type short. |
UCHAR_MAX | Maximum value of type unsigned char. |
UINT_MAX | Maximum value of type unsigned int. |
ULONG_MAX | Maximum value of type unsigned long. |
USHRT_MAX | Maximum value of type unsigned short. |
Whether the char type is signed or not is compiler-dependent.
Naturally, either way is designed for character storage.
About this Site
The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or more complete than any book on the market.