Introduction to - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to

Description:

return-type function-name (argument declarations) declarations; statements; ... int atoi(const char *nPtr) long atol(const char *nPtr) ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 19
Provided by: bento2
Category:

less

Transcript and Presenter's Notes

Title: Introduction to


1
Introduction to
  • C programming

2
C language Functions
  • Functions
  • return-type function-name (argument declarations)
  • declarations
  • statements

3
C language Functions
  • Example - 1

include ltstdio.hgt Int square(int) / function
prototype / main() int i, num /
declaration / for (i1, ilt9 i) i
square(x) int square (int mynum) int y
y mynummynum return y
Type must match
4
C language Functions
  • Example - 1
  • Note
  • This is passed by Value
  • mynum (in function) is a copy of x (in main
    function)
  • Change of mynum value does not change x in main

5
C language Functions
  • Example - 2

include ltstdio.hgt Int max(int, int, int) /
function prototype / main() int a, b, c,
large / declaration / large max (a,
b, c) int square (int x, int y, int z)
int max x if (ygtx) max y if (zgtmax)
max z return max
6
C language Functions
  • Scope - 1
  • The extent to which an identifier can be
    referenced
  • Local to function
  • Local to file
  • Global to entire program
  • Identifier local to function
  • The variable value is visible to this function
    only

int square (int x, int y, int z) int value
5
7
C language Functions
  • Scope - 2
  • Identifier local to file
  • The variable maxval is visible in the file

include ltstdio.hgt int maxval 10 / visible to
file / main() int i, total / local
variable / for(i 0 i lt maxval i)
total i int test (int x) int value
5 if (value lt maxval) printf(value is d,
value)
8
C language Functions
  • Scope - 3
  • Identifier global to program
  • The variable SIZE is visible in the file
  • File 1
  • File 2

include ltstdio.hgt int SIZE 100 / global to
file / main() int value / local
variable /
include ltstdio.hgt extern int SIZE / global to
file / Other functions
9
C language Functions
  • Scope - example

include ltstdio.hgt int value / visible to file
/ static int static_value / visible to file
/ main() int value / local variable
/ large max (a, b, c) int square (int
x, int y, int z) int value 5
10
C language Functions
  • Preprocessor
  • It contains information that will first be
    separately processed before compilation
  • 3 types used frequently
  • include file inclusion
  • e.g.
  • include ltstdio.hgt
  • define macro substitution
  • e.g.
  • define max(A,B) ((A) gt (B) ? (A) (B))
  • if
  • endif conditional inclusion
  • e.g.
  • if !define(NULL)
  • define NULL 0
  • endif

11
C language Functions
  • Recursion
  • A programming method by which a functions calls
    itself
  • Adv A powerful concept
  • Dis can strain a computers memory resources
  • Implementation ideas
  • Divide a problem into a slightly smaller version
    of the original problem
  • The recursive function returns a value when a
    base case is reached
  • E.g.
  • Factorial
  • Quick sort

12
C language Functions
  • Library functions - 1
  • Character handling functions

int isdigit(int c) int isalpha(int c) int
isalnum(int c) int isxdigit(int c) int
islower(int c) int isupper(int c) int tolower(int
c) int toupper(int c) int isspace(int c) int
iscntrl(int c) int ispunct(int c) int isprint(
int c) int isgraph(int c)
13
C language Functions
  • Library functions - 2
  • String conversion functions

double atof(const char nPtr) int atoi(const
char nPtr) long atol(const char nPtr) double
strtod(const char nPtr, char endPtr) long
strtol(const char nPtr, char endPtr, int
base) unsigned long strtoul(const char nPtr,
char endPtr, int base)
14
C language Functions
  • Library functions - 3
  • Standard I/O functions

int getchar (void) char gets(char s) int
putchar(int c) int puts(const char s) int
sprintf(char s, const char format, ...) int
sscanf(char s, const char format, ...)
15
C language Functions
  • Library functions - 4
  • String manipulation functions

char strcpy(char s1, const char s2) char
strncpy(char s1, const char s2, size_t
n) char strcat(char s1, const char s2) char
strncat(char s1, const s2, size_t n)
16
C language Functions
  • Library functions - 5
  • Comparison functions

int strcmp(const char s1, const char s2) int
strncmp(const char s1, const char s2, size_t
n)
17
C language Functions
  • Library functions - 6
  • Search functions

char strchr(const char s, int c) size_t
strcspn(const char s1, const char s2) size_t
strspn(const char s1, const char s2) char
strpbrk(const char s1, cost char s2) char
strrchr(const char s, int c) char
strstr(const char s1, const char s2) char
strtok(char s1, const char s2)
18
C language Functions
  • Library functions - 7
  • Memory functions

void memcpy(void s1, const void s2, size_t
n) void memmove(void s1, const void s2,
size_t n) int memcmp(const void s1, const void
s2, size_t n) void memchr(const void s, int
c, size_t n) void memset(void s, int c, size_t
n)
Write a Comment
User Comments (0)
About PowerShow.com