C Theory Original
C Theory Original
Facts about C
C was invented to write an operating system called UNIX.
C is a successor of B language which was introduced around 1970
The language was formalized in 1988 by the American National
Standard Institue (ANSI).
By 1973 UNIX OS almost totally written in C.
Today C is the most widely used System Programming Language.
Most of the state of the art software have been implemented using C
Where is C useful?
C’s ability to communicate directly with hardware makes it a powerful
choice for system programmers. In fact, popular operating systems such
as Unix and Linux are written entirely in C. Additionally, even compilers
and interpreters for other languages such as FORTRAN, Pascal, and
BASIC are written in C. However, C’s scope is not just limited to
developing system programs. It is also used to develop any kind of
application, including complex business ones. The following is a partial
list of areas where C language is used:
Ø Embedded Systems
Ø Systems Programming
Ø Artificial Intelligence
Ø Industrial Automation
Ø Computer Graphics
Ø Space Research
Ø Image Processing
Ø Game Programming
C Language Advantages :
C Language has list of advantages due to this it is very much popular
language around the world and best suitable for the programmer to
learn at the fist stage of the programming.
1. Procedure Oriented Language
C Language is procedure oriented language, Here user creates
procedures or functions to execute their task. Procedure oriented
language is very much easy to learn because it follows algorithm to
execute your statements. To develop program using procedure oriented
language, you need to draw/prepare algorithm and then start
converting it into procedure or functions.
2. Lots of Libraries
C Language provides lots of functions which consist of system generated
functions and user defined functions. C Compiler comes with list of
header files which consist of many general functions which can be used
to develop program. while programmer can also create function as per
their requirements that is called as user generated/defined function.
3. Speed of Compilation
C compiler produces machine code very fast compared to other
language compiler. C compiler can compile around 1000 lines of code in
a seconds or two. One more benefit of the C Compiler is that it also
optimize the code for faster execution.
4. Easy to Learn (Syntax is near to English Language)
C Language syntax is very easy to understand. It uses keyword like if,
else, goto, switch, goto, main, etc. This kind of keyword we all are using
in our day to day life to convey meaning or to get some decisions.
5. Portable
C Language setup is around 3-5 MB. So you can carry this language in
your Floppy Drive or Pen Drive. Its very easy to install and operate,
Again its output is exe file which can be executed in any computer
without any other framework / software.
C Language Disadvantages :
Every coin has two sides, as C Language has also some disadvantages. C
Language has not any major disadvantages but some features is missing
in the C Language, obviously that's why C Language is very much
powerful now.
1. Object Oriented Programming Features (OOPS)
Object Oriented Programming Features is missing in C Language, You
have to develop your program using procedure oriented language only.
2. Run Time Type Checking is Not Available
In C Language there is no provision for run time type checking, for
example i am passing float value while receiving parameter is of integer
type then value will be changed, it will not give any kind of error
message.
3. Namespace Feature
C does not provides namespace features, so you can't able to use the
same variable name again in one scope. If namespace features is
available then you can able to reuse the same variable name.
4. Constructor and Destructor is not available:
C does not provides object oriented features, so it don't have
Constructor and Destructor features. Constructor and Destructor is used
to construct object and destroy object. So in C Language you have to
implement manually construction and destruction of the variable using
function or by other means.
Variables
Variables
Variables are means for location in memory used by a program to
store data. The size of that block depends upon the range over which the
variable is allowed to vary.
For example, on personal computer the size of an integer variable is two
bytes, and that of a long integer is four bytes.
A variable region is temporarily remember a number or string value, such as
covered by the program. To identify the variables, you have a name unique to
every single variable. This is called a variable name. Before using a variable,
use variables to what is called a variable declaration that you have to reveal
the names and data types that can be stored in the variable variable.
The same data type and storage class variable can be declared, separated by
commas.
[Storage-class] type data variable name [= initial value] variable [= initial
value] variable [= initial value];
In C the size of a variable type such as an integer need not be the same
on all types of machines. When we declare a variable we inform the compiler
of two things, the name of the variable and the type of the variable. For
example, we declare a variable of type character with the name i by writing:
char i;
On seeing the "char" part of this statement the compiler sets aside one
bytes of memory to hold the value of the character. It also sets up a symbol
table. In that table it adds the symbol i and the relative address in memory
where those one byte was set aside. Thus, later if we write: i = 'x'; we expect
that,at run time when this statement is executed, the value 'x' will be placed in
that memory location reserved for the storage of the value of i.
#include<stdio.h>
#include<conio.h>
int main ()
{
printf (“Welcome to C language”);
return 0;
}
Here you'll be able to see #include at the beginning of the program. It
is a pre-processor directive. It's not a part of our program; it's an instruction
to the compiler to make it do something. It says the C compiler to include the
contents of a file, in this case the system file stdio.h. This is the name of the
standard library definition file for all Standard Input Output. Your program
will almost certainly want to send stuff to the screen and read things from the
keyboard. stdio.h is the name of the file in which the functions that we want to
use are defined. A function is simply a group of related statements that we
can use later. Here the function we used is printf . To use printf correctly C
needs to know what it looks like, i.e. what things it can work on and what
value it returns. The actual code which performs the printf will be tied in later
by the linker. Note that without the definition of what printf looks like the
compiler makes a guess when it sees the use of it. This can lead to the call
failing when the program runs, a common cause of programs crashing.
The <> characters around the name tell C to look in the system area
for the file stdio.h. If I had given the name "abc.h" instead it would tell the
compiler to look in the current directory. This means that I can arrange
libraries of my own routines and use them in my programs.
Example
#include<stdio.h>
#include<conio.h>
int main ()
{
clrscr();
printf (“Welcome to C language”);
return 0;
}
When you execute above program you won’t see ‘Welcome to C
language’ on the console because the screen will just flash and go away .If
you want to see the line you can use getch() function just below the printf()
statement. Actually it waits until a key is pressed.
#include<stdio.h>
With this line of code we include a file called stdio.h. (Standard Input/Output
header file). This file lets us use certain commands for input or output which
we can use in our program. (Look at it as lines of code commands) that have
been written for us by someone else). For instance it has commands for input
like reading from the keyboard and output commands like printing things on
the screen.
int main()
The int is what is called the return value (in this case of the type integer).
Where it used for will be explained further down. Every program must have a
main(). It is the starting point of every program. The round brackets are there
for a
reason, in a later tutorial it will be explained, but for now it is enough to know
that they have to be there.
{}
The two curly brackets (one in the beginning and one at the end) are used to
group all commands together. In this case all the commands between the two
curly brackets belong to main(). The curly brackets are often used in the C
language to group commands together. (To mark the beginning and end of a
group or function.).
printf(“Hello World\n”);
The printf is used for printing things on the screen, in this case the words:
Hello World. As you can see the data that is to be printed is put inside round
brackets. The words Hello World are inside inverted ommas, because they are
what is called a string. (A single letter is called a character and a series of
characters is called a string). Strings must always be put between inverted
commas. The \n is called an escape sequence. In this case it represents a
newline character. After printing something to the screen you usually want to
print something on the next line. If there is no \n then a next printf command
will print the string on the same line.
Commonly used escape sequences are:
\n (newline)
\t (tab)
\v (vertical tab)
\f (new page)
\b (backspace)
\r (carriage return)
After the last round bracket there must be a semi-colon. The semi-colon
shows that it is the end of the command. (So in the future, don’t forget to put a
semi-colon if a command ended).
return 0;
When we wrote the first line “int main()”, we declared that main must return
an integer int main(). (int is short for integer which is another word for
number). With the command return 0; we can return the value null to the
operating system. When you return with a zero you tell the operating system
that there were no errors while running the program. (Take a look at the C
tutorial – variables and constants for more information on integer variables).
int main()
{
/* Our first simple C basic program */
printf("Hello World! ");
return 0;
}
Output:
Hello World!
Let us see the parts of the program line by line.
S.no Command Explanation
This is a preprocessor command that includes
1 #include<stdio.h> standard input output header file(stdio.h) from the C
library before compiling a C program
This is the main function from where execution of
2 int main()
any C program begins
whatever is given inside /* */ this command won’t
/* some comments
3 be considered by C program for compilation and
*/
execution.
printf(“Hello
4 printf command prints the output onto the screen
World! “);
This command terminates main function then
5 return 0;
returns 0
The basic building block of a C program is the function. Every C program is a
collection
of one or more functions. Functions are made of variable declarations and
statements, or
complex commands, and are surrounded by curly brackets (`{' and `}').
One and only one of the functions in a program must have the name main.
This function
is always the starting point of a C program, so the simplest C program is a
single function
de_nition:
main ()
{
}
The parentheses `()' that follow the name of the function must be included.
This is how C
distinguishes functions from ordinary variables.
The function main does not need to be at the top of a program, so a C program
does not
necessarily start at line 1, but wherever the function called main is located.
The function
main cannot be called, or started, by any other function in the program. Only
the operating
system can call main; this is how a C program is started.
The next most simple C program is perhaps a program that starts, calls a
function that
does nothing, and then ends.
Compile and execute C program:
Compilation is the process of converting a C program which is user
readable code into machine readable code which is 0′s and 1′s.
This compilation process is done by a compiler which is an inbuilt
program in C.
As a result of compilation, we get another file called executable file. This
is also called as binary file.
This binary file is executed to get the output of the program based on
the logic written into it.
Steps to compile & execute a C program:
Step1 Type the above C basic program in a text editor and save as “sample.c”.
To compile this program, open the command prompt and goto the
Step2
directory where you have saved this program and type “cc sample.c” or
“gcc sample.c”.
If there is no error in above program, executable file will be generated
Step3
in the name of “a.out”.
Step4 To run this executable file, type “./a.out” in command prompt.
Step5 You will see the output as shown in the above basic program.
Compilation
There are many C compilers around. The cc being the default Sun compiler.
The GNU C compiler gcc is popular and available for many platforms.
The printf() and scanf() family of functions are part of the standard C library,
and make it easy to take input from the user, and display data to the user.
Learning how to effectively use these functions can open up a whole world of
possibilities when building new projects.
The formatting placeholders in scanf are more or less the same as that in
printf, its reverse function.
There are rarely constants (i.e. characters that are not formatting
placeholders) in a format string, mainly because a program is usually not
designed to read known data. The exception is one or more whitespace
characters, which discards all whitespace characters in the input.
The printf() function is used to print the character, string, float, integer, octal
and hexadecimal values onto the output screen.
in the first printf statement \t requests for the tab displacement on the screen
the argument %d tells the compiler that the value of num should be printed as
decimal integer. \n causes the new output to start from new line.
In second printf statement %5.2f tells the compiler that the output must be in
floating point, with five places in all and two places to the right of the decimal
point.
The data from the keyboard is received by scanf function. In the above format,
the & (ampersand) symbol before each variable name is an operator that
specifies the address of variable name.
\b Backspace
\t Horizontal Tab
\f Form feed
\r Carriage Return
\v Vertical Tab
\? Question Mark
\\ Backslash
\0 Null
Code Meaning
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\" Double quote
\' Single quote
\\ Backslash
\v Vertical tab
\a Alert
\? Question mark
\N Octal constant (N is an octal constant)
\xN Hexadecimal constant (N is a hexadecimal constant)
C Character Set :
Whenever we write any C program then it consists of different statements.
Each C Program is set of statements and each statement is set of different c
programming lexims. In C Programming each and every character is
considered as single lexim. i.e [ Basic Lexical Element ]
Character Set Consists Of -
Types Character Set
Lowercase Letters a-z Sp
Uppercase Letters A to Z eci
al
Digits 0-9
Ch
Special Characters !@#$%^&* ara
White Spaces Tab Or New line Or Space cte
rs:
C language contains the following special character in association with the letters and digits.
Symbol Meaning
~ Tilde
! Exclamation mark
# Number sign
$ Dollar sign
% Percent sign
^ Caret
& Ampersand
* Asterisk
( Lest parenthesis
) Right parenthesis
_ Underscore
+ Plus sign
| Vertical bar
\ Backslash
` Apostrophe
- Minus sign
= Equal to sign
{ Left brace
} Right brace
[ Left bracket
] Right bracket
: Colon
" Quotation mark
; Semicolon
< Opening angle bracket
> Closing angle bracket
? Question mark
, Comma
. Period
/ Slash
Tokens : The smallest individual units of a C- program are called Tokens.
Key words, Identifiers, Constants, Operators, Delimiters.
Key words : have a predefined meaning and these meanings cannot be
changed. All keywords must be written in small letters (except
additional c99 keywords).
Keywords
Keywords are standard identifiers that have standard predefined
meaning in C. Keywords are all lowercase, since uppercase and lowercase
characters are not equivalent it's possible to utilize an uppercase keyword as
an identifier but it's not a good programming practice.
Points to remember
1. Keywords can be used only for their intended purpose.
2. Keywords can't be used as programmer defined identifier.
3. The keywords can't be used as names for variables
Data types in C :
Data type determines the type of data a variable will hold. If a
variable ‘x’ is declared as ‘int’. it means x can hold only integer values.
Every variable which is used in the program must be declared as what
data-type it is.
System defined data types: these data types can be defined by C
compiler, these are two types:
Numeric data types : these are two types: integers and characters.
Character data types: these are two types single characters and string
characters.
Derived data types: these data types can be declared by user by deriving
the system defined data type concepts. These are two types.
Arrays : set of similar data elements are called as arrays.
Ex: int a[10], char name[20];
Pointers: pointer is a variable which holds the address of similar data
variable.
Ex: int a=5,*p; - p is a pointer variable declared with * character.
User defined data types: these data types can be defined by user, these
are three types.
Structures : group of deferent data items combined together are called as
structures. All the data items can stored in individual memory locations.
Unions : group of deferent data items combined together are called as
unions. All the data items can share unique memory location.
Enumerations: these data types allows t declare string constants with
integer equalant values.
below
The size and range of these data types may vary among processor
types and compilers. Data type qualifiers modify the behavior of variable type
to which they are applied. Data type qualifiers can be classified into two types.
two
Size qualifiers:
Size qualifiers alter the size of the basic data types. There are two size
qualifiers that can be applied to integer: short and long.
The minimum size of short int is 16 bit. The size of int must be greater than or
equal to that of a short int. The size of long int must be greater than or equal to
a short int. The minimum size of a long int is 32 bits.
Sign qualifiers:
The keywords signed and unsigned are the two sign qualifiers that
specify whether a variable can hold both –ve and +ve numbers, or only +ve
numbers. These qualifiers can be applied to the data types int and char only.
Example: unsigned int I;
The following table shows all valid data type combinations supported
by C, along with their minimal ranges and typical memory size.
char
char is a special integer type designed for storing single characters.
The integer value of a char corresponds to an ASCII character. E.g., a value of
65 corresponds to the letter A, 66 corresponds to B, 67 to C, and so on.
As in the table below, unsigned char permits values from 0 to 255,
and signed char permits values from -127 (or -128) to 127. The char type is
signed by default on some computers, but unsigned on others the sizes of the
char types are as follows.
Type Bytes Minimal range
char 1 -127 to 127
unsigned char 1 0 to 255
signed char 1 -127 to 127
int
An integer is a whole number (a number without a fractional part).
It can be positive or negative numbers like 1, -2, 3, etc., or zero.
The sizes of the integer variables depend on the hardware and
operating system of the computer. On a typical 16-bit system, the sizes of the
integer types are as follows.
TYPE Bytes Possible Values le Values
char
char is a special integer type designed for storing single characters.
The integer value of a char corresponds to an ASCII character. E.g., a value of
65 corresponds to the letter A, 66 corresponds to B, 67 to C, and so on.
As in the table below, unsigned char permits values from 0 to 255,
and signed char permits values from -127 (or -128) to 127. The char type is
signed by default on some computers, but unsigned on others the sizes of the
char types are as follows.
Type Bytes Minimal range
char 1 -127 to 127
unsigned char 1 0 to 255
signed char 1 -127 to 127
Float
Floating point numbers are numbers with a decimal point. The float
type can take large floating point numbers with a small degree of precision
(Precision is simply the number of decimal places to which a number can be
calculated with accuracy. If a number can be calculated to three decimal
places, it is said to have three significant digits.)
Memory size : 4 bytes
Minimal range : IE-38 to IE+38 with six digit of precision
Double
Double-precision floating point numbers are also numbers with a
decimal point. We know that the float type can take large floating point
numbers with a small degree of precision but the double-precision double
type can hold even larger numbers with a higher degree of precision.
The sizes of the double types are as follows.
Type Bytes Minimal range
doubleprecision 8 IE-38 to IE+38 with 10 digit of precision
long double 8 IE-38 to IE+38 with 10 digit of precision
Constants
The term constant means that it does not change during the execution
of program.In the language C, constant and is the data with a constant value
that does not change in the program. For example, in the program "100"
"3.14" "'A'" "" Hello "" and the like, if you write data directly, and constant.
Moreover, also called a literal constant. Constant expression is an expression
consisting only of constants. There are four basic types of constants in C. They
are:
1. Integer constants
2. Floating-point constants
3. Character constants
4. String constants
Integer and floating-point constants represent numbers. They are
often referred to as numeric-type constants. The following rule applies to all
numeric type constants:
Comma and blank spaces cannot be included within the constants.
Constants can be preceded by a – or + sign, if desired. If either sign does
not precede the constant it is assumed to be positive.
The value of a constant cannot exceed specified minimum and maximum
bounds. For each type of constant, these bound vary from one C compiler
to another.
Integer constants
Integer constants are whole numbers without any fractional part.
Thus integer constants consist of a sequence of digits. Integer constants can be
written in three different number systems: Decimal, Octal and Hexadecimal.
A decimal integer constant consists of any combination of digits
taken from the set 0 through 9. If the decimal constant contains two or more
digits, the first digit must be something other than 0. The following are valid
decimal integer constants.
0 1 1234 -786
Floating-point constants
A floating-point constant is a base-10 number that contains either a
decimal point or an exponent or both. A floating-point constant can be written
in two forms: Factorial form or Exponential form. A floating-point constant in
a fractional form must have at least one digit each to the left and right of the
decimal point. A floating-point in exponent form consists of a mantissa and an
exponent. The mantissa itself is represented as a decimal integer constant or a
decimal floating-point constant in fractional form. The letter E or e and the
exponent follow the mantissa. The exponent must be a decimal integer. The
actual number of digits in the mantissa and the exponent depends on the
computer being used.
The following are valid floating-point constants.</1.0 0.1 2E-4 -
0.1555e-4
Character constants
A character constant is a single character, enclosed in single quotation
marks.
e.g., ‘A’ ‘B’ ‘1’
Characters are stored internally in computer as coded set of binary
digits, which have positive decimal integer equivalents. The value of a
character constant is the numeric value of the character in the machine’s
character set. This means that the value of a character constant can vary from
one machine to the next, depending on the character set being used on the
particular machine. For example, on ASCII machine the value of ‘A’ is 65 and
on EBCDIC machine it is 193.
String constants
A string constant consists of zero or more character enclosed in
quotation marks. Several string constants are given below.
“Welcome to C Programming” “”
“a+b\n” “Error\a\a\a”
There is a difference between the constant ‘A’ and “A” in C, the first ‘A’
is a character constant while the second “A” is string constant. The notation ‘A’
is a constant occupying a single byte containing the ASCII code of the
character A. The notation “A” on the other hand ,is a constant that occupies
two bytes one for ASCII code of A and another for the null character with the
value 0,that terminates all the string.
Expressions
An expression is a sequence of operators and operands that specifies
computation of a value. An expression may consist of single entity or some
combination of such entities interconnected by one or more operators. All
expression represents a logical connection that's either true or false. Thus
logical type expression actually represents numerical quantities.
In C every expression evaluates to a value i.e., every expression results
in some value of a certain type that can be assigned to a variable. Some
examples of expressions are shown in the table given below.
A+b
3.14*r*r
a*a+2*a*b+b*b
Symbolic constant in c Language
A symbolic constant is name that substitute for a sequence of
character that cannot be changed. The character may represent a numeric
constant, a character constant, or a string. When the program is compiled,
each occurrence of a symbolic constant is replaced by its corresponding
character sequence. They are usually defined at the beginning of the program.
The symbolic constants may then appear later in the program in place of the
numeric constants, character constants, etc., that the symbolic constants
represent.
For example
, a C program consists of the following symbolic constant definitions.
#define PI 3.141593
#define TRUE 1
#define FALSE 0
#define PI 3.141593 defines a symbolic constant PI whose value is 3.141593.
When the program is preprocessed, all occurrences of the symbolic constant
PI are replaced with the replacement text 3.141593.
Note that the preprocessor statements begin with a #symbol, and are not
end with a semicolon. By convention, preprocessor constants are written in
UPPERCASE.
Declarations
An object declaration in C follows the global format:
<class> <type> <name> <initialization> ;
This global format is shared by all the C objects including functions.
Each field differs depending on the object type.
<class> is the storage class, and gives information about how the
object is allocated and where it is known and then accessible.
<type> gives the basic type of the object and is generally completed by
the name information.
<name> gives a name to the object and is generally an identifier. It
may be followed or preceded by additional information to declare complex
objects.
<initialization> gives an initial value to the object. Depending on how
the object is allocated, it may be static information to be built by the
linker, or it may be some executable code to set up the variable when it
is created. This field is optional and may be omitted.
Each declaration is terminated by the semicolon character ;. To be
more convenient, a declaration may contain several occurrences of the
pair <name> <initialization>, separated by commas. Each variable
shares the same storage class and the same basic type.
Blocks (compound statements)
A block of statements, also called a compound statement, is a group of
statements that is treated by the compiler as if it were a single statement.
Blocks begin with a { symbol, end with a } symbol, and the statements to be
executed are placed in between. Blocks can be used any place where a single
statement is allowed.
You have already seen an example of a block when writing the function
main():
1 int main()
2 { // start a block
3
4 // multiple statements
5 int nValue = 0;
6 return 0;
7
8 } // end a block
C Tokens:
C tokens includes identifiers, keywords, variables, constants
and operators.
Identifiers : the names of variables, functions, labels and various other
user defined items are called as identifiers. Identifiers contains alphabets,
digits and underscore. It can’t include spaces. It starts with alphabet.
Ex: lcm( ), ab_cd, pi,r etc.
Operators
An operator in general, is a symbol that operates on a certain data
type. C language is very rich in operators. The commonly used operators
include
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Conditional operators
6. Comma operators
7. Unary operators
8. Bitwise Operators
Arithmetic operators
The Arithmetic operators perform Arithmetic operations . The
arithmetic operators can operate on any built in data types. A list of arithmetic
operator and their meaning are given below.
Operator Meaning
+ Addition or Unary plus
- Subtraction or Unary minus
* Multiplication
/ Division
% Modulo Division
This table shows the symbols of arithmetic, together with their duties. These
operators allow you to write expressions whose evaluation is precisely the
treatment of information that made the computer. Arithmetic operators, along
with a wide range of features resident in the library of the language used
make it possible to perform calculations of all kinds.
Suppose that a and b are integer variables whose values are 100 and 4,
respectively. Several arithmetic expressions involving these variables are
shown below, together with their resulting values.
Expression Value
a+b 104
a-b 96
a*b 400
a/b 25
a%b 0
Relational operators
Relational operators are used to compare, logical, arithmetic and
character expression. Each of these six relational operators takes two
operands. Each of these operators compares their left side with their right
side. The whole expression involving the relation operator then evaluate to an
integer. It evaluates to 0 if the condition is false and 1 if it is true.
Operator Meaning
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
!= Not equal to
Suppose that a and b are integer variables whose values are 100 and 4,
respectively. Several arithmetic expressions involving these variables are
shown below, together with their resulting values.
Expression Interpretation Value
a<b False 0
a>b True 1
a<=b False 0
a>=b True 1
a==b False 0
a!=b True 1
Logical operators
A Logical operator is used to compare or evaluate logical and
relational expression. There are three logical operators in C language. They
are
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
Used to calculate the value of logical expressions, ie expressions that the only
values that can take operands are True or False. Two versions of these
operators. When you simply want to know if an expression is true or false (eg,
x>7 ), operators are used lógicos . These operators do not consider the
structure of bits, simply take the value 0 as false and any other as true. There
is also another version (the bitwise) imposed by the logical operations to the
bits used to internally represent the operands. These are the operators & , |
and ~ , respectively.
Suppose that a and b are integer variables whose values are 100 and 4,
respectively. Several arithmetic expressions involving these variables are
shown below, together with their resulting values.
Expression Interpretation Value
(a>b)&&(a==100) True 1
(b>a)||(a>b) True 1
!(a>b) False 0
Assignment operator
An Assignment operator is used to form an assignment expression,
which assigns the value to an identifier. The most commonly used assignment
operator is = . Assignment expressions that make use of this operator are
written in the form<
Identifier=expression;
Example:
i=100;
j=200;
Other forms of assignment operator exist that are obtaining various
operators such as +,-,*,/,% etc with the = sign.
operator Equation Output
+= i=i+1 i+=1
-= i=i-10 i-=10
*= i=i*11 i*=11
/= i=i/12 i/=12
%= i=i%5 i%=5
Conditional operator
Ternary ( Conditional ) operator:
Conditional operator is an equivalent form of if –else loop.
It is called as ternary operator because used on three operands.
Syntax:
true
(Condition) ? statement1 : statement2
false
This expression evaluates statement 1 or statement2 depends on the
condition. If the condition is true statement1 gets executed other wise
statement2 gets executed.
Ex:
main( )
{
Intx;
clrscr( );
printf(“entera no”);
scanf(“%d”,&x);
(x%2==0) ? printf(“even no”) : printf(“odd no”);
getch();
Comma operator
A set of expression separated by comma is a valid constant in the C
language. For example i and j are declared by the statements
inti, j; i=(j=10,j+20);
Bitwise Operators
Bitwise operators operate on individual bits of integer (int and long)
values. This is useful for writing low-level hardware or OS code where the
ordinary abstractions of numbers, characters, pointers, and so on, are
insufficient. Bit manipulation code tends to be less "portable". Code is
"portable" if without any programmer intervention it compiles and runs
correctly on different types of computers. The bit wise operations are
commonly used with unsigned types. These operators perform bit wise
logical operations on values. Both operands must be of the same type and
width: the resultant value will also be this type and width.
^ Bitwise Exclusive
>> Right Shift by right hand side (RHS) (divide by power of 2)
<< Left Shift by RHS (multiply by power of 2)
Definition of bitwise logical operators:
Bit a Bit b a&b a|b a^b ~a
0 0 0 0 0 1
0 1 0 1 1 1
1 0 0 1 1 0
1 1 1 1 0 0
Example:
Bitwise Negation
a=00000011
~a =11111100
Bitwise And
A = 00000011
B = 00010110
a&b=00000010
Bitwise Or
A = 00000011
B = 00010110
a|b=00010111
Bitwise Exclusive Or
A = 00000011
B = 00010110
a^b=00010101
Right Shift
a = 0000 0011 =3
(a<<=1) = 00000110 = 6
(a<<=2) = 00011000 = 24
(a<<=3) = 11000000 = 192
Left Shift
a=11000000 =192
(a>>=1) = 01100000 = 96
(a>>=2) = 00011000 = 24
(a>>=3) = 0000 0011 = 3
Comments
Comments are added to make a program more readable to you but the
compiler must ignore the comments. Everything that is inside /* and */ is
considered a comment and will be ignored by the compiler. You must not
include comments within other comments, so something like this is not
allowed: [*].
/* this is a /* comment */ inside a comment, which is wrong! */
Comments are a way of inserting remarks and reminders into a program
without affecting its content. Comments do not have a fixed place in a
program: the compiler treats them as though they were white space or blank
characters and they are consequently ignored. Programs can contain any
number of comments without losing speed. This is because comments are
stripped out of a source program by the compiler when it converts the source
program into machine code.
Comments are marked out or delimited by the following pairs of charac-
ters:
/* ...... comment ....... */
Because a comment is skipped over as though it were a single space, it can
be placed anywhere where spaces are valid characters, even in the middle of a
statement, though this is not to be encouraged. You should try to minimize
the use of comments in a program while trying to maximize the readability
of the program. If there are too many comments you obscure your code and
it is the code which is the main message in a program.
Float constants : 0.2 , 876.345, .345623 , 23.4E+8, 47.
Operators : a symbol, which indicates an operation to be performed.
Operators are used to manipulate data in program.
Delimiters : Language Pattern of c-language uses special kind of symbols
: (colon, used for labels) ; (semicolon terminates statement ) ( )
parameter list [ ] ( array declaration and subscript ), { } ( block
statement )
# ( hash for preprocessor directive ) , (comma variable separator )
Conversion Specifiers
Code Format
%a Hexa decimal output in the form of 0xh.hhhhp+d(C99
only)
%s String of characters (until null zero is reached )
%c Character
%d Decimal integer
%f Floating-point numbers
%e Exponential notation floating-point numbers
%g Use the shorter of %f or %e
%u Unsigned integer
%o Octal integer
%x Hexadecimal integer
%i Signed decimal integer
%p Display a pointer
%n The associated argument must be a pointer to integer,
This sepecifier causes the number of characters written in to be
stored in that integer.
%hd short integer
%ld long integer
%lf long double
%% Prints a percent sign (%)