SlideShare ist ein Scribd-Unternehmen logo
1 von 64
Downloaden Sie, um offline zu lesen
Tutorial on C Language
    Programming
                     Teodor Rus
                  rus@cs.uiowa.edu


The University of Iowa, Department of Computer Science




                                                         Introduction to System Software – p.1/64
Tutorial on C programming
C program structure:
   Data structure
   Control structure
   Program structure




                             Introduction to System Software – p.2/64
Data structures
Predefined data types:
   integer (int), small integers (short), large integers
   (long)
   real numbers (float), large real numbers (double)
   character data (char)
User defined data types using type constructors array,
record, pointer, file




                                                Introduction to System Software – p.3/64
Declarations
A data object of a defined type T is declared using the
construct of the form T data where T is a type expression
and data is the data object name
Example:
   int x declares x an object of type integer
   short x declares x an object of type small integer
   long x declares x an object of type large integer
   float x declares x an object of type real
   double x declares x an object of type large real
   char x declares x an object of type character




                                                        Introduction to System Software – p.4/64
Definitions
An object of a user defined type T is constructed using
one of the type constructors struct, [], *, FILE that takes
as arguments objects of already defined types.
A new user defined type T is constructed using the
meta-constructor typedef and a type or a type
constructor




                                                Introduction to System Software – p.5/64
Record type definition
   A record type is defined using the struct constructor
   following the template:
   struct TypeName
         {
           component1;
           component2;
           component3;
         }
   Components are object declarations of the form T
   ObjName;

Note: TypeName is an abstraction


                                                Introduction to System Software – p.6/64
Record object declaration
An object of type TypeName is obtained by the
declaration
TypeName MyRecord
One can put together the definition and the declaration
getting:
struct TypeName
      {
        component1;
        component2;
        component3;
      } MyRecord;



                                            Introduction to System Software – p.7/64
Example record
Example of a record type definition and declaration is:
struct Data
      {
        int Day;
        int Month;
        int Year;
      } MyData, *MyPT, MyArray[Max];
Note: type expressions are obtained by combining the
type constructors struct, *, [], in a well defined manner




                                              Introduction to System Software – p.8/64
Reference to record components
MyData.Year, MyData.Month, MyData.Day are
references at the components of the data object
MyData
M yP T − > Y ear, M yP T − > M onth, M yP T − > Day are
pointer reference to the same components.
Note, we need to use MyPT = &MyData before this
reference make sense; i.e.,
M yP T − > Y ear ≡ (∗M yP T ).Y ear.




                                            Introduction to System Software – p.9/64
Memory representation of records
Consider the following definition and declarations:
struct example
      {
        int x;
        int *y;
      } Obj, *PtObj;




                                                 Introduction to System Software – p.10/64
Memory representation
Memory representation of Obj is in Figure 1
                             integer
                        integer address



           Figure 1:   Record memory representation




                                                      Introduction to System Software – p.11/64
Memory representation of PtObj
This is shown in Figure 2
                            integer
                       integer address
                                c
                            integer


       Figure 2:   Pointer to record memory representation




                                                      Introduction to System Software – p.12/64
Facts about records
To give few important facts about records, assume that
PtObj = & Obj has been executed. Then we have:
   Obj.x is the integer x; P tObj− > x is the integer x
   Obj.y is the (integer) address y; Obj− > y is the address y;
   + + P tObj− > x increments x not PtObj; (+ + P t)− > x increments
   PtObj before accessing x; (P tObj + +)− > x increments PtObj after
   accessing x
   ∗P tObj− > y fetches whatever y points to (an integer);
   ∗P tObj− > y + + increments y after accessing whatever it points to
   (this is an address operation); (∗P tObj− > y) + + increments
   whatever y points to (this is an integer operation);




                                                           Introduction to System Software – p.13/64
Array data type
   A unidimensional array of n objects of type T is defined
   by
   T UniName[n]
   Note, this is both a definition and a declaration
   A bidimensional array of m × n objects of type T is
   defined by
   T   BidimName[m][n]
   The element i of the array UniName is referenced by
   ArrayName[i]. Note, 0 <= i < n

Examples:   int x[20], struct example MyArray[100][100]


                                                  Introduction to System Software – p.14/64
Array memory representation
The indices of the elements of an unidimensional array
of size n are 0, 1, . . ., n-1
The elements of a bidimensional array
BidimName[m][n] are stored in memory on a row-major,
i.e., they are:
BidimName[0][0], BidimName[0][1], ...   BidimName[0][n-1]
BidimName[1][0], BidimName[1][1], ...   BidimName[1][n-1]
BidimName[2][0], BidimName[2][1], ...   BidimName[2][n-1]
...
BidimName[m-1][0], BidimName[m-1][1],   ... BidimName[m-1][n-1]




                                                      Introduction to System Software – p.15/64
Union data type
Unions are records with variable fields like in Pascal
Example:
union UniName
     {
       int ival;
       float fval;
       char *pval;
     } uval, *p;
The variable uval may have as value an integer, a real,
or a pointer to a character.
Only one of the components is the value hold by the
uval


                                             Introduction to System Software – p.16/64
Reference to union components
The elements of a union are referenced in the same
way as elements of a record (struct) are referenced
The memory representation of variable uval will be
large enough to accommodate any of the values that
are used in its definition
It is the programmer’s task to provide a discriminant that
will show what component of a union is in the variable
uval at a given time.




                                              Introduction to System Software – p.17/64
Example of a union usage
The symbol table entry of a symbol table used by a
compiler:
struct SymTabEntry
       {
          char *name;
          int flags;
          int stype;
          union
              {
                int    ival;
                float fval;
                char *pval;
              }sval;
          } SymTab[MaxSymb], *PtSymTab[MaxSymb];

                                        Introduction to System Software – p.18/64
Reference to union components
SymTab[i].Object and P tSymT ab[i]− > Object, where
Object ∈ {∗name, f lags, stype, sval}
are references to symbol table element components.




                                              Introduction to System Software – p.19/64
Pointer data type
Every object has an address (name) and a value
An object of type pointer has as its value the address of
an object of a given type
An object of type pointer is defined by the construct
T *PtName;
where * show that PtNamed is a pointer and T shows
the type of object address it may hold




                                             Introduction to System Software – p.20/64
Example pointers
int x, z; /* x and z are variables of type integer */
int *y, *w; /* y and w are variables of type pointer to
integer */
char v, *p; /* p is a variable of type pointer to character */
Address of an object x of type T is obtained by the
operator &, i.e., is &x
y = &x is a valid assignment while y = x is not




                                                 Introduction to System Software – p.21/64
Pointer references
direct by name, indirect by *name
   The name of a variable of type pointer references the
   address of the object it holds. Hence, w = y is valid but
   w = p is invalid
   Dereferencing of a variable of type pointer leads us to
   the value hold in the object whose address is hold by
   the pointer. Hence, (*y) is the integer whose address is
   in y
   Operation on a variable of type pointer (such as y) are
   address type operations
   Operations on the value of the objects whose
   addresses are hold by pointers (such as (*y)) are data
   type operations
                                                 Introduction to System Software – p.22/64
File data type
A file is a potentially infinite stream of objects
(characters, integers, reals, strings, arrays, etc)
A file is described by descriptor that shows:
   type of the objects it contains
   order relation among its components
   access method used to file components
In C-language a file is specified by a name and a
file-descriptor
   File name is user defined
   File descriptor is obtained from the system using the
   declaration FILE *fp;


                                                Introduction to System Software – p.23/64
Operations with file
The main operations on a file area: open, doio, close
   File open links the file abstraction defined in the
   program with the physical media where the file objects
   are stored. In C this is done by
   fp = fopen(name,mode), where mode is "w", "r" or "rw"
   File close removes the links established by open.
   I/O operations: printf, fprintf store objects in the file, and
   scanf and fscanf access objects in a file
   printf, fprintf, scanf, fscanf have a formate that can be
   learn by inspecting the man page of these functions



                                                    Introduction to System Software – p.24/64
User defined types
Programmers may define their own types using typedef
construct
The usage pattern is
typedef TypeDefinition TypeName
where TypeDefinition is the type expression defining the
new type and TypeName is the name of the new type
Objects of type TypeName are then declared as usual
TypeName can also be used as component of various
type expressions using constructors struct, [], *, and
FILE.



                                            Introduction to System Software – p.25/64
Examples
typedef int LENGTH; /* LENGTH is a new type */
LENGTH len, maxlen, *L[]; /* variable of type LENGTH */
typedef char *string; /* string is synonymous to char * */ string p,
lineptr[L]; /* These are variable of type string */
typedef struct node
                    {
                      char *value;
                      int      count;
                      struct node *Left;
                      struct node *Right;
                    } TreeRoot, *TreePTR;
TreeRoot a; /* a is a variable of type TreeRoot */
TreePTR b; /* b is a variable of type TreeRoot * */


                                                           Introduction to System Software – p.26/64
Control Flow Structures




                      Introduction to System Software – p.27/64
C language computation units
Assignment statements
Block statements: {statement1; ... ;statement}
Control statements: branching and looping statements
Function calls;




                                             Introduction to System Software – p.28/64
Assignment statement
    Syntax: identifier = expression;
    Semantics: evaluate expression to val and then
    assign val as the value of identifier

Note:
    Type of val should be the same as the type of
    identifier
    Peculiarities: id++ is equivalent to id = id + 1 and
    id- - is equivalent to id = id - 1
    C expressions are arithmetic or logic; but assignment
    statements are also expressions.



                                                Introduction to System Software – p.29/64
Branching statements
if-statements
if-else statement
switch-statement
break-statement
continue-statement
unconditional jump statement




                               Introduction to System Software – p.30/64
If-statement
Syntax: if (expr) statement; where expr is
boolean
Semantic: evaluate expression expr to val; if val is true
execute statement, otherwise execute next statement
of the program




                                              Introduction to System Software – p.31/64
If-else statement
Syntax: if (expr) statement1; else
statement2;
Semantics: evaluate expression expr to val; if val is
true execute statement1 otherwise execute
statement2; in any case control flows to the next
statement of the program




                                             Introduction to System Software – p.32/64
Switch statement
Syntax:
switch (expr) /*       expr is a boolean expressio
      {
          case C1:     {statement0;break}
          case C2:     {statement1;break}
          ...
          default:     {DefaultStatement;break}
        }
Semantic: evaluate expr to val; if val is equal to one of
the case constants C1, C2, . . ., the associated
statement is executed; otherwise DefaultStatement is
executed. Note, default clause is optional; if not there
and val is not equal with any case constant, no action
take place
                                              Introduction to System Software – p.33/64
Break statement
Syntax: break;
Semantic: terminates the execution of a loop or a switch




                                            Introduction to System Software – p.34/64
Continue statement
Syntax: continue;
Semantic: terminates the current iteration of a loop




                                             Introduction to System Software – p.35/64
Unconditional jump statement
Syntax: goto Label; where Label:Statement;
belongs to the program
Semantic: forces control to go to the Statement;




                                          Introduction to System Software – p.36/64
Looping statements
while-statement
do-while statement
for-statement




                              Introduction to System Software – p.37/64
While statement
    Syntax: while (expr) Statement; where expr is
    boolean
    Semantic: evaluate expr to val; if val is true
    Statement is execute and while statement is repeated;
    if val is false control flows to the next instruction of the
    program

Note:   true boolean values are any integer different from zero;
false boolean value is the integer zero.




                                                    Introduction to System Software – p.38/64
Do-while statement
    Syntax: do Statement; while (expr);
    Semantic: equivalent to
    Statement;
    while (Expr) Statement;

Note: while statement executes zero or more iterations of the
loop; do-while statement executes one or more iterations of
the loop.




                                                 Introduction to System Software – p.39/64
For statement
Syntax: for(expr1; expr2; expr3) Statement;
Semantic: equivalent to
expr1;
while (expr2)
     {
       Statement;
       expr3;
     }
Note: any of the expressions expr1, expr2, expr3 may
be omitted; if expr3 is omitted it is interpreted as true,
hence various sorts of infinite loops can be performed



                                                Introduction to System Software – p.40/64
Block statement
Syntax:
{
Declaration list;
Statement list;
}
Declaration list:
      Declaration;
      Declaration list Declaration;
Statement list:
      Statement;
      Statement list Statement;
Semantics: statements in Statement list are executed in
sequence in the environment provided by Declaration
list
                                            Introduction to System Software – p.41/64
Function definition
Syntax:
type name (formal parameter list)
         {
           Declaration list;
           Statement list;
           return result
         }
Semantic: a function definition specifies the
computation defined by the Statement list in the
environment defined by formal parameter list and
Declaration list and return a result of type type




                                            Introduction to System Software – p.42/64
Example
/* power: raises the value of variable base to /*
/* the power values of variable n, n >= 0 */

int power (int base, int n)
         {
           int i, p;
           p = 1;
           for (i = 1; i <= n; i++)
              p = p * base;
           return p;
         }

Note:   comments in C are enclosed in /* ... */ Use comments outside of func-

tion definition; formate function body such that the text indentation allows

reader to understand it.
                                                               Introduction to System Software – p.43/64
Function declaration
    Syntax: type name (type1, type2, ...) where
    type is the function type (i.e., the type of result
    returned by the function) and type1, type2, ...
    are the types of the formal parameters
    Semantics: declare name as the name of a function
    whose arguments are of types type1, type2, ...
    and whose result if of type type

Note:   since a function declaration is a declaration it must be
provided in the declaration list of the statement that uses it.




                                                    Introduction to System Software – p.44/64
Function call
Syntax: identifier = name (actual
parameters);
  identifier must have the same type as the type
  specified in the definition and the declaration of name
  Actual parameters must expressions whose values
  are of the types that type1, type2, ...
  specified in the definition and the declaration of name
Semantic: execute computation encapsulated in the
definition of function name() in the environment
provided by actual parameters and return the result.
       int x; int power(int, int);...; x =
Example:
power(2,3); ...


                                            Introduction to System Software – p.45/64
Parameter passing
Actual parameters are passed by value, except arrays, which
are passed by reference.




                                                Introduction to System Software – p.46/64
Remember
Arrays are transmitted by reference, i.e., the address of
the array variable is transmitted.
To operate on the local elements of a function using
them as parameters to another function pointers need
to be transmitted at function call
Initialization of the pointers is required upon function
call.
Note that pointers are typed i.d., int *x, char *x, struct
name *x are different pointers.




                                                 Introduction to System Software – p.47/64
Function memory representation
A function is represented in memory by two components:
   Execution code, i.e., memory image of executable
   statements
   Activation record




                                              Introduction to System Software – p.48/64
Activation record
Activation record is a data structure constgructed by the
compiler and contains:
   Function return value;
   Static link: a pointer to the activation record of the function that
   contains the definition of the function. In C this is nill.
   Dynamic link: a pointer to the activation record of the function that
   contains the call of the function
   Stack extension value
   Return address
   List of locations for actual parameters
   Local variables of the function



                                                               Introduction to System Software – p.49/64
Structure of a function in memory
 Figure 3 shows the structure of a function in memory:


                  c                            c
                              r      r
       Global variables   Executable code   Activation record




        Figure 3:         Function memory representation




                                                                Introduction to System Software – p.50/64
Structure of a C language program
A C program is composed of four componentgs:
Macro definitions (optional)
Global declarations (optional)
Main function (mandatory)

Other functions components of the program (optional)




                                                       Introduction to System Software – p.51/64
Note
A C program has four components: macro definitions,
global declarations, main() function, and other
functions. Only main() function is mandatory.
A C language program is executed by the operating
system by calling its functions main().
A C language program is implicitly declared to the
system by the presence of the unique names, main()




                                          Introduction to System Software – p.52/64
Macro definition component
Syntax: sequence of macro-operations of the form:
#define name value
#include "filename"
#include <filename>
Semantics:
  #define name value allows programmer to use
  name in the program while compiler replaces it with
  value which can be any string of characters.
  #include "filename" allows the programmer to
  develop a program on various separate files.
  #include <filename> allows the programmer to
  make use of files contained in various libraries of the
  system

                                             Introduction to System Software – p.53/64
Global declarations
Syntax: declarations of variables that occur outside of
the function components of the program.
Semantic: all global variables are accessible to all
function components of the program




                                              Introduction to System Software – p.54/64
Main function of the program
Syntax:
main (int argc, char *argv[])
    {
      Declaration list;
      Statement list;
    }
Note:since a function may have no arguments
main() { Body } is also valid.




                                         Introduction to System Software – p.55/64
Program execution
A program is executed by the system calling the
function main() as consequence of a command given by
the programmer. This command has the form
%name arg1 arg2,...
argc is an integer variable where the number of the
arguments used in the execution command is stored
argv[] is an array of pointers to strings where the
arguments arg1, arg2, ... of the execution command are
stored.




                                           Introduction to System Software – p.56/64
Other function components
Syntax: any function definition
Semantic: function components of a program may be
called by the main() or among themselves. However, in
order for main() or any other function to call a function
f() the following must be done:
    f() must have a definition accessible to main() and to
    other functions that intend to call it
    f() must be declared in main() and in the functions
    which intend to call it




                                              Introduction to System Software – p.57/64
Example program
#include <stndio.h>
main ()
    {
     int C;
     C = gethchar();
     while (C != EOF)
          {
            putchar(C);
            C = getchar();
          }
     }

This program copies the standard input to the standard out-
put

                                                Introduction to System Software – p.58/64
Bonus point assignment
Rewrite th program such that it will copy a file f1 into another
file f2; files f1 and f2 should be given in the command line.




                                                   Introduction to System Software – p.59/64
Program memory representation
C compiler maps a C program into three segments called
data, text and stack as seen in Figure 4
                       Memory image

                       ¨ ¨¨rrr
                 %
                 ¨¨¨         c
                                      rr
                                        j
                                        r



                Data       Text        Stack




      Figure 4: Memory image of a C program

                                               Introduction to System Software – p.60/64
Data segment
Contains all global data of the program
Data segment is constructed by the compiler




                                              Introduction to System Software – p.61/64
Text segment
Contains all executable code of the program
Each function component of the Text segment has
access to the global data in the Data segment and to
the local data in the activation record of that function.




                                                Introduction to System Software – p.62/64
Stack segment
Stack segment is dynamically generated by program
execution
When a function is called its activation record is pushed
on the stack segment
When a function return its activation record is popped
out from the stack segment




                                             Introduction to System Software – p.63/64
Development of a C program
Use an editor to generate the file that contains the
program. Example, execute %vi mylms.c
Compile the C program in the file mylms.c using the
command % cc [Options] mylms.c
If mylms.c contains a C program syntactically correct
the result of the compilation is an executable file called
a.out.
If you want to give the name mylms (or any other name)
to the executable use option -o mylms in cc command
Test the program on the test data; use dbx to help this
Read the documentation for vi, cc, dbx using manual
page

                                              Introduction to System Software – p.64/64

Weitere ähnliche Inhalte

Was ist angesagt?

OpenGurukul : Language : C Programming
OpenGurukul : Language : C ProgrammingOpenGurukul : Language : C Programming
OpenGurukul : Language : C ProgrammingOpen Gurukul
 
Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_outputAnil Dutt
 
C programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti DokeC programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti DokePranoti Doke
 
Managing input and output operation in c
Managing input and output operation in cManaging input and output operation in c
Managing input and output operation in cyazad dumasia
 
C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)sachindane
 
Functions, Strings ,Storage classes in C
 Functions, Strings ,Storage classes in C Functions, Strings ,Storage classes in C
Functions, Strings ,Storage classes in Carshpreetkaur07
 
C language for Semester Exams for Engineers
C language for Semester Exams for Engineers C language for Semester Exams for Engineers
C language for Semester Exams for Engineers Appili Vamsi Krishna
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)Ashishchinu
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programminggajendra singh
 
Unit 4 Foc
Unit 4 FocUnit 4 Foc
Unit 4 FocJAYA
 
C programming session 05
C programming session 05C programming session 05
C programming session 05Dushmanta Nath
 
Computer programming(CP)
Computer programming(CP)Computer programming(CP)
Computer programming(CP)nmahi96
 

Was ist angesagt? (20)

Intro to C++ - language
Intro to C++ - languageIntro to C++ - language
Intro to C++ - language
 
OpenGurukul : Language : C Programming
OpenGurukul : Language : C ProgrammingOpenGurukul : Language : C Programming
OpenGurukul : Language : C Programming
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_output
 
C programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti DokeC programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti Doke
 
C language ppt
C language pptC language ppt
C language ppt
 
Managing input and output operation in c
Managing input and output operation in cManaging input and output operation in c
Managing input and output operation in c
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)
 
Functions, Strings ,Storage classes in C
 Functions, Strings ,Storage classes in C Functions, Strings ,Storage classes in C
Functions, Strings ,Storage classes in C
 
C program
C programC program
C program
 
C language for Semester Exams for Engineers
C language for Semester Exams for Engineers C language for Semester Exams for Engineers
C language for Semester Exams for Engineers
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)
 
Structures-2
Structures-2Structures-2
Structures-2
 
Clanguage
ClanguageClanguage
Clanguage
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
C language basics
C language basicsC language basics
C language basics
 
Unit 4 Foc
Unit 4 FocUnit 4 Foc
Unit 4 Foc
 
C programming session 05
C programming session 05C programming session 05
C programming session 05
 
Computer programming(CP)
Computer programming(CP)Computer programming(CP)
Computer programming(CP)
 

Andere mochten auch

Andere mochten auch (13)

Unit1 C
Unit1 CUnit1 C
Unit1 C
 
C language tutorial
C language tutorialC language tutorial
C language tutorial
 
bin'Abdullah_JACM 2015
bin'Abdullah_JACM 2015bin'Abdullah_JACM 2015
bin'Abdullah_JACM 2015
 
Async Web Frameworks in Python
Async Web Frameworks in PythonAsync Web Frameworks in Python
Async Web Frameworks in Python
 
Sensor Localization presentation1&2
Sensor Localization  presentation1&2Sensor Localization  presentation1&2
Sensor Localization presentation1&2
 
Solid C++ by Example
Solid C++ by ExampleSolid C++ by Example
Solid C++ by Example
 
C the basic concepts
C the basic conceptsC the basic concepts
C the basic concepts
 
The smartpath information systems c pro
The smartpath information systems c proThe smartpath information systems c pro
The smartpath information systems c pro
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
 
C ppt
C pptC ppt
C ppt
 
01 c++ Intro.ppt
01 c++ Intro.ppt01 c++ Intro.ppt
01 c++ Intro.ppt
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpoint
 
Deep C
Deep CDeep C
Deep C
 

Ähnlich wie Tutorial on c language programming

unit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptxunit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptxusvirat1805
 
Structure and Typedef
Structure and TypedefStructure and Typedef
Structure and TypedefAcad
 
James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonCP-Union
 
Concept Of C++ Data Types
Concept Of C++ Data TypesConcept Of C++ Data Types
Concept Of C++ Data Typesk v
 
Str
StrStr
StrAcad
 
Pointer and polymorphism
Pointer and polymorphismPointer and polymorphism
Pointer and polymorphismSangeethaSasi1
 
Python (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizePython (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizeIruolagbePius
 
Data structures using C
Data structures using CData structures using C
Data structures using CPdr Patnaik
 
Ds12 140715025807-phpapp02
Ds12 140715025807-phpapp02Ds12 140715025807-phpapp02
Ds12 140715025807-phpapp02Salman Qamar
 
HexRaysCodeXplorer: object oriented RE for fun and profit
HexRaysCodeXplorer: object oriented RE for fun and profitHexRaysCodeXplorer: object oriented RE for fun and profit
HexRaysCodeXplorer: object oriented RE for fun and profitAlex Matrosov
 

Ähnlich wie Tutorial on c language programming (20)

Structures
StructuresStructures
Structures
 
Python 3.pptx
Python 3.pptxPython 3.pptx
Python 3.pptx
 
Data types
Data typesData types
Data types
 
unit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptxunit (1)INTRODUCTION TO PYTHON course.pptx
unit (1)INTRODUCTION TO PYTHON course.pptx
 
Structured Languages
Structured LanguagesStructured Languages
Structured Languages
 
Structure and Typedef
Structure and TypedefStructure and Typedef
Structure and Typedef
 
James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on Python
 
Theory1&amp;2
Theory1&amp;2Theory1&amp;2
Theory1&amp;2
 
unit 1 (1).pptx
unit 1 (1).pptxunit 1 (1).pptx
unit 1 (1).pptx
 
See through C
See through CSee through C
See through C
 
Concept Of C++ Data Types
Concept Of C++ Data TypesConcept Of C++ Data Types
Concept Of C++ Data Types
 
unit 1.docx
unit 1.docxunit 1.docx
unit 1.docx
 
Str
StrStr
Str
 
Pointer and polymorphism
Pointer and polymorphismPointer and polymorphism
Pointer and polymorphism
 
Python (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualizePython (Data Analysis) cleaning and visualize
Python (Data Analysis) cleaning and visualize
 
Data structures using C
Data structures using CData structures using C
Data structures using C
 
Ds12 140715025807-phpapp02
Ds12 140715025807-phpapp02Ds12 140715025807-phpapp02
Ds12 140715025807-phpapp02
 
Python for beginners
Python for beginnersPython for beginners
Python for beginners
 
Data Handling
Data HandlingData Handling
Data Handling
 
HexRaysCodeXplorer: object oriented RE for fun and profit
HexRaysCodeXplorer: object oriented RE for fun and profitHexRaysCodeXplorer: object oriented RE for fun and profit
HexRaysCodeXplorer: object oriented RE for fun and profit
 

Kürzlich hochgeladen

Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 

Kürzlich hochgeladen (20)

Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 

Tutorial on c language programming

  • 1. Tutorial on C Language Programming Teodor Rus rus@cs.uiowa.edu The University of Iowa, Department of Computer Science Introduction to System Software – p.1/64
  • 2. Tutorial on C programming C program structure: Data structure Control structure Program structure Introduction to System Software – p.2/64
  • 3. Data structures Predefined data types: integer (int), small integers (short), large integers (long) real numbers (float), large real numbers (double) character data (char) User defined data types using type constructors array, record, pointer, file Introduction to System Software – p.3/64
  • 4. Declarations A data object of a defined type T is declared using the construct of the form T data where T is a type expression and data is the data object name Example: int x declares x an object of type integer short x declares x an object of type small integer long x declares x an object of type large integer float x declares x an object of type real double x declares x an object of type large real char x declares x an object of type character Introduction to System Software – p.4/64
  • 5. Definitions An object of a user defined type T is constructed using one of the type constructors struct, [], *, FILE that takes as arguments objects of already defined types. A new user defined type T is constructed using the meta-constructor typedef and a type or a type constructor Introduction to System Software – p.5/64
  • 6. Record type definition A record type is defined using the struct constructor following the template: struct TypeName { component1; component2; component3; } Components are object declarations of the form T ObjName; Note: TypeName is an abstraction Introduction to System Software – p.6/64
  • 7. Record object declaration An object of type TypeName is obtained by the declaration TypeName MyRecord One can put together the definition and the declaration getting: struct TypeName { component1; component2; component3; } MyRecord; Introduction to System Software – p.7/64
  • 8. Example record Example of a record type definition and declaration is: struct Data { int Day; int Month; int Year; } MyData, *MyPT, MyArray[Max]; Note: type expressions are obtained by combining the type constructors struct, *, [], in a well defined manner Introduction to System Software – p.8/64
  • 9. Reference to record components MyData.Year, MyData.Month, MyData.Day are references at the components of the data object MyData M yP T − > Y ear, M yP T − > M onth, M yP T − > Day are pointer reference to the same components. Note, we need to use MyPT = &MyData before this reference make sense; i.e., M yP T − > Y ear ≡ (∗M yP T ).Y ear. Introduction to System Software – p.9/64
  • 10. Memory representation of records Consider the following definition and declarations: struct example { int x; int *y; } Obj, *PtObj; Introduction to System Software – p.10/64
  • 11. Memory representation Memory representation of Obj is in Figure 1 integer integer address Figure 1: Record memory representation Introduction to System Software – p.11/64
  • 12. Memory representation of PtObj This is shown in Figure 2 integer integer address c integer Figure 2: Pointer to record memory representation Introduction to System Software – p.12/64
  • 13. Facts about records To give few important facts about records, assume that PtObj = & Obj has been executed. Then we have: Obj.x is the integer x; P tObj− > x is the integer x Obj.y is the (integer) address y; Obj− > y is the address y; + + P tObj− > x increments x not PtObj; (+ + P t)− > x increments PtObj before accessing x; (P tObj + +)− > x increments PtObj after accessing x ∗P tObj− > y fetches whatever y points to (an integer); ∗P tObj− > y + + increments y after accessing whatever it points to (this is an address operation); (∗P tObj− > y) + + increments whatever y points to (this is an integer operation); Introduction to System Software – p.13/64
  • 14. Array data type A unidimensional array of n objects of type T is defined by T UniName[n] Note, this is both a definition and a declaration A bidimensional array of m × n objects of type T is defined by T BidimName[m][n] The element i of the array UniName is referenced by ArrayName[i]. Note, 0 <= i < n Examples: int x[20], struct example MyArray[100][100] Introduction to System Software – p.14/64
  • 15. Array memory representation The indices of the elements of an unidimensional array of size n are 0, 1, . . ., n-1 The elements of a bidimensional array BidimName[m][n] are stored in memory on a row-major, i.e., they are: BidimName[0][0], BidimName[0][1], ... BidimName[0][n-1] BidimName[1][0], BidimName[1][1], ... BidimName[1][n-1] BidimName[2][0], BidimName[2][1], ... BidimName[2][n-1] ... BidimName[m-1][0], BidimName[m-1][1], ... BidimName[m-1][n-1] Introduction to System Software – p.15/64
  • 16. Union data type Unions are records with variable fields like in Pascal Example: union UniName { int ival; float fval; char *pval; } uval, *p; The variable uval may have as value an integer, a real, or a pointer to a character. Only one of the components is the value hold by the uval Introduction to System Software – p.16/64
  • 17. Reference to union components The elements of a union are referenced in the same way as elements of a record (struct) are referenced The memory representation of variable uval will be large enough to accommodate any of the values that are used in its definition It is the programmer’s task to provide a discriminant that will show what component of a union is in the variable uval at a given time. Introduction to System Software – p.17/64
  • 18. Example of a union usage The symbol table entry of a symbol table used by a compiler: struct SymTabEntry { char *name; int flags; int stype; union { int ival; float fval; char *pval; }sval; } SymTab[MaxSymb], *PtSymTab[MaxSymb]; Introduction to System Software – p.18/64
  • 19. Reference to union components SymTab[i].Object and P tSymT ab[i]− > Object, where Object ∈ {∗name, f lags, stype, sval} are references to symbol table element components. Introduction to System Software – p.19/64
  • 20. Pointer data type Every object has an address (name) and a value An object of type pointer has as its value the address of an object of a given type An object of type pointer is defined by the construct T *PtName; where * show that PtNamed is a pointer and T shows the type of object address it may hold Introduction to System Software – p.20/64
  • 21. Example pointers int x, z; /* x and z are variables of type integer */ int *y, *w; /* y and w are variables of type pointer to integer */ char v, *p; /* p is a variable of type pointer to character */ Address of an object x of type T is obtained by the operator &, i.e., is &x y = &x is a valid assignment while y = x is not Introduction to System Software – p.21/64
  • 22. Pointer references direct by name, indirect by *name The name of a variable of type pointer references the address of the object it holds. Hence, w = y is valid but w = p is invalid Dereferencing of a variable of type pointer leads us to the value hold in the object whose address is hold by the pointer. Hence, (*y) is the integer whose address is in y Operation on a variable of type pointer (such as y) are address type operations Operations on the value of the objects whose addresses are hold by pointers (such as (*y)) are data type operations Introduction to System Software – p.22/64
  • 23. File data type A file is a potentially infinite stream of objects (characters, integers, reals, strings, arrays, etc) A file is described by descriptor that shows: type of the objects it contains order relation among its components access method used to file components In C-language a file is specified by a name and a file-descriptor File name is user defined File descriptor is obtained from the system using the declaration FILE *fp; Introduction to System Software – p.23/64
  • 24. Operations with file The main operations on a file area: open, doio, close File open links the file abstraction defined in the program with the physical media where the file objects are stored. In C this is done by fp = fopen(name,mode), where mode is "w", "r" or "rw" File close removes the links established by open. I/O operations: printf, fprintf store objects in the file, and scanf and fscanf access objects in a file printf, fprintf, scanf, fscanf have a formate that can be learn by inspecting the man page of these functions Introduction to System Software – p.24/64
  • 25. User defined types Programmers may define their own types using typedef construct The usage pattern is typedef TypeDefinition TypeName where TypeDefinition is the type expression defining the new type and TypeName is the name of the new type Objects of type TypeName are then declared as usual TypeName can also be used as component of various type expressions using constructors struct, [], *, and FILE. Introduction to System Software – p.25/64
  • 26. Examples typedef int LENGTH; /* LENGTH is a new type */ LENGTH len, maxlen, *L[]; /* variable of type LENGTH */ typedef char *string; /* string is synonymous to char * */ string p, lineptr[L]; /* These are variable of type string */ typedef struct node { char *value; int count; struct node *Left; struct node *Right; } TreeRoot, *TreePTR; TreeRoot a; /* a is a variable of type TreeRoot */ TreePTR b; /* b is a variable of type TreeRoot * */ Introduction to System Software – p.26/64
  • 27. Control Flow Structures Introduction to System Software – p.27/64
  • 28. C language computation units Assignment statements Block statements: {statement1; ... ;statement} Control statements: branching and looping statements Function calls; Introduction to System Software – p.28/64
  • 29. Assignment statement Syntax: identifier = expression; Semantics: evaluate expression to val and then assign val as the value of identifier Note: Type of val should be the same as the type of identifier Peculiarities: id++ is equivalent to id = id + 1 and id- - is equivalent to id = id - 1 C expressions are arithmetic or logic; but assignment statements are also expressions. Introduction to System Software – p.29/64
  • 31. If-statement Syntax: if (expr) statement; where expr is boolean Semantic: evaluate expression expr to val; if val is true execute statement, otherwise execute next statement of the program Introduction to System Software – p.31/64
  • 32. If-else statement Syntax: if (expr) statement1; else statement2; Semantics: evaluate expression expr to val; if val is true execute statement1 otherwise execute statement2; in any case control flows to the next statement of the program Introduction to System Software – p.32/64
  • 33. Switch statement Syntax: switch (expr) /* expr is a boolean expressio { case C1: {statement0;break} case C2: {statement1;break} ... default: {DefaultStatement;break} } Semantic: evaluate expr to val; if val is equal to one of the case constants C1, C2, . . ., the associated statement is executed; otherwise DefaultStatement is executed. Note, default clause is optional; if not there and val is not equal with any case constant, no action take place Introduction to System Software – p.33/64
  • 34. Break statement Syntax: break; Semantic: terminates the execution of a loop or a switch Introduction to System Software – p.34/64
  • 35. Continue statement Syntax: continue; Semantic: terminates the current iteration of a loop Introduction to System Software – p.35/64
  • 36. Unconditional jump statement Syntax: goto Label; where Label:Statement; belongs to the program Semantic: forces control to go to the Statement; Introduction to System Software – p.36/64
  • 37. Looping statements while-statement do-while statement for-statement Introduction to System Software – p.37/64
  • 38. While statement Syntax: while (expr) Statement; where expr is boolean Semantic: evaluate expr to val; if val is true Statement is execute and while statement is repeated; if val is false control flows to the next instruction of the program Note: true boolean values are any integer different from zero; false boolean value is the integer zero. Introduction to System Software – p.38/64
  • 39. Do-while statement Syntax: do Statement; while (expr); Semantic: equivalent to Statement; while (Expr) Statement; Note: while statement executes zero or more iterations of the loop; do-while statement executes one or more iterations of the loop. Introduction to System Software – p.39/64
  • 40. For statement Syntax: for(expr1; expr2; expr3) Statement; Semantic: equivalent to expr1; while (expr2) { Statement; expr3; } Note: any of the expressions expr1, expr2, expr3 may be omitted; if expr3 is omitted it is interpreted as true, hence various sorts of infinite loops can be performed Introduction to System Software – p.40/64
  • 41. Block statement Syntax: { Declaration list; Statement list; } Declaration list: Declaration; Declaration list Declaration; Statement list: Statement; Statement list Statement; Semantics: statements in Statement list are executed in sequence in the environment provided by Declaration list Introduction to System Software – p.41/64
  • 42. Function definition Syntax: type name (formal parameter list) { Declaration list; Statement list; return result } Semantic: a function definition specifies the computation defined by the Statement list in the environment defined by formal parameter list and Declaration list and return a result of type type Introduction to System Software – p.42/64
  • 43. Example /* power: raises the value of variable base to /* /* the power values of variable n, n >= 0 */ int power (int base, int n) { int i, p; p = 1; for (i = 1; i <= n; i++) p = p * base; return p; } Note: comments in C are enclosed in /* ... */ Use comments outside of func- tion definition; formate function body such that the text indentation allows reader to understand it. Introduction to System Software – p.43/64
  • 44. Function declaration Syntax: type name (type1, type2, ...) where type is the function type (i.e., the type of result returned by the function) and type1, type2, ... are the types of the formal parameters Semantics: declare name as the name of a function whose arguments are of types type1, type2, ... and whose result if of type type Note: since a function declaration is a declaration it must be provided in the declaration list of the statement that uses it. Introduction to System Software – p.44/64
  • 45. Function call Syntax: identifier = name (actual parameters); identifier must have the same type as the type specified in the definition and the declaration of name Actual parameters must expressions whose values are of the types that type1, type2, ... specified in the definition and the declaration of name Semantic: execute computation encapsulated in the definition of function name() in the environment provided by actual parameters and return the result. int x; int power(int, int);...; x = Example: power(2,3); ... Introduction to System Software – p.45/64
  • 46. Parameter passing Actual parameters are passed by value, except arrays, which are passed by reference. Introduction to System Software – p.46/64
  • 47. Remember Arrays are transmitted by reference, i.e., the address of the array variable is transmitted. To operate on the local elements of a function using them as parameters to another function pointers need to be transmitted at function call Initialization of the pointers is required upon function call. Note that pointers are typed i.d., int *x, char *x, struct name *x are different pointers. Introduction to System Software – p.47/64
  • 48. Function memory representation A function is represented in memory by two components: Execution code, i.e., memory image of executable statements Activation record Introduction to System Software – p.48/64
  • 49. Activation record Activation record is a data structure constgructed by the compiler and contains: Function return value; Static link: a pointer to the activation record of the function that contains the definition of the function. In C this is nill. Dynamic link: a pointer to the activation record of the function that contains the call of the function Stack extension value Return address List of locations for actual parameters Local variables of the function Introduction to System Software – p.49/64
  • 50. Structure of a function in memory Figure 3 shows the structure of a function in memory: c c r r Global variables Executable code Activation record Figure 3: Function memory representation Introduction to System Software – p.50/64
  • 51. Structure of a C language program A C program is composed of four componentgs: Macro definitions (optional) Global declarations (optional) Main function (mandatory) Other functions components of the program (optional) Introduction to System Software – p.51/64
  • 52. Note A C program has four components: macro definitions, global declarations, main() function, and other functions. Only main() function is mandatory. A C language program is executed by the operating system by calling its functions main(). A C language program is implicitly declared to the system by the presence of the unique names, main() Introduction to System Software – p.52/64
  • 53. Macro definition component Syntax: sequence of macro-operations of the form: #define name value #include "filename" #include <filename> Semantics: #define name value allows programmer to use name in the program while compiler replaces it with value which can be any string of characters. #include "filename" allows the programmer to develop a program on various separate files. #include <filename> allows the programmer to make use of files contained in various libraries of the system Introduction to System Software – p.53/64
  • 54. Global declarations Syntax: declarations of variables that occur outside of the function components of the program. Semantic: all global variables are accessible to all function components of the program Introduction to System Software – p.54/64
  • 55. Main function of the program Syntax: main (int argc, char *argv[]) { Declaration list; Statement list; } Note:since a function may have no arguments main() { Body } is also valid. Introduction to System Software – p.55/64
  • 56. Program execution A program is executed by the system calling the function main() as consequence of a command given by the programmer. This command has the form %name arg1 arg2,... argc is an integer variable where the number of the arguments used in the execution command is stored argv[] is an array of pointers to strings where the arguments arg1, arg2, ... of the execution command are stored. Introduction to System Software – p.56/64
  • 57. Other function components Syntax: any function definition Semantic: function components of a program may be called by the main() or among themselves. However, in order for main() or any other function to call a function f() the following must be done: f() must have a definition accessible to main() and to other functions that intend to call it f() must be declared in main() and in the functions which intend to call it Introduction to System Software – p.57/64
  • 58. Example program #include <stndio.h> main () { int C; C = gethchar(); while (C != EOF) { putchar(C); C = getchar(); } } This program copies the standard input to the standard out- put Introduction to System Software – p.58/64
  • 59. Bonus point assignment Rewrite th program such that it will copy a file f1 into another file f2; files f1 and f2 should be given in the command line. Introduction to System Software – p.59/64
  • 60. Program memory representation C compiler maps a C program into three segments called data, text and stack as seen in Figure 4 Memory image ¨ ¨¨rrr % ¨¨¨ c rr j r Data Text Stack Figure 4: Memory image of a C program Introduction to System Software – p.60/64
  • 61. Data segment Contains all global data of the program Data segment is constructed by the compiler Introduction to System Software – p.61/64
  • 62. Text segment Contains all executable code of the program Each function component of the Text segment has access to the global data in the Data segment and to the local data in the activation record of that function. Introduction to System Software – p.62/64
  • 63. Stack segment Stack segment is dynamically generated by program execution When a function is called its activation record is pushed on the stack segment When a function return its activation record is popped out from the stack segment Introduction to System Software – p.63/64
  • 64. Development of a C program Use an editor to generate the file that contains the program. Example, execute %vi mylms.c Compile the C program in the file mylms.c using the command % cc [Options] mylms.c If mylms.c contains a C program syntactically correct the result of the compilation is an executable file called a.out. If you want to give the name mylms (or any other name) to the executable use option -o mylms in cc command Test the program on the test data; use dbx to help this Read the documentation for vi, cc, dbx using manual page Introduction to System Software – p.64/64