1 / 16

Today’s Agenda

Today’s Agenda. Structure of C program C Character Set Variables & Constants Data Types Operators Arithmetic Operators Relational Operators Logical Operators Assignment Operators Increment and Decrement Operators. Void indicate that main( ) receives no data from OS.

jael-glenn
Download Presentation

Today’s Agenda

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. Today’s Agenda • Structure of C program • C Character Set • Variables & Constants • Data Types • Operators • Arithmetic Operators • Relational Operators • Logical Operators • Assignment Operators • Increment and Decrement Operators

  2. Void indicate that main( ) receives no data from OS Returns 0 to the OS after execution. Structure of a C Language Program • /* Program to compute …………………………. */ • #include<stdio.h> /*Library files inclusion */ • int main(void) • { • /* local variable declarations */ • /* initialization */ • /* statements */ • return(0); • }

  3. Standard Header File constant Preprocessor directive Variables or User defined identifiers ReservedWords Comments Standard Identifier Simple C Program #include<stdio.h> #define KMS_PER_MILE 1.609 int main (void) { float miles, kms; /* Read distance in miles */ printf(“ Enter distance in miles \n”); scanf (“%f”, &miles); /*Convert the distance to kms*/ kms = KMS_PER_MILES * miles; printf(“ Distance in kms = \t %f ”,kms ) ; // Display the resultant distance in Kms return(0); }

  4. C Character Set • Uppercase letters A to Z , lower case letters a to z, digits 0 to 9 • Special characters. • e.g. + , - , *, &, ?, /, \, #, (, ), {, },[, ], <, >, ==, =, ;, :, ‘, etc • Escape sequences • e.g. \n (new line character), \t (horizontal tab), \v (vertical tab)

  5. Variables & Constants in Programming Language • Variable • It’s a name associated with a memory cell whose value can be changed. • All variables must be declared before they can appear in executable statements • It consists of a data type, followed by one or more variables. • E.g. double miles, kms;

  6. Variables

  7. Variables & Constantsin Programming Language • Constant • It’s a name associated with a memory cell whose value can not be changed. • Declared by #define directive. • E.g. #define KMS_PER_MILE 1.609 • const int x = 300; • const char c = ‘A’;

  8. Data Types • It is a set of values and a set of operations on those values. • Primary Data types • Derived Data types (discussed later) • User defined data types (discussed later)

  9. Primary Data Type

  10. Assignment operator Multiply operator Assignment Statement • It stores a values or a computation result in a variable • It is used to perform arithmetic operations in a program • e.g. kms = KMS_PER_MILES * miles;

  11. Escape sequence placeholder function arguments Function name Format String printlist printf() Function printf(“ Distance in kms = \t %f ”,kms ) ;

  12. Addressof operator Function name Format String Variables scanf() Function scanf ( “%d %d”, &age, &year);

  13. Sample Program Input Enter your marks: 70 Enter average marks: 50.5 Enter your grade: A #include<stdio.h> /* program to display use of I/O and data types */ int main() { int marks; float average; char grade; printf(“Enter your marks:\n”); scanf(“%d”, &marks); printf(“Enter average marks:\n”); scanf(“%f”, &average); printf(“Enter your grade:\n”); scanf(“%c”, &grade); printf(“Marks = %d\n”,marks); printf(“Average = %f\n”,average); printf(“Grade = %c\n”,grade); return (0); } Output Marks = 70 Average = 50.5 Grade = A

  14. Arithmetic Operators

  15. Arithmetic Expression • Let int x = 15; int y = 6;int z; • Example: • z = x+y • z = x-y • z = x*y • z = x/y (decimal part truncated) • z = x%y (remainder of the division) • Q. If x and y are declared as float then? • Q. If x is integer and y is float then?

  16. Example 2 :Program to compute area and circumference of a circle #include<stdio.h> #define PI 3.14 int main(void) { int radius; float area,circum; printf("Enter radius of a circle\n"); scanf("%d", &radius); area = PI * radius * radius; circum = 2 * PI * radius; printf("Area = %f \t Circumference = %f ",area, circum); return(0); }

More Related