160 likes | 274 Views
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.
E N D
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 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); • }
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); }
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)
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;
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’;
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)
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;
Escape sequence placeholder function arguments Function name Format String printlist printf() Function printf(“ Distance in kms = \t %f ”,kms ) ;
Addressof operator Function name Format String Variables scanf() Function scanf ( “%d %d”, &age, &year);
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
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?
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); }