Beruflich Dokumente
Kultur Dokumente
It is int which causes the greatest confusion. Some people are certain that an int has 16
bits and sizeof(int) is 2. Others are equally sure that an int has 32 bits and sizeof(int) is 4.
Who is right? On any given compiler, one or the other could be right. On some compilers,
both would be wrong. I know of one compiler for a 24 bit DSP where an int has 24 bits.
The actual range of values which the signed and unsigned int types can hold are:
A signed int can hold all the values between INT_MIN and INT_MAX inclusive.
INT_MIN is required to be -32767 or less, INT_MAX must be at least 32767. Again,
many 2's complement implementations will define INT_MIN to be -32768 but this is
not required.
An unsigned int can hold all the values between 0 and UINT_MAX inclusive.
UINT_MAX must be at least 65535. The int types must contain at least 16 bits to hold
the required range of values.
NOTE: The required ranges for signed and unsigned int are identical to those for signed and unsigned
short. On compilers for 8 and 16 bit processors (including Intel x86 processors executing in 16 bit mode,
such as under MS-DOS), an int is usually 16 bits and has exactly the same representation as a short. On
compilers for 32 bit and larger processors (including Intel x86 processors executing in 32 bit mode, such as
Win32 or Linux) an int is usually 32 bits long and has exactly the same representation as a long.
A signed short can hold all the values between SHRT_MIN and SHRT_MAX inclusive.
SHRT_MIN is required to be -32767 or less, SHRT_MAX must be at least 32767.
Again, many 2's complement implementations will define SHRT_MIN to be -32768 but
this is not required.
An unsigned short can hold all the values between 0 and USHRT_MAX inclusive.
USHRT_MAX must be at least 65535.
The short types must contain at least 16 bits to hold the required range of values.
On many (but not all) C implementations, a short is smaller than an int. Programs which
need to have very large arrays of integer values in memory, or store very large numbers
of integers in files might save memory or storage space by using short in place of int if
both of the conditions below are met:
1. Short is truly smaller than int on the implementation.
2. All of the required values can fit into a short.
NOTE: On some processor architectures code to manipulate shorts can be larger and
slower than corresponding code which deals with ints. This is particularly true on the
Intel x86 processors executing 32 bit code, as in programs for Windows (NT/95/98),
Linux, and other UNIX derivatives. Every instruction which references a short in such
code is one byte larger and usually takes extra processor time to execute.
A signed long can hold all the values between LONG_MIN and LONG_MAX inclusive.
LONG_MIN is required to be -2147483647 or less, LONG_MAX must be at least
2147483647. Again, many 2's complement implementations will define LONG_MIN to
be -2147483648 but this is not required.
An unsigned long can hold all the values between 0 and ULONG_MAX inclusive.
ULONG_MAX must be at least 4294967295. The long types must contain at least 32
bits to hold the required range of values.
The 1999 update to the ANSI/ISO C language standard has added a new integer type to
C, one that is required to be at contain at least 64 bits.
Included in this update are the new variable types signed and unsigned long long . The
selected name comes from gcc and several other compilers which already provide this
type as an extension. On 32 bit Windows compilers from Microsoft, Borland (and maybe
others) this same extension has the name __int64.
While the C++ standard is quite new and most likely will not change for several years, it
is almost certain that C++ compilers will add support for these types as well. The C++
compilers which come with the implementations mentioned above do.
There are two types of long long int, signed and unsigned. If neither is specified the long
long is signed. The "int" in the declaration is optional. All 6 of the following declarations
are correct:
A signed long long can hold all the values between LLONG_MIN and LLONG_MAX
inclusive. LLONG_MIN is required to be -9223372036854775807 or less,
LLONG_MAX must be at least 9223372036854775807. Again, many 2's complement
implementations will define LLONG_MIN to be -9223372036854775808 but this is not
required.
An unsigned long long can hold all the values between 0 and ULLONG_MAX inclusive.
ULLONG_MAX must be at least 18446744073709551615. The long types must contain
at least 64 bits to hold the required range of values.
Values In <limits.h>
The standard header <limits.h> contains macros which expand to values which allow a
program to determine information about the ranges of values each integer type can hold
at run time. Each of these types (except for "plain" char, that is char without a signed or
unsigned in front of it) must be able to contain a minimum range of values.
SCHAR_MIN -127
signed char
SCHAR_MAX 127
0
unsigned char
UCHAR_MAX 255
CHAR_MIN
"plain" char (note 1)
CHAR_MAX
SHRT_MIN -32767
signed short
SHRT_MAX 32767
0
unsigned short
USHRT_MAX 65535
INT_MIN -32767
signed int
INT_MAX 32767
0
unsigned int
UINT_MAX 65535
LONG_MIN -2147483647
signed long
LONG_MAX 2147483647
0
unsigned long
ULONG_MAX 4294967295
LLONG_MIN -9223372036854775807
signed long long
LLONG_MAX 9223372036854775807
0
unsigned long long
ULLONG_MAX 18446744073709551615
OPERATORS:
C includes a large number of operators which fall into different categories. These are
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
Unary Operators
Conditional Operators
Bit-Wise Operators
Arithmetic Operators:
/ Division a / b is 5 c / d is 0.4
b / a is 3 d / c is 2.5
% Remainder a % b is 5
(Modulo b % a is 1 Not Possible
Division)
Each operator manipulates two operands, which may be constants, variables, or other
arithmetic expression. The arithmetic operators may be used with int or double data type
operands. On the other hand, the remainder operator also known as modulus operator can
be used with integer operands to find the remainder of the division.
For example
Relational Operators:
The double equal sign ‘= =’ used to compare equality is different from the single equal to
‘=’ sign which is used as an assignment operator.
These six operators are used to form logical expressions, which represent conditions that
are either true or false. The result of the expressions is of type integer, since true is
represented by the integer value 1 and false is represented by the value 0.
Among the relational and equality operators each operator is a complement of another
operator in group.
Logical Operators:
In addition to arithmetic and relational operators, C has 3 logical operators for combining
logical values and creating new logical values. These operators are
Logical Operator Meaning
! NOT
&& Logical AND
|| Logical OR
NOT:
The NOT operator (!) is a unary operator (Operator that acts upon single operand). It
changes a true value (1) to false (zero) and a false value to true.
AND:
The AND operator (&&) is a binary operator. Its result is true only when both operands
are true; otherwise it is false.
OR:
The OR operator (| |) is a binary operator. Its result is false only when both the operands
are false; otherwise true.
Assignment Operator:
Assignment operator assigns the value of expression on the right side of it to the variable
on the left of it. The assignment expression evaluates the operand on the right side of the
operator (=) and places its value in the variable on the left. Assignment expressions that
make use of assignment operator have the form:
Identifier = expression;
Ex: a = 2;
Area = length * width;
Note:
If the two operands in an assignment expression are of different data types, then
the value of the expression on the right side of assignment operator will
automatically be converted to the type of the identifier on the left of assignment
operator.
For example
A floating value may be truncated if it is assigned to an integer identifier.
A double value may be rounded if it is assigned to a floating point identifier.
An integer value may be changed if it is assigned to a short integer identifier or to
a character identifier.
Unary Operators:
The operators that act upon a single operand to produce a new value are known as unary
operators. C supports the following unary operators.
Operators:
Minus operator –
Increment operator ++
Decrement operator - -
Size operator
(type) operator
Minus operator –
The most common unary operation is a unary minus, where a numerical constant,
variable or expression is preceded by a minus sign. It is important to note that the unary
minus operation is distinctly different from the arithmetic operator which denotes
subtraction (-). The subtraction operator requires two separate operands.
The increment operator (++) adds 1 to the operand, whereas the decrement operator (--)
subtract 1 from the operand. The increment and decrement operators can each be used in
two different ways, depending on whether the operator is written before or after the
operand. For example
a++; /* operator is written after the operand */
++a; /* operator is written before the operand */
If the operator precedes the operand (e.g., ++a), then it is called pre-increment operator
and the operand will be changed in value before it is used for its intended purpose within
the program. On the other hand, if the operator follows the operand (i.e a++), then it is
called post-increment operator and the value of the operand will be changed after it is
used.
sizeof Operator:
In C, an operator sizeof is used to calculate size of various datatypes. These can be basic
or primitive datatypes present in the language, as well as the ones, created by
programmer. The sizeof opearator looks like a function, but it is actually an operator that
returns the length, in bytes. It is used to get size of space any data-element/datatype
occupies in memory. If type name is used, it always needs to be enclosed in parentheses,
whereas variable name can be specified with or without parentheses.
Ex:
int i;
sizeof i;
sizeof(int);
Rather than let the compiler implicitly convert data, we can convert data from one type to another
by using cast operator. To cast data from one type to another it is necessary to specify type in
parentheses before the value we want to be converted. For example to convert integer variable
count, to a float we code the expression as
(float) count;
Since cast operator is an unary operator, we must put binary expression to be casted in
parentheses to get the correct conversion. For example
(float) (a+b)
One use of the cast is to ensure that the result of a division is a floating point number. In case of
division, proper casting is necessary.
Conditional Expressions
Simple conditional operations can be carried out with the conditional operator (? :). The
conditional operator (? :) is a ternary operator since it takes three operands. The general form of
conditional expression is
Therefore the result of the conditional operator is the result of whichever expression is evaluated
– the first or the second. Only on of the last 2 expressions is evaluated in a conditional
expression.
Bitwise operators
The bitwise operators are the bit manipulation operators. They can manipulate individual
bits within the piece of data. These operators can operate on integers and characters but
not on floating point numbers or numbers having double data type.
& Address
Ternary (Conditional)
Ternary ? : Right to left 13
Assignment
=
Addition/Subtraction
+= -=
Assignment
Multiplication/Division
*= /=
Assignment
Assignment Right to left 14
Modulus/bitwise AND
%= &=
assignment
Bitwise exclusive/inclusive
^= !=
OR assignment
Bitwise Shift left/right
<<= >>=
assignment
Comma (separate
Comma , Expression) Left to right 15
Reading data, processing it and writing the processed data known as information or a
result of a program are the essential functions of a program. The C is a functional
language. It provides a number of macros and functions to enable the programmer to
effectively carry out these input and output operations. Some of the input/output
functions / macros in C are
1. getchar() 2. putchar()
3. scanf() 4. printf()
5. gets() 6. puts()
The data entered by the user through the standard input device and processed data is
displayed on the standard output device.
I/O Functions:
The Formatted I/O functions allows programmers to specify the type of data and the way
in which it should be read in or written out. On the other hand, unformatted I/O functions
do not specify the type of data and the way is should be read or written. Amongst the
above specified I/O functions scanf() and printf() are formatted I/O functions.
Function Formatted Unformatted
Input scanf() getchar(),gets()
C provides the printf function to display the data on the monitor. This function can be
used to display any combination of numerical values, single characters and strings. The
general form of printf statement
Function name
Function arguments
%c Single character
There are certain letters which may be used as prefix for certain placeholders. These are
h: the h letter can be applied to d,i,o,u and x placeholders. The h letter tells printf() to
display short integers. As an example, %hu indicates that the data is of type short
unsigned integer.
l: the l letter can be applied to d,i,o,u and x placeholder. The l letter tells printf() to
display long integers. As an example, %ld indicates that the data is of type long integer.
When it is applied to f, it indicates double data type.
We have seen that how integer numbers can be displayed on the monitor using %d
placeholder. In case of integer numbers, the placeholder can accept modifiers to
specify the minimum field width and left justification by default, all output is right
justified. We can force the information to be left justified by putting a minus sign
directly after the %. For example
printf statement Output (assume x=1234)
1 2 3 4
printf(“%d”,x);
1 2 3 4
printf(“%6d”,x);
Right justified
1 2 3 4
printf(“%-6d”,x);
Left justified
As shown in the above examples, if the number width in digits is less than the minimum
field width the empty places are filled with spaces. However if the number width is
greater than the minimum field width, the number is printed in the full, overriding the
minimum field width specification. This is illustrated in the following list.
It is also possible to pad the empty places with zeros. If we want to pad with zeros, we
have to place a zero before the minimum field width specifier.
1 2 3 4
printf(“%3d”,x);
Overriding the minimum field width
0 0 1 2 3 4
printf(“%06d”,x);
Padding with zeros
As shown in the above examples, if the number width in digits is less than the minimum
field width the empty places are filled with spaces. However if the number width is
greater than the minimum field width, the number is printed in the full, overriding the
minimum field width specification. This is illustrated in the following list.
It is also possible to pad the empty places with zeros. If we want to pad with zeros, we
have to place a zero before the minimum field width specifier.
0 0 1 2 3 4
printf(“%06d”,x);
Padding with zeros
Formatting floating point output
In case of floating point numbers placeholder can accept modifiers that specify the
minimum field width, precision and left justification. To add a modifier we have to place
a decimal point follower by the precision after the field width specifier. For e and f
formats, the precision modifier determines the number of decimal places to be displayed.
For example %8.4f will display a number at least 8 characters wide including decimal
point with four decimal places.
3 4 5 6 . 1 2 0 6 5 0
printf(“%f”,x);
Default precision is 6
3 4 5 6 . 1 2
printf(“%7.2f”,x);
3 4 5 6 . 1 2
printf(“%9.2f”,x);
Right justified
3 4 5 6 . 1 2
printf(“%-9.2f”,x);
left justified
3 . 4 5 6 E + 0 3
printf(“%9.3e”,x);
When the precision is applied to strings, the number preceding period specifies the
minimum field width and the number following the period specifies the number of
characters of the string to be displayed. For examples %16.9s will display a string that
will be at least sixteen characters long; however only first nine characters from the string
are displayed with remaining blank characters. Let us see the important points related to
formatted string display.
When minimum field width is specified without negative sign, display is right
justified.
When only minimum field width is specified and it is less than the actual string
length, the minimum field width is overridden and complete string is displayed.
We can insert negative sign before minimum field width to get left justified
display.
T H I S I S A T E S T S T R I N G
printf(“%s”,str);
T HI S I S A T ES T S T R I NG
printf(“%24s”,
str);
T H I S I S A T ES T
printf(“%24.14s
”,str);
The backslash symbol( \ ) is known as escape character. Since the newline character and
the tab character begin with backslash they are called escape sequences. They are
Escape Function
Sequences
Newline : Displays the message or variable values following it on
\n
the next line.
Tab: Moves cursor to the next tab
\t
Functions
In structural programming, the program is divided into small independent tasks. These
tasks are small enough to be understood easily without having to understand the entire
program at once. Each task is designed to perform specific functionality on its own.
When these tasks are performed, their outcomes are combined together to solve the
problem. Such a structural programming can be implemented using modular
programming. In modular programming, the program is divided into separate small
programs called modules. Each module is designed to perform specific function. Modules
make out actual program shorter, hence easier to read and understand. A defined function
or set of similar functions is coded in a separate module or sub module, which means that
code can be loaded into memory more efficiently and that modules can be reused in other
programs. After a module has been tested individually, it is then integrated with other
modules into the overall program structure.
Main
Module
The main module is known as a calling module because it has submodule. Each of the
sub module is known as called modules. However, modules 1, 2 and 3 also have sub
modules therefore they are also calling modules. The communication between modules
in a structure chart is allowed only through a calling module. If module 1 need to send
data to module 2, the data must be passed through the calling module, main module. No
communication can take place directly between modules that do not have a clling-called
relationship. This means that in a structural chart, a module can be called by one and only
one higher module.
There are several advantages of modular / structural programming, some of them are
Easy Debugging: Since each function is smaller and has a logical clarity, it is easy to
locate and correct errors in it.
Basics of Functions
Ex:
#include <stdio.h>
Ex:
The following is function that computes the xn for given values of x and n.
for(k=1;k<=n;k++)
p=p*x;
return p;
}
The function is called from another function. For example, we can call the above
function form main(). This function returns the value of xn.
power(x,n);
Depending upon whether arguments are passed or not, and whether a value from the
function is returned or not, can be classified into the following four categories.
float power(float,int);
The arguments passing from the calling function are known as actual parameters; the
parameters in the called function are named as formal parameters. The following is an
example:
If we call power(x,n) from the main(), then the actual arguments are x and n. In the called
function, float power(float p, int q), p and q are formal argument or parameters. After
execution of this function, the function returns a float-point value to the calling function.
When a function is called, the data is A function definition starts with the type
passed from a calling function as of the function. The function name is
arguments. These arguments are known followed by a list of argument declaration
as actual arguments. Following is an within parentheses. The arguments act as
example of calling function. placeholders for values that are passed
when function is called. These are called
main() formal arguments. A function example:
{
float func(float,int); float func(float y, int m)
…… {
func(x,n); ……..
…… return …..;
} }
The names of the formal arguments The actual and formal arguments must
need not be the same as the names of the match in number, type and order.
actual arguments.
Actual arguments must be assigned The values of the actual arguments are
values before a function call is made. assigned to the formal variables on a one-
to-one basis.
When a function call is made, only a
copy of the values of actual arguments is The formal arguments y and m are local to
passed to the called function. The the function.
processing inside the called function
does not have any effect on the variables
used in the actual argument list.
The global variables are declared The variables declared within a function
outside a function. These variables are are called local variables. Their scope and
available to all the functions of a life-time exists as long as the execution of
program. Example the function. Local variables vanish when
function is terminated.
int a,b,c; /* global variable */
main() int func1()
{ {
….. flaot x,y; /* local variables */
Func1(); ........
Func2(); x=a+b ;
} y=b*c+10 ;
.........
When we declare variables as global, }
they are automatically initialized to zero
in case of numeric variables, and null in Local variables contain garbage values if
case of character variables. they are not initialized.