test1 (1)
test1 (1)
1.1 Overview of C:
1.1.1. Importance of C
C is called as a robust language, which has so many built-in functions and operations,
which can be used to write any complex program.
Generally, we use to call C as a middle level language. Because, the ‘C’ compiler
combines the capabilities of an assembly language with the features of a high-level
language. Therefore, it is best for writing both system software and business packages.
‘C’ Programs are efficient and fast.
C is highly portable, that is, ‘C’ programs written on one computer can be run on another
with little (or) no modification.
‘C’ language is best for structured programming, where the user can think of a problem in
terms of function modules (or) blocks.
It has the ability to extend itself.
It was named ‘C’ because it is an offspring of BCPL (Basic Combined Programming
Language) which was popularly called ‘B’ language.
#include <stdio.h>
v1 = 150;
v2 = 25;
Output:
Syntax
1) Comment line
2) Preprocessor directive
4) main function( )
Local variables;
Statements;
{
Statements;
Comment line
1. //Single Line
2. /*…………Multiple Lines…………..*/
Comment line is used for increasing the readability of the program. It is
useful in explaining the program and generally used for documentation. It is
enclosed within the decimeters. Comment line can be single or multiple lines
but should not be nested. It can be anywhere in the program except inside
string constant & character constant.
Preprocessor Directive:
Global Declaration:
This is the section where variable are declared globally so that it can
be access byall the functions used in the program. And it is generally
declared outside the function.
main() function
It is the user defined function and every function has one main()
function fromwhere actually program is started and it is encloses within the
pair of curly braces.The main( ) function can be anywhere in the program but
in general practice it isplaced in the first position.
Syntax :
main()
……..
……..
……..
int main( )
return 0
The main function does not return any value when void (means null/empty)
asvoid
}
Output: C language
The program execution start with opening braces and end with closing
brace.And in between the two braces declaration part as well as executable
part ismentioned. And at the end of each line, the semi-colon is given which
indicatesstatement termination.
C code:
When you first write the code in the C language, that source code is
sent to the Preprocessing section.
Preprocessing:
This part usually generates the Object code, after taking the assembly-
level code from the compiler.
This object code is quite similar to the machine code or the set of
binary digits.
After this assembler part, The Linker continues the process, producing
an executable.exe file at the end.
Linker:
Before getting started with this, we should know that the library
functions are a part of the C software but not of any C program.
Hence, the compiler has no idea about the working of the function,
whether it is a printf function or scanf function.
The information for each of these functions is kept in the
corresponding library, which the compiler must to be able to connect.
The linker does this task.
So, when the #include is written, it includes the studio.h library, which
is basically used for giving access to the Standard Output and Input.
The basic goal of the linker is to link the object file to the library
functions so that the programme may be run as an executable file
(.exe).
In this Linker process, the .exe file will be created and this file is in an
executable format.
After this process, the next step is the loader process.
Loader:
1.2.2. C tokens
The smallest individual units are known as tokens. C has six types of
tokens as shown in figure
Special symbols [ ] {}
Constants 15.0,100
Operators +, - , *
1.2.3. Keyword & identifiers
Keyword
There are certain words reserved for doing specific task, these words
are known as reserved word or keywords.
These words are predefined and always written in lower case or small
letter. These keywords can’t be used as a variable name as it assigned with
fixed meaning.
Some examples are int, short, signed, unsigned, default, volatile, float,
long, double, break, continue, typedef, static, do, for, union, return, while,
do, extern, register, enum, case, goto, struct, char, auto, const etc.
Identifiers
Name should only consist of alphabets (both upper and lower case),
digits and underscore (_) sign.
First characters should be alphabet or underscore
Name should not be a keyword
Since C is a case sensitive, the upper case and lower case considered
differently, for example code, Code, CODE etc. are different identifiers.
Identifiers are generally given in some meaningful name
An identifier name may be long, some implementation recognizes only
first eight characters, most recognize 31 characters.
1.2.4. Constants
Numeric constant
Character constant
String constant
Numeric constant:
1.2.5. Variables
Variable is a data name which is used to store some data value or
symbolic names for storing program computations and results.
The value of the variable can be change during the execution.
The rule for naming the variables is same as the naming identifier.
Before used in the program it must be declared.
Declaration of variables specifies its name, data types and range of the
value that variables can store depends upon its data types. Syntax: int
a; char c; float f;
Syntax
data_typevariable_name;
Example:
Examples
The expression on the right hand side of the assignment statement can be:
An arithmetic expression
A relational expression
A logical expression
A mixed expression.
The above mentioned expressions are different in terms of the type of
operators connecting the variables and constants on the right hand side of
the variable. Arithmetic operators, relational
Examples
int a;
floatb,c ,avg, t;
#include <stdio.h>
int main()
constint a;
constint b = 12;
Output
#include <stdio.h>
#define num 25
int main()
Output
Syntax
Arithmetic operators
Relational operators
Logical operators
Assignment operators
Increment and decrement operators
Conditional operators
Bitwise operators
Special operators
Arithmetic Operator
C provides the entire basic arithmetic operator. There are two types
Binary operators
Unary operators
Binary operators
+ 2+2=4
- 5-3=2
/ 10/2=5
% 11%3=2
* 2*3=6
Unary operators
minus(-)
Operator Description
- Minus
++ Increment
-- Decrement
Relational Operators
Relational operators are symbols that are used to test the relationship
between two variables or between a variable and a constant. These
operators are used for checking conditions in control statements. The table
shows the various relational operators and their meaning.
< Less than operator a < b; True if a less than b else False
> Greater than operator a > b; True if a greater than b else False
Logical Operators
OR || exp1 || exp2
NOT ! ! exp1
Assignment Operators
But if the value already exists in that variable then it will be overwritten by
the assignment operator(=)
Syntax
<variable> = <expression>;
Example:
X = y = z = 2;
X = (y + z);
Symbo
Operator Action Examples
l
Incremen Increments the operand by
++ ++x, x++
t one
Conditional Operators
Example:
int x=(a>b) ? a b;
In the above example the value of x is assigned the value of b since the
condition is false.
Bitwise Operators
Special Operators
Example
Example:
inta,b,c;
Expression Result
Sizeof(char) 1
Sizeof(int) 2
Sizeof(float) 4
Sizeof(double
8
)
1.3.2. Expressions
An expression is a combination of variables, constants, operators and
function call.
It can be
Syntax:
variable = expression;
Example:
x=a*b-c;