1 / 14

Understanding Basic C Programming Concepts

Learn about variables, declarations, data types, arithmetic expressions, formatting numbers in C language, with exercises and examples.

smincey
Download Presentation

Understanding Basic C Programming Concepts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lab 5 • C language Elements • Variables Declarations and Data Types • General Form of a C Program • Arithmetic Expressions • Formatting Numbers • Exercises Note: Look at String.h (Appendix B AP16), time.h (Appendix B AP16). Be Careful Read Chapter 4(All Self-Check exercises ). C KEYWORDS and format must be learned by heart

  2. Hello, World • /* Hello, world program */ • #include <stdio.h> • int main(void) • { • printf("hello, "); • printf("world\n"); • return 0; • }

  3. Basic Programming Concepts • Comment To explain what the program is for and why it is the way it is. • Preprocessing Done before the program is compiled. • To include header files containing information about C libraries • Statement A line of code that tells computer to perform some action. Always ended with a semicolon(;). • Function A module often consisting of a set of statements. • Two functions in the hello world program: main, printf

  4. Arithmetic Operators • To form expressions • Many statements are merely expressions. • Normal order of operations is followed. Parentheses can be used.

  5. Arithmetic Operators: An Example • /* Arithmetic operators */ • #include <stdio.h> • int main(void) • { • printf("7+3=%d\n",7+3); • printf("7-3=%d\n",7-3); • printf("7*3=%d\n",7*3); • printf("7/3=%d\n",7/3); • printf("7%%3=%d\n",7%3); • printf("7.0/3.0=%f\n",7.0/3.0); • return 0; • }

  6. Variables • Variable Name for a memory object. Variable names must start with letters and contain letters, digits, and underscores. • a, b, i, j, counter, number_of_points, c1234, … • Type What the variable represents. Could be of integer, floating point number, character, string, and many others. • int, float, double, char, … • Declaration Tells compiler about variables and their types. • int i,j; • float sum;

  7. Numeric Types

  8. Rules of Evaluating Expressions A-Parentheses rules: all expressions in parentheses must be evaluated separately. Nested parenthesized expressions must be evaluated from the inside out. B-Operator Precedence: unary+, - *, /,% binary +,- C-Associativity rule: Unary operators are evaluated right to left. Binary operators are evaluated left to right. Exp: x*y*z +a /b –c*d area=PI*radius*radius

  9. Reading Inputs • #include <stdio.h> • int main(void) • { • float a,b; • printf("Please input the first number:\n"); • scanf("%f",&a); • printf("Please input the second number:\n"); • scanf("%f",&b); • printf("a+b=%f\n",a+b); • printf("a-b=%f\n",a-b); • printf("a*b=%f\n",a*b); • printf("a/b=%f\n",a/b); • return 0; • }

  10. Assignment • /* Arithmetic operators */ • #include <stdio.h> • int main() • { • int a,c; • int b=4; • a=3; • c=a+b; • printf(“Sum: %d + %d -> %d\n”,a,b,c); • a++;b--; • printf(“Now a=%d and b=%d\n”,a,b); • return 0; • }

  11. Printf Function • printf(format string, print list) • Exp: printf(“I am %d years old and my gpa is %f \n”,age,gpa); • Placeholder: is a symbol beginning with % that indicates where and how to display the output value. (%c char, %d int, %f float and %lf for double). • Exr1: if letter_1 holds ‘E’, letter_2 holds ‘A’ (they are of type char) and age of type int holds 24, what should be displayed by the following: printf(“Hi %c%c- your age is %d \n”, letter_1 , letter_2, age);

  12. Scanf Function • scanf (format string, input list) • Exp: scanf (“%c%d”,&first_initial,&age); • Each input is preceded by ampersand &. Commas are used to separate variable names. The order of the placeholders must correspond to the order of variables in the input list. • Exr2:Write a C program that asks the user to enter the radius of a circle and then displays the area and the circumference of the circle.PI is the constant macro 3.14159 (use #define)

  13. Formatting numbers Format Value Displayed Output %6d 234 %1d 234 %6d -234 %1d -234 %5.1f 3.14159 %.4 -3.14159

  14. Other Exercises • The New-Wave Computer Company sells its product, the NW-PC for 7000 Dhs. In addition, it sells memory extension cards for 750 Dhs, disk drives for 2000 Dhs, and software for 350 Dhs. Given the number of memory cards, disk drives, and software packages desired by a customer purchasing an NW-PC, write a C to print out a bill of sale. • We would like to develop an algorithm that produces a student’s interim reports for a computer science class. To solve this problem you need to know the following: • There are 3 tests, each worth 10% • All the Quizzes are worth 10% • Projects and assignments are worth 30% • The Final Exam is worth 30% Write a C program that given the grades of all of the above components calculates the final average grade.

More Related