Chapter 1 Basic C Programming
-
Upload
joseph-york -
Category
Documents
-
view
222 -
download
1
description
Transcript of Chapter 1 Basic C Programming
Chapter 1 Basic C Programming
1. Introduction to C and UNIX Objectives of these slides: Review C
Program Introduce gcc 1st C Program hello.c /* My first C
programming */
#include int main() { printf(Hello World); return 0; } Source file
Executable file
/* My first C programming */ #include int main() { printf(Hello
World); return 0; } Compile Link Executable file 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 2nd C program
add.c /* Addition program */ #include
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; } Compile and Run $ gcc Wall
o add add.c
$ ./add Enter first integer: 2 Enter second integer: 3 Sum is 5
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 3rd C Program
bigger.c #include 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 if(a > b) printf("%d is > than %d\n",
a, b); if(a = b) printf("%d is >= to %d\n", a, b);return 0; }
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 = 60)
printf(Passed \n); else { printf(Failed \n); printf(Repeat Course
\n); } : : scanf(%d, &temp); if(temp < 0) setting = setting
+ 20; else if(temp < 10) setting = setting + 10; else setting =
setting + 1; : The switch statement For multiple choices: c1 c2 c3
task-A task-B
condition task-A task-B task-C c1 c2 c3 switch(int-expression)
{
case c1: task-A; break; case c2: task-B; break; case c3: task-C;
break; : } Ex. Counting Letter Grades (switch-case)
#include 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 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 printf(\nThe totals for each
lettergrade 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; } 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 theend-of-file value. Defined in stdio.h
Output from letter grades program
Enter the letter grades. Enter EOF to end input.ABC CAXIncorrect
letter grade entered. Enter a new grade.D I typed return and
ctrl>D continued Totals for each letter grade were:
B: 1 C: 2 D: 1 The while statement sum.c
/* sum between 1 and 10 */ #include int main() {int i = 1, sum = 0;
while(i z) return x; else if ( y > z) return y; else return z; }
Execution Enter three integers: Maximum is 85 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 Ex. Array #include
#define SIZE 10 int main()
/* Initialize array s to the even integers from 2 to 20 */ #include
#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
Output: Element Value 02 14 26 :: 920 Functions using Arrays
/* A Program which illustrates the differences between passing an
array to a function and passing an array element */ #include
#define SIZE 5 void modifyArray(int b[], int size); void
modifyElement(int e); : continued 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; iprintf(The values of the
modified array are: \n); for(i=0; ib[j] *= 2; } void
modifyElement(int e) { printf(Value in modifyElement is %d\n,
e*=2); 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. 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 What is a String? A string is a sequence of
characters which end with the NULL character \0. e.g. Hello Call 9
Use: char color[] = blue; Or equivalence : String Manipulation Copy
a string: strcpy()
Concatenating strings: strcat() Comparing strings: strcmp()
Searching a string: strchr() String Length: strlen() Copying a
string: strcpy()
#include #include 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; } 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); Comparing Strings: strcmp()
int strcmp(const char *s1, const char *s2); Compares the string s1
to the string s2 Returns 0ifs1 > s2 Using strcmp() #include
#include
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; } Output: s1 = Happy New Year s2 = Happy
New Year s3 = Happy Holidays
strcmp(s1,s2) = 0 strcmp(s1,s3) = 6 strcmp(s3,s1) = -6 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]; } Searching a
String: strchr()
#include #include int main() {char *rest, *sh = "Hello"; char c =
'l';rest = strchr(sh, c); printf("%s", rest); /* gives llo */
return 0; } Function prototype in string.h:
l o \0 sh rest Function prototype in string.h: char *strchr(const
char *s, int c);