Post on 30-Dec-2015
2
1st C Program hello.c /* My first C programming */#include<stdio.h>int main(){ printf(“Hello World”); return 0;}
/* 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;}
/* My first C programming */#include<stdio.h>int main(){ printf(“Hello World”); return 0;}
CompileCompile LinkLink Executable fileExecutable file
4
Compiling using gcc $ gcc hello.c //CompileOutput is a.out $ ./a.out //Run ProgramHello World
$ gcc -o hello hello.c //CompileOutput is hello $ ./helloHello World
$ gcc –Wall –o hello hello.c //Compile with warning$ ./helloHello 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;
}
/* 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
$ ./addEnter 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
#include<stdio.h>
int main()
{ int a, b;
printf(“Enter 2 nums 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);
3rd C Program bigger.c
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)
pritnf(“%d is >= to %d\n”,a,b);
return 0;
}
10
Compile and Run
$ gcc –Wall –o bigger bigger.c
$ ./biggerEnter 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;}
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
15
The if statement
:scanf(“%d”, &grade);
if(grade >= 60)
printf(“Passed \n”);
else {
printf(“Failed \n”);
printf(“Repeat Course \n”);
}
:
:scanf(“%d”, &grade);
if(grade >= 60)
printf(“Passed \n”);
else {
printf(“Failed \n”);
printf(“Repeat Course \n”);
}
:
16
:scanf(“%d”, &temp);
if(temp < 0)
setting = setting + 20;
else if(temp < 10)
setting = setting + 10;
else
setting = setting + 1;
:
:scanf(“%d”, &temp);
if(temp < 0)
setting = setting + 20;
else if(temp < 10)
setting = setting + 10;
else
setting = setting + 1;
:
18
switch(int-expression) {
case c1: task-A; break;
case c2: task-B; break;
case c3: task-C; break;
:
}
19
Ex. Counting Letter Grades
/* Counting letter grades */
#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");
:
/* Counting letter grades */
#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");
:
20
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;
}}
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;
}}
21
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;
}
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;
}
22
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
23
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
(for winOS type ctrl+z to exit /
for unix type ctrl+d to exit)
25
/* 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;
}
/* 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;
}
The while statement sum.c
27
#include<stdio.h>
int main()
{ int count;
for(count = 1; count <= 10; count++)
printf("%d \n",count);
return 0;
}
#include<stdio.h>
int main()
{ int count;
for(count = 1; count <= 10; count++)
printf("%d \n",count);
return 0;
}
final value
increment
initialvalue
test forcontinuing
The for statement
28
for(expr1; expr2; expr3)
statement;
is equivalent to:
expr1;
while(expr2) {
statement;
expr3;
}
The for is a sort of while
29
FunctionFUNCTIONS IN CFUNCTIONS IN C
PROGRAMMER DEFINEDFUNCTION
PROGRAMMER DEFINEDFUNCTION
C STANDARDLIBRARY
C STANDARDLIBRARY
INCLUDEHEADER FILE
#include<stdio.h>
INCLUDEHEADER FILE
#include<stdio.h>
FUNCTION PROTOTYPE
int add(int a, int b);
FUNCTION PROTOTYPE
int add(int a, int b);
30
C Standard Library
#include<stdio.h>
#include<math.h>
int main()
{
printf("%f", sqrt(16.0));
return 0;
}
#include<stdio.h>
#include<math.h>
int main()
{
printf("%f", sqrt(16.0));
return 0;
}
header file
Call function
31
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;
}
/* 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
32
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;
}
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
Array
An array is a data structure which can hold a sequence of values.
The array C[ ] holds 3 integer values:
C[1]
-45
6
72C[2]
C[0]
35
Ex. Array/* Initialize array s to the even integers
from 2 to 20 */
#include<stdio.h>#define SIZE 10int main(){ int s[SIZE], j; for(j = 0; j < SIZE; j++) s[j] = 2 + 2 * j; printf("%s%13s \n","Element","Value");
for(j = 0; j < SIZE; j++) printf("%7d%13d \n",j, s[j]); return 0;}
/* Initialize array s to the even integers from 2 to 20 */
#include<stdio.h>#define SIZE 10int main(){ int s[SIZE], j; for(j = 0; j < SIZE; j++) s[j] = 2 + 2 * j; printf("%s%13s \n","Element","Value");
for(j = 0; j < SIZE; j++) printf("%7d%13d \n",j, s[j]); return 0;}
Typical rangeexpression
37
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);
:
/* 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);
:
38
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");:
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");:
39
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;}
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;}
40
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 =%d\n", e*=2);
}
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 =%d\n", e*=2);
}
41
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.
Passing Arrays to Functions
42
Output:
The values of the original array are: 0 1 2 3 4
The values of the modified array are:
0 2 4 6 8
The value of a[3] is 6
Value in modifyElement is 12
The value of a[3] is 6
43
A string is a sequence of characters which end with the NULL character ‘\0’.
e.g.
"Hello"
"234-56789 Call"
"9"
What is a String?
45
String Manipulation Copy a string: strcpy()
Concatenating strings: strcat()
Comparing strings: strcmp()
Searching a string: strchr()
String Length: strlen()
46
#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;
}
#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;
}
Copying a string: strcpy()
47
Output:String in x is Happy Birthday to YouString in y is Happy Birthday to You
Function prototype in string.h: char *strcpy(char *s1,const char *s2);
48
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
49
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; }
#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; }
50
Output:
s1 = Happy New Year
s2 = Happy New Year
s3 = Happy Holidays
strcmp(s1,s2) = 0
strcmp(s1,s3) = 6
strcmp(s3,s1) = -6
51
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];
}
52
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;}