SlideShare a Scribd company logo
1 of 70
Introduction to C
History of C
• C is a procedural lang (or) structure oriented language, used
to develop different kinds of app’s.
• ALGOL-1960-International committees.
• CPL-1963-Cambridge University
• BCPL-1967- Martin Richards
• B-1970-Ken Thomson-for UNIX OS
• C-1972-dennis ritchie-Bell Labs.
Features of C
• Simple , general purpose language
• Modularity
• Speed
• Flexibility
• Extendability
• Rich set of operators
Uses of C
C can be used to develop,
•database systems
•word processors
•spread sheets
•operating systems
•compilers and assemblers etc.
Structure of a C program
1. Documentation section
2. Link Section
3. Definition Section
4. Global declaration section
5. Function prototype declaration section
6. Main function
7. User defined function definition section
1 #include <stdio.h> //This is a preprocessor command that includes standard input
//output header file(stdio.h) from the C library
2 void main() //This is the main function from where execution of any C program begins.
3 { //This indicates the beginning of the main function.
4 printf(“Hello_World! “); //Statements terminated by semicolon.
5 } //This indicates the end of the main function.
First C program
#include <stdio.h>
void main()
{
/* Our first simple C basic program */
printf(“Hello World! “);
}
How to execute a C program
In DOSBOX:
Write the program.
Press alt+f9 to compile the program.
Press ctrl+f9 to execute the program.
Internal process of program
execution
Source code
compiler
Machine code/ Object
Linker
executable code
C Tokens
• Constants
• Variables
• Keywords
• Identifiers
• Data types
• Operators
• Characters strings
• Special symbols
Constants
• A constant is a quantity that doesn’t change.
• A constant can be declared using the keyword 'const'.
• Syntax: const data_type variable_name;
eg: const int x;
Types of C constants
1. Integer constants. eg: 5,344, -32,232 etc
2. Real or Floating point constants. eg: 190.1221, 878.56767876 etc
3. Octal & Hexadecimal constants. eg: 016, 0X45
4. Character constants. eg: 'w', 'D' etc.
5. String constants. eg: “hello”,”good”
6. Backslash character constants. eg: 'n','t' etc.
Variables
• Name given to a location in memory where any constant is
stored.
• A Variable should be declared before use.
• Syntax: datatype variable_name;
eg: int a; // 2 bytes of memory is
// set aside in memory
Ex: 3X + 2Y = 20
Where 3, 2, 20 can’t change, they are called constants, where as the
quantities X & Y can vary or change , hence are called variables.
Keywords
• Also called reserved words.
• Words whose meaning has already been
explained to the C compiler.
• 1)auto 2)break 3)case 4)char 5)const
6)continue 7)default 8)do 9)double 10)else
11)enum 12) extem 13)float 14)for 15)goto
16)if 17)int 18)long 19)register 20)return
21)short 22)signed 23)size of 24)static 25)struct
26)switch 27)typedef 28)union 29) unsigned 30)
void 31)volatile 32)while
Identifiers
• Each program elements in a C program are given a name
called identifiers.
• Names given to identify Variables, functions and arrays are
examples for identifiers.
eg: a, x, swap(), m[] etc.
Rules for constructing identifier
names in C:
1. First character should be an alphabet or
underscore.
2. Succeeding characters might be digits or letter.
3. Punctuation and special characters aren’t
allowed except underscore.
4. Identifiers should not be keywords.
DATA TYPES
•C data types are defined as the data storage format that a
variable can store a data to perform a specific operation.
•Data types are used to define a variable before their use in a
program.
•Size of a variable, a constant and an array are determined by
data types.
C-Datatypes
There are four kinds of datatypes in C.
Sl.
no
kind examples
1 PRIMITIVE DATATYPES int, float, double, char
2 DERIVED DATATYPES array, pointer, enum
3 USER DEFINED DATATYPES structure, union
4 VOID void
int
• Int datatype is used to store numeric values.
• Keyword is int.
• Its size is 2 bytes.
• Its range is –32,767 to 32,767.
• Declaration and initialization:
int n=12333;
float
• Float datatype allows to store decimal point number.
• Keyword is float.
• Its size is 4 bytes.
• Precision is 6.
• Declaration and initialization:
float a=12.223233;
double
• Double datatype is used to store a decimal point number .
• Keyword is double.
• Its size is 8 bytes.
• Precision is 10 digits.
• Declaration and initialization:
double a=123.121224;
char
• Char datatype is used to store character type of data.
• Keyword is char.
• Its size is 1 byte.
• Declaration and initialization:
char a=‘A’.
Modifiers
Modifiers allow us to change the size of the datatypes
•Short
•Long
•Signed
•Unsigned
•Long long
S.No C Data types storage Size Range
1 Char 1 –127 to 127
2 Int 2 –32,767 to 32,767
3 Float 4 1E–37 to 1E+37 with six digits of precision
4 Double 8 1E–37 to 1E+37 with ten digits of precision
5 long double 10 1E–37 to 1E+37 with ten digits of precision
6 long int 4 –2,147,483,647 to 2,147,483,647
7 short int 2 –32,767 to 32,767
8 unsigned short int 2 0 to 65,535
9 signed short int 2 –32,767 to 32,767
10 long long int 8 –(2power(63) –1) to 2(power)63 –1
11 signed long int 4 –2,147,483,647 to 2,147,483,647
12 unsigned long int 4 0 to 4,294,967,295
13 unsigned long long int 8 2(power)64 –1
Operators
• An operator is a symbol , used to perform certain operations
on the operands.
• Eg: int a=10;
int b=20;
int c;
c=a + b;
a and b are called as operands and ‘+’ is the operator.
Types of operators
• Arithmetic operators
• Relational operators
• Logical operators
• Assignment operators
• Increment and decrement operators
• Conditional operator
• Bitwise operatorsS
• Special operators
Arithmetic operators
OPERATOR MEANING
+ Addition
- subtraction
* Multiplication
/ Division(co-efficient)
% Modulo division(remainder)
Relational operators
OPERATOR MEANING
> Is greater than
< Is less than
<= Is less than or equal to
>= Is greater than or equal to
== Is equal to
!= Is not equal to
logical operators
• Logical operators help us to combine two or more relational
expressions . Ex:- (A>B)&&(A>C)
OPERATOR MEANING
&& AND
|| OR
! NOT
Assignment operators
• These are used to assign the result of an expression to a
variable.
Operators Example Explanation
Simple assignment operator = sum=10 10 is assigned to variable sum
Compound assignment
operators
+= sum+=10 This_is_same_as_sum=sum+10…………
-= sum-=10 This is same as sum = sum-10
*= sum*=10 This is same as sum = sum*10
/+ sum/=10 This is same as sum = sum/10
%= sum%=10 This is same as sum = sum%10
&= sum&=10 This is same as sum = sum&10
^= sum^=10 This is same as sum = sum^10
Increment and decrement
operators
Operator Meaning
++ Increment by one
-- Decrement by one
...continued
• Prefix:- First, the variable value is increment or
decrement by one then the expression is
evaluated using the new value of the variable.
Syntax: ++var_name;
- -var_name;
• Postfix:- First, the expression is evaluated then
the variable value is increment or decrement by
one.
Syntax: var_name++;
var_name – -;
Difference between pre/post increment &
decrement operators in C
S.
no
Operator
type
Operator Description
1
Pre
increment
++i Value of i is incremented before assigning it to
variable i.
2
Post-
increment
i++ Value of i is incremented after assigning it to
variable i.
3
Pre
decrement
– –i Value of i is decremented before assigning it to
variable i.
4
Post_decrem
ent
i– – Value of i is decremented after assigning it to
variable i.
Conditional operator
Also called as ternary operator.
Syntax:
Condition ? Expression1 : Expression2
Operator meaning
? : If then
Bitwise operator
• Bitwise operators operate upon bits.
OPERATOR MEANING
& Bitwise AND
| Bitwise OR
^ XOR
<< Shift left
>> Shift right
Special operators
S.no Operators Description
1 &
This is used to get the address of the variable.
Example : &a will give address of a.
2 *
This is used as pointer to a variable.
Example : * a where, * is pointer to the variable a.
3 Sizeof ()
This gives the size of the variable.
Example : size of (char) will give us 1.
Control structures
• Instructions are executed one by one. That is called as
sequential flow of control.
• Control structures alter this flow of control.
• 3 kinds.
• Branching or conditional statements.
• Looping or iteration statements
• Jumping statements
Branching statements
• 5 kinds.
• if statement
• if.. else statement
• If..else..if ladder
• Nested if ..else statement
• Switch case statement
Simple if statement
• If statement tests a condition , if it is true then a set a
statements are executed.
• Syntax: if(condition)
{
// statement1;
//statement2;
.
.
//statementn;
}
if.. else statement
• If statement tests a condition ,if it is true it executes a set of
statements, if it is false then another set of statements.
• Syntax: if(condition)
{
// if part statements;
}
else
{
//else part statements;
}
if ..else..if ladder
• syntax: if(condition)
{
// if part statements;
}
else if(condition)
{
//else part statements;
}
else
{
//else part
}
Nested if else
• syntax: if(condition)
{
if(condition)
{
// if part statements;
}
else
{
//else part statements;
}
}
else
{
//else part statements;
}
Inner if else stmt
Switch ..case statement
• Tests multiple conditions.
• Syntax: switch(expression)
{
case value1: stmts;
break;
case value2: stmts;
break;
.
.
default : stmts;
}
Looping statements
• Repeats a set of statements specified number of times.
• For
• While
• Do..while
For loop
• Syntax:
for(initialization ; test ; increment/decrement)
{
//statements;
}
Steps in execution of for loop
1.Initialization of the loop counter.
2.Condition is tested.
3.If the test is true then loop statements are executed.
4.When the loop ends the loop counter is incremented
5.Again the condition is tested , if it is true, the loop is executed, if it is
false , the loop exits.
While loop
• Syntax:
initialization statement;
while(condition)
{
//statements;
increment statement;
{
Do..while loop
• Syntax:
do{
//statements;
}while(condition);
• Statements are executed until the condition is true.
• Condition is tested at the end.
• Statements are executed at least once.
Jumping statements
• goto
• break
• continue
• return
Goto statement
• Goto statement allows you to jump from one place to another
in a program, unconditionally.
• Syntax: goto labelname;
.
.
labelname: statements;
Break statement
• Break statement is used to
• break out of loops
• break out of switch statement
• Syntax: break;
• Break when used in loops , takes you to the end of the loop.
Continue statement
• Continue statement allows us to continue the loop, bypassing
some of the statements of the loop.
• Continue when used in loop, takes you to the beginning of the
loop.
• Syntax: continue;
Return statement
• Return statement allow us to return a value from a function.
• Syntax: return value;
Arrays
• An Array is a collection of similar datatypes.
• An array elements are allocated memory in contigous memory
locations.
• An array is denoted by a pair of square brackets ‘[ ]’.
• Array index starts with zero.
..continued
• Array declaration:
• Syntax: datatype array_variable_name[size];
• Eg : int arr[5]; //array of type int and it
//allocates space for 5 elements.
float arr1[10]; // an array of type float and
//space for 10 float elements.
..continued
• Array initialization: 3 ways
• An element at once
• An array can be initialized when it is declared
• Can be initialized from the keyboard
..continued
1. An element at once :
int arr[5] ;
arr[0]=1;
arr[1]=2;
arr[2]=34;
arr[3]=4;
arr[4]=55;
arr
1
554342
..continued
2. Declaration and initialization:
Initialization can be done at the time of declaration using
flower brackets.
eg : int arr[3]= {1,2,3};
arr
1 2 3
..continued
3. From the keyboard using for loop and scanf() function.
eg : int a[10], i;
printf(“enter values into an”);
for(i=0;i<10;i++)
{
scanf(“%d”,&a[i]);
}
Functions
• A function is a sub program, which performs a
particular task when called.
• A C program is a collection of some functions.
• main() is the predefined function from where
the execution of a program starts.
• C program = Main() + user defined functions
..continued
• Uses of functions:
• Re-useability
• Modularity
• Re-usability: C functions are used to avoid rewriting same code again and
again in a program.
• We can call functions any number of times in a program and from any place
in a program for the same task to be performed.
• Modularity: Dividing a big task into small pieces improves understandability
of very large C programs.
• A large C program can easily be tracked when it is divided into functions.
Defining a Function
• Syntax:
function definition function declaration
return_type function_name(List of parameters)
{
//set of statements
function body
}
..continued
• Function definition – This contains all the statements to be executed.
Function definition = function declaration +function body
A function body is a group of statements enclosed in
flower brackets.
eg: void main() function declaration
{
int a=10; function definition
function body
printf(“a = %d”,a);
}
Function terminology
• Function prototype
• Function definition
• Function call
Function prototype or declaration - This informs compiler about
the function name, function parameters and return value’s data
type.
Syntax: return_type function_name(parameter list);
This is written before main() function. To inform the
compiler that there exists a function in the program.
Function Call
• This statement actually calls the function.
• Syntax: function_name( list of parameter values);
• This statement is written in the calling function.
• With this statement, the control is transferred to the function
definition.
• Eg: sum(10,20);
Function calls
• Functions can be defined in any one of the following 4
different ways.
 No arguments , No return value .
 No arguments , With return value .
 With arguments , No return value .
 With arguments , With return value .
• Arguments:-
These values are passed from main() function to the invoked
function . Generally these values are called as inputs.
• Return value:-
These values are passed from one function to another
function while invoking function. Generally these values are
called as outputs.
Ex:- Program to print the sum of given two numbers
using functions?
i)No arguments , No return value
void add ( ) ; //function prototype
void main()
{
add();  //function calling
}
void add() //function definition
{
int a,b,c;
printf(“enter any numbers”);
scanf(“%d%d”, &a , &b) ;
c = a + b ;
printf(“n sum is=%d ” , c ) ;
}
ii)No arguments , With return value
int add ( ) ; //function prototype
void main()
{
printf(“nsum is=%d”,add()); // function call
}
int add() ; //function definition
{
int a,b,c;
printf(“enter any numbers”);
scanf(“%d%d”, &a , &b) ;
c = a + b ;
return c ;
}
iii)With arguments , No return value
void add ( int , int ) ; //function prototype
void main()
{
int a , b ;
printf(“enter any two numbers”);
scanf(“%d%d”,&a,&b);
add(a,b);  //function call
}
void add ( int x , int y ) //function definition
{
int z = x + y ;
printf(“sum is=%d”,z) ; }
iv)With arguments , With return value
int add ( int , int ) ; //function prototype
void main()
{
int a , b ;
printf(“enter any two numbers”);
scanf(“%d%d”,&a,&b);
printf(“sum is=%d”,add(a,b));  //function call
}
int add ( int x , int y ) //function definition
{
int z = x + y ;
return z ; }
Function calls
• There are two ways that a C function can be called from
a program. They are,
• Call by value
• Call by reference

More Related Content

What's hot

Functions
FunctionsFunctions
FunctionsOnline
 
Functions in c language
Functions in c language Functions in c language
Functions in c language tanmaymodi4
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in CHarendra Singh
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in CSyed Mustafa
 
M11 operator overloading and type conversion
M11 operator overloading and type conversionM11 operator overloading and type conversion
M11 operator overloading and type conversionNabeelaNousheen
 
Lecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.pptLecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.ppteShikshak
 
predefined and user defined functions
predefined and user defined functionspredefined and user defined functions
predefined and user defined functionsSwapnil Yadav
 
Presentation on function
Presentation on functionPresentation on function
Presentation on functionAbu Zaman
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)Mansi Tyagi
 
Function in c program
Function in c programFunction in c program
Function in c programumesh patil
 
Function in c language(defination and declaration)
Function in c language(defination and declaration)Function in c language(defination and declaration)
Function in c language(defination and declaration)VC Infotech
 

What's hot (19)

Functions
FunctionsFunctions
Functions
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Function in c
Function in cFunction in c
Function in c
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
Cpp functions
Cpp functionsCpp functions
Cpp functions
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
 
Function in C Language
Function in C Language Function in C Language
Function in C Language
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
M11 operator overloading and type conversion
M11 operator overloading and type conversionM11 operator overloading and type conversion
M11 operator overloading and type conversion
 
Lecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.pptLecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.ppt
 
Function in c
Function in cFunction in c
Function in c
 
predefined and user defined functions
predefined and user defined functionspredefined and user defined functions
predefined and user defined functions
 
Presentation on function
Presentation on functionPresentation on function
Presentation on function
 
Function lecture
Function lectureFunction lecture
Function lecture
 
Function
FunctionFunction
Function
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Function in c language(defination and declaration)
Function in c language(defination and declaration)Function in c language(defination and declaration)
Function in c language(defination and declaration)
 

Viewers also liked

Python PPT
Python PPTPython PPT
Python PPTEdureka!
 
Introduction to python for Beginners
Introduction to python for Beginners Introduction to python for Beginners
Introduction to python for Beginners Sujith Kumar
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python amiable_indian
 
Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Paige Bailey
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesMatt Harrison
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to PythonNowell Strite
 

Viewers also liked (8)

Python 101
Python 101Python 101
Python 101
 
Python PPT
Python PPTPython PPT
Python PPT
 
Introduction to python for Beginners
Introduction to python for Beginners Introduction to python for Beginners
Introduction to python for Beginners
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
 
Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 Minutes
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 

Similar to Introduction to c

Introduction to c
Introduction to cIntroduction to c
Introduction to cAjeet Kumar
 
Computer programming - variables constants operators expressions and statements
Computer programming - variables constants operators expressions and statementsComputer programming - variables constants operators expressions and statements
Computer programming - variables constants operators expressions and statementsJohn Paul Espino
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Rasan Samarasinghe
 
Cs1123 4 variables_constants
Cs1123 4 variables_constantsCs1123 4 variables_constants
Cs1123 4 variables_constantsTAlha MAlik
 
Chapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingChapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingEric Chou
 
CS4443 - Modern Programming Language - I Lecture (2)
CS4443 - Modern Programming Language - I  Lecture (2)CS4443 - Modern Programming Language - I  Lecture (2)
CS4443 - Modern Programming Language - I Lecture (2)Dilawar Khan
 
Lamborghini Veneno Allegheri #2004@f**ck
Lamborghini Veneno Allegheri #2004@f**ckLamborghini Veneno Allegheri #2004@f**ck
Lamborghini Veneno Allegheri #2004@f**ckseidounsemel
 
java in Aartificial intelligent by virat andodariya
java in Aartificial intelligent by virat andodariyajava in Aartificial intelligent by virat andodariya
java in Aartificial intelligent by virat andodariyaviratandodariya
 
JAVA in Artificial intelligent
JAVA in Artificial intelligentJAVA in Artificial intelligent
JAVA in Artificial intelligentVirat Andodariya
 
C language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C languageC language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C languageRakesh Roshan
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overviewTAlha MAlik
 

Similar to Introduction to c (20)

Basic concept of c++
Basic concept of c++Basic concept of c++
Basic concept of c++
 
C Language Part 1
C Language Part 1C Language Part 1
C Language Part 1
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
 
Computer programming - variables constants operators expressions and statements
Computer programming - variables constants operators expressions and statementsComputer programming - variables constants operators expressions and statements
Computer programming - variables constants operators expressions and statements
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++
 
Cs1123 4 variables_constants
Cs1123 4 variables_constantsCs1123 4 variables_constants
Cs1123 4 variables_constants
 
Prog1-L2.pptx
Prog1-L2.pptxProg1-L2.pptx
Prog1-L2.pptx
 
C_plus_plus
C_plus_plusC_plus_plus
C_plus_plus
 
Chapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingChapter 2: Elementary Programming
Chapter 2: Elementary Programming
 
C language
C languageC language
C language
 
CS4443 - Modern Programming Language - I Lecture (2)
CS4443 - Modern Programming Language - I  Lecture (2)CS4443 - Modern Programming Language - I  Lecture (2)
CS4443 - Modern Programming Language - I Lecture (2)
 
Lamborghini Veneno Allegheri #2004@f**ck
Lamborghini Veneno Allegheri #2004@f**ckLamborghini Veneno Allegheri #2004@f**ck
Lamborghini Veneno Allegheri #2004@f**ck
 
java in Aartificial intelligent by virat andodariya
java in Aartificial intelligent by virat andodariyajava in Aartificial intelligent by virat andodariya
java in Aartificial intelligent by virat andodariya
 
JAVA in Artificial intelligent
JAVA in Artificial intelligentJAVA in Artificial intelligent
JAVA in Artificial intelligent
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
Java chapter 2
Java chapter 2Java chapter 2
Java chapter 2
 
C language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C languageC language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C language
 
Chapter 2.datatypes and operators
Chapter 2.datatypes and operatorsChapter 2.datatypes and operators
Chapter 2.datatypes and operators
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overview
 
Csc240 -lecture_4
Csc240  -lecture_4Csc240  -lecture_4
Csc240 -lecture_4
 

Recently uploaded

How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 

Recently uploaded (20)

How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 

Introduction to c

  • 2. History of C • C is a procedural lang (or) structure oriented language, used to develop different kinds of app’s. • ALGOL-1960-International committees. • CPL-1963-Cambridge University • BCPL-1967- Martin Richards • B-1970-Ken Thomson-for UNIX OS • C-1972-dennis ritchie-Bell Labs.
  • 3. Features of C • Simple , general purpose language • Modularity • Speed • Flexibility • Extendability • Rich set of operators
  • 4. Uses of C C can be used to develop, •database systems •word processors •spread sheets •operating systems •compilers and assemblers etc.
  • 5. Structure of a C program 1. Documentation section 2. Link Section 3. Definition Section 4. Global declaration section 5. Function prototype declaration section 6. Main function 7. User defined function definition section
  • 6. 1 #include <stdio.h> //This is a preprocessor command that includes standard input //output header file(stdio.h) from the C library 2 void main() //This is the main function from where execution of any C program begins. 3 { //This indicates the beginning of the main function. 4 printf(“Hello_World! “); //Statements terminated by semicolon. 5 } //This indicates the end of the main function.
  • 7. First C program #include <stdio.h> void main() { /* Our first simple C basic program */ printf(“Hello World! “); }
  • 8. How to execute a C program In DOSBOX: Write the program. Press alt+f9 to compile the program. Press ctrl+f9 to execute the program.
  • 9. Internal process of program execution Source code compiler Machine code/ Object Linker executable code
  • 10. C Tokens • Constants • Variables • Keywords • Identifiers • Data types • Operators • Characters strings • Special symbols
  • 11. Constants • A constant is a quantity that doesn’t change. • A constant can be declared using the keyword 'const'. • Syntax: const data_type variable_name; eg: const int x;
  • 12. Types of C constants 1. Integer constants. eg: 5,344, -32,232 etc 2. Real or Floating point constants. eg: 190.1221, 878.56767876 etc 3. Octal & Hexadecimal constants. eg: 016, 0X45 4. Character constants. eg: 'w', 'D' etc. 5. String constants. eg: “hello”,”good” 6. Backslash character constants. eg: 'n','t' etc.
  • 13. Variables • Name given to a location in memory where any constant is stored. • A Variable should be declared before use. • Syntax: datatype variable_name; eg: int a; // 2 bytes of memory is // set aside in memory Ex: 3X + 2Y = 20 Where 3, 2, 20 can’t change, they are called constants, where as the quantities X & Y can vary or change , hence are called variables.
  • 14. Keywords • Also called reserved words. • Words whose meaning has already been explained to the C compiler. • 1)auto 2)break 3)case 4)char 5)const 6)continue 7)default 8)do 9)double 10)else 11)enum 12) extem 13)float 14)for 15)goto 16)if 17)int 18)long 19)register 20)return 21)short 22)signed 23)size of 24)static 25)struct 26)switch 27)typedef 28)union 29) unsigned 30) void 31)volatile 32)while
  • 15. Identifiers • Each program elements in a C program are given a name called identifiers. • Names given to identify Variables, functions and arrays are examples for identifiers. eg: a, x, swap(), m[] etc.
  • 16. Rules for constructing identifier names in C: 1. First character should be an alphabet or underscore. 2. Succeeding characters might be digits or letter. 3. Punctuation and special characters aren’t allowed except underscore. 4. Identifiers should not be keywords.
  • 17. DATA TYPES •C data types are defined as the data storage format that a variable can store a data to perform a specific operation. •Data types are used to define a variable before their use in a program. •Size of a variable, a constant and an array are determined by data types.
  • 18. C-Datatypes There are four kinds of datatypes in C. Sl. no kind examples 1 PRIMITIVE DATATYPES int, float, double, char 2 DERIVED DATATYPES array, pointer, enum 3 USER DEFINED DATATYPES structure, union 4 VOID void
  • 19. int • Int datatype is used to store numeric values. • Keyword is int. • Its size is 2 bytes. • Its range is –32,767 to 32,767. • Declaration and initialization: int n=12333;
  • 20. float • Float datatype allows to store decimal point number. • Keyword is float. • Its size is 4 bytes. • Precision is 6. • Declaration and initialization: float a=12.223233;
  • 21. double • Double datatype is used to store a decimal point number . • Keyword is double. • Its size is 8 bytes. • Precision is 10 digits. • Declaration and initialization: double a=123.121224;
  • 22. char • Char datatype is used to store character type of data. • Keyword is char. • Its size is 1 byte. • Declaration and initialization: char a=‘A’.
  • 23. Modifiers Modifiers allow us to change the size of the datatypes •Short •Long •Signed •Unsigned •Long long
  • 24. S.No C Data types storage Size Range 1 Char 1 –127 to 127 2 Int 2 –32,767 to 32,767 3 Float 4 1E–37 to 1E+37 with six digits of precision 4 Double 8 1E–37 to 1E+37 with ten digits of precision 5 long double 10 1E–37 to 1E+37 with ten digits of precision 6 long int 4 –2,147,483,647 to 2,147,483,647 7 short int 2 –32,767 to 32,767 8 unsigned short int 2 0 to 65,535 9 signed short int 2 –32,767 to 32,767 10 long long int 8 –(2power(63) –1) to 2(power)63 –1 11 signed long int 4 –2,147,483,647 to 2,147,483,647 12 unsigned long int 4 0 to 4,294,967,295 13 unsigned long long int 8 2(power)64 –1
  • 25. Operators • An operator is a symbol , used to perform certain operations on the operands. • Eg: int a=10; int b=20; int c; c=a + b; a and b are called as operands and ‘+’ is the operator.
  • 26. Types of operators • Arithmetic operators • Relational operators • Logical operators • Assignment operators • Increment and decrement operators • Conditional operator • Bitwise operatorsS • Special operators
  • 27. Arithmetic operators OPERATOR MEANING + Addition - subtraction * Multiplication / Division(co-efficient) % Modulo division(remainder)
  • 28. Relational operators OPERATOR MEANING > Is greater than < Is less than <= Is less than or equal to >= Is greater than or equal to == Is equal to != Is not equal to
  • 29. logical operators • Logical operators help us to combine two or more relational expressions . Ex:- (A>B)&&(A>C) OPERATOR MEANING && AND || OR ! NOT
  • 30. Assignment operators • These are used to assign the result of an expression to a variable. Operators Example Explanation Simple assignment operator = sum=10 10 is assigned to variable sum Compound assignment operators += sum+=10 This_is_same_as_sum=sum+10………… -= sum-=10 This is same as sum = sum-10 *= sum*=10 This is same as sum = sum*10 /+ sum/=10 This is same as sum = sum/10 %= sum%=10 This is same as sum = sum%10 &= sum&=10 This is same as sum = sum&10 ^= sum^=10 This is same as sum = sum^10
  • 31. Increment and decrement operators Operator Meaning ++ Increment by one -- Decrement by one
  • 32. ...continued • Prefix:- First, the variable value is increment or decrement by one then the expression is evaluated using the new value of the variable. Syntax: ++var_name; - -var_name; • Postfix:- First, the expression is evaluated then the variable value is increment or decrement by one. Syntax: var_name++; var_name – -;
  • 33. Difference between pre/post increment & decrement operators in C S. no Operator type Operator Description 1 Pre increment ++i Value of i is incremented before assigning it to variable i. 2 Post- increment i++ Value of i is incremented after assigning it to variable i. 3 Pre decrement – –i Value of i is decremented before assigning it to variable i. 4 Post_decrem ent i– – Value of i is decremented after assigning it to variable i.
  • 34. Conditional operator Also called as ternary operator. Syntax: Condition ? Expression1 : Expression2 Operator meaning ? : If then
  • 35. Bitwise operator • Bitwise operators operate upon bits. OPERATOR MEANING & Bitwise AND | Bitwise OR ^ XOR << Shift left >> Shift right
  • 36. Special operators S.no Operators Description 1 & This is used to get the address of the variable. Example : &a will give address of a. 2 * This is used as pointer to a variable. Example : * a where, * is pointer to the variable a. 3 Sizeof () This gives the size of the variable. Example : size of (char) will give us 1.
  • 37. Control structures • Instructions are executed one by one. That is called as sequential flow of control. • Control structures alter this flow of control. • 3 kinds. • Branching or conditional statements. • Looping or iteration statements • Jumping statements
  • 38. Branching statements • 5 kinds. • if statement • if.. else statement • If..else..if ladder • Nested if ..else statement • Switch case statement
  • 39. Simple if statement • If statement tests a condition , if it is true then a set a statements are executed. • Syntax: if(condition) { // statement1; //statement2; . . //statementn; }
  • 40. if.. else statement • If statement tests a condition ,if it is true it executes a set of statements, if it is false then another set of statements. • Syntax: if(condition) { // if part statements; } else { //else part statements; }
  • 41. if ..else..if ladder • syntax: if(condition) { // if part statements; } else if(condition) { //else part statements; } else { //else part }
  • 42. Nested if else • syntax: if(condition) { if(condition) { // if part statements; } else { //else part statements; } } else { //else part statements; } Inner if else stmt
  • 43. Switch ..case statement • Tests multiple conditions. • Syntax: switch(expression) { case value1: stmts; break; case value2: stmts; break; . . default : stmts; }
  • 44. Looping statements • Repeats a set of statements specified number of times. • For • While • Do..while
  • 45. For loop • Syntax: for(initialization ; test ; increment/decrement) { //statements; } Steps in execution of for loop 1.Initialization of the loop counter. 2.Condition is tested. 3.If the test is true then loop statements are executed. 4.When the loop ends the loop counter is incremented 5.Again the condition is tested , if it is true, the loop is executed, if it is false , the loop exits.
  • 46. While loop • Syntax: initialization statement; while(condition) { //statements; increment statement; {
  • 47. Do..while loop • Syntax: do{ //statements; }while(condition); • Statements are executed until the condition is true. • Condition is tested at the end. • Statements are executed at least once.
  • 48. Jumping statements • goto • break • continue • return
  • 49. Goto statement • Goto statement allows you to jump from one place to another in a program, unconditionally. • Syntax: goto labelname; . . labelname: statements;
  • 50. Break statement • Break statement is used to • break out of loops • break out of switch statement • Syntax: break; • Break when used in loops , takes you to the end of the loop.
  • 51. Continue statement • Continue statement allows us to continue the loop, bypassing some of the statements of the loop. • Continue when used in loop, takes you to the beginning of the loop. • Syntax: continue;
  • 52. Return statement • Return statement allow us to return a value from a function. • Syntax: return value;
  • 53. Arrays • An Array is a collection of similar datatypes. • An array elements are allocated memory in contigous memory locations. • An array is denoted by a pair of square brackets ‘[ ]’. • Array index starts with zero.
  • 54. ..continued • Array declaration: • Syntax: datatype array_variable_name[size]; • Eg : int arr[5]; //array of type int and it //allocates space for 5 elements. float arr1[10]; // an array of type float and //space for 10 float elements.
  • 55. ..continued • Array initialization: 3 ways • An element at once • An array can be initialized when it is declared • Can be initialized from the keyboard
  • 56. ..continued 1. An element at once : int arr[5] ; arr[0]=1; arr[1]=2; arr[2]=34; arr[3]=4; arr[4]=55; arr 1 554342
  • 57. ..continued 2. Declaration and initialization: Initialization can be done at the time of declaration using flower brackets. eg : int arr[3]= {1,2,3}; arr 1 2 3
  • 58. ..continued 3. From the keyboard using for loop and scanf() function. eg : int a[10], i; printf(“enter values into an”); for(i=0;i<10;i++) { scanf(“%d”,&a[i]); }
  • 59. Functions • A function is a sub program, which performs a particular task when called. • A C program is a collection of some functions. • main() is the predefined function from where the execution of a program starts. • C program = Main() + user defined functions
  • 60. ..continued • Uses of functions: • Re-useability • Modularity • Re-usability: C functions are used to avoid rewriting same code again and again in a program. • We can call functions any number of times in a program and from any place in a program for the same task to be performed. • Modularity: Dividing a big task into small pieces improves understandability of very large C programs. • A large C program can easily be tracked when it is divided into functions.
  • 61. Defining a Function • Syntax: function definition function declaration return_type function_name(List of parameters) { //set of statements function body }
  • 62. ..continued • Function definition – This contains all the statements to be executed. Function definition = function declaration +function body A function body is a group of statements enclosed in flower brackets. eg: void main() function declaration { int a=10; function definition function body printf(“a = %d”,a); }
  • 63. Function terminology • Function prototype • Function definition • Function call Function prototype or declaration - This informs compiler about the function name, function parameters and return value’s data type. Syntax: return_type function_name(parameter list); This is written before main() function. To inform the compiler that there exists a function in the program.
  • 64. Function Call • This statement actually calls the function. • Syntax: function_name( list of parameter values); • This statement is written in the calling function. • With this statement, the control is transferred to the function definition. • Eg: sum(10,20);
  • 65. Function calls • Functions can be defined in any one of the following 4 different ways.  No arguments , No return value .  No arguments , With return value .  With arguments , No return value .  With arguments , With return value . • Arguments:- These values are passed from main() function to the invoked function . Generally these values are called as inputs. • Return value:- These values are passed from one function to another function while invoking function. Generally these values are called as outputs.
  • 66. Ex:- Program to print the sum of given two numbers using functions? i)No arguments , No return value void add ( ) ; //function prototype void main() { add();  //function calling } void add() //function definition { int a,b,c; printf(“enter any numbers”); scanf(“%d%d”, &a , &b) ; c = a + b ; printf(“n sum is=%d ” , c ) ; }
  • 67. ii)No arguments , With return value int add ( ) ; //function prototype void main() { printf(“nsum is=%d”,add()); // function call } int add() ; //function definition { int a,b,c; printf(“enter any numbers”); scanf(“%d%d”, &a , &b) ; c = a + b ; return c ; }
  • 68. iii)With arguments , No return value void add ( int , int ) ; //function prototype void main() { int a , b ; printf(“enter any two numbers”); scanf(“%d%d”,&a,&b); add(a,b);  //function call } void add ( int x , int y ) //function definition { int z = x + y ; printf(“sum is=%d”,z) ; }
  • 69. iv)With arguments , With return value int add ( int , int ) ; //function prototype void main() { int a , b ; printf(“enter any two numbers”); scanf(“%d%d”,&a,&b); printf(“sum is=%d”,add(a,b));  //function call } int add ( int x , int y ) //function definition { int z = x + y ; return z ; }
  • 70. Function calls • There are two ways that a C function can be called from a program. They are, • Call by value • Call by reference

Editor's Notes

  1. The amount of memory space to be allocated for a variable is derived by modifiers. Modifiers are prefixed with basic data types to modify (either increase or decrease) the amount of storage space allocated to a variable. For example, storage space for int data type is 4 byte for 32 bit processor. We can increase the range by using long int which is 8 byte. We can decrease the range by using short int which is 2 byte.
  2. #include &amp;lt;stdio.h&amp;gt;int main(){int a=40,b=20, add,sub,mul,div,mod; add = a+b;sub = a-b;mul = a*b;div = a/b;mod = a%b; printf(&amp;quot;Addition of a, b is : %d\n&amp;quot;, add);printf(&amp;quot;Subtraction of a, b is : %d\n&amp;quot;, sub);printf(&amp;quot;Multiplication of a, b is : %d\n&amp;quot;, mul);printf(&amp;quot;Division of a, b is : %d\n&amp;quot;, div);printf(&amp;quot;Modulus of a, b is : %d\n&amp;quot;, mod);} Output: Addition of a, b is : 60Subtraction of a, b is : 20Multiplication of a, b is : 800Division of a, b is : 2Modulus of a, b is : 0
  3. #include &amp;lt;stdio.h&amp;gt; int main(void)  {         int a,b;         a=10; b=20;         printf(&amp;quot;Relational Operators output \n&amp;quot;);         printf(&amp;quot;a &amp;lt; b is : %d \n&amp;quot;,a&amp;lt;b);         printf(&amp;quot;a &amp;lt;= b is : %d \n&amp;quot;,a&amp;lt;=b);         printf(&amp;quot;a == b is : %d \n&amp;quot;,a==b);         printf(&amp;quot;a != b is : %d \n&amp;quot;,a!=b);         printf(&amp;quot;a &amp;gt; b is : %d \n&amp;quot;,a&amp;gt;b);         printf(&amp;quot;a &amp;gt;= b is : %d \n&amp;quot;,a&amp;gt;=b);         return 0; } Output : Relational Operators outputa &amp;lt; b is : 1a &amp;lt;= b is : 1a == b is : 0a != b is : 1a &amp;gt; b is : 0a &amp;gt;= b is : 0
  4. Program to show Prefix and Postfix operators on int value and char value #include&amp;lt;stdio.h&amp;gt; void main() { int x=5; char ch=&amp;apos;a&amp;apos;; printf(&amp;quot;postfix is %d\n&amp;quot;,x++); printf(&amp;quot;prefix is %d\n&amp;quot;,++x); printf(&amp;quot;\n\n&amp;quot;); printf(&amp;quot;postfix is %c\n&amp;quot;,ch++); printf(&amp;quot;prefix is %c\n&amp;quot;,++ch); }
  5. If the condition is true, expression1 will be executed. If it is false, then expression2 is executed.