C Programming Tutorial by Examples - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

C Programming Tutorial by Examples

Description:

Rules for Making an User Define Identifier/Variable name: ... An identifier defined in a C standard library (e.g ... The general form for function definition: ... – PowerPoint PPT presentation

Number of Views:6956
Avg rating:5.0/5.0
Slides: 26
Provided by: ics78
Category:

less

Transcript and Presenter's Notes

Title: C Programming Tutorial by Examples


1
C Programming Tutorial by Examples
  • Dr. Kalim Qureshi

2
Introduction
  • C is a system programming language.
  • C programs are comparable in efficiency to
    assembly language programs.
  • The source text of a C program can be distributed
    among several files, each of which can be
    compiled independently.
  • Full type checking is not possible.
  • The predefined data types are int, char and float
    together with short and long and double.
  • User-defined types are enumeration types, arrays
    and structure

3
Structure of C Program
  • Preprocessor directives
  • main()
  • Variable declarations
  • Statements
  • A simple C Program 1
  • main ()
  • printf (hello, world)

4
Sample Program 2
  • include ltstdio.hgt //preprocessor directive
  • define PI 3.14159 / Directive for creating
    constant /
  • int main (void) //main function
  • double radius, area circum //variable
    declaration
  • / get the circle radius /
  • print( Enter radius )
  • scanf (lf, radius)
  • / Calculate the area /
  • area PI radius radius
  • / calculate the circumference /
  • circum 2 PI radius
  • / Display the area and circumference /
  • printf (The area is f\n, area)
  • printf (The circumference is f\n, circum)
  • Enter radius 5.0
  • The area is 78.539750
  • The circumference is 31.415900

5
Preprocessor Directives
  • Syntax
  • includeltstandard header filegt
  • e.g.
  • includeltstdio.hgt
  • includeltmath.hgt
  • Directives for creating constant
  • Syntax
  • define NAME value
  • e.g.
  • define PI 3.141593
  • define MILES_PER_KM 0.62137

6
Sample Program 3
  • To find addition of two numbers
  • include ltstdio.hgt //preprocessor directive
  • main () //main function
  • int number1, number2, sum //variable
    declarations
  • printf (Please input two numbers )
  • scanf (d d, number1, number2)
  • sum number1 number2
  • printf(The sum of two numbers is d, sum)

7
Sample Program 4
  • To displays the users nickname and current year
    in a welcoming message
  • include ltstdio.hgt
  • int main (void)
  • char letter_1, letter_2, letter_3, letter_4,
    letter_5
  • int year
  • printf(Enter a 5-letter name and press return
    )
  • Scanf(c c c c c, letter_1, letter_2,
    letter_3, letter_4, letter_5)
  • printf (Enter the current year and press
    return)
  • scanf (d, year)
  • Printf(Welcome, ccccc . d is a great year
    to study C!, letter_1, letter_2, letter_3,
    letter_4, letter_5)
  • return (0)
  • Enter a 5-letter name and press return Kalim
  • Enter the current year and press return 2004
  • Welcome, Kalim . 2004 is a great year to study C!

8
Reserved Key Words
  • All reserved keywords in C language appears in
    lower case they have special meaning in C and
    cannot be used for any other purposes at any
    place.
  • E.g. int, char, void, double, return etc.
  • Syntax for variable declaration
  • int varible_list
  • double varible_list
  • char varible_list
  • float varible_list

9
Rules for Making an User Define
Identifier/Variable name
  • An identifier name can not begin with a digit.
  • An identifier name must consist only of letters
    digits, or underscores.
  • A C reserved key word (e.g. return, double, int
    etc.) cannot be used as an identifier name.
  • An identifier defined in a C standard library
    (e.g printf, scanf) should not be redefined.
  • For example 4 name, int, two-by-four, scanf are
    invalid identifier name.

10
scanf and printf functions
  • Syntax
  • scanf ( format, input list separated by coma)
  • e.g. scanf (d c age, first_initia)
  • Syntax
  • printf (format, print list)
  • e.g. printf(I am d years old, and my GPA is
    f\n, age, gpa)
  • char c
  • int d
  • float f
  • double lf

11
Function with Input Parameter /Argument
  • The general form for function definition
  • function_return_type function name (parameter
    list separated by commas )
  • Example int sum (int x, int y)
  • void abc (void)
  • void abc (int x, float y, double d, int a)
  • int abc (int b, double k, char p)
  • float abc (int g, char h, double r)

12
Function with Input Parameter /Argument
  • / Function with Input Arguments and a Single
    Result /
  • includeltstdio.hgt
  • void print_rboxed(double rnum) // User defined
    function
  • printf("\n")
  • printf(" \n")
  • printf(" 7.2f \n", rnum)
  • printf(" \n")
  • printf("\n")
  • // end of print_rboxed user defined function
  •  
  • void main( ) // calling function
  • actual
    parameter
  • print_rboxed(2.24567) // function call
  •  
  • // end of main

13
/ Function to find larger of two numbers /
  • includeltstdio.hgt
  •  
  • double bigger(double n1, double n2) // function
    definition
  • double larger // variable declaration
  •  
  • if(n1gtn2)
  • largern1
  • else
  • largern2
  • return (larger) // returns value of larger to
    calling plaice in main function
  •  
  • // end of bigger function
  • void main( )
  •  
  • double number1,number2, max // variable
    declaration
  • printf("Please input two numbers ")
  • scanf("lf lf", number1, number2) // input
  •  
  • max bigger(number1, number2) // function call
  •  
  • printf("The max of lf and lf is
    lf",number1,number2,max)
  •  
  • // end of calling main function

14
Pointer
  • Pointer variable is a special variable which
    stores the address of other variable. If a
    pointer variable stores the address of a char
    variable , we call it a character pointer and so
    on.
  • To handle pointers in C language we use two unary
    operators ,
  • Address operator (ampersand symbol)
  • Pointer operator OR Indirection
    operator OR Value at address.
  • Examples
  •  
  • int j --------? Means the value at address
    contained in j is an int OR in other words j is
    an integer pointer.
  • int k ------? Means the value at address
    contained in k is an int.

15
Pointer (Cont. )
  • Example1
  • includeltstdio.hgt
  • char gz
  • int main(void)
  • char ca
  • char ppc
  • printfc\n,p)
  • pg
  • printf(c\n,p)
  • return 0

16
Pointer (Cont.)
  • includeltstdio.hgt
  • void myfunction(int a, int b, int c, int sum,
    int prod, float average) // function
  • sumabc
  • prod abc
  • average(abc)/3.0
  • // end of function
  • int main (void) // main program or main function
  • int x,y,z,su_m,product
  • float av_g
  • printf("Enter three integer input parameters a,
    b and c ")
  • scanf("d d d",x,y,z)
  •  
  • myfunction(x, y, z, su_m, product, av_g) //
    function call
  •  
  • printf("
    ")

17
Array
  • define SIZE1 10
  • define SIZE2 100
  • int arriSIZE1
  • float arrfSIZE2
  • Example
  • double abc50
  • int n
  • for (n 0 n lt 50 n)
  • printf (The dth element of array
    is lf\n, n, abcn)
  •  Array Initialization
  • int vector 5 12, 67, -56, 89, 1
  • float xyz 8 1.5, 7.8, 5.6, -9.55, 0.5,
    8.4
  • char ccc 4 r, 4, , s
  • int sss 12, 45, 78

18
Array with Function
  • printf("\nthe sum of first three array elements
    is d", total)
  • // end of main
  • int sum_elements( int a, int b, int c ) //
    Function header
  • int sum
  • sumabc
  • return (sum)
  • includeltstdio.hgt
  • int sum_elements(int a , int b, int c) //
    Function Prototype
  • void main()
  • int x10, total
  • x010 x120 x230
  • total sum_elements( x0, x1, x2 )
    //Function call to compute sum
  • printf("For the array values d d d\n", x0,
    x1, x2 ) // Output results

19
How to pass whole array as a parameter in
Function
  • Unlike simple variables, array is not passed into
    a function, but rather its address is passed.
  • This is done by specifying the array name
    followed by brackets (size is not necessary).

20
How to pass whole array as a parameter in Function
  • include ltstdio.hgt
  • define SIZE 4 // defining the size of the array
    as 4 .
  • int max (int arr ) // declaring the
    function max which returns integer value and
    accepts an
  • // integer
    array called arr
  •  
  • void main (void)
  • int arr SIZE , j , big //declaring the array
    arr along with other variables
  •  
  • for ( j 0 j lt SIZE j ) //using for loop
    to read the array
  • printf ("Enter the dth element of array ",
    j )
  • scanf ( "d", arrj )
  • // end of for
  •  
  • big max ( arr ) //calling the function max
    by passing the array with only name
  • printf ("\nThe maximum value is d\n", big )
  •  
  • // end of main

21
How to pass whole array as a parameter in Function
  • int max ( int arr ) /function declaring that
    it accepts integer array arr as its input/
  • int j , big 0
  • for ( j 0 j lt SIZE j ) /using for loop
    to find the maximum number of array arr/
  • if ( arrj gt big )
  • big arrj
  •  
  • // end of for loop
  • return (big) //returning the maximum value
  •  
  • //end of max function

22
Structure
  • The struct (structure) specifier is used to
    declare a data structure that can store
    heterogeneous data elements.
  • The elements can be of any type including
    enumerated types, arrays and even other
    structures.
  • E.g
  • struct user_record
  • int id_no
  • char name40
  • char dept40
  • int no_of_books
  • A variable with the above structure can be
    declared as
  • struct user_record user

23
Structure (Cont.)
  • Typedef
  • Look at the following statement
  • Typedef long double F
  •  
  • Once this is done we can use F wherever we intend
    to use long double.
  •  
  • E.g F a,b,c

24
Structure (Cont. )
  • We can simplify the variable declaration above,
    if we use typedef in the declaration. The above
    can then be defined as
  • E.g typedef struct
  • int id_no
  • char name40
  • char dept40
  • int no_of_books
  • USER_RECORD
  •  
  • The variable declaration then becomes
  • USER_RECORD user
  • Once a variable is declared, we can access the
    individual elements (called fields) of the record
    using the dot (.) operator.

25
Structure (Conti)
  • Example
  • include ltstdio.hgt
  • main()
  • struct date
  • int month
  • int day
  • int year
  • struct date today
  • today.month 9
  • today.day 25
  • today.year 1988
  • printf (Todays date is d/d/d.\n,
    today.month, today.day, today.year )
  • Output Todays date is 9/25/1988.
Write a Comment
User Comments (0)
About PowerShow.com