Gnu make reference manual
Operands are typed objects such as constants, variables, and function calls that return values. Innermost expressions are evaluated first. Then 12 is subtracted from 13 , resulting in 1. Finally, 1 is multiplied by 2 , resulting in 2. The outermost parentheses are completely optional. An operator specifies an operation to be performed on its operand s. Operators may have one, two, or three operands, depending on the operator. Assignment operators store values in variables.
C provides several variations of assignment operators. Note that, unlike the other assignment operators described below, you can use the plain assignment operator to store values of a structure type. Compound assignment operators perform an operation involving both the left and right operands, and then assign the resulting expression to the left operand. Here is a list of the compound assignment operators, and a brief description of what they do:. Subtract the right operand from the left operand, and then assign the result of the subtraction to the left operand.
Multiply the two operands together, and then assign the result of the multiplication to the left operand. Divide the left operand by the right operand, and assign the result of the division to the left operand.
Perform modular division on the two operands, and assign the result of the division to the left operand. Perform a left shift operation on the left operand, shifting by the number of bits specified by the right operand, and assign the result of the shift to the left operand. Perform a right shift operation on the left operand, shifting by the number of bits specified by the right operand, and assign the result of the shift to the left operand.
Perform a bitwise conjunction operation on the two operands, and assign the result of the operation to the left operand. Performs a bitwise exclusive disjunction operation on the two operands, and assign the result of the operation to the left operand. Performs a bitwise inclusive disjunction operation on the two operands, and assign the result of the operation to the left operand.
Since there are no side effects wrought by evaluating the variable x as an lvalue, the above code produces the same result as:. The operand must be a either a variable of one of the primitive data types, a pointer, or an enumeration variable. You can apply the increment operator either before or after the operand.
Note that incrementing a pointer only makes sense if you have reason to believe that the new pointer value will be a valid memory address. A prefix increment adds 1 before the operand is evaluated. A postfix increment adds 1 after the operand is evaluated.
In the previous examples, changing the position of the operator would make no difference. However, there are cases where it does make a difference:. C provides operators for standard arithmetic operations: addition, subtraction, multiplication, and division, along with modular division and negation.
Usage of these operators is straightforward; here are some examples:. However, if either operand is negative, the direction of rounding is implementation-defined. Signed Integer Division for information about overflow in signed integer division. The operands must be expressions of a primitive data type. Modular division returns the remainder produced after performing integer division on the two operands. The operands must be of a primitive integer type. If the operand you use with the negative operator is of an unsigned data type, then the result cannot negative, but rather is the maximum value of the unsigned data type, minus the value of the operand.
Many systems use twos-complement arithmetic, and on such systems the most negative value a signed type can hold is further away from zero than the most positive value. For example, on one platform, this program:. Numeric values are assumed to be positive unless explicitly made negative, so this operator has no effect on program operation.
The operand must be an expression of a complex number type. You use the comparison operators to determine how two operands relate to each other: are they equal to each other, is one larger than the other, is one smaller than the other, and so on.
When you use any of the comparison operators, the result is either 1 or 0, meaning true or false, respectively. In the following code examples, the variables x and y stand for any two expressions of arithmetic types, or pointers. The result is 1 if the operands are equal, and 0 if the operands are not equal. The not-equal-to operator! The result is 1 if the operands are not equal, and 0 if the operands are equal. Comparing floating-point values for exact equality or inequality can produce unexpected results.
Real Number Types for more information. You can compare function pointers for equality or inequality; the comparison tests if two function pointers point to the same function or not. Beyond equality and inequality, there are operators you can use to test if one value is less than, greater than, less-than-or-equal-to, or greater-than-or-equal-to another value.
Here are some code samples that exemplify usage of these operators:. Logical operators test the truth value of a pair of operands. Any nonzero expression is considered true in C, while an expression that evaluates to zero is considered false. If the first expression is false, then the second expression is not evaluated.
The logical disjunction operator tests if at least one of two expressions it true. If the first expression is true, then the second expression is not evaluated. You can prepend a logical expression with a negation operator! Since the second operand in a logical expression pair is not necessarily evaluated, you can write code with perhaps unintuitive results:. If foo is ever zero, then not only would bar not be called, but x would not be incremented.
If you intend to increment x regardless of the value of foo , you should do so outside of the conjunction expression. The second operand denotes the number of bit places to shift. Bits shifted off the left side of the value are discarded; new bits added on the right side will all be 0. Bits shifted off the right side are discarded; new bits added on the left side are usually 0, but if the first operand is a signed negative value, then the added bits will be either 0 or whatever value was previously in the leftmost bit position.
You can use the shift operators to perform a variety of interesting hacks. For example, given a date with the day of the month numbered as d , the month numbered as m , and the year y , you can store the entire date in a single number x :. You can then extract the original day, month, and year out of x using a combination of shift operators and modular division:. C provides operators for performing bitwise conjunction, inclusive disjunction, exclusive disjunction, and negation complement.
Biwise conjunction examines each bit in its two operands, and when two corresponding bits are both 1, the resulting bit is 1. All other resulting bits are 0. Here is an example of how this works, using binary numbers:.
Bitwise inclusive disjunction examines each bit in its two operands, and when two corresponding bits are both 0, the resulting bit is 0. All other resulting bits are 1. Bitwise exclusive disjunction examines each bit in its two operands, and when two corresponding bits are different, the resulting bit is 1.
In C, you can only use these operators with operands of an integer or character type, and for maximum portability, you should only use the bitwise negation operator with unsigned integer types. Here are some examples of using these operators in C code:.
Function pointers and data pointers are not compatible, in the sense that you cannot expect to store the address of a function into a data pointer, and then copy that into a function pointer and call it successfully.
See The goto Statement. This is called dereferencing the pointer. You can use the sizeof operator to obtain the size in bytes of the data type of its operand. The operand may be an actual type specifier such as int or float , as well as any valid expression. When the operand is a type name, it must be enclosed in parentheses. The sizeof operator can be used to automatically compute the number of elements in an array:. There are two cases where this technique does not work.
The second is where the array is in fact a function parameter see Function Parameters. You can use a type cast to explicitly cause an expression to be of a specified data type. A type cast consists of a type specifier enclosed in parentheses, followed by an expression. To ensure proper casting, you should also enclose the expression that follows the type specifier in parentheses.
In that example, since y and z are both integers, integer division is performed, and even though x is a floating-point variable, it receives the value 2. To fix this problem, you need to convert one of the operands to a floating-point type before the division takes place:. Type casting only works with scalar types that is, integer, floating-point or pointer types.
Therefore, this is not allowed:. You can access array elements by specifying the name of the array, and the array subscript or index, or element number enclosed in brackets. This means that many uses of an array name are equivalent to a pointer expression. It also means that you cannot subscript an array having the register storage class. You use the comma operator , to separate two ostensibly related expressions.
For instance, the first expression might produce a value that is used by the second expression:. This lets you conveniently set, monitor, and modify multiple control expressions for the for statement. A comma is also used to separate function parameters; however, this is not the comma operator in action. In fact, if the comma operator is used as we have discussed here in a function call, then the compiler will interpret that as calling the function with an extra parameter.
If you want to use the comma operator in a function argument, you need to put parentheses around it. You can use the member access operator. You use the conditional operator to cause the entire conditional expression to evaluate to either its second or its third operand, based on the truth value of its first operand.
If expression a is true, then expression b is evaluated and the result is the value of b. Otherwise, expression c is evaluated and the result is c. Here, if x equals 5, then a will receive the value of y. Otherwise, a will receive the value of z. This can be considered a shorthand method for writing a simple if … else statement.
The following example will accomplish the same task as the previous one:. If the first operand of the conditional operator is true, then the third operand is never evaluated. Similarly, if the first operand is false, then the second operand is never evaluated. The first operand is always evaluated. As a GNU C extension, you can build an expression using compound statement enclosed in parentheses. This allows you to included loops, switches, and local variables within an expression.
Recall that a compound statement also known as a block is a sequence of statements surrounded by braces. In this construct, parentheses go around the braces. That is a valid though slightly more complex than necessary expression for the absolute value of function.
The last thing in the compound statement should be an expression followed by a semicolon; the value of this subexpression serves as the value of the entire construct.
If you use some other kind of statement last within the braces, the construct has type void , and thus effectively no value. But this definition computes either a or b twice, with bad results if the operand has side effects. Embedded statements are not allowed in constant expressions, such as the value of an enumeration constant, the width of a bit field, or the initial value of a static variable. For instance, the meaning of that expression is to call the function f with no arguments, multiply the result by b , then add that result to a.
The following is a list of types of expressions, presented in order of highest precedence first. Sometimes two or more operators have equal precedence; all those operators are applied from left to right unless stated otherwise. The above list is somewhat dry and is apparently straightforward, but it does hide some pitfalls. Take this example:. There are other examples of potential surprises lurking behind the C precedence table. For this reason if there is the slightest risk of the reader misunderstanding the meaning of the program, you should use parentheses to make your meaning clear.
In C you cannot assume that multiple subexpressions are evaluated in the order that seems natural. Does this increment a before or after calling the function f? The compiler could do it in either order, so you cannot make assumptions. This manual explains the semantics of the C language in the abstract.
However, an actual compiler translates source code into specific actions in an actual computer, and may re-order operations for the sake of efficiency.
The correspondence between the program you write and the things the computer actually does are specified in terms of side effects and sequence points. These are essentially the externally-visible effects of running a program. The compiler is allowed to perform the operations of your program in an order different to the order implied by the source of your program, provided that in the end all the necessary side effects actually take place.
Another requirement on the compiler is that side effects should take place in the correct order. In order to provide this without over-constraining the compiler, the C89 and C90 standards specify a list of sequence points.
A sequence point is one of the following:. At a sequence point, all the side effects of previous expression evaluations must be complete, and no side effects of later evaluations may have taken place. This may seem a little hard to grasp, but there is another way to consider this. Imagine you wrote a library some of whose functions are external and perhaps others not and compiled it, allowing someone else to call one of your functions from their code.
The definitions above ensure that, at the time they call your function, the data they pass in has values which are consistent with the behaviour specified by the abstract machine, and any data returned by your function has a state which is also consistent with the abstract machine. This includes data accessible via pointers i. The above is a slight simplification, since compilers exist that perform whole-program optimisation at link time.
Importantly however, although they might perform optimisations, the visible side effects of the program must be the same as if they were produced by the abstract machine. The C standards both C89 and C99 both forbid this construct in conforming programs. Not allowed in a conforming program; modifies x twice before argument evaluation is complete.
Allowed; the function bar takes one argument the value 2 is passed here , and there is a sequence point at the comma operator. Allowed; there is a sequence point at the end of the controlling expression of the if 3. Allowed; there is a sequence point before the?
Not allowed; the object at p is being modified twice before the evaluation of the arguments to bar is complete. The fact that this is done once via p and once via q is irrelevant, since they both point to the same object.
Suppose the code actually looks like this:. Is this code allowed in a standard-conforming program? Although the expression in foo modifies a twice, this is not a problem.
Since f returns a value other than void, it must contain a return statement. Therefore, there is a sequence point at the end of the return expression. That comes between the modification to a that f makes and the evaluation of the left operand. First, a is incremented. Then the arguments to f are evaluated there are zero of them. Then there is a sequence point before f is actually called. So, we see that our program is standard-conforming. Notice that the above argument does not actually depend on the details of the body of the function f.
It only depends on the function containing something ending in a sequence point — in our example this is a return statement, but an expression statement or a full declarator would do just as well. If the left-hand operand is evaluated first, foo returns 6. Otherwise, it returns The C standard does not specify in which order the operands should be evaluated, and also does not require an implementation either to document the order or even to stick to one order.
The effect of this code is unspecified , meaning that one of several specific things will happen, but the C standards do not say which. When a signal is received, this will happen between sequence points.
Side effects on volatile objects prior to the previous sequence point will have occurred, but other updates may not have occurred yet. The C standard is quite restrictive about what data access can occur within a signal handler.
The POSIX standard also allows a small number of library functions to be called from a signal handler. These functions are referred to as the set of async-signal-safe functions. If your program is intended to run on a POSIX system but not on other systems, you can safely call these from your signal handler too.
You write statements to cause actions and to control flow within your programs. You can also write statements that do not do anything at all, or do things that are uselessly trivial. You can use labels to identify a section of source code for use with a later goto see The goto Statement. A label consists of an identifier such as those used for variable names followed by a colon. GCC will compile code that does not meet this requirement, but be aware that if you violate it, your code may have portability issues.
You can turn any expression into a statement by adding a semicolon to the end of the expression. In each of those, all that happens is that each expression is evaluated.
However, they are useless because they do not store a value anywhere, nor do they actually do anything, other than the evaluation itself. The compiler is free to ignore such statements. Expression statements are only useful when they have some kind of side effect, such as storing a value, calling a function, or this is esoteric causing a fault in the program.
Here are some more useful examples:. You can use the if statement to conditionally execute part of your program, based on the truth value of a given expression.
Here is the general form of an if statement:. If test evaluates to true, then then-statement is executed and else-statement is not. If test evaluates to false, then else-statement is executed and then-statement is not. The else clause is optional. You can use the switch statement to compare one expression with others, and then execute a series of sub-statements based on the result of the comparisons. Here is the general form of a switch statement:.
The switch statement compares test to each of the compare expressions, until it finds one that is equal to test. Then, the statements following the successful case are executed. All of the expressions compared must be of an integer type, and the compare-N expressions must be of a constant integer type e. Optionally, you can specify a default case. Notice the usage of the break statement in each of the cases. This is because, once a matching case is found, not only are its statements executed, but so are the statements for all following cases:.
This is often not desired. Including a break statement at the end of each case redirects program flow to after the switch statement. As a GNU C extension, you can also specify a range of consecutive integer values in a single case label, like this:. This has the same effect as the corresponding number of individual case labels, one for each integer value from low to high , inclusive.
Be careful to include spaces around the For example, write this:. It is common to use a switch statement to handle various possible values of errno. The while statement is a loop statement with an exit test at the beginning of the loop.
Here is the general form of the while statement:. The while statement first evaluates test. If test evaluates to true, statement is executed, and then test is evaluated again. The do statement is a loop statement with an exit test at the end of the loop. Here is the general form of the do statement:.
The do statement first executes statement. After that, it evaluates test. If test is true, then statement is executed again. The for statement is a loop statement whose structure allows easy variable initialization, expression testing, and variable modification. It is very convenient for making counter-controlled loops. Here is the general form of the for statement:.
The for statement first evaluates the expression initialize. Then it evaluates the expression test. If test is false, then the loop ends and program control resumes after statement.
Otherwise, if test is true, then statement is executed. Finally, step is evaluated, and the next iteration of the loop begins with evaluating test again. Here is another example that prints the integers from zero through nine:. First, it evaluates initialize , which assigns x the value 0. Then, as long as x is less than 10, the value of x is printed in the body of the loop. Then x is incremented in the step clause and the test re-evaluated.
All three of the expressions in a for statement are optional, and any combination of the three is valid. Since the first expression is evaluated only once, it is perhaps the most commonly omitted expression. You could also write the above example as:. In this example, x receives its value prior to the beginning of the for statement. If you leave out the test expression, then the for statement is an infinite loop unless you put a break or goto statement somewhere in statement. This is like using 1 as test ; it is never false.
This for statement starts printing numbers at 1 and then continues indefinitely, always printing x incremented by If you leave out the step expression, then no progress is made toward completing the loop—at least not as is normally expected with a for statement. Perhaps confusingly, you cannot use the comma operator see The Comma Operator for monitoring multiple variables in a for statement, because as usual the comma operator discards the result of its left operand.
This loop:. Here is an example of a function that computes the summation of squares, given a starting integer to square and an ending integer to square:. A block is a set of zero or more statements enclosed in braces. Blocks are also known as compound statements. Often, a block is used as the body of an if statement or a loop statement, to group statements together.
You can declare variables inside a block; such variables are local to that block. In C89, declarations must occur before other statements, and so sometimes it is useful to introduce a block simply for this purpose:. A null statement does not do anything. It does not store a value anywhere. It does not cause time to pass during the execution of your program.
Most often, a null statement is used as the body of a loop statement, or as one or more of the expressions in a for statement. Here is an example of a for statement that uses the null statement as the body of the loop and also calculates the integer square root of n , just for fun :.
Here is another example that uses the null statement as the body of a for loop and also produces output:. How to append more text to the old value of a variable. How to set a variable in the makefile even if the user has set it with a command argument.
An alternate way to set a variable to a multi-line string. How to undefine a variable so that it appears as if it was never set. Variable values can be defined on a per-target basis. Target-specific variable values can be applied to a group of targets that match a pattern. Referencing a variable with substitutions on the value. How to use goal arguments to specify which parts of the makefile to use. How to use mode flags to specify what kind of thing to do with the recipes in the makefile other than simply execute them.
How to override a variable to specify an alternate compiler and other things. How to proceed past some errors, to test compilation. See The eval Function. Expand the arguments, then open the file filename using mode op and write text to that file. See The file Function. Evaluates to the contents of the variable var , with no expansion performed on it. See The value Function. Here is a summary of the automatic variables. See Automatic Variables , for full information.
The names of all the prerequisites that are newer than the target, with spaces between them. For prerequisites which are archive members, only the named member is used see Archives. The names of all the prerequisites, with spaces between them. The stem with which an implicit rule matches see How Patterns Match.
Makefiles to be read on every invocation of make. Directory search path for files not found in the current directory.
See Recipe Execution. See Choosing the Shell. The name with which make was invoked. Using this variable in recipes has special meaning. The number of levels of recursion sub- make s. The flags given to make. You can set this in the environment or a makefile to set flags. See Communicating Options to a Sub- make. Other flags parsed by make.
You can set this in the environment or a makefile to set make command-line flags. GNU make never sets this variable itself. This variable will be seen by GNU make and ignored by other make implementations.
The targets given to make on the command line. Setting this variable has no effect on the operation of make. See Arguments to Specify the Goals. Set to the absolute pathname of the current working directory after all -C options are processed, if any.
0コメント