SlideShare a Scribd company logo
Slide 1 of 44Ver. 1.0
Programming in C
In this session, you will learn to:
Implement modular approach in C programs
Use library functions for string manipulation
Work with data storage types
Objectives
Slide 2 of 44Ver. 1.0
Programming in C
Implementing Modular Approach in C Programs
Functions are the building blocks of C.
Every C program must consist of at least one function,
main().
The main() function is the entry point of a C program.
Slide 3 of 44Ver. 1.0
Programming in C
Advantages of Functions
Functions:
Allow reusability of code and structuring of programs.
Provide programmers a convenient way of designing
programs.
Slide 4 of 44Ver. 1.0
Programming in C
Parameters of Functions
A parameter:
Is the data that the function must receive when called from
another function.
May or may not be present in a function.
Of a user-defined function is declared outside the {} of that
function.
Slide 5 of 44Ver. 1.0
Programming in C
Practice: 5.1
1. From the following program, identify the functions invoked
from main(), and state which functions have parameters.
Also state the parameters.
Microsoft Office
Word 97 - 2003 Document
Slide 6 of 44Ver. 1.0
Programming in C
Practice: 5.1 (Contd.)
Solution:
1. The standard functions used in this program within main() are
as follows:
scanf() – parameters are format of input, and pointers to the
variable(s) in which the input must be stored
fflush() – parameter is stdin
The user-defined functions are:
output() – no parameters
calc() – one parameter, g, an int type data
Slide 7 of 44Ver. 1.0
Programming in C
Invoking Functions
Functions that have parameters are invoked in one of the
following ways:
Call by value: In call by value, the called function cannot refer
to the variables of the caller function directly, but creates its
own copy of the values in different variables.
Call by reference: In call by reference, the called function
should be able to refer to the variables of the caller function
directly, and does not create its own copy of the values in
different variables. It is possible only if the addresses of the
variables are passed as parameters to a function.
Slide 8 of 44Ver. 1.0
Programming in C
Passing Arrays to Functions
Arrays are inherently passed to functions through call by
reference method.
An array can be passed to a function in the following way:
Function name (array name);
Slide 9 of 44Ver. 1.0
Programming in C
Practice: 5.2
1. If the variable avar is passed to a function by a call by
reference, can the value of the variable avar be modified in
the called function? Give reasons for your answer.
2. State whether True or False:
When an array is passed to a function, the array elements
are copied into the parameter of the function.
3. Consider the program code given in the following file.
Microsoft Office
Word 97 - 2003 Document
Slide 10 of 44Ver. 1.0
Programming in C
Practice: 5.2 (Contd.)
Based on the code, answer the following questions:
a. The function (max() / min()) is invoked by a call by value.
b. The function (max() / min()) is invoked by a call by reference.
c. After the function max() is executed, where does the control go to:
i. The min() function.
ii. The first line of the main() function.
iii. The first line of the main() following the line on which max() was invoked.
d. After execution of the function min(), program execution:
i. Stops without returning to main().
ii. Goes back to the main() function.
e. If the values of i and j were to be printed after the function max() and
again after the function min(), what values would be displayed?
Slide 11 of 44Ver. 1.0
Programming in C
Practice: 5.2 (Contd.)
4. Write a program that calls a function called power(m,n),
which displays the nth power of the integer m (m and n are
parameters). The function must be invoked by a call by
reference.
Slide 12 of 44Ver. 1.0
Programming in C
Practice: 5.2 (Contd.)
Solution:
1. Yes, because the addresses of the variables are passed in by
using call by reference, the memory locations of the variables
are known to the function. Using pointers to the variables, their
values can be modified.
2. False. Values of variables are copied into the parameters only
in the case of a call by value.
3. a. max()
b. min()
c. iii
d. ii
e. After max() is executed, the values of i and j printed out
would be the same as those entered during execution. After
min() is executed, the value of i would still be the same, but
j would increase by 5 (since b is a pointer to the variable j).
Slide 13 of 44Ver. 1.0
Programming in C
4.main() {
int x, y;
printf(“Enter Number: ”);
scanf(“%d”, &x);
fflush(stdin);
printf(“Enter power raise to : “);
scanf(“%d”, &y);
fflush(stdin);
power(&x, &y); }
power(m,n)
int *m, *n; /* power is pointed to by n,
value is pointed to by m */
{ int i=1,val=1;
while(i++<= *n)
val = val ** m;
printf(“%d the power of %d is %dn”, *n,*m,
val);}
Practice: 5.2 (Contd.)
Slide 14 of 44Ver. 1.0
Programming in C
Returning Values from a Function
A function can return a value to the caller function.
The return statement is used to send back a value to the
caller function.
The return statement also transfers control back to calling
function.
The default return value is int type.
The return statement can return only one value.
The syntax for the return statement is:
return[expression]
A function can also return an array. This could be done by:
return [array name]
Slide 15 of 44Ver. 1.0
Programming in C
Practice: 5.3
1. Point out the error(s), if any, in the functions given in the
following file:
2. The following program should calculate the square of any
float value, using a function called square(). The float value
is an input to the program. The program is incomplete. Put
in the appropriate statements in the program given in the
following file:
Microsoft Word
Document
Microsoft Word
Document
Slide 16 of 44Ver. 1.0
Programming in C
Practice: 5.3 (Contd.)
3. The function, makeint(), was coded to convert any
number entered into a char array to integer type. The
function takes the string as parameter and returns the
value, as given in the following file:
Microsoft Word
Document
Slide 17 of 44Ver. 1.0
Programming in C
Practice: 5.3 (Contd.)
Solution:
Microsoft Word
Document
Slide 18 of 44Ver. 1.0
Programming in C
Command-Line Arguments
Command-line arguments:
Are the parameters that the main() function can receive from
the command line.
Are passed as information from the OS prompt to a program.
The main() function has 2 arguments, argc and argv.
The format of the main() function with parameters is as
follows:
main(argc, argv)
int argc;
char *argv[];
{
:
}
Here, argc is integer and argv is a character array of
unlimited size (hence [ ] in the declaration).
Slide 19 of 44Ver. 1.0
Programming in C
Practice: 5.4
1. Given that a C program called temp is executed by the
following command:
temp start 6
match the following:
a. value of argc 1. points to array "6"
b. argv [0] 2. points to arrm/ "start"
c. argv [1] 3. 3
d. argv[2] 4. points to array "temp"
2. Modify the program upper so that it first checks the number
of arguments entered on the command line. The program
should display an error message if no arguments have been
entered and also allow conversion of as many strings to
upper-case as have been specified as arguments on the
command line.
Slide 20 of 44Ver. 1.0
Programming in C
Practice: 5.4 (Contd.)
3. Consider the following program to calculate the sum of 2
integers specified on the command line:
main (argc, argv)
int argc;
char *argv [ ];{
sum (argv [1], argv [2]);
}
sum (num1, num2)
int numl, num2;{
return numl + num2;
}
The program has some logical errors. Point out the errors and
correct the code.
Slide 21 of 44Ver. 1.0
Programming in C
Practice: 5.4 (Contd.)
Solution:
Microsoft Word
Document
Slide 22 of 44Ver. 1.0
Programming in C
Using Library Functions for String Manipulation
Library functions:
Are also known as built-in functions.
Can be used by including the concerned header files.
Slide 23 of 44Ver. 1.0
Programming in C
Standard String-Handling Functions
Some of the standard string-handling functions are:
strcmp(): Compares 2 strings (its parameters) character by
character (ASCII comparison).
strcpy(): Copies the second string to the first string named
in the strcpy() parameters.
strcat(): Appends the second string passed at the end of
the first string passed to it .
strlen(): Returns the number of characters in the string
passed to it.
Slide 24 of 44Ver. 1.0
Programming in C
Practice: 5.5
1. What will the following function call return?
x = strcmp(“Cada”, “CADA”);
What should the declaration of x be?
2. Assume that array contains the string 846*.
What will array contain when the following statement is executed?
strcat(array,”>”);
3. State whether True or False:
The following statement returns a value of 4 to x.
x = strlen ("abc");
Slide 25 of 44Ver. 1.0
Programming in C
Practice: 5.5 (Contd.)
Solution:
1. Value returned - 32
Declaration - int x;
2. 846*>
3. False
Slide 26 of 44Ver. 1.0
Programming in C
String to Numeric Conversion Functions
Conversion functions:
Are available as a part of the standard library.
Are used to convert one data type into another.
The following functions are used to convert a string to a
numeric value:
atoi(): Returns the int type value of a string passed to it
and the value 0 in the case the string does not begin with a
digit.
atof(): Returns the double type value of a string passed to it
and the value 0 in the case the string does not begin with a
digit or a decimal point.
Slide 27 of 44Ver. 1.0
Programming in C
Practice: 5.6
1. What value will the variable val contain in each of the
following situations?
a. val = atoi ("A345"); /* val is int type */
b. val = atof ("345A"); /* val is double type */
Slide 28 of 44Ver. 1.0
Programming in C
Practice: 5.6 (Contd.)
Solution:
1. a. 0
b. 345.000000
Slide 29 of 44Ver. 1.0
Programming in C
Functions for Formatting Data in Memory
The formatting functions are available as a part of the
standard library.
The following functions are used to format data in memory:
sprintf():
Writes to a variable in the memory and stores the data in different
variables specified.
Are used for transferring data between variables in a specific
format.
Has the following syntax:
sprintf(string, format-specification, data, ….);
sscanf():
Performs formatted input from a string.
Has the following syntax:
sscanf(string, format-specification, data, ….);
Slide 30 of 44Ver. 1.0
Programming in C
Practice: 5.7
1. What data is assigned to the variable string by each of the
following?
a. sprintf(string,"%04d%3.2f%2s",21,4.576, "Hi“);
b. sprintf (string, "%10s", "ABC");
c. sscanf ("0987APZ", "%4d%s", &num, string);
1. What is the error, if any, in the instructions given below
against each purpose? Give the correct instruction in case
of an error.
Purpose Instruction
Accept a name from keyboard printf(“%s”, name);
Format the contents of variables
i_num(int) and f_num(float), and store
them into a character array called string.
printf (string,"%d%f, i_num,f_num)
Slide 31 of 44Ver. 1.0
Programming in C
Practice: 5.7 (Contd.)
Solution:
Microsoft Word
Document
Slide 32 of 44Ver. 1.0
Programming in C
Working with Data Storage Types
C language provides the following data storage types:
auto: Variables of this type retain their value only as long as
the function is in the stage of execution.
static: Variables of this type retain its value even after the
function to which it belongs has been executed.
extern: Variables of this type are declared at the start of the
program and can be accessed from any function.
Slide 33 of 44Ver. 1.0
Programming in C
Practice: 5.8
1. Given the following declarations:
float area;
static float val;
auto char number;
State which variable(s) will be:
a. Created each tune the function is invoked.
b. Created only once.
2. A numeric array has to store 4 values - 2.5, 6,3, 7.0 and 8.0.
This array is to be declared and used in a function called
compute(). Which of the following is/are correct
declarations of this array?
a. static int values[4] = {2.5,6.3,7.0,8.0};
b. auto float values[4] = {2.5,6.3,7.0,8.0 };
c. float values [4]= {2.5,6.3,7.0,8.0};
d. static float values [4] = {2.5,6.3,7.0,8.0};
Slide 34 of 44Ver. 1.0
Programming in C
Practice: 5.8 (Contd.)
Solution:
1. a. area, number
b. val
2. (a) Is invalid because the array should be float or double type.
(b) Is invalid because it is declared as auto type.
(c) Is invalid because it is declared as auto type.
(d) Is correct.
.
Slide 35 of 44Ver. 1.0
Programming in C
Practice: 5.9
1. If the variable val is declared as global in the program B,
just illustrated, how would program A be modified? Give the
appropriate declarations required in both programs.
2. Consider the following 2 program files:
Program A
float x;
calc() {
int i;
: } printout()
{ static char z;
: }
Program B
char numarray[5];
main() {
char c ;
: }
Slide 36 of 44Ver. 1.0
Programming in C
Practice: 5.9 (Contd.)
Based on this code, answer the following:
a. The variable z can be accessed in the function(s)
____________________.
b. The variable(s) that can be accessed from functions of both program
files is/are ___________.
c. Slate whether True or False:
The variable i can be used in the function printout().
d. Memory for variable z is reserved each time the function printout()
is invoked. State whether true or false.
e. If the function printout() has to access the variable x, does x have
to be declared within the function printout()?
If so, give the declaration.
f. The auto variable(s) in these programs is/are _________________.
Slide 37 of 44Ver. 1.0
Programming in C
Practice: 5.9 (Contd.)
Solution:
1. In program B, val would be declared as follows:
int val;
calc(){
:}
In program A, the declaration would be as follows:
main()
{ extern int val;
:}
2. a. printout() only (Since it is declared within the function
printout() and hence is not global)
x and numarray (if proper extern statements are coded).
b. False (Since it is declared within the function calc(), and
hence it is not global)
Slide 38 of 44Ver. 1.0
Programming in C
Practice: 5.9 (Contd.)
c. False (Since z is a static variable, it is created only
once – the function printout() is invoked.)
d. No (Since x is declared as global in program A, and
printout() is defined in the same program file. However,
declaring it as extern while within printout() is wrong.)
e. The variable i defined in calc() and the variable c defined
in main().
Slide 39 of 44Ver. 1.0
Programming in C
Practice: 5.10
1. The following file contains a C program called remdigit.c
and a list of errors in the program indicated by the compiler.
Go through the error list and correct the program. Since the
C compiler does not always give very meaningful error
messages, go through the program given in the following
file carefully.
Microsoft Office
Word 97 - 2003 Document
Slide 40 of 44Ver. 1.0
Programming in C
Practice: 5.10 (Contd.)
2. Write a program to display all the positions at which a
character occurs in a string. Both the character to be
located and the string to be searched should be passed to a
function called nextpos (findchar, searchstr).
Each time the function locates the diameter, it should pass
back the position.
After searching the entire string, the function should return
the value -1.
Slide 41 of 44Ver. 1.0
Programming in C
Practice: 5.10 (Contd.)
Solution:
Work out your answers. A discussion on these follows in the
Classroom.
Slide 42 of 44Ver. 1.0
Programming in C
Summary
In this session, you learned that:
Functions provide advantages of reusability and structuring of
programs.
A parameter of a function is the data that the function must
receive when called or invoked from another function.
Functions that have parameters are invoked in one of the
following two ways:
Call by value
Call by reference
Call by value means that the called function creates its own
copy of the values in different variables.
Call by reference means that the called function should be able
to refer to the variables of the caller function directly, and does
not create its own copy of the values in different variables.
Slide 43 of 44Ver. 1.0
Programming in C
Summary (Contd.)
Arrays are passed to functions by the call by reference
method.
Functions can return values by using the return statement.
The main() function can have parameters, argc and argv.
argc is integer type while argv is a string.
The information that is passed to a program from the OS
prompt is known as command-line arguments.
Some of the standard string-handling functions are:
strcmp(): Compares two strings.
strcpy(): Copies the second string to the first string.
strcat(): Appends the second string passed at the end of the
first string passed as parameters.
strlen(): Returns the number of characters in the string
passed as a parameter.
Slide 44 of 44Ver. 1.0
Programming in C
Summary (Contd.)
atoi(): Returns the int type value of a string passed to it.
aof(): Returns the double type value of a string passed to it.
The following functions are used to format data in memory:
sprintf()
sscanf()
C language provides the following data storage types:
auto: Variables of this type retain their value only as long as the
function is in the stage of execution.
static: Variables of this type retain its value even after the
function to which it belongs has been executed.
extern: Variables of this type are declared at the start of the
program and can be accessed from any function.

More Related Content

What's hot

Control Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, StructuresControl Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, Structures
indra Kishor
 
Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14IIUM
 
Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14
IIUM
 
02. functions & introduction to class
02. functions & introduction to class02. functions & introduction to class
02. functions & introduction to class
Haresh Jaiswal
 
User_Defined_Functions_ppt_slideshare.
User_Defined_Functions_ppt_slideshare.User_Defined_Functions_ppt_slideshare.
User_Defined_Functions_ppt_slideshare.
NabeelaNousheen
 
Aspect-Oriented Technologies
Aspect-Oriented TechnologiesAspect-Oriented Technologies
Aspect-Oriented Technologies
Esteban Abait
 
Important C program of Balagurusamy Book
Important C program of Balagurusamy BookImportant C program of Balagurusamy Book
Important C program of Balagurusamy Book
Abir Hossain
 
UNIT-II CP DOC.docx
UNIT-II CP DOC.docxUNIT-II CP DOC.docx
UNIT-II CP DOC.docx
JavvajiVenkat
 
CP Handout#5
CP Handout#5CP Handout#5
CP Handout#5
trupti1976
 
Ocs752 unit 4
Ocs752   unit 4Ocs752   unit 4
Ocs752 unit 4
mgrameshmail
 
Python programming workshop session 4
Python programming workshop session 4Python programming workshop session 4
Python programming workshop session 4
Abdul Haseeb
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
Nilesh Dalvi
 
c++ lab manual
c++ lab manualc++ lab manual
c++ lab manual
Shrunkhala Wankhede
 
Develop Embedded Software Module-Session 3
Develop Embedded Software Module-Session 3Develop Embedded Software Module-Session 3
Develop Embedded Software Module-Session 3
Naveen Kumar
 
C Programming
C ProgrammingC Programming
C Programming
Rumman Ansari
 
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
VTU 1ST SEM  PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...VTU 1ST SEM  PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
vtunotesbysree
 
01. introduction to C++
01. introduction to C++01. introduction to C++
01. introduction to C++
Haresh Jaiswal
 

What's hot (20)

Control Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, StructuresControl Statements, Array, Pointer, Structures
Control Statements, Array, Pointer, Structures
 
Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14
 
Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14
 
02. functions & introduction to class
02. functions & introduction to class02. functions & introduction to class
02. functions & introduction to class
 
User_Defined_Functions_ppt_slideshare.
User_Defined_Functions_ppt_slideshare.User_Defined_Functions_ppt_slideshare.
User_Defined_Functions_ppt_slideshare.
 
Apclass (2)
Apclass (2)Apclass (2)
Apclass (2)
 
Aspect-Oriented Technologies
Aspect-Oriented TechnologiesAspect-Oriented Technologies
Aspect-Oriented Technologies
 
Important C program of Balagurusamy Book
Important C program of Balagurusamy BookImportant C program of Balagurusamy Book
Important C program of Balagurusamy Book
 
Intro
IntroIntro
Intro
 
Intro to c++
Intro to c++Intro to c++
Intro to c++
 
UNIT-II CP DOC.docx
UNIT-II CP DOC.docxUNIT-II CP DOC.docx
UNIT-II CP DOC.docx
 
CP Handout#5
CP Handout#5CP Handout#5
CP Handout#5
 
Ocs752 unit 4
Ocs752   unit 4Ocs752   unit 4
Ocs752 unit 4
 
Python programming workshop session 4
Python programming workshop session 4Python programming workshop session 4
Python programming workshop session 4
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
 
c++ lab manual
c++ lab manualc++ lab manual
c++ lab manual
 
Develop Embedded Software Module-Session 3
Develop Embedded Software Module-Session 3Develop Embedded Software Module-Session 3
Develop Embedded Software Module-Session 3
 
C Programming
C ProgrammingC Programming
C Programming
 
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
VTU 1ST SEM  PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...VTU 1ST SEM  PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
 
01. introduction to C++
01. introduction to C++01. introduction to C++
01. introduction to C++
 

Similar to C programming session 08

C programming session 08
C programming session 08C programming session 08
C programming session 08AjayBahoriya
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman
 
C programming session 05
C programming session 05C programming session 05
C programming session 05Vivek Singh
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
imtiazalijoono
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
JAVVAJI VENKATA RAO
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
Mohammed Saleh
 
Computer science sqp
Computer science sqpComputer science sqp
Computer science sqp
B Bhuvanesh
 
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++
Muhammad Hammad Waseem
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
Mehul Desai
 
21: Which method determines if a JRadioButton is selected?
21: Which method determines if a JRadioButton is selected?21: Which method determines if a JRadioButton is selected?
21: Which method determines if a JRadioButton is selected?sukeshsuresh189
 
Comp 328 final guide (devry)
Comp 328 final guide (devry)Comp 328 final guide (devry)
Comp 328 final guide (devry)sukeshsuresh189
 
6: Which of the following statements about creating arrays and initializing t...
6: Which of the following statements about creating arrays and initializing t...6: Which of the following statements about creating arrays and initializing t...
6: Which of the following statements about creating arrays and initializing t...sukeshsuresh189
 
Comp 328 final guide (devry)
Comp 328 final guide (devry)Comp 328 final guide (devry)
Comp 328 final guide (devry)sukeshsuresh189
 
10: In the Java graphics system, coordinate units are measured in ________.
10: In the Java graphics system, coordinate units are measured in ________.10: In the Java graphics system, coordinate units are measured in ________.
10: In the Java graphics system, coordinate units are measured in ________.sukeshsuresh189
 
1: The .class extension on a file means that the file
1:  The .class extension on a file means that the file1:  The .class extension on a file means that the file
1: The .class extension on a file means that the filesukeshsuresh189
 
4: Which of the following is a Scanner method?
4: Which of the following is a Scanner method?4: Which of the following is a Scanner method?
4: Which of the following is a Scanner method?sukeshsuresh189
 
202: When the user clicks a JCheckBox, a(n) occurs.
202: When the user clicks a JCheckBox, a(n) occurs.202: When the user clicks a JCheckBox, a(n) occurs.
202: When the user clicks a JCheckBox, a(n) occurs.sukeshsuresh189
 
3: A(n) ________ enables a program to read data from the user.
3: A(n) ________ enables a program to read data from the user.3: A(n) ________ enables a program to read data from the user.
3: A(n) ________ enables a program to read data from the user.sukeshsuresh189
 
22: The logical relationship between radio buttons is maintained by objects o...
22: The logical relationship between radio buttons is maintained by objects o...22: The logical relationship between radio buttons is maintained by objects o...
22: The logical relationship between radio buttons is maintained by objects o...sukeshsuresh189
 
19: When the user presses Enter in a JTextField, the GUI component generates ...
19: When the user presses Enter in a JTextField, the GUI component generates ...19: When the user presses Enter in a JTextField, the GUI component generates ...
19: When the user presses Enter in a JTextField, the GUI component generates ...sukeshsuresh189
 

Similar to C programming session 08 (20)

C programming session 08
C programming session 08C programming session 08
C programming session 08
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
C programming session 05
C programming session 05C programming session 05
C programming session 05
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Computer science sqp
Computer science sqpComputer science sqp
Computer science sqp
 
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
21: Which method determines if a JRadioButton is selected?
21: Which method determines if a JRadioButton is selected?21: Which method determines if a JRadioButton is selected?
21: Which method determines if a JRadioButton is selected?
 
Comp 328 final guide (devry)
Comp 328 final guide (devry)Comp 328 final guide (devry)
Comp 328 final guide (devry)
 
6: Which of the following statements about creating arrays and initializing t...
6: Which of the following statements about creating arrays and initializing t...6: Which of the following statements about creating arrays and initializing t...
6: Which of the following statements about creating arrays and initializing t...
 
Comp 328 final guide (devry)
Comp 328 final guide (devry)Comp 328 final guide (devry)
Comp 328 final guide (devry)
 
10: In the Java graphics system, coordinate units are measured in ________.
10: In the Java graphics system, coordinate units are measured in ________.10: In the Java graphics system, coordinate units are measured in ________.
10: In the Java graphics system, coordinate units are measured in ________.
 
1: The .class extension on a file means that the file
1:  The .class extension on a file means that the file1:  The .class extension on a file means that the file
1: The .class extension on a file means that the file
 
4: Which of the following is a Scanner method?
4: Which of the following is a Scanner method?4: Which of the following is a Scanner method?
4: Which of the following is a Scanner method?
 
202: When the user clicks a JCheckBox, a(n) occurs.
202: When the user clicks a JCheckBox, a(n) occurs.202: When the user clicks a JCheckBox, a(n) occurs.
202: When the user clicks a JCheckBox, a(n) occurs.
 
3: A(n) ________ enables a program to read data from the user.
3: A(n) ________ enables a program to read data from the user.3: A(n) ________ enables a program to read data from the user.
3: A(n) ________ enables a program to read data from the user.
 
22: The logical relationship between radio buttons is maintained by objects o...
22: The logical relationship between radio buttons is maintained by objects o...22: The logical relationship between radio buttons is maintained by objects o...
22: The logical relationship between radio buttons is maintained by objects o...
 
19: When the user presses Enter in a JTextField, the GUI component generates ...
19: When the user presses Enter in a JTextField, the GUI component generates ...19: When the user presses Enter in a JTextField, the GUI component generates ...
19: When the user presses Enter in a JTextField, the GUI component generates ...
 

More from Vivek Singh

C programming session 14
C programming session 14C programming session 14
C programming session 14Vivek Singh
 
C programming session 13
C programming session 13C programming session 13
C programming session 13Vivek Singh
 
C programming session 11
C programming session 11C programming session 11
C programming session 11Vivek Singh
 
C programming session 10
C programming session 10C programming session 10
C programming session 10Vivek Singh
 
C programming session 07
C programming session 07C programming session 07
C programming session 07Vivek Singh
 
C programming session 04
C programming session 04C programming session 04
C programming session 04Vivek Singh
 
C programming session 01
C programming session 01C programming session 01
C programming session 01Vivek Singh
 
C programming session 16
C programming session 16C programming session 16
C programming session 16Vivek Singh
 
Niit aptitude question paper
Niit aptitude question paperNiit aptitude question paper
Niit aptitude question paperVivek Singh
 
Excel shortcut and tips
Excel shortcut and tipsExcel shortcut and tips
Excel shortcut and tipsVivek Singh
 
Sql where clause
Sql where clauseSql where clause
Sql where clauseVivek Singh
 
Sql update statement
Sql update statementSql update statement
Sql update statementVivek Singh
 
Sql tutorial, tutorials sql
Sql tutorial, tutorials sqlSql tutorial, tutorials sql
Sql tutorial, tutorials sqlVivek Singh
 
Sql select statement
Sql select statementSql select statement
Sql select statementVivek Singh
 
Sql query tuning or query optimization
Sql query tuning or query optimizationSql query tuning or query optimization
Sql query tuning or query optimizationVivek Singh
 
Sql query tips or query optimization
Sql query tips or query optimizationSql query tips or query optimization
Sql query tips or query optimizationVivek Singh
 
Sql order by clause
Sql order by clauseSql order by clause
Sql order by clauseVivek Singh
 
Sql operators comparision & logical operators
Sql operators   comparision & logical operatorsSql operators   comparision & logical operators
Sql operators comparision & logical operatorsVivek Singh
 

More from Vivek Singh (20)

C programming session 14
C programming session 14C programming session 14
C programming session 14
 
C programming session 13
C programming session 13C programming session 13
C programming session 13
 
C programming session 11
C programming session 11C programming session 11
C programming session 11
 
C programming session 10
C programming session 10C programming session 10
C programming session 10
 
C programming session 07
C programming session 07C programming session 07
C programming session 07
 
C programming session 04
C programming session 04C programming session 04
C programming session 04
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
C programming session 16
C programming session 16C programming session 16
C programming session 16
 
Niit aptitude question paper
Niit aptitude question paperNiit aptitude question paper
Niit aptitude question paper
 
Excel shortcut and tips
Excel shortcut and tipsExcel shortcut and tips
Excel shortcut and tips
 
Sql where clause
Sql where clauseSql where clause
Sql where clause
 
Sql update statement
Sql update statementSql update statement
Sql update statement
 
Sql tutorial, tutorials sql
Sql tutorial, tutorials sqlSql tutorial, tutorials sql
Sql tutorial, tutorials sql
 
Sql subquery
Sql subquerySql subquery
Sql subquery
 
Sql select statement
Sql select statementSql select statement
Sql select statement
 
Sql rename
Sql renameSql rename
Sql rename
 
Sql query tuning or query optimization
Sql query tuning or query optimizationSql query tuning or query optimization
Sql query tuning or query optimization
 
Sql query tips or query optimization
Sql query tips or query optimizationSql query tips or query optimization
Sql query tips or query optimization
 
Sql order by clause
Sql order by clauseSql order by clause
Sql order by clause
 
Sql operators comparision & logical operators
Sql operators   comparision & logical operatorsSql operators   comparision & logical operators
Sql operators comparision & logical operators
 

Recently uploaded

GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 

Recently uploaded (20)

GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 

C programming session 08

  • 1. Slide 1 of 44Ver. 1.0 Programming in C In this session, you will learn to: Implement modular approach in C programs Use library functions for string manipulation Work with data storage types Objectives
  • 2. Slide 2 of 44Ver. 1.0 Programming in C Implementing Modular Approach in C Programs Functions are the building blocks of C. Every C program must consist of at least one function, main(). The main() function is the entry point of a C program.
  • 3. Slide 3 of 44Ver. 1.0 Programming in C Advantages of Functions Functions: Allow reusability of code and structuring of programs. Provide programmers a convenient way of designing programs.
  • 4. Slide 4 of 44Ver. 1.0 Programming in C Parameters of Functions A parameter: Is the data that the function must receive when called from another function. May or may not be present in a function. Of a user-defined function is declared outside the {} of that function.
  • 5. Slide 5 of 44Ver. 1.0 Programming in C Practice: 5.1 1. From the following program, identify the functions invoked from main(), and state which functions have parameters. Also state the parameters. Microsoft Office Word 97 - 2003 Document
  • 6. Slide 6 of 44Ver. 1.0 Programming in C Practice: 5.1 (Contd.) Solution: 1. The standard functions used in this program within main() are as follows: scanf() – parameters are format of input, and pointers to the variable(s) in which the input must be stored fflush() – parameter is stdin The user-defined functions are: output() – no parameters calc() – one parameter, g, an int type data
  • 7. Slide 7 of 44Ver. 1.0 Programming in C Invoking Functions Functions that have parameters are invoked in one of the following ways: Call by value: In call by value, the called function cannot refer to the variables of the caller function directly, but creates its own copy of the values in different variables. Call by reference: In call by reference, the called function should be able to refer to the variables of the caller function directly, and does not create its own copy of the values in different variables. It is possible only if the addresses of the variables are passed as parameters to a function.
  • 8. Slide 8 of 44Ver. 1.0 Programming in C Passing Arrays to Functions Arrays are inherently passed to functions through call by reference method. An array can be passed to a function in the following way: Function name (array name);
  • 9. Slide 9 of 44Ver. 1.0 Programming in C Practice: 5.2 1. If the variable avar is passed to a function by a call by reference, can the value of the variable avar be modified in the called function? Give reasons for your answer. 2. State whether True or False: When an array is passed to a function, the array elements are copied into the parameter of the function. 3. Consider the program code given in the following file. Microsoft Office Word 97 - 2003 Document
  • 10. Slide 10 of 44Ver. 1.0 Programming in C Practice: 5.2 (Contd.) Based on the code, answer the following questions: a. The function (max() / min()) is invoked by a call by value. b. The function (max() / min()) is invoked by a call by reference. c. After the function max() is executed, where does the control go to: i. The min() function. ii. The first line of the main() function. iii. The first line of the main() following the line on which max() was invoked. d. After execution of the function min(), program execution: i. Stops without returning to main(). ii. Goes back to the main() function. e. If the values of i and j were to be printed after the function max() and again after the function min(), what values would be displayed?
  • 11. Slide 11 of 44Ver. 1.0 Programming in C Practice: 5.2 (Contd.) 4. Write a program that calls a function called power(m,n), which displays the nth power of the integer m (m and n are parameters). The function must be invoked by a call by reference.
  • 12. Slide 12 of 44Ver. 1.0 Programming in C Practice: 5.2 (Contd.) Solution: 1. Yes, because the addresses of the variables are passed in by using call by reference, the memory locations of the variables are known to the function. Using pointers to the variables, their values can be modified. 2. False. Values of variables are copied into the parameters only in the case of a call by value. 3. a. max() b. min() c. iii d. ii e. After max() is executed, the values of i and j printed out would be the same as those entered during execution. After min() is executed, the value of i would still be the same, but j would increase by 5 (since b is a pointer to the variable j).
  • 13. Slide 13 of 44Ver. 1.0 Programming in C 4.main() { int x, y; printf(“Enter Number: ”); scanf(“%d”, &x); fflush(stdin); printf(“Enter power raise to : “); scanf(“%d”, &y); fflush(stdin); power(&x, &y); } power(m,n) int *m, *n; /* power is pointed to by n, value is pointed to by m */ { int i=1,val=1; while(i++<= *n) val = val ** m; printf(“%d the power of %d is %dn”, *n,*m, val);} Practice: 5.2 (Contd.)
  • 14. Slide 14 of 44Ver. 1.0 Programming in C Returning Values from a Function A function can return a value to the caller function. The return statement is used to send back a value to the caller function. The return statement also transfers control back to calling function. The default return value is int type. The return statement can return only one value. The syntax for the return statement is: return[expression] A function can also return an array. This could be done by: return [array name]
  • 15. Slide 15 of 44Ver. 1.0 Programming in C Practice: 5.3 1. Point out the error(s), if any, in the functions given in the following file: 2. The following program should calculate the square of any float value, using a function called square(). The float value is an input to the program. The program is incomplete. Put in the appropriate statements in the program given in the following file: Microsoft Word Document Microsoft Word Document
  • 16. Slide 16 of 44Ver. 1.0 Programming in C Practice: 5.3 (Contd.) 3. The function, makeint(), was coded to convert any number entered into a char array to integer type. The function takes the string as parameter and returns the value, as given in the following file: Microsoft Word Document
  • 17. Slide 17 of 44Ver. 1.0 Programming in C Practice: 5.3 (Contd.) Solution: Microsoft Word Document
  • 18. Slide 18 of 44Ver. 1.0 Programming in C Command-Line Arguments Command-line arguments: Are the parameters that the main() function can receive from the command line. Are passed as information from the OS prompt to a program. The main() function has 2 arguments, argc and argv. The format of the main() function with parameters is as follows: main(argc, argv) int argc; char *argv[]; { : } Here, argc is integer and argv is a character array of unlimited size (hence [ ] in the declaration).
  • 19. Slide 19 of 44Ver. 1.0 Programming in C Practice: 5.4 1. Given that a C program called temp is executed by the following command: temp start 6 match the following: a. value of argc 1. points to array "6" b. argv [0] 2. points to arrm/ "start" c. argv [1] 3. 3 d. argv[2] 4. points to array "temp" 2. Modify the program upper so that it first checks the number of arguments entered on the command line. The program should display an error message if no arguments have been entered and also allow conversion of as many strings to upper-case as have been specified as arguments on the command line.
  • 20. Slide 20 of 44Ver. 1.0 Programming in C Practice: 5.4 (Contd.) 3. Consider the following program to calculate the sum of 2 integers specified on the command line: main (argc, argv) int argc; char *argv [ ];{ sum (argv [1], argv [2]); } sum (num1, num2) int numl, num2;{ return numl + num2; } The program has some logical errors. Point out the errors and correct the code.
  • 21. Slide 21 of 44Ver. 1.0 Programming in C Practice: 5.4 (Contd.) Solution: Microsoft Word Document
  • 22. Slide 22 of 44Ver. 1.0 Programming in C Using Library Functions for String Manipulation Library functions: Are also known as built-in functions. Can be used by including the concerned header files.
  • 23. Slide 23 of 44Ver. 1.0 Programming in C Standard String-Handling Functions Some of the standard string-handling functions are: strcmp(): Compares 2 strings (its parameters) character by character (ASCII comparison). strcpy(): Copies the second string to the first string named in the strcpy() parameters. strcat(): Appends the second string passed at the end of the first string passed to it . strlen(): Returns the number of characters in the string passed to it.
  • 24. Slide 24 of 44Ver. 1.0 Programming in C Practice: 5.5 1. What will the following function call return? x = strcmp(“Cada”, “CADA”); What should the declaration of x be? 2. Assume that array contains the string 846*. What will array contain when the following statement is executed? strcat(array,”>”); 3. State whether True or False: The following statement returns a value of 4 to x. x = strlen ("abc");
  • 25. Slide 25 of 44Ver. 1.0 Programming in C Practice: 5.5 (Contd.) Solution: 1. Value returned - 32 Declaration - int x; 2. 846*> 3. False
  • 26. Slide 26 of 44Ver. 1.0 Programming in C String to Numeric Conversion Functions Conversion functions: Are available as a part of the standard library. Are used to convert one data type into another. The following functions are used to convert a string to a numeric value: atoi(): Returns the int type value of a string passed to it and the value 0 in the case the string does not begin with a digit. atof(): Returns the double type value of a string passed to it and the value 0 in the case the string does not begin with a digit or a decimal point.
  • 27. Slide 27 of 44Ver. 1.0 Programming in C Practice: 5.6 1. What value will the variable val contain in each of the following situations? a. val = atoi ("A345"); /* val is int type */ b. val = atof ("345A"); /* val is double type */
  • 28. Slide 28 of 44Ver. 1.0 Programming in C Practice: 5.6 (Contd.) Solution: 1. a. 0 b. 345.000000
  • 29. Slide 29 of 44Ver. 1.0 Programming in C Functions for Formatting Data in Memory The formatting functions are available as a part of the standard library. The following functions are used to format data in memory: sprintf(): Writes to a variable in the memory and stores the data in different variables specified. Are used for transferring data between variables in a specific format. Has the following syntax: sprintf(string, format-specification, data, ….); sscanf(): Performs formatted input from a string. Has the following syntax: sscanf(string, format-specification, data, ….);
  • 30. Slide 30 of 44Ver. 1.0 Programming in C Practice: 5.7 1. What data is assigned to the variable string by each of the following? a. sprintf(string,"%04d%3.2f%2s",21,4.576, "Hi“); b. sprintf (string, "%10s", "ABC"); c. sscanf ("0987APZ", "%4d%s", &num, string); 1. What is the error, if any, in the instructions given below against each purpose? Give the correct instruction in case of an error. Purpose Instruction Accept a name from keyboard printf(“%s”, name); Format the contents of variables i_num(int) and f_num(float), and store them into a character array called string. printf (string,"%d%f, i_num,f_num)
  • 31. Slide 31 of 44Ver. 1.0 Programming in C Practice: 5.7 (Contd.) Solution: Microsoft Word Document
  • 32. Slide 32 of 44Ver. 1.0 Programming in C Working with Data Storage Types C language provides the following data storage types: auto: Variables of this type retain their value only as long as the function is in the stage of execution. static: Variables of this type retain its value even after the function to which it belongs has been executed. extern: Variables of this type are declared at the start of the program and can be accessed from any function.
  • 33. Slide 33 of 44Ver. 1.0 Programming in C Practice: 5.8 1. Given the following declarations: float area; static float val; auto char number; State which variable(s) will be: a. Created each tune the function is invoked. b. Created only once. 2. A numeric array has to store 4 values - 2.5, 6,3, 7.0 and 8.0. This array is to be declared and used in a function called compute(). Which of the following is/are correct declarations of this array? a. static int values[4] = {2.5,6.3,7.0,8.0}; b. auto float values[4] = {2.5,6.3,7.0,8.0 }; c. float values [4]= {2.5,6.3,7.0,8.0}; d. static float values [4] = {2.5,6.3,7.0,8.0};
  • 34. Slide 34 of 44Ver. 1.0 Programming in C Practice: 5.8 (Contd.) Solution: 1. a. area, number b. val 2. (a) Is invalid because the array should be float or double type. (b) Is invalid because it is declared as auto type. (c) Is invalid because it is declared as auto type. (d) Is correct. .
  • 35. Slide 35 of 44Ver. 1.0 Programming in C Practice: 5.9 1. If the variable val is declared as global in the program B, just illustrated, how would program A be modified? Give the appropriate declarations required in both programs. 2. Consider the following 2 program files: Program A float x; calc() { int i; : } printout() { static char z; : } Program B char numarray[5]; main() { char c ; : }
  • 36. Slide 36 of 44Ver. 1.0 Programming in C Practice: 5.9 (Contd.) Based on this code, answer the following: a. The variable z can be accessed in the function(s) ____________________. b. The variable(s) that can be accessed from functions of both program files is/are ___________. c. Slate whether True or False: The variable i can be used in the function printout(). d. Memory for variable z is reserved each time the function printout() is invoked. State whether true or false. e. If the function printout() has to access the variable x, does x have to be declared within the function printout()? If so, give the declaration. f. The auto variable(s) in these programs is/are _________________.
  • 37. Slide 37 of 44Ver. 1.0 Programming in C Practice: 5.9 (Contd.) Solution: 1. In program B, val would be declared as follows: int val; calc(){ :} In program A, the declaration would be as follows: main() { extern int val; :} 2. a. printout() only (Since it is declared within the function printout() and hence is not global) x and numarray (if proper extern statements are coded). b. False (Since it is declared within the function calc(), and hence it is not global)
  • 38. Slide 38 of 44Ver. 1.0 Programming in C Practice: 5.9 (Contd.) c. False (Since z is a static variable, it is created only once – the function printout() is invoked.) d. No (Since x is declared as global in program A, and printout() is defined in the same program file. However, declaring it as extern while within printout() is wrong.) e. The variable i defined in calc() and the variable c defined in main().
  • 39. Slide 39 of 44Ver. 1.0 Programming in C Practice: 5.10 1. The following file contains a C program called remdigit.c and a list of errors in the program indicated by the compiler. Go through the error list and correct the program. Since the C compiler does not always give very meaningful error messages, go through the program given in the following file carefully. Microsoft Office Word 97 - 2003 Document
  • 40. Slide 40 of 44Ver. 1.0 Programming in C Practice: 5.10 (Contd.) 2. Write a program to display all the positions at which a character occurs in a string. Both the character to be located and the string to be searched should be passed to a function called nextpos (findchar, searchstr). Each time the function locates the diameter, it should pass back the position. After searching the entire string, the function should return the value -1.
  • 41. Slide 41 of 44Ver. 1.0 Programming in C Practice: 5.10 (Contd.) Solution: Work out your answers. A discussion on these follows in the Classroom.
  • 42. Slide 42 of 44Ver. 1.0 Programming in C Summary In this session, you learned that: Functions provide advantages of reusability and structuring of programs. A parameter of a function is the data that the function must receive when called or invoked from another function. Functions that have parameters are invoked in one of the following two ways: Call by value Call by reference Call by value means that the called function creates its own copy of the values in different variables. Call by reference means that the called function should be able to refer to the variables of the caller function directly, and does not create its own copy of the values in different variables.
  • 43. Slide 43 of 44Ver. 1.0 Programming in C Summary (Contd.) Arrays are passed to functions by the call by reference method. Functions can return values by using the return statement. The main() function can have parameters, argc and argv. argc is integer type while argv is a string. The information that is passed to a program from the OS prompt is known as command-line arguments. Some of the standard string-handling functions are: strcmp(): Compares two strings. strcpy(): Copies the second string to the first string. strcat(): Appends the second string passed at the end of the first string passed as parameters. strlen(): Returns the number of characters in the string passed as a parameter.
  • 44. Slide 44 of 44Ver. 1.0 Programming in C Summary (Contd.) atoi(): Returns the int type value of a string passed to it. aof(): Returns the double type value of a string passed to it. The following functions are used to format data in memory: sprintf() sscanf() C language provides the following data storage types: auto: Variables of this type retain their value only as long as the function is in the stage of execution. static: Variables of this type retain its value even after the function to which it belongs has been executed. extern: Variables of this type are declared at the start of the program and can be accessed from any function.

Editor's Notes

  1. Begin the session by explaining the objectives of the session.
  2. Give an example where you need to use a function for reusability.
  3. Tell the students that a parameter is a means by which functions pass information.
  4. Use this slide to test the student’s understanding on parameters.
  5. Tell the students that calling a function is termed as invoking a function. A function is invoked or called by writing the function name with parameters, if any. Also discuss actual parameters and formal parameters. What is implemented in C is not a true call by reference. A call by reference actually involves providing an alias for the original variable being referenced. There should be no difference in the way the original variable and the referencing variable is used (even in the syntax). What happens in C is that the address of the variable is passed. This address is received into a pointer and the pointer is dereferenced. Here a local variable (the pointer) is created in the called function. In a true call by reference , a variable is not created.
  6. Use this slide to test the student’s understanding on call by value and call by reference.
  7. As parameters are used to pass the required information to the called function, similarly, the return statement is used to pass the result back to the calling function.
  8. Use this slide to test the student’s understanding on returning values from a function.
  9. Explain command-line arguments to the students. Give the example of the copy command in MS DOS. Tell the students that copy is the command and the name of the source and the target file are the arguments to the copy command. The command-line arguments are separated by a space.
  10. Use this slide to test the student’s understanding on command-line arguments.
  11. Tell the students that if they want to ignore case while comparing two strings, they can use the strcmpi() function.
  12. Use this slide to test the student’s understanding on string-handling functions.
  13. Use this slide to test the student’s understanding on string to numeric conversion functions.
  14. Use this slide to test the student’s understanding on sscanf() and sscanf() functions.
  15. Use this slide to test the student’s understanding on data storage types .
  16. Use this slide to test the student’s understanding on data storage types .
  17. Use this and the next 2 slides to summarize the session.