You are on page 1of 36

What is ‘C’

• C is a structured programming language


developed by Dennis Ritchie in 1973 at Bell
Laboratories. It is one of the most popular
computer languages today because of its
structure, high-level abstraction, machine
independent feature.
Concepts
C includes concepts such as
• data types
• data(variable)
• function
• structure
• pointer.
‘C’ Language Syntax
Rules
Different parts of ‘C’
program
• Pre-processor
• Header file
• Function
• Variables
• expression
• Comment
Pre-processor
• #include, the first word of any C
program. It is also known as pre-
processor. The main work of pre-
processor is to initialize the
environment of program, i.e to
link the program with the header
file.
Header file
• Header file is a collection of built-in functions
that help us in our program. Header files
contain definitions of functions and variables
which can be incorporated into any C program
by pre-processor #include statement.
• To use any of the standard functions, the
appropriate header file must be included. This
is done at the beginning of the C source file.
• For example, to use the printf() function in a
program, the line #include< stdio.h> is
responsible.
Main() function
• main() function is a function that must be used in
every C program.
• A function is a sequence of statement required to
perform a specific task.
• main() function starts the execution of C program.
• In the above example,int in front of main() function
is the return type of main() function.
• The curly braces { } just after the main() function
encloses the body of main() function.
Compile and Run
Keywords
• Keywords are preserved words that have
special meaning in C language.
• The meaning has already been described.
These meaning cannot be changed.
• There are total 32 keywords in C language.
Auto Double Int Struct

Break Else Long Switch

Case Enum Register Typedef

Const Extern Return Union

Char Float Short Unsigned

Continue For Signed Volatile

Default Goto Sizeof Void

do If static While
Data types in ‘C’
Language
• Data types specify how we enter data into
our programs and what type of data we enter.
C language has some predefined set of data
types to handle various kinds of data that we
use in our program. These datatypes have
different storage capacities.

• C language supports 2 different type of data


types,
• Primary data types
• These are fundamental data types in C namely
integer(int), floating(float), charater(char)
and void.
• Derived data types
• Derived data types are like arrays, functions,
stuctures and pointers.
Integer type
Type Size(bytes) Range
int or signed 2 -32,768 to 32767
int
unsigned int 2 0 to 65535
short int or 1 -128 to 127
signed short int
long int or 4 -2,147,483,648 to
signed long int 2,147,483,647
unsigned long 4 0 to 4,294,967,295
int
Floating type

Type Size(bytes) Range


Float 4 3.4E-38 to
3.4E+38
double 8 1.7E-308 to
1.7E+308
long double 10 3.4E-4932 to
1.1E+4932
Character type

Type Size(bytes) Range


char or signed 1 -128 to 127
char
unsigned char 1 0 to 255
void type
Variables in ‘C’
• Variable are used in C, where we need storage
for any value, which will change in program.
• Variable can be declared in multiple ways each
with different memory requirements and
functioning.
• Variable is the name of memory location
allocated by the compiler depending upon the
datatype of the variable.
Example :
int i=10 //declared & initialized

Memory location
10 reserved and is named
as i

RAM
Declaration and Initialization
• Example :
• int i; // declared but not initialised
• char c;
• int i, j, k; // Multiple declaration
• Initialization means assigning value to an already
declared variable,
• int i; // declaration
• i = 10; // initialization Initialization and
declaration can be done in one single step also,
• int i=10; //initialization and declaration in
same step int i=10, j=11;
Scope of Variables

• All the variables have their area of


functioning, and out of that boundary they
don't hold their value, this boundary is called
scope of the variable.
• On this basis we can broadly divide variables
into two main types,
– Global Variables
– Local variables
Global variable
• Example : Only declared, not initialized
include <stdio.h>
int x; // Global variable declared
int main()
{
x=10; // Initialized once
printf("first value of x = %d" ,x);
x=20; // Initialized again
printf("Initialized again with value = %d“, x);
}
Local variable
• Example :
include <stdio.h>
int main()
{
int i=10;
if(i<20) // if condition scope starts
{
int n=100; // Local variable declared and initialized
} // if condition scope ends
printf(“%d”,n); // Compile time error, n not available here
}
Some special types of variable

• Final - Once initialized, its value cant be changed.


• Static - These variables holds their value between
function calls.
• Example : include <stdio.h>
int main()
{
final int i=10;
static int y=20;
}
Operators in C Language
• An operator is a symbol that tells the compiler to
perform certain mathematical or logical
manipulations. Operators are used in program to
manipulate data and variables.
• C operators can be classified into following types,
Arithmetic operators
Relation operators
Logical operators
Bitwise operators
Assignment operators
Conditional operators
Special operators
Arithmetic operators
C supports all the basic arithmetic operators.
The following table shows all the basic arithmetic operators.
Operator Description
+ adds two operands
- subtract second operands from first
* multiply two operand
/ divide numerator by denumerator
% remainder of division
++ Increment operator increases integer
value by one
-- Decrement operator decreases integer
value by one
Relation operators
Operator Description
== Check if two operand are equal

!= Check if two operand are not equal.

> Check if operand on the left is greater


than operand on the right
< Check operand on the left is smaller
than right operand
>= check left operand is greater than or
equal to right operand
<= Check if operand on left is smaller than
or equal to right operand
Logical operators
Operator Description Example

&& Logical AND (a && b) is false

|| Logical OR (a || b) is true

! Logical NOT (!a) is false


Bitwise operators
Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< left shift
>> right shift
Assignment Operators
Operator Description Example
= assigns values from right side operands to a=b
left side operand
+= adds right operand to the left operand and a+=b is same as
assign the result to left a=a+b
-= subtracts right operand from the left a-=b is same as
operand and assign the result to left a=a-b
operand
*= mutiply left operand with the right operand a*=b is same as
and assign the result to left operand a=a*b
/= divides left operand with the right operand a/=b is same as
and assign the result to left operand a=a/b
%= calculate modulus using two operands and a%=b is same as
assign the result to left operand a=a%b
Conditional operator

• It is also known as ternary operator and used


to evaluate conditional expression.
• epr1 ? expr2 : expr3
• If epr1 Condition is true ? Then value expr2 :
Otherwise value expr3
Special operator
Operator Description Example

sizeof Returns the size of an sizeof(x) return


variable size of the
variable x
& Returns the address of &x ; return
an variable address of the
variable x
* Pointer to a variable *x ; will be
pointer to a
variable x

You might also like