SlideShare una empresa de Scribd logo
1 de 44
Recall
• Difference between while and do while?
• Difference between == and =
• Difference between break and continue?
• What is the difference between i++ and
++i
• What if i didn’t give break in switch?
• What is the difference between logical
operators and relational operators
Introduction to C
functions - Arrays – structures - pointers
Week 2- day 2
Functions
Functions
• A function is a group of statements that
together perform a task.
return_type function_name ( parameter list )
{
body of the function
}
Eg: main()
{
Printf(“hello world”);
Functions
int equation(int num1, int num2)
{
int result;
result = num1*num1+2*num1*num2+num2*num2;
return result;
}
Return Type
Function name
Arguments
#include<stdio.h>
int equation(int a, int b)
{
int c;
c= a*a+2*a*b+b*b;
return c;
}
Int main ()
{
int a,b,c;
Printf(“Enter the number”);
Scanf(“%d %d”,&a ,&b);
c = equation (a,b) ;
Printf(“%d”,c);
}
#include<stdio.h>
int equation(int a, int b)
{
int c;
c= a*a+2*a*b+b*b;
return c;
}
Int main ()
{
int a,b,c;
Printf(“Enter the number”);
Scanf(“%d %d”,&a ,&b);
c = equation (a,b) ; // Calling the function
Printf(“%d”,c);
}
//Function body / function Definition
#include<stdio.h>
int equation(int a, int b)
{
int c;
c= a*a+2*a*b+b*b;
return c;
}
Int main ()
{
int a,b,c;
Printf(“Enter the number”);
Scanf(“%d %d”,&a ,&b);
c = equation (a,b) ; // Calling the function
Printf(“%d”,c);
}
Should I use the same variable names?
Are they same of what I used in main() ?
#include<stdio.h>
int equation(int a, int b)
{
int c;
c= a*a+2*a*b+b*b;
return c;
}
Int main ()
{
int a,b,c;
Printf(“Enter the number”);
Scanf(“%d %d”,&a ,&b);
c = equation (a,b) ; // Calling the function
Printf(“%d”,c);
}
Absolutely not !!!
Variable a in equation() is different from
varaible a in main(). There is no
connection between those two
#include<stdio.h>
int equation(int a, int b)
{
int c;
c= a*a+2*a*b+b*b;
return c;
}
Int main ()
{
int a,b,c;
Printf(“Enter the number”);
Scanf(“%d %d”,&a ,&b);
c = equation (a,b) ; // Calling the function
Printf(“%d”,c);
}
What is this return???
#include<stdio.h>
int equation(int a, int b)
{
int c;
c= a*a+2*a*b+b*b;
return c;
}
Int main ()
{
int a,b,c;
Printf(“Enter the number”);
Scanf(“%d %d”,&a ,&b);
c = equation (a,b) ; // Calling the function
Printf(“%d”,c);
}
Return will simply send value out of
function to whomsoever it was called
#include<stdio.h>
int equation(int a, int b)
{
int c;
c= a*a+2*a*b+b*b;
return c;
}
Int main ()
{
int a,b,c;
Printf(“Enter the number”);
Scanf(“%d %d”,&a ,&b);
c = equation (a,b) ; // Calling the function
Printf(“%d”,c);
}
Can i write the whole function body after
main function??
#include<stdio.h>
int equation(int a, int b) ;
Int main ()
{
int a,b,c;
Printf(“Enter the number”);
Scanf(“%d %d”,&a ,&b);
c = equation (a,b) ; // Calling the function
Printf(“%d”,c);
}
int equation(int a, int b)
{
int c;
c= a*a+2*a*b+b*b;
return c;
}
Yes you can. But only additional thing you
need to do is
#include<stdio.h>
int equation(int a, int b) ;
Int main ()
{
int a,b,c;
Printf(“Enter the number”);
Scanf(“%d %d”,&a ,&b);
c = equation (a,b) ; // Calling the function
Printf(“%d”,c);
}
int equation(int a, int b)
{
int c;
c= a*a+2*a*b+b*b;
return c;
}
You must declare it at the beginning
Arrays
Arrays
• Is a collection of data of same type with
common name
• Eg: int a[6]
A[5]
A[4]
A[3]
A[2]
A[1]
A[0]
Memory
Each Cell will be size of int
Example
Main()
{
int a[10];
For(i=0;i<10;i++)
Scanf(“%d”,&a[i]);
For(i=0;i<10;i++)
printf(“%d”, a[i]);
}`
Strings in C
• There is no string data type in C
• But we can do the same with character
array in C
– Eg: char a[8] =“baabtra”//enables us to store
string with 6 characters
/0
A
R
T
B
A
A
B
A[7]
A[6]
a[5]
a[4]
a[3]
a[2]
a[1]
a[0]
main()
{
Char a[7]=“baabtra”; //Correct
Char *p=“baabtra”; //Correct
a=“baabtra”; //Incorrect
Scanf(“%s”,&a); //Correct
Scanf(“%s”, a); //Correct;But
why?? will discuss soon
Printf(“%s”,a);
}
Structures
• A structure is a collection of variables
under a single name
• These variables can be of different types,
and each has a name which is used to
select it from the structure.
struct struct_name
{
//Variable declarations;
}
Why structures?
• A structure is a convenient way of
grouping several pieces of related
information together.
• Eg: suppose there are several variables
called name,age,gender related to a
student . So we can bring all of them
under one name student using structures
• Eg: struct student
{
Char name[25];
Int age;
struct student
{
Char gender;
Int age;
}
main()
{
Struct student john,Mary;
John.age=15;
John.gender=“m”;
Mary.age=16;
Mary.gender=“f”;
}
age
gender
Student
Pointers
• Pointers simply points to locations in
memory
• Each variables will be having a address in
the memory. So pointer is just another
variable which simply stores the address
of it
Main()
{
int a,b, *p,*q;
1002
1001
25
99
1004
1003
1002
1001
q
p
b
a
Main()
{
int a,*p;
a=15;
P=&a;
Printf(“%d,%d ”,&a,p); // Will print 1001,1001
Printf (“%d, %d”,a,*p); // Will print 15,15
}
1001
15
1002
1001
p
a
Character array as pointer
Character array is a pointer to the first location of
a group of locations
Eg : char a[10];
Here a stores memory address of a[0]
main()
{
char a[10]="baabtra";
printf("%d %d",&a[0],a); // will print the
same address
A
R
T
B
A
A
B
A[6]
a[5]
a[4]
a[3]
a[2]
a[1]
a[0]
1006
1005
1004
1003
1002
1001
1000
Questions?
“A good question deserve a good
grade…”
Self Check !!
Self - Check
#include<stdio.h>
Main()
{
Int a,b,c;
Printf(“Sum = %d”,sum(a,b));
}
Sum(int a,int b)
{
Return a+b;
}
Self - Check
#include<stdio.h>
Int sum(int a,int b)
Main()
{
Int a,b,c;
Printf(“Sum = %d”,sum(a,b));
}
Int sum(int a,int b)
{
Return a+b;
}
Self- Check
int main()
{
int a[5] = {5, 1, 15, 20, 25};
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf("%d, %d, %d", i, j, m);
return 0;
}
A.2, 1,
15
B.1, 2, 5
C.3, 2,
15
D.2, 3,
Self- Check
int main()
{
int a[5] = {5, 1, 15, 20, 25};
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf("%d, %d, %d", i, j, m);
return 0;
}
A.2, 1, 1
5
B.1, 2, 5
C.3, 2, 1
5
D.2, 3, 2
Self- Check
• A pointer to a block of memory is
effectively same as an array
A.True
B.False
Self- Check
• A pointer to a block of memory is
effectively same as an array
A.True
B.False
Self- Check
Which of the following are themselves
a collection of different data types?
a) string
b) structures
c) char
d) All of the mentioned
Self- Check
Which of the following are themselves
a collection of different data types?
a) string
b) structures
c) char
d) All of the mentioned
Self- Check
Which of the following cannot
be a structure member?
a) Another structure
b) Function
c) Array
d) None of the mentioned
Self- Check
Which of the following cannot
be a structure member?
a) Another structure
b) Function
c) Array
d) None of the mentioned
Self- Check
#include <stdio.h>
void main()
{
char *s= "hello";
char *p = s;
printf("%ct%c", p[0], s[1]);
}
a) Run time err
b) h h
c) h e
d) h l
Self- Check
#include <stdio.h>
void main()
{
char *s= "hello";
char *p = s;
printf("%ct%c", p[0], s[1]);
}
a) Run time err
b) h h
c) h e
d) h l
Self- Check
#include<stdio.h>
struct course
{
int courseno;
char coursename[25];
};
int main()
{
struct course c[] = { {102, "Java"},
{103, "PHP"},
{104, "DotNet"} };
printf("%d ", c[1].courseno);
printf("%sn", (*(c+2)).coursename);
return 0;
}
A.103 DotNet
B.102 Java
C.103 PHP
D.104 DotNet
Self- Check
#include<stdio.h>
struct course
{
int courseno;
char coursename[25];
};
int main()
{
struct course c[] = { {102, "Java"},
{103, "PHP"},
{104, "DotNet"} };
printf("%d ", c[1].courseno);
printf("%sn", (*(c+2)).coursename);
return 0;
}
A.104 DotNet
B.102 Java
C.103 PHP
D.103 DotNet
Self- Check
#include <stdio.h>
void foo( int[] );
int main()
{
int ary[4] = {1, 2, 3, 4};
foo(ary);
printf("%d ", ary[0]);
}
void foo(int p[4])
{
int i = 10;
p = &i;
printf("%d ", p[0]);
}
a) 10 10
b) Compile time
c) 10 1
d) Undefined be
Self- Check
#include <stdio.h>
void foo( int[] );
int main()
{
int ary[4] = {1, 2, 3, 4};
foo(ary);
printf("%d ", ary[0]);
}
void foo(int p[4])
{
Int i=10;
p = &i;
printf("%d ", p[0]);
}
a) 10 10
b) Compile time
c) 10 1
d) Undefined be
End of Day 2

Más contenido relacionado

La actualidad más candente

Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manualSANTOSH RATH
 
C programming pointer
C  programming pointerC  programming pointer
C programming pointerargusacademy
 
Function recap
Function recapFunction recap
Function recapalish sha
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manualnikshaikh786
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shortingargusacademy
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rathSANTOSH RATH
 
C tech questions
C tech questionsC tech questions
C tech questionsvijay00791
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Saket Pathak
 
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)Make Mannan
 
C++ Question on References and Function Overloading
C++ Question on References and Function OverloadingC++ Question on References and Function Overloading
C++ Question on References and Function Overloadingmohamed sikander
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manualSyed Mustafa
 
CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY
CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORYCS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY
CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORYRadha Maruthiyan
 
Recursion concepts by Divya
Recursion concepts by DivyaRecursion concepts by Divya
Recursion concepts by DivyaDivya Kumari
 

La actualidad más candente (20)

Data struture lab
Data struture labData struture lab
Data struture lab
 
L7 pointers
L7 pointersL7 pointers
L7 pointers
 
6. function
6. function6. function
6. function
 
Arrays
ArraysArrays
Arrays
 
Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manual
 
C programming pointer
C  programming pointerC  programming pointer
C programming pointer
 
Function recap
Function recapFunction recap
Function recap
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shorting
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rath
 
C Prog - Array
C Prog - ArrayC Prog - Array
C Prog - Array
 
C tech questions
C tech questionsC tech questions
C tech questions
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
 
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
 
C++ Question on References and Function Overloading
C++ Question on References and Function OverloadingC++ Question on References and Function Overloading
C++ Question on References and Function Overloading
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manual
 
C++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLESC++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLES
 
CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY
CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORYCS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY
CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY
 
Recursion concepts by Divya
Recursion concepts by DivyaRecursion concepts by Divya
Recursion concepts by Divya
 

Similar a Introduction to c part 2

An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of cTushar B Kute
 
Introduction to programming c and data structures
Introduction to programming c and data structuresIntroduction to programming c and data structures
Introduction to programming c and data structuresPradipta Mishra
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 FocJAYA
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Languagemadan reddy
 
Introduction to programming c and data-structures
Introduction to programming c and data-structures Introduction to programming c and data-structures
Introduction to programming c and data-structures Pradipta Mishra
 
LDCQ paper Dec21 with answer key_62cb2996afc60f6aedeb248c1d9283e5.pdf
LDCQ paper Dec21 with answer key_62cb2996afc60f6aedeb248c1d9283e5.pdfLDCQ paper Dec21 with answer key_62cb2996afc60f6aedeb248c1d9283e5.pdf
LDCQ paper Dec21 with answer key_62cb2996afc60f6aedeb248c1d9283e5.pdfVedant Gavhane
 
Understand more about C
Understand more about CUnderstand more about C
Understand more about CYi-Hsiu Hsu
 
C- Programming Assignment 4 solution
C- Programming Assignment 4 solutionC- Programming Assignment 4 solution
C- Programming Assignment 4 solutionAnimesh Chaturvedi
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in cgkgaur1987
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notesGOKULKANNANMMECLECTC
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4MKalpanaDevi
 
Function recap
Function recapFunction recap
Function recapalish sha
 

Similar a Introduction to c part 2 (20)

An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
Introduction to programming c and data structures
Introduction to programming c and data structuresIntroduction to programming c and data structures
Introduction to programming c and data structures
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
Introduction to programming c and data-structures
Introduction to programming c and data-structures Introduction to programming c and data-structures
Introduction to programming c and data-structures
 
C language
C languageC language
C language
 
Embedded C - Day 2
Embedded C - Day 2Embedded C - Day 2
Embedded C - Day 2
 
LDCQ paper Dec21 with answer key_62cb2996afc60f6aedeb248c1d9283e5.pdf
LDCQ paper Dec21 with answer key_62cb2996afc60f6aedeb248c1d9283e5.pdfLDCQ paper Dec21 with answer key_62cb2996afc60f6aedeb248c1d9283e5.pdf
LDCQ paper Dec21 with answer key_62cb2996afc60f6aedeb248c1d9283e5.pdf
 
Fucntions & Pointers in C
Fucntions & Pointers in CFucntions & Pointers in C
Fucntions & Pointers in C
 
C- Programming Assignment 3
C- Programming Assignment 3C- Programming Assignment 3
C- Programming Assignment 3
 
Understand more about C
Understand more about CUnderstand more about C
Understand more about C
 
C- Programming Assignment 4 solution
C- Programming Assignment 4 solutionC- Programming Assignment 4 solution
C- Programming Assignment 4 solution
 
Session06 functions
Session06 functionsSession06 functions
Session06 functions
 
Functions
FunctionsFunctions
Functions
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
CHAPTER 6
CHAPTER 6CHAPTER 6
CHAPTER 6
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in c
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notes
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Function recap
Function recapFunction recap
Function recap
 

Más de baabtra.com - No. 1 supplier of quality freshers

Más de baabtra.com - No. 1 supplier of quality freshers (20)

Agile methodology and scrum development
Agile methodology and scrum developmentAgile methodology and scrum development
Agile methodology and scrum development
 
Best coding practices
Best coding practicesBest coding practices
Best coding practices
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Microsoft holo lens
 
Blue brain
Blue brainBlue brain
Blue brain
 
5g
5g5g
5g
 
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Aptitude skills baabtra
 
Gd baabtra
Gd baabtraGd baabtra
Gd baabtra
 

Último

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 

Último (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 

Introduction to c part 2

  • 1. Recall • Difference between while and do while? • Difference between == and = • Difference between break and continue? • What is the difference between i++ and ++i • What if i didn’t give break in switch? • What is the difference between logical operators and relational operators
  • 2. Introduction to C functions - Arrays – structures - pointers Week 2- day 2
  • 4. Functions • A function is a group of statements that together perform a task. return_type function_name ( parameter list ) { body of the function } Eg: main() { Printf(“hello world”);
  • 5. Functions int equation(int num1, int num2) { int result; result = num1*num1+2*num1*num2+num2*num2; return result; } Return Type Function name Arguments
  • 6. #include<stdio.h> int equation(int a, int b) { int c; c= a*a+2*a*b+b*b; return c; } Int main () { int a,b,c; Printf(“Enter the number”); Scanf(“%d %d”,&a ,&b); c = equation (a,b) ; Printf(“%d”,c); }
  • 7. #include<stdio.h> int equation(int a, int b) { int c; c= a*a+2*a*b+b*b; return c; } Int main () { int a,b,c; Printf(“Enter the number”); Scanf(“%d %d”,&a ,&b); c = equation (a,b) ; // Calling the function Printf(“%d”,c); } //Function body / function Definition
  • 8. #include<stdio.h> int equation(int a, int b) { int c; c= a*a+2*a*b+b*b; return c; } Int main () { int a,b,c; Printf(“Enter the number”); Scanf(“%d %d”,&a ,&b); c = equation (a,b) ; // Calling the function Printf(“%d”,c); } Should I use the same variable names? Are they same of what I used in main() ?
  • 9. #include<stdio.h> int equation(int a, int b) { int c; c= a*a+2*a*b+b*b; return c; } Int main () { int a,b,c; Printf(“Enter the number”); Scanf(“%d %d”,&a ,&b); c = equation (a,b) ; // Calling the function Printf(“%d”,c); } Absolutely not !!! Variable a in equation() is different from varaible a in main(). There is no connection between those two
  • 10. #include<stdio.h> int equation(int a, int b) { int c; c= a*a+2*a*b+b*b; return c; } Int main () { int a,b,c; Printf(“Enter the number”); Scanf(“%d %d”,&a ,&b); c = equation (a,b) ; // Calling the function Printf(“%d”,c); } What is this return???
  • 11. #include<stdio.h> int equation(int a, int b) { int c; c= a*a+2*a*b+b*b; return c; } Int main () { int a,b,c; Printf(“Enter the number”); Scanf(“%d %d”,&a ,&b); c = equation (a,b) ; // Calling the function Printf(“%d”,c); } Return will simply send value out of function to whomsoever it was called
  • 12. #include<stdio.h> int equation(int a, int b) { int c; c= a*a+2*a*b+b*b; return c; } Int main () { int a,b,c; Printf(“Enter the number”); Scanf(“%d %d”,&a ,&b); c = equation (a,b) ; // Calling the function Printf(“%d”,c); } Can i write the whole function body after main function??
  • 13. #include<stdio.h> int equation(int a, int b) ; Int main () { int a,b,c; Printf(“Enter the number”); Scanf(“%d %d”,&a ,&b); c = equation (a,b) ; // Calling the function Printf(“%d”,c); } int equation(int a, int b) { int c; c= a*a+2*a*b+b*b; return c; } Yes you can. But only additional thing you need to do is
  • 14. #include<stdio.h> int equation(int a, int b) ; Int main () { int a,b,c; Printf(“Enter the number”); Scanf(“%d %d”,&a ,&b); c = equation (a,b) ; // Calling the function Printf(“%d”,c); } int equation(int a, int b) { int c; c= a*a+2*a*b+b*b; return c; } You must declare it at the beginning
  • 16. Arrays • Is a collection of data of same type with common name • Eg: int a[6] A[5] A[4] A[3] A[2] A[1] A[0] Memory Each Cell will be size of int
  • 18. Strings in C • There is no string data type in C • But we can do the same with character array in C – Eg: char a[8] =“baabtra”//enables us to store string with 6 characters /0 A R T B A A B A[7] A[6] a[5] a[4] a[3] a[2] a[1] a[0]
  • 19. main() { Char a[7]=“baabtra”; //Correct Char *p=“baabtra”; //Correct a=“baabtra”; //Incorrect Scanf(“%s”,&a); //Correct Scanf(“%s”, a); //Correct;But why?? will discuss soon Printf(“%s”,a); }
  • 20. Structures • A structure is a collection of variables under a single name • These variables can be of different types, and each has a name which is used to select it from the structure. struct struct_name { //Variable declarations; }
  • 21. Why structures? • A structure is a convenient way of grouping several pieces of related information together. • Eg: suppose there are several variables called name,age,gender related to a student . So we can bring all of them under one name student using structures • Eg: struct student { Char name[25]; Int age;
  • 22. struct student { Char gender; Int age; } main() { Struct student john,Mary; John.age=15; John.gender=“m”; Mary.age=16; Mary.gender=“f”; } age gender Student
  • 23. Pointers • Pointers simply points to locations in memory • Each variables will be having a address in the memory. So pointer is just another variable which simply stores the address of it Main() { int a,b, *p,*q; 1002 1001 25 99 1004 1003 1002 1001 q p b a
  • 24. Main() { int a,*p; a=15; P=&a; Printf(“%d,%d ”,&a,p); // Will print 1001,1001 Printf (“%d, %d”,a,*p); // Will print 15,15 } 1001 15 1002 1001 p a
  • 25. Character array as pointer Character array is a pointer to the first location of a group of locations Eg : char a[10]; Here a stores memory address of a[0] main() { char a[10]="baabtra"; printf("%d %d",&a[0],a); // will print the same address A R T B A A B A[6] a[5] a[4] a[3] a[2] a[1] a[0] 1006 1005 1004 1003 1002 1001 1000
  • 26. Questions? “A good question deserve a good grade…”
  • 28. Self - Check #include<stdio.h> Main() { Int a,b,c; Printf(“Sum = %d”,sum(a,b)); } Sum(int a,int b) { Return a+b; }
  • 29. Self - Check #include<stdio.h> Int sum(int a,int b) Main() { Int a,b,c; Printf(“Sum = %d”,sum(a,b)); } Int sum(int a,int b) { Return a+b; }
  • 30. Self- Check int main() { int a[5] = {5, 1, 15, 20, 25}; int i, j, m; i = ++a[1]; j = a[1]++; m = a[i++]; printf("%d, %d, %d", i, j, m); return 0; } A.2, 1, 15 B.1, 2, 5 C.3, 2, 15 D.2, 3,
  • 31. Self- Check int main() { int a[5] = {5, 1, 15, 20, 25}; int i, j, m; i = ++a[1]; j = a[1]++; m = a[i++]; printf("%d, %d, %d", i, j, m); return 0; } A.2, 1, 1 5 B.1, 2, 5 C.3, 2, 1 5 D.2, 3, 2
  • 32. Self- Check • A pointer to a block of memory is effectively same as an array A.True B.False
  • 33. Self- Check • A pointer to a block of memory is effectively same as an array A.True B.False
  • 34. Self- Check Which of the following are themselves a collection of different data types? a) string b) structures c) char d) All of the mentioned
  • 35. Self- Check Which of the following are themselves a collection of different data types? a) string b) structures c) char d) All of the mentioned
  • 36. Self- Check Which of the following cannot be a structure member? a) Another structure b) Function c) Array d) None of the mentioned
  • 37. Self- Check Which of the following cannot be a structure member? a) Another structure b) Function c) Array d) None of the mentioned
  • 38. Self- Check #include <stdio.h> void main() { char *s= "hello"; char *p = s; printf("%ct%c", p[0], s[1]); } a) Run time err b) h h c) h e d) h l
  • 39. Self- Check #include <stdio.h> void main() { char *s= "hello"; char *p = s; printf("%ct%c", p[0], s[1]); } a) Run time err b) h h c) h e d) h l
  • 40. Self- Check #include<stdio.h> struct course { int courseno; char coursename[25]; }; int main() { struct course c[] = { {102, "Java"}, {103, "PHP"}, {104, "DotNet"} }; printf("%d ", c[1].courseno); printf("%sn", (*(c+2)).coursename); return 0; } A.103 DotNet B.102 Java C.103 PHP D.104 DotNet
  • 41. Self- Check #include<stdio.h> struct course { int courseno; char coursename[25]; }; int main() { struct course c[] = { {102, "Java"}, {103, "PHP"}, {104, "DotNet"} }; printf("%d ", c[1].courseno); printf("%sn", (*(c+2)).coursename); return 0; } A.104 DotNet B.102 Java C.103 PHP D.103 DotNet
  • 42. Self- Check #include <stdio.h> void foo( int[] ); int main() { int ary[4] = {1, 2, 3, 4}; foo(ary); printf("%d ", ary[0]); } void foo(int p[4]) { int i = 10; p = &i; printf("%d ", p[0]); } a) 10 10 b) Compile time c) 10 1 d) Undefined be
  • 43. Self- Check #include <stdio.h> void foo( int[] ); int main() { int ary[4] = {1, 2, 3, 4}; foo(ary); printf("%d ", ary[0]); } void foo(int p[4]) { Int i=10; p = &i; printf("%d ", p[0]); } a) 10 10 b) Compile time c) 10 1 d) Undefined be