Professional Documents
Culture Documents
int age; 3
auto else long switch The type void
there must be at least one whitespace
The type specifier void indicates
character (usually a space) between int and
that no value is available.
break enum register typedef age for the compiler to be able to distinguish
them. On the other hand, in the following
statement − 4
Derived types
case extern return union fruit = apples + oranges; // get
the total fruit They include (a) Pointer types,
(b) Array types, (c) Structure
no whitespace characters are necessary types, (d) Union types and (e)
char float short unsigned
between fruit and =, or between = and apples, Function types.
although you are free to include some if you
wish to increase readability.
const for signed void The array types and structure types are
referred collectively as the aggregate types.
The type of a function specifies the type of the
Data types in c refer to an extensive system function's return value. We will see the basic
used for declaring variables or functions of
types in the following section, where as other printf("SHRT_MIN : %d\n",
types will be covered in the upcoming chapters. to
SHRT_MIN);
9223372036854775807
printf("UCHAR_MAX : %d\n",
Integer Types
UCHAR_MAX);
The following table provides the details of printf("UINT_MAX : %u\n",
standard integer types with their storage sizes unsigne 8 bytes 0 to (unsigned int) UINT_MAX);
and value ranges − d long 1844674407370955161 printf("ULONG_MAX : %lu\n",
5 (unsigned long) ULONG_MAX);
Type Storag Value range printf("USHRT_MAX : %d\n",
(unsigned short) USHRT_MAX);
e size
To get the exact size of a type or a variable on return 0;
a particular platform, you can use }
char 1 byte -128 to 127 or 0 to 255 the sizeof operator. The
expressions sizeof(type) yields the storage size When you compile and execute the above
of the object or type in bytes. Given below is an program, it produces the following result on
example to get the size of various type on a Linux −
unsigne 1 byte 0 to 255 machine using different constant defined in
d char limits.h header file − CHAR_BIT : 8
CHAR_MAX : 127
#include <stdio.h> CHAR_MIN : -128
#include <stdlib.h> INT_MAX : 2147483647
signed 1 byte -128 to 127
#include <limits.h> INT_MIN : -2147483648
char LONG_MAX : 9223372036854775807
#include <float.h>
LONG_MIN : -9223372036854775808
int main(int argc, char** argv) { SCHAR_MAX : 127
-32,768 to 32,767 or SCHAR_MIN : -128
2 or 4 SHRT_MAX : 32767
int -2,147,483,648 to printf("CHAR_BIT : %d\n",
bytes CHAR_BIT); SHRT_MIN : -32768
2,147,483,647
printf("CHAR_MAX : %d\n", UCHAR_MAX : 255
CHAR_MAX); UINT_MAX : 4294967295
printf("CHAR_MIN : %d\n", ULONG_MAX : 18446744073709551615
unsigne 2 or 4 0 to 65,535 or 0 to CHAR_MIN); USHRT_MAX : 65535
d int bytes 4,294,967,295 printf("INT_MAX : %d\n", Floating-Point Types
INT_MAX);
printf("INT_MIN : %d\n", The following table provide the details of
short 2 bytes -32,768 to 32,767 INT_MIN); standard floating-point types with storage sizes
printf("LONG_MAX : %ld\n", and value ranges and their precision −
(long) LONG_MAX);
printf("LONG_MIN : %ld\n", Type Storage Value range Precision
unsigne 2 bytes 0 to 65,535 (long) LONG_MIN); size
d short printf("SCHAR_MAX : %d\n",
SCHAR_MAX);
printf("SCHAR_MIN : %d\n",
SCHAR_MIN); float 4 byte 1.2E-38 to 6 decimal
long 8 bytes -
printf("SHRT_MAX : %d\n", 3.4E+38 places
9223372036854775808
SHRT_MAX);
When you compile and execute the above
double 8 byte 2.3E-308 to 15 decimal program, it produces the following result on A pointer of type void *
1.7E+308 places Linux − represents the address of an
Storage size for float : 4 object, but not its type. For
FLT_MAX : 3.40282e+38 example, a memory allocation
long 10 byte 3.4E-4932 to 19 decimal FLT_MIN function void *malloc( size_t
: 1.17549e-38
double 1.1E+4932 places -FLT_MAX : -3.40282e+38 size ); returns a pointer to
-FLT_MIN : -1.17549e-38 void which can be casted to
DBL_MAX : 1.79769e+308 any data type.
The header file float.h defines macros that DBL_MIN : 2.22507e-308
allow you to use these values and other details -DBL_MAX : -1.79769e+308 A variable is nothing but a name given to a
about the binary representation of real numbers Precision value: 6 storage area that our programs can manipulate.
in your programs. The following example prints The void Type Each variable in C has a specific type, which
the storage space taken by a float type and its determines the size and layout of the variable's
range values − The void type specifies that no value is
memory; the range of values that can be stored
available. It is used in three kinds of situations
within that memory; and the set of operations
−
that can be applied to the variable.
#include <stdio.h> Sr.No Types & Description The name of a variable can be composed of
#include <stdlib.h> . letters, digits, and the underscore character. It
#include <limits.h> must begin with either a letter or an
#include <float.h> underscore. Upper and lowercase letters are
distinct because C is case-sensitive. Based on
int main(int argc, char** argv) { 1
Function returns as void the basic types explained in the previous
chapter, there will be the following basic
printf("Storage size for float : There are various functions in variable types −
%d \n", sizeof(float)); C which do not return any
printf("FLT_MAX : %g\n", value or you can say they
Sr.No Type & Description
(float) FLT_MAX); return void. A function with no
printf("FLT_MIN : %g\n", return value has the return .
(float) FLT_MIN); type as void. For
printf("-FLT_MAX : %g\n", example, void exit (int
(float) -FLT_MAX); status); 1
printf("-FLT_MIN : %g\n", char
(float) -FLT_MIN); Typically a single octet(one byte).
printf("DBL_MAX : %g\n", 2
Function arguments as void It is an integer type.
(double) DBL_MAX);
printf("DBL_MIN : %g\n", There are various functions in
(double) DBL_MIN); C which do not accept any 2
int
printf("-DBL_MAX : %g\n", parameter. A function with no
(double) -DBL_MAX); parameter can accept a void. The most natural size of integer for
printf("Precision value: %d\n", For example, int rand(void); the machine.
FLT_DIG );
return 0; 3 3
Pointers to void float
}
consists of an equal sign followed by a constant
A single-precision floating point expression as follows −
value. type variable_name = value; #include <stdio.h>
1 1
1 0 ^ Binary XOR Operator copies the bit if it is set in one operand but not both.
+= Add AND assignment operator. It adds the r
result to the left operand.
Assume A = 60 and B = 13 in binary format,
they will be as follows −
A = 0011 1100 ~
B = 0000 1101 -= Subtract AND assignment operator. It subtra
Binary One's Complement Operator is unary and has the assigns
effect ofthe
'flipping'
result bits.
to the left operand.
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001 << Binary Left Shift Operator. The left operands *=
value is moved
Multiply
left by
AND
the assignment
number of bits
operator. It multip
specified by the right operand. assigns the result to the left operand.
~A = 1100 0011
The following table lists the bitwise operators
supported by C. Assume variable 'A' holds 60
and variable 'B' holds 13, then −
Show Examples >> Binary Right Shift Operator. The left operands/=value is moved
Divide
right
AND byassignment
the numberoperator.
of bits It divides
specified by the right operand. assigns the result to the left operand.
Operato
r
Operato
<<= Left shift AND assignment operator.
r
* Pointer to a variable.
>>= Right shift AND assignment operator. ?: Conditional Expression. Relational <<
Operators Precedence in C
Equality
Operator precedence determines the grouping
of terms in an expression and decides how an
&= Bitwise AND assignment operator. expression is evaluated. Certain operators Bitwise AND
have higher precedence than others; for
example, the multiplication operator has a
higher precedence than the addition operator.
Bitwise XOR
For example, x = 7 + 3 * 2; here, x is assigned
^= Bitwise exclusive OR and assignment operator.
13, not 20 because operator * has a higher
precedence than +, so it first gets multiplied
Bitwise OR
with 3*2 and then adds into 7.
Here, operators with the highest precedence
appear at the top of the table, those with the Logical AND
|= Bitwise inclusive OR and assignment operator.
lowest appear at the bottom. Within an
expression, higher precedence operators will
be evaluated first.
Logical OR
Show Examples
Misc Operators ↦ sizeof & ternary
Category Conditional
Besides the operators discussed above, there
are a few other important operators
including sizeof and ? : supported by the C
Postfix Assignment = += -= *= /= %
Language.
Show Examples
Unary + - ! ~ ++ - - (type)* &Comma
sizeof
Operato Description
r
Multiplicative
sizeof() Returns the size of a variable. Decision making structures require that the
programmer specifies one or more conditions
Additive to be evaluated or tested by the program, along
with a statement or statements to be executed
& Returns the address of a variable. if the condition is determined to be true, and
Shift optionally, other statements to be executed if
the condition is determined to be false.
Show below is the general form of a typical You may encounter situations, when a block of
decision making structure found in most of the code needs to be executed several number of
An if statement can be followed by an optional
programming languages − expression is false. times. In general, statements are executed
sequentially: The first statement in a function is
executed first, followed by the second, and so
3 nested if statements on.
You can use one if or else if statement inside Programming
another languages provide various
control structures that allow for more
complicated execution paths.
4 switch statement
A loop statement allows us to execute a
A switch statement allows a variable to be tested for equality
statement or against a liststatements
group of of values. multiple
times. Given below is the general form of a loop
statement in most of the programming
5 nested switch statements languages −
You can use one switch statement inside another
The ? : Operator
We have covered conditional operator ? : in
the previous chapter which can be used to
replace if...else statements. It has the following
general form −
Exp1 ? Exp2 : Exp3;
Where Exp1, Exp2, and Exp3 are expressions.
C programming language assumes any non- Notice the use and placement of the colon.
zero and non-null values as true, and if it is
either zero or null, then it is assumed The value of a ? expression is determined like
as false value. this −
int main () {
int enable
Passing an argument by reference or by address main the
() passed
{ argument to be changed in the calling
6
function by the called function. strstr(s1, s2);
char greeting[6] = {'H', 'e',
Returns a pointer to the first occurrence of s
'l', 'l', 'o', '\0'};
5 Return pointer from functions in C printf("Greeting message: %s\n",
greeting ); The following example uses some of the
C allows a function to return a pointer to the local
returnvariable,
0; static variable, and dynamically allocated
memory as well. above-mentioned functions −
}
#include <stdio.h>
if( argc == 2 ) {
printf("The argument supplied
is %s\n", argv[1]);
}
else if( argc > 2 ) {
printf("Too many arguments
supplied.\n");
}
else {
printf("One argument
expected.\n");
}
}