2. C is System Programming Language: it is useful for
writing system programs (interface between the hardware
and user application) like compiler, device driver, BIOS(Basic
input/output system), linker, assembler etc.
It is a robust language with rich set of built-in functions and
operators that can be used to write any complex program.
C is Structured Language: C supports functions that
enables easy maintainability of code, by breaking large file
into smaller modules.
A C program is basically a collection of built-in functions
that are supported by C library. C contains many built-in
memory management functions.
Characteristics Of C
3. The portability, efficiency, ability to access specific hardware
addresses and low run time demand on system resources
makes it a good choice for implementing O.S. and embedded
system applications.
Case sensitive language.
C programs are fast, efficient, easy to debug(detects errors
quickly).
C also supports graphics programming.
It is as close to the machine as possible while it is almost
universally available for existing processor architectures.
There is at least one C compiler for almost every existent
architecture.
Characteristics Of C …
6. Type a program
Save it with .c extension – F2
Compile the program – This will generate an exe file
(executable) – alt+F9
Run the program (Actually the exe created out of
compilation will run and not the .c file) – ctrl+F9
Copy- Ctrl+insert
Cut- Shift+delete
Paste- Shift+insert
RUNNING A „C‟ PROGRAM
12. Program to print Hello World.
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
printf("Hello World");
getch();
return 0;
}
SIMPLE C PROGRAM
13. FILES USED IN C PROGRAMMING
Source File
(file with .c
extension)
Object File
(file with .o
extension)
Compiler
Header File
(file with .h
extension)
Linker
14. The files that are specified in the include section is
called as header file.
These are precompiled files that has some functions
defined in them.
We can call those functions in our program by
supplying parameters.
Some header files are “stdio.h”, “math.h”, “string.h”
and “conio.h” etc.
HEADER FILES
15. When a file is executed, the start point is the main
function.
Main function is the user-defined function.
From main function the flow goes as per the
programmers choice.
There may or may not be other functions written by
user in a program.
Main function is compulsory for any C program.
MAIN FUNCTION
16. Token is source-program text that the compiler does
not break down into component elements.
Basic element recognized by the compiler is the
"token“.
C LANGUAGE TOKENS
17. C has 6 different types of tokens viz.
1. Keywords [e.g. float, int, while]
2. Identifiers: The term identifier is usually used for name of
variable, function or array etc. [e.g. sum, amount]
3. Constants: Constants are expressions with a fixed value.
[e.g. -25.6, 100]
4. Strings: Sequence of characters. [e.g. “IIT”, “year”]
5. Special Symbols: Symbols other than the Alphabets and
Digits and white-spaces. [e.g. @, #, {, }, [, ] ]
6. Operators [e.g. +, -, *]
TOKEN TYPES IN „C‟
18. "Keywords" are words that have special meaning to
the C compiler.
C Keywords are also called as Reserved words .
Cannot be used as Variable Name.
In C there are 32 keywords.
All keywords are written in only lowercase.
THE KEYWORDS
19.
20. Identifiers are the names given to program elements such as
variables, array and functions. These are user defined names.
Cannot use C keywords as identifiers.
First character can only be an alphabet or underscore.
It can contain letters, digits or underscore ( _ ).
Must NOT contain white spaces in between.
case-sensitive.
THE IDENTIFIERS
21. CASE SENSITIVITY
C is a case sensitive language.
It matters whether an identifier, such as a variable
name, is uppercase or lowercase.
Example:
area
Area
AREA
ArEa
are all seen as different variables by the compiler.
23. Constants are entities with a fixed value. Value of constant is
known to the compiler at compile time.
CONSTANTS
CONSTANTS
Integer Type
Constants
Floating
Point type
Constants
Character
Type
Constants
String
Type
Constants
24. Integer Constants
Some of the examples are 112, 0551 etc.
The floating point Constants
The floating point constants such as 0.0083, -0.78, +67.89 etc.
Character Constants
For example, „8‟, „a‟ , „i‟ , „=‟ etc.
String Constants
For example, “0211”, “Stack Overflow” etc.
CONSTANTS EXAMPLES
25. DECLARATION
Must be declared before use.
A constant declaration specifies Type, Name and
the Value.
A variable declaration specifies Type, Name and
possibly the Initial Value.
When you declare a constant or a variable, the
compiler:
1. Reserves a memory location in which to store the
value of the constant or variable.
2. Associates the name of the constant or variable
with the memory location.
26. Container to store the data.
Variables names are the names given to memory locations.
x = a + b
z = 3(y - 5)
VARIABLES
20
Memory Cells
X 65209
27. DECLARING VARIABLES
Before using a variable, you must give the compiler
some information about the variable; i.e., you must
declare it.
Examples of variable declarations:
int length ;
float area ;
Variables are not automatically initialized.
Initializing variable: int rate = 5;
28. DATA TYPES IN C
•int, float, double, charPrimitive/ Primary
data-type
•Array, functions, pointers
Derived Data-type
•Structure, union, enum, typedefUser-defined Data-
type
•The void data type has no values .
Void
31. DATA TYPES : 1- INTEGER
Declarations tell the compiler what variable
names will be used and what type of data each
can handle (store).
Variables of integer type can be defined
- On separate lines:
int length;
int width;
unsigned int area;
- On the same line:
int length, width;
unsigned int area;
32. DATA TYPE: 2- CHARACTER
Internally char is just a number
Numerical value is associated with character.
Variables of character type can be defined:
char x;
char x, y;
Numeric value of character is stored in memory.
CODE:
char letter;
letter = 'C';
MEMORY:
letter
67
34. DATA TYPES: 3- FLOATING-POINT
A floating-point type is a number with a fractional
part
Represent typically 32 bit = 4 byte real numbers.
Available in different sizes (number of bytes): float,
double, and long double
Size of
float size of double size of long double
Variables of floating point type can be defined:
- double x;
- double x, y;
38. Used for performing numeric calculations
Operator precedence
Some arithmetic operators act before others (i.e., multiplication before
addition)
Use parenthesis when needed:
Example: Find the average of three variables a, b and c
Do not use: a + b + c / 3
Use: (a + b + c ) / 3
ARITHMETIC OPERATORS
39. ARITHMETIC OPERATORS
C operation Arithmetic
operator
Algebraic
expression
C expression
Addition + f + 7 f + 7
Subtraction - p – c p - c
Multiplication * bm b * m
Division / x / y x / y
Modulus % r mod s r % s
40. #include<stdio.h>
#include<conio.h>
int main()
{
int num1 = 10;
int num2 = 50;
int sum;
sum = num1+num2;
printf("sum of %d and %d is %d",num1,num2,sum);
getch();
return 0;
}
EXAMPLE
Write down a program to find subtraction of two numbers.
41. RULES OF OPERATOR PRECEDENCE
Operator(s) Operation(s) Order of evaluation
(precedence)
() Parentheses Evaluated First. If there are
several pairs of parentheses
are evaluated left to right.
*, /, or % Multiplication,
Division, Modulus
Evaluated Second. If there
are several, they are
evaluated left to right.
+ or - Addition and
Subtraction
Evaluated last. If there are
several, they are
evaluated left to right.
42. Relational operator are used to compare two operands.
Operands may be variable, constant or expression.
• Relational operators return Boolean values:
0 if relation is FALSE
1 if relation is TRUE
RELATIONAL OPERATORS
Operator Meaning
< Is less than
<= Is less than equal to
> Is greater than
>= Is greater than equal to
== Equal to
!= is not equal to
44. EXAMPLE OF RELATIONAL OPERATOR
main()
{
int a=10,b=20,c=30,d,e;
d=a>b;
e=b<=c;
printf(“%d %d”,d,e);
getch();
}
Output:-
0 1
45. Logical Operators are used to create compound expressions
1. if ( (a == 1) && (b < 3) || (c == 1) ) etc.
However, these can also be used in regular expressions
LOGICAL OPERATORS
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
47. Assignment operator are used to assign the value or an
expression or a value of a variable to another variable
int x = 9;
Some examples are::
x= x+y can be written as x+=y
a=a*(n+1) can be written as a *= n+1
z= z%d can be written as z%=d
ASSIGNMENT OPERATORS ( = )
a = 8
Expression
48. In C, we have 2 very useful operators called the increment &
decrement operators:
Increment : ++ adds 1 to the operand
Decrement : -- subtracts 1 from the operand
INCREMENT/DECREMENT OPERATORS
Operator Meaning
++x Pre increment
- -x Pre decrement
x++ Post increment
X- - Post decrement
50. Used to test the conditions in the conditional expressions.
Conditional expression
Format: <Expression 1> ? <Expression 2> : <Expression 3>
Example:
CONDITIONAL OPERATORS
Test Condition True expression False expression
x ? y : z
51. USE OF CONDITIONAL OPERATORS
Consider the following
statements:
a= 80;
b= 95;
if(a>b)
z=a;
else
z=b;
Now consider these
statements:
a=80;
b=95;
z= (a>b) ? a : b;
Both the statements are
resulting the same values.
This is an example of usage
of conditional expressions
52.
53. Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
BITWISE OPERATORS
These operators are used by the programmer to
communicate directly with the hardware.
54. BITWISE OPERATORS
x : 1 0 1 0 (binary)
y : 1 1 0 0 (binary)
x & y : 1 0 0 0 (binary)
x | y : 1 1 1 0 (binary)
x ^ y : 0 1 1 0 (binary)
~x : 0 1 0 1 (binary)
55. The sizeof operator is used with an operand to return the
number of bytes the operand occupies.
It‟s a compile time operator.
Also used to allocate memory spaces dynamically to different
variables while any program execution.
For example::
k= sizeof (sum);
j= sizeof(long int);
w= sizeof(32767);
THE SIZE OF OPERATOR
58. C executes program sequentially.
C provides various key condition statements to check
conditions and execute statements according conditional
criteria.
This statements are called decision making statements and
control statements.
CONDITIONS IN C PROGRAMMING
60. DECISION CONTROL STATEMENT
Decision control statement disrupt or alter the sequential
execution of the statement of the program depending on the
test condition in program.
Decision
control
statement
1. If
statement
3. Switch
statement
4. Go To
statement
2. If else
statement
61. if Statement
Example: if Statement
if...else Statement
Example: if...else Statement
if...else Ladder
Example: if...else Ladder
Nested if...else
Example: Nested if...else
C IF...ELSE STATEMENT
62. The If statement is a powerful decision making statement and
is used to control the flow of execution of statement.
IF STATEMENT
condition
Block of if
Next statement
STOP
FALSE
TRUE
63. The syntax of if statement is:
if (testExpression)
{
// statement(s)
}
// statement(s)
64. EXAMPLE OF IF STATEMENT
#include<stdio.h>
#include<conio.h>
int main()
{
int age = 20;
if(age>= 18)
{
printf(" You are an adult");
}
getch();
return 0;
}
65. The if statement evaluates the test expression inside the
parenthesis.
If the test expression is evaluated to true (nonzero),
statement(s) inside the body of if is executed.
If the test expression is evaluated to false (0), statement(s)
inside the body of if is skipped from execution.
66. If the condition is true the true block is execute otherwise
False block is execute.
IF-ELSE STATEMENT
condition
Block of if
Next statement
STOP
Block of else
FALSE
TRUE
67. if (testExpression)
{ // statement(s) inside the body of if
}
else
{
// statement(s) inside the body of else
}
68.
69. EXAMPLE OF IF ELSE STATEMENT
#include<stdio.h>
#include<conio.h>
int main()
{
int age = 16;
if(age<= 18)
{
printf(" You are a minor");
}
else
{
printf(" You are an adult");
}
getch();
return 0;
}
73. Switch statement is a multi-way decision making statement
which selects one of the several alternative based on the value
of integer variable or expression.
SWITCH STATEMENT