Download presentation
Presentation is loading. Please wait.
1
Tutorial ICS431 – Introduction to C
2
Example ( 1 ) int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius * radius * ; printf(“area = %f\n”, area); return 0; }
3
Formatting Specifier Format ٍSpecifier Float %f Integer %d or %i
Long integer %ld Double %lf Char %c String %s Address %p
4
Again: Example ( 1 ) #define PI int main() { float radius, area; printf(“Enter the radius : “); scanf(“%f”, &radius); area = radius * radius * PI; printf(“area = %f\n”, area); return 0; }
5
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”); }
6
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().
7
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); }
8
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); } 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;
9
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”); case 2: printf(“28 or 29\n”); default: printf(“\nInvalid number\n”); } return 0;
10
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; }
11
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.
12
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.
13
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;
14
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;
15
Declare before Use
16
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;
17
#include <stdio.h> int is_prime (int);
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;
18
SCOPE LIFE TIME LINKAGE
Global vs Local SCOPE LIFE TIME LINKAGE
19
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.
20
LIFE TIME Global variable has global life-time
Local variable has local life-time Local variable with static modifier has global life-time.
21
LINKAGE Internal linkage External linkage
22
SCOPE LIFE TIME LINKAGE
Files SCOPE LIFE TIME LINKAGE
23
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
24
Standard files printf(“Area = %d\n”, area);
fprintf(stdout, “Area = %d\n”, area); scanf(“%f”, &radius); fscanf(stdin, “%f”, &radius);
25
User files FILE *myfile; myfile = fopen(“data.dat”, “w”);
printf(myfile, “Hello world!\n”);
26
Integer Representation
Integer values can be represented: As decimal. As octal. As hexadecimal. 0xC 0x64 0xbc
27
char The data type char means 1-byte integer.
Characters are represented as 1-byte.
28
unsigned modifier unsigned char unsigned int OR unsigned unsigned long
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.