Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 1 Basic C Programming

Similar presentations


Presentation on theme: "Chapter 1 Basic C Programming"— Presentation transcript:

1 Chapter 1 Basic C Programming
1. Introduction to C and UNIX Objectives of these slides: Review C Program Introduce gcc

2 1st C Program hello.c /* My first C programming */
#include<stdio.h> int main() { printf(“Hello World”); return 0; }

3 Source file Executable file
/* My first C programming */ #include<stdio.h> int main() { printf(“Hello World”); return 0; } Compile Link Executable file

4 Compiling using gcc $ gcc hello.c //Compile  Output is a.out $ ./a.out //Run Program Hello World $ gcc -o hello hello.c //Compile  Output is hello $ ./hello Hello World $ gcc –Wall –o hello hello.c //Compile with warning $ ./hello Hello World output output output

5 2nd C program add.c /* Addition program */ #include <stdio.h>
int main() { int x,y,z; printf(“Enter first integer: ”); scanf(“%d”, &x); printf(“Enter second integer: ”); scanf(“%d”, &y); z = x + y; printf(“Sum is %d \n”, z); return 0; }

6 Compile and Run $ gcc –Wall –o add add.c
$ ./add Enter first integer: 2 Enter second integer: 3 Sum is 5

7 Memory as Many Boxes Each box is labelled with an identifier; it is the name of the box. e.g x, y, z The address of a box is & and its name e.g &x, &y, &z; Boxes are typed (e.g. int, float, etc.). This specifies their size and shape Boxes contain values (e.g. 5, 3.4) C Programs Mess With Boxes

8 3rd C Program bigger.c #include <stdio.h> int main()
{ int a, b; printf(“Enter two integers, and I will tell you the relationships they satisfy: ”); scanf(“%d%d”, &a, &b); if(a == b) printf("%d is equal to %d\n", a, b); if(a != b) printf("%d is not equal to %d\n", a, b); if(a < b) printf("%d is < than %d\n", a, b); continued

9 if(a > b) printf("%d is > than %d\n", a, b); if(a <= b) printf("%d is <= to %d\n", a, b); if(a >= b) printf("%d is >= to %d\n", a, b); return 0; }

10 Compile and Run $ gcc –Wall –o bigger bigger.c
$ ./bigger Enter two integers, and I will tell you the relationships they satisfy: 3 7 3 is not equal to 7 3 is < than 7 3 is <= to 7

11 A C Program ‘Skeleton’ /* Name, student no. (E-mail addr.) Date
Description of Code (5-10 lines) */ #include<stdio.h> int main() { /* declare some variables */ /* do something */ return 0; }

12 Compile and Execute: $ gcc –Wall –o skeleton skeleton.c $ ./skeleton

13 How to Start Writing a Program
Program = Algorithm + Data Structures Algorithm: a series of functions, each using a mix of sequencing, branches, loops, recursion and other functions Data Structures: they reflect the structure of the data in the Problem Statement e.g int, float, arrays, struct, etc

14 Consider the input–output requirements
Dissect (‘Pull apart’) the Problem Statement Use top-down design (also called stepwise refinement)

15 Control Flow Sequence Choice if-else switch-case Loop for while
do-while Recursion

16 The if statement : scanf(“%d”, &grade); if(grade >= 60)
printf(“Passed \n”); else { printf(“Failed \n”); printf(“Repeat Course \n”); } :

17 : scanf(“%d”, &temp); if(temp < 0) setting = setting + 20; else if(temp < 10) setting = setting + 10; else setting = setting + 1; :

18 The switch statement For multiple choices: c1 c2 c3 task-A task-B
condition task-A task-B task-C c1 c2 c3

19 switch(int-expression) {
case c1: task-A; break; case c2: task-B; break; case c3: task-C; break; : }

20 Ex. Counting Letter Grades (switch-case)
#include<stdio.h> int main() { int grade; int acount = 0, bcount = 0, ccount = 0, dcount = 0, fcount = 0; printf(“Enter the letter grades.\n”); printf(“Enter EOF to end.\n”); : continued

21 while((grade = getchar()) != EOF) {
switch(grade) { case ‘A’: case ‘a’: ++acount; break; case ‘B’: case ‘b’: ++bcount; break; case ‘C’: case ‘c’: ++ccount; break; case ‘D’: case ‘d’: ++dcount; break; case ‘F’: case ‘f’: ++fcount; break; case ‘\n’: case ‘ ‘: break; default: printf(“Incorrect letter grade entered\n Enter a new grade\n”); break; } continued

22 printf(“\nThe totals for each letter grade are: \n”);
printf(“A: %d \n”, acount); printf(“B: %d \n”, bcount); printf(“C: %d \n”, ccount); printf(“D: %d \n”, dcount); printf(“F: %d \n”, fcount); return 0; }

23 Some Points Very common coding style:
while((grade = getchar()) != EOF) getchar() reads the next character as an integer and assigns it to grade EOF is an integer constant representing the end-of-file value. Defined in stdio.h

24 Output from letter grades program
Enter the letter grades. Enter EOF to end input. A B C C A X Incorrect letter grade entered. Enter a new grade. D I typed return and ctrl>D continued

25 Totals for each letter grade were:
B: 1 C: 2 D: 1

26 The while statement sum.c
/* sum between 1 and 10 */ #include<stdio.h> int main() { int i = 1, sum = 0; while(i <= 10) { sum = sum + i; i = i + 1; } printf(“Sum is %d \n”, sum); return 0;

27 Compile and run $ gcc -Wall –o sum sum.c $ ./sum Sum is 55

28 The for statement int main() test for { int count; continuing initial
#include<stdio.h> int main() { int count; for(count = 1; count <= 10; count++) printf(“%d \n”, count); return 0; } test for continuing initial value final value increment

29 The for is a sort of while
for(expr1; expr2; expr3) statement; is equivalent to: expr1; while(expr2) { expr3; }

30 #include<stdio.h> FUNCTION PROTOTYPE int add(int a, int b);
FUNCTIONS IN C PROGRAMMER DEFINED FUNCTION C STANDARD LIBRARY INCLUDE HEADER FILE #include<stdio.h> FUNCTION PROTOTYPE int add(int a, int b);

31 C Standard Library #include<stdio.h> #include<math.h>
int main() { printf("%f", sqrt(16.0)); return 0; } header file Call function

32 Programmer defined function
/* Finding the maximum of three integers */ #include <stdio.h> int maximum(int x, int y, int z); int main() { int a, b, c; printf(“Enter three integers: “); scanf(“%d%d%d”,&a,&b,&c); printf(“Maximum is %d\n”, maximum(a,b,c)); return 0; } function prototype Call function continued

33 Function definition int maximum(int x, int y, int z)
/*returns the biggest of x, y and z */ { if(x > y && x > z) return x; else if ( y > z) return y; else return z; }

34 Execution Enter three integers: Maximum is 85

35 Array An array is a data structure which can hold a sequence of values. The array C[ ] holds 3 integer values: c[0] c[1] c[2] -45 6 72

36 Ex. Array #include<stdio.h> #define SIZE 10 int main()
/* Initialize array s to the even integers from 2 to 20 */ #include<stdio.h> #define SIZE 10 int main() { int s[SIZE], j; for(j = 0; j < SIZE; j++) s[j] = * j; printf(“%s%13s \n”,”Element”, “Value”); printf(“%7d%13d \n”,j, s[j]); return 0; } Typical range expression

37 Output: Element Value 0 2 1 4 2 6 : : 9 20

38 Functions using Arrays
/* A Program which illustrates the differences between passing an array to a function and passing an array element */ #include<stdio.h> #define SIZE 5 void modifyArray(int b[], int size); void modifyElement(int e); : continued

39 printf(“The values of the original array are: \n”);
int main() { int a[SIZE] = {0,1,2,3,4}; int i; printf(“The values of the original array are: \n”); for(i = 0; i<SIZE; i++) printf(“%3d”, a[i]); printf(“\n”); : continued

40 continued modifyArray(a, SIZE);
printf(“The values of the modified array are: \n”); for(i=0; i<SIZE; i++) printf(“%3d”, a[i]); printf(“\n”); printf(“The value of a[3] is %d\n”,a[3]); modifyElement(a[3]); printf(“The value of a[3] is %d\n”, a[3]); return 0; } continued

41 void modifyArray(int b[], int size) { int j; for(j=0; j<size; j++)
b[j] *= 2; } void modifyElement(int e) { printf(“Value in modifyElement is %d\n”, e*=2);

42 Passing Arrays to Functions
The call to the function modifyArray() is: modifyArray(a, SIZE); C treats array arguments differently . Any changes to the elements of a[] will be retained when the function returns. This is call by reference.

43 Output: The values of the original array are: 0 1 2 3 4
The values of the modified array are: The value of a[3] is 6 Value in modifyElement is 12

44 What is a String? A string is a sequence of characters which end with the NULL character ‘\0’. e.g. “Hello” “ Call” “9”

45 Use: char color[] = “blue”; Or equivalence :

46 String Manipulation Copy a string: strcpy()
Concatenating strings: strcat() Comparing strings: strcmp() Searching a string: strchr() String Length: strlen()

47 Copying a string: strcpy()
#include<stdio.h> #include<string.h> int main() { char x[]=“Happy birthday to you”; char y[25]; printf(“String in x is: %s\n String in y is: %s\n”, x, strcpy(y,x)); return 0; }

48 Output: Function prototype in string.h:
String in x is Happy Birthday to You String in y is Happy Birthday to You Function prototype in string.h: char *strcpy(char *s1, const char *s2);

49 Comparing Strings: strcmp()
int strcmp(const char *s1, const char *s2); Compares the string s1 to the string s2 Returns <0 if s1 < s2 0 if s1 == s2 >0 if s1 > s2

50 Using strcmp() #include<stdio.h> #include<string.h>
int main() { char s1[] = “Happy New Year”; char s2[] = “Happy New Year”; char s3[] = “Happy Holidays”; printf(“%s%s\n %s%s\n %s%s\n %s%2d\n %s%2d\n %s%2d\n”, “s1 = ”,s1,“s2 = ”,s2,“s3 = ”,s3, “strcmp(s1,s2) = ”,strcmp(s1,s2), “strcmp(s1,s3) = ”,strcmp(s1,s3), “strcmp(s3,s1) = ”,strcmp(s3,s1), return 0; }

51 Output: s1 = Happy New Year s2 = Happy New Year s3 = Happy Holidays
strcmp(s1,s2) = 0 strcmp(s1,s3) = 6 strcmp(s3,s1) = -6

52 Coding strcmp() The array version:
int strcmp(const char *s, const char *t) { int i; for(i=0; s[i] == t[i]; i++) if(s[i] == ‘\0’) return 0; return s[i] – t[i]; }

53 Searching a String: strchr()
#include <stdio.h> #include <string.h> int main() { char *rest, *sh = "Hello"; char c = 'l'; rest = strchr(sh, c); printf("%s", rest); /* gives llo */ return 0; }

54 Function prototype in string.h:
‘l’ ‘o’ ‘\0’ sh rest Function prototype in string.h: char *strchr(const char *s, int c);


Download ppt "Chapter 1 Basic C Programming"

Similar presentations


Ads by Google