For details on tokens, check part 1
Operator precedence determines the grouping of terms in an expression and decides how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has a higher precedence than the addition operator.
Category | Operator | Associativity |
---|---|---|
Postfix | () [] −> . ++ −− |
Left to right |
Unary | + − ! ~ ++ −− (type) * & sizeof |
Right to left |
Multiplicative | * / % |
Left to right |
Additive | + − |
Left to right |
Shift | << >> |
Left to right |
Relational | < <= > >= |
Left to right |
Equality | == != |
Left to right |
Bitwise AND | & |
Left to right |
Bitwise XOR | ^ |
Left to right |
Bitwise OR | | | Left to right |
Logical AND | && |
Left to right |
Logical OR | || | Left to right |
Conditional (Ternary) | ?: |
Right to left |
Assignment | = += −= *= /= %= >>= <<= &= ^= |= |
Right to left |
Comma | , |
Left to right |
(type)
means type casting operation
The following special symbols are used in C having some special meaning and thus, cannot be used for some other purpose.
[] () {} , ; * = #
[]
: Opening and closing brackets are used as array element reference. These indicate single and multidimensional subscripts()
: These special symbols are used to indicate function calls and function parameters.{}
: These opening and ending curly braces mark the start and end of a block of code containing more than one executable statement.,
): It is used to separate more than one statements like for separating parameters in function calls.:
): It is an operator that essentially invokes something called an initialization list.;
): It is known as a statement terminator. It indicates the end of one logical entity. That’s why each individual statement must be ended with a semicolon.*
): It is used to create a pointer variable and for the multiplication of variables.=
): It is used to assign values and for the logical operation validation.#
): The preprocessor is a macro processor that is used automatically by the compiler to transform your program before actual compilation.A statement that gives a finite value.
Types:
In most cases, we made need to show values or status of the program during the program execution.
printf
statementIt is usually done by using the printf
statement.
Syntax:
printf("format specifier", identifier or expression);
printf("statement");
%d
- integer (int)%f
- float%c
- character (char)%s
- string (str)%lf
- doubleWriting your first Program - printing “Hello World”
Printing out values by functions like putc
, puts
, fputc
, fputs
, etc. will be dealt with later.
Modifiers are key words that modify the default state of int
and char
data types. There are 4 modifiers in C
:
1. short
:
It limits user to store small integer values from -32768
to 32767
. It can be used only on int
data type.
short int myShortInt = 10;
2. long
:
It allows storage of large value than default limit for int
. Tt can be use once or twice to mention the limits.
long int myLongInt;
long myLongerInt;
long long myLongLongInt;
3. signed
:
It is default modifier of int
and char
data type if no modifier is specified. It says that user can store negative and positive values.
int myValue = -289;
signed int myValues = -544;
4. unsigned
:
When user intends to store only positive values in the given data type (int
and char
).
unsigned int myVal = 10;
To check the limitations of the modifiers, check Part 3
Having understood the modifiers in C, we can look at the complete list of format specifiers.
Format Specifier | Type |
---|---|
%c |
Character |
%d |
Signed integer |
%e or %E |
Scientific notation of floats |
%f |
Float values |
%g or %G |
Similar as %e or %E |
%hi |
Signed integer (short ) |
%hu |
Unsigned Integer (short ) |
%i |
Unsigned integer |
%l or %ld or %li |
Long or long int |
%lf |
Double |
%Lf |
Long double |
%lu |
Unsigned int or unsigned long |
%lli or %lld |
Long long |
%llu |
Unsigned long long |
%o |
Octal representation |
%p |
Pointer |
%s |
String |
%u |
Unsigned int |
%x or %X |
Hexadecimal representation |
%n |
Prints nothing |
%% |
Prints % character |
The above are the basic format specifiers. We can add some other parts with the format specifiers for custom formatting. These are like below:
A minus symbol (-
) sign tells left alignment
A number after %
specifies the minimum field width. If string is less than the width, it will be filled with spaces
A period (.
) is used to separate field width and precision
Lines that are not executed. It used only for understanding by the programmers or users.
//
is used to comment a single line
/**/
- comment multiple lines
/***/
- Documentation
// single line comment
/*
This is used to comment multiple lines
*/
/**
* Documentation String
*/
scanf("format specifier", &identifier);
The format specifiers are as listed for printf
part 1. It is to be noted that for all format specifiers except %c
, the input is taken till the next character < space (in terms ASCII value), mostly \n
or space (' '
)
Few special characters defined under ASCII for formatting strings/output. The \
is used to escape the original use of a character.
Sequence | Explanation |
---|---|
\ \ | Back slash |
\‘ | Apostrophe or Single quotes |
\n | new line or line feed |
\f | form feed |
\r | carriage return |
\t | horizontal tab |
\“ | Double quotes |
\0 | Null character |
\a | bell |
\v | vertical tab |
Converting one datatype into another is known as type casting or, type-conversion. For example, if you want to store a ‘long’ value into a simple integer then you can type cast long
to int
.
It happens in two ways:
At points, we use some operation that needs values in different type than defined. The compiler, by default, converts the data type automatically to support the expression.
This is done by the Arithmetic converter
. The following is the basic hierarchy of conversion.
$$\text{long double}$$ $$\uparrow$$ $$\text{double} $$ $$\uparrow$$ $$\text{float}$$ $$\uparrow$$ $$\text{unsigned long long}$$ $$\uparrow$$ $$\text{long long}$$ $$\uparrow$$ $$\text{unsigned long}$$ $$\uparrow$$ $$\text{long}$$ $$\uparrow$$ $$\text{unsigned int}$$ $$\uparrow$$ $$\text{int}$$
Eg:
#include <stdio.h>
int main() {
int i = 17;
char c = 'c'; /* ascii value is 99 */
float sum;
sum = i + c;
printf("Value of sum : %f\n", sum);
return 0;
}
output
Value of sum : 116.000000
Here, it is simple to understand that first c gets converted to integer, but as the final value is float
, usual arithmetic conversion applies and the compiler converts i
and c
into float
and adds them yielding a float
result.
To have custom type conversion that are not supported by default, we do type casting.
Syntax:
(typeName) expression
Eg:
#include <stdio.h>
int main() {
int sum = 17, count = 5;
double mean;
mean = (double) sum / count;
printf("Value of mean : %f\n", mean );
return 0;
}
Output
Value of mean : 3.400000