28
Tutorial ICS431 – Introduction to C

Tutorial ICS431 – Introduction to C. Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius

Embed Size (px)

Citation preview

Tutorial

ICS431 – Introduction to C

Example ( 1 )

int main(){

float radius, area;

printf(“Enter the radius : “);scanf(“%f”, &radius);area = radius * radius * 3.1415;printf(“area = %f\n”, area);

return 0;}

Formatting Specifier

(ٍSpecifierFormat

%fFloat

%d or %iInteger

%ldLong integer

%lfDouble

%cChar

%sString

%pAddress

Again: Example ( 1 )

#define PI 3.14159

int main(){

float radius, area;

printf(“Enter the radius : “);scanf(“%f”, &radius);area = radius * radius * PI;printf(“area = %f\n”, area);

return 0;}

Example ( 2 )

int main(){

int std_id;float grade;

printf(“Enter student ID: “); scanf(“%d”, &std_id);printf(“Enter his grade: “); scanf(“%f”, &grade);printf(“Student %d got “, std_id);if (grade >= 95.0) printf(“A+\n”);else if (grade >= 85.0) printf(“A\n”);else if (grade >= 75.0) printf(“B+\n”);else if (grade >= 70.0) printf(“B\n”);else if (grade >= 65.0) printf(“C\n”);else if (grade >= 60.0) printf(“D+\n”);else if (grade >= 50.0) printf(“D\n”);else printf(“F\n”);

}

Exercise ( 1 )

Write C code to solve the quadratic equation ax^2 + bx + c = 0.

- Consider only the real solutions.

- Check the input’s validity

- Make you program friendly interface.

- Square root function is sqrt().

Solution ( 1 )

int main(){

float a, b, c;float d, x1, x2;

printf(“Enter the coefficient of x^2: “); scanf(“%f”, &a);printf(“Enter the coefficient of x: “); scanf(“%f”, &b);printf(“Enter the constant value: “); scanf(“%f”, &c);

if (a == 0 && b == 0)printf(“Invalid data: no equation\n”);

else if (a == 0) {

x1 = -c / b;printf(“First order equation. x = %.2f\n”, x1);

}

Solution ( 1 )

else {d = b*b – 4*a*c;if (d < 0)

printf(“No real solution\n”);else if (d == 0) {

x1 = -b / (2*a);printf(“Single solution. x = %.2f\n”, x1);

}else {

x1 = (-b + sqrt(d)) / (2*a);x2 = (-b – sqrt(d)) / (2*a);printf(“ x[1] = %.2f\n”, x1);printf(“ x[2] = %.2f\n”, x2);

}}return 0;

}

Example ( 3 )

int main(){

int month;

printf(“Month number: “); scanf(“%d”, &month);printf(“Number of days is “);switch (month) {

case 1: case 3: case 5: case 7:case 8: case 10: case 12: printf(“31\n”);break;case 4: case 6: case 9: case 11: printf(“30\n”);break;case 2: printf(“28 or 29\n”); break;default: printf(“\nInvalid number\n”);

}return 0;

}

Example ( 4 )

int main(){

int number;int i;

printf(“Enter an integer number: “); scanf(“%d”, &number);printf(“The positive divisor(s) of %d is(are): “);for (i=2; i<number/2; i++)

if (number % i == 0) printf(“%d”, i);

return 0;}

Boolean type

C does not support boolean data type.

- However, boolean variables (in C) are integer variables.

- ZERO value means FALSE.

- NON-ZERO value means TRUE.

Exercise ( 2 )

Write a C code to determine if a given number is prime or not.

- You can use break or continue within the loop body.

Solution ( 2 )

int main(){

int number, i;int is_prime = 1; /* boolean variable */

printf(“Enter a number: “); scanf(“%d”, &number);for (i=2; i<number/2; i++)

if (number % i == 0) {is_prime = 0;break;

}if (is_prime)

printf(“%d is a prime number\n”, number);else

printf(“%d is not a prime number\n”, number);return 0;

}

Example ( 4 )

int is_prime (int number){

int flag = 1, i;

for (i=2; i<number/2; i++)if (number % i == 0) {

flag = 0;break;

}return flag;

}

int main(){

int number;

printf(“Enter a number: “); scanf(“%d”, &number);if (is_prime(number))

printf(“%d is a prime number\n”);else

printf(“%d is not a prime number\n”);return 0;

}

Declare before Use

Example ( 5a )

int main(){

int number;

printf(“Enter a number: “); scanf(“%d”, &number);if (is_prime(number))

printf(“%d is a prime number\n”);else

printf(“%d is not a prime number\n”);return 0;

}

int is_prime (int number){

int flag = 1, i;

for (i=2; i<number/2; i++)if (number % i == 0) {

flag = 0;break;

}return flag;

}

Example ( 5b )

#include <stdio.h>

int is_prime (int);

int main(){

int number;

printf(“Enter a number: “); scanf(“%d”, &number);if (is_prime(number))

printf(“%d is a prime number\n”);else

printf(“%d is not a prime number\n”);return 0;

}

int is_prime (int number){

int flag = 1, i;

for (i=2; i<number/2; i++)if (number % i == 0) {

flag = 0;break;

}return flag;

}

Global vs Local

• SCOPE

• LIFE TIME

• LINKAGE

SCOPE

• Variable declared outside any function has global scope. Global variable

• Variable declared inside a function has local scope (within the function body). local variable

• Global variable with static modifier has scope within the file only.

LIFE TIME

• Global variable has global life-time

• Local variable has local life-time

• Local variable with static modifier has global life-time.

LINKAGE

• Internal linkage

• External linkage

Files

• SCOPE

• LIFE TIME

• LINKAGE

Standard files

• Any C program has THREE standard files– stdin (refer to keyboard)– stdout (refer to terminal window)– stderr (refer to terminal window)

• These files are declared in stdio.h

• fprintf() is used to print to a file

• fscanf() is used to read from a file

Standard files

• printf(“Area = %d\n”, area);

• fprintf(stdout, “Area = %d\n”, area);

• scanf(“%f”, &radius);

• fscanf(stdin, “%f”, &radius);

User files

FILE *myfile;

myfile = fopen(“data.dat”, “w”);

printf(myfile, “Hello world!\n”);

Integer Representation

• Integer values can be represented:– As decimal.

12 100 188– As octal.

014 0144 0274– As hexadecimal.

0xC 0x64 0xbc

char

• The data type char means 1-byte integer.

• Characters are represented as 1-byte.

unsigned modifier

• unsigned char

• unsigned int OR unsigned

• unsigned long