A C program basically has the following form:
·
Preprocessor Commands
·
Functions
·
Variables
·
Statements & Expressions
·
Comments
Preprocessor Commands: These
commands tells the compiler to do preprocessing before doing actual
compilation. Like #include <stdio.h> is a preprocessor command
which tells a C compiler to include stdio.h file before going to actual
compilation. You will learn more about C Preprocessors in C Preprocessors
session.Functions: are main building blocks of any C Program. Every C Program will have one or more functions and there is one mandatory function which is called main() function. This function is prefixed with keyword int which means this function returns an integer value when it exits. This integer value is retured using return statement.
The C Programming language provides a set of built-in functions. In the above example printf() is a C built-in function which is used to print anything on the screen. Check Builtin function section for more detail.
You will learn how to write your own functions and use them in Using Function session.
Variables: are used to hold numbers, strings and complex data for manipulation. You will learn in detail about variables in C Variable Types.
Statements & Expressions : Expressions combine variables and constants to create new values. Statements are expressions, assignments, function calls, or control flow statements which make up C programs.
Comments: are used to give additional useful information inside a C Program. All the comments will be put inside /*...*/ as given in the example above. A comment can span through multiple lines.
#include <stdio.h>
int main()
{
/* My first program */
printf("Hello, World! \n");
return 0;
}
C Language History:
C
is a general-purpose computer programming language developed in 1972 by Dennis
Ritchie at the AT&T Bell Laboratories(American Telephone & Telegraph)
for use with the UNIX operating system.
C is one of the most popular programming language of all
time and there are very few computer architectures for which a C compiler does
not exist.
In early of 1960 there were many languages available but
they were used for some specific purpose.For example COBOL was used for
commercial application FORTRAN was used for some
scientific and
engineering application. So at this stage people were searching some language
which can fulfill the need of all type of application. So invention of
different languages taken place.
The 'C' language become very popular due to its robustness rich set of built in
function and operators that can be used to written any complex program.
History:
Note the followings
·
C is a case sensitive
programming language. It means in C printf and Printf will have
different meanings.
·
C has a free-form line
structure. End of each C statement must be marked with a semicolon.
·
Multiple statements can be one
the same line.
·
White Spaces (ie tab space and
space bar ) are ignored.
·
Statements can continue over
multiple lines.
C Character Set
- Letters
- Digits
- Special Characters
Letters :
- C language comprises the following set of letters to form a standard program. They are :
- A to Z in Capital letters.
- a to z in Small letters.
Digits :
C language comprises the following sequence of
numbers to associate the letters.
0 to 9 digits.
Special Characters:
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
|
Constants, Variables and Keywords
in C
Variables:
A variable is an entity that may change it value. In any program we typically do lots of calculations. The results of these calculations are stored in computer memory locations. To make the retrieval and usage of these values we give names to the memory locations. These names are called variables.
Keywords:
A keyword is a word that is part of C Language itself. These words have predefined meanings and these words cannot be used as variable names.
C Keywords
|
|||
char | signed | break | for |
auto | const | sizeof | case |
if | extern | double | struct |
continue | goto | register | enum |
typedef | default | return | static |
float | union | do | switch |
volatile | int | unsigned | else |
while | long | void | short |
Operators:
C programming language provides several
operators to perform different kind to operations. There are operators for
assignment, arithmetic functions, logical functions and many more. These
operators generally work on many types of variables or constants, though some
are restricted to work on certain types. Most operators are binary, meaning
they take two operands. A few are unary and only take one operand.
simple answer can be given using expression
4 + 5 is equal to 9. Here 4 and 5 are called operands and + is called
operator. C language supports following type of operators.
Hierarchy of operators:
Arithmetic Operators:
There are following arithmetic operators supported by C language:Assume variable A holds 10 and variable B holds 20 then:
Operator
|
Description
|
Example
|
+
|
Adds two operands
|
A + B will give 30
|
-
|
Subtracts second operand from the first
|
A - B will give -10
|
*
|
Multiply both operands
|
A * B will give 200
|
/
|
Divide numerator by denumerator
|
B / A will give 2
|
%
|
Modulus Operator and remainder of after
an integer division
|
B % A will give 0
|
Relational Operators:
There are following logical operators supported by C languageAssume variable A holds 10 and variable B holds 20 then:
Operator
|
Description
|
Example
|
==
|
Checks if the value of two operands is
equal or not, if yes then condition becomes true.
|
(A == B) is not true.
|
!=
|
Checks if the value of two operands is
equal or not, if values are not equal then condition becomes true.
|
(A != B) is true.
|
>
|
Checks if the value of left operand is
greater than the value of right operand, if yes then condition becomes true.
|
(A > B) is not true.
|
<
|
Checks if the value of left operand is
less than the value of right operand, if yes then condition becomes true.
|
(A < B) is true.
|
>=
|
Checks if the value of left operand is
greater than or equal to the value of right operand, if yes then condition
becomes true.
|
(A >= B) is not true.
|
<=
|
Checks if the value of left operand is
less than or equal to the value of right operand, if yes then condition
becomes true.
|
(A <= B) is true.
|
Logical Operator
&&
|
Called Logical AND operator. If both the operands
are non zero then then condition becomes true.
|
(A && B) is true.
|
||
|
Called Logical OR Operator. If any of the
two operands is non zero then then condition becomes true.
|
(A || B) is true.
|
!
|
Called Logical NOT Operator. Use to
reverses the logical state of its operand. If a condition is true then
Logical NOT operator will make false.
|
!(A && B) is false.
|
Increment Decrement Operator
++
|
Increment operator, increases integer
value by one
|
A++ will give 11
|
--
|
Decrement operator, decreases integer
value by one
|
A-- will give 9
|
Assignment Operators:
There are following assignment operators supported by C language:
Operator
|
Description
|
Example
|
=
|
Simple assignment operator, Assigns
values from right side operands to left side operand
|
C = A + B will assigne value of A + B
into C
|
+=
|
Add AND assignment operator, It adds
right operand to the left operand and assign the result to left operand
|
C += A is equivalent to C = C + A
|
-=
|
Subtract AND assignment operator, It
subtracts right operand from the left operand and assign the result to left
operand
|
C -= A is equivalent to C = C - A
|
*=
|
Multiply AND assignment operator, It
multiplies right operand with the left operand and assign the result to left
operand
|
C *= A is equivalent to C = C * A
|
/=
|
Divide AND assignment operator, It
divides left operand with the right operand and assign the result to left
operand
|
C /= A is equivalent to C = C / A
|
%=
|
Modulus AND assignment operator, It takes
modulus using two operands and assign the result to left operand
|
C %= A is equivalent to C = C % A
|
Bitwise Operators:
Bitwise operator works on bits and performs bit by bit operation.Assume if A = 60; and B = 13; Now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
There are following Bitwise operators supported by C language
Operator
|
Description
|
Example
|
&
|
Binary AND Operator copies a bit to the
result if it exists in both operands.
|
(A & B) will give 12 which is 0000 1100
|
|
|
Binary OR Operator copies a bit if it
exists in eather operand.
|
(A | B) will give 61 which is 0011 1101
|
^
|
Binary XOR Operator copies the bit if it
is set in one operand but not both.
|
(A ^ B) will give 49 which is 0011 0001
|
~
|
Binary Ones Complement Operator is unary
and has the effect of 'flipping' bits.
|
(~A ) will give -60 which is 1100 0011
|
<<
|
Binary Left Shift Operator. The left
operands value is moved left by the number of bits specified by the right
operand.
|
A << 2 will give 240 which is 1111
0000
|
>>
|
Binary Right Shift Operator. The left
operands value is moved right by the number of bits specified by the right
operand.
|
A >> 2 will give 15 which is 0000
1111
|
Conditional Operator
Operator
|
Description
|
Example
|
?
|
To put the question
|
|
:
|
Condition related to answer
|
E1? E2: E3
Comma operator (,)
The comma
operator (,) is used to separate two or more expressions that are included
where only one expression is expected. When the set of expressions has to be
evaluated for a value, only the rightmost expression is considered.
For example, the following code:
For example, the following code:
|
|
Would first assign the value 3 to b, and then assign b+2 to variable a. So, at the end, variable a would contain the value 5 while variable b would contain value 3.
Various Data Types
"Data type can be defined as the type of data of variable or constant store."
When we use a variable in a program then we have to mention the type of data. This can be handled using data type in C.
Followings are the most commonly used data types in C.
- short
- long
- signed
- unsigned
Example:
int ---> key word is used to declare Integer variables.
Eg:
int a, b;
float ---> key word is used to declare Real numbers or floating point numbers.
Eg:
float x, y;
char ---> key word is used to declare characters or string.
Eg:
char ch;
double ---> key word is used to declare a variable which can hold a large number.
Eg:
double z;
0 comments:
Post a Comment