1 / 22

C Language Elements Preprocessor Directives

C Language Elements Preprocessor Directives. # (sign for preprocessor directive commands) #include <standard header file> #include <stdio.h> #include <math> Standard header file (.h) Library. C Language Elements Preprocessor Directives. Constant Macros

Download Presentation

C Language Elements Preprocessor Directives

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. C Language Elements Preprocessor Directives # (sign for preprocessor directive commands) #include <standard header file> #include <stdio.h> #include <math> Standard header file (.h) Library

  2. C Language Elements Preprocessor Directives Constant Macros • Program does not change their values # define Name value #define KILOS_PER_POUND 0.45359 • #define MILES_PER_KM 0.62137 • #define PI 3.141593

  3. C Language Elements Function main • Every C program has main function • Has heading and body {} int main(void) { printf("This is a C program\n"); return (0); } The control returns back to OS Program was executed without error

  4. C Language Elements Reserved Word • They have special meaning and can not be used for something else • All are lowercase some of them are: void case switch return for signed else long If do int while

  5. C Language Elements Identifiers • Standard identifiers: scanf, printf • User-defined identifiers (should begin with a letter and only can contain digit or _ ) Invalid Valid 1rate rate1 int Rate1 joe’s x • Can be uppercase & lowercase • Meaningful name should be used for identifiers • They should not be redefined

  6. C Language Elements Variable and data types • Variables are the name (identifiers) for memory cells that can be changed • It should start with a data type: int count; double x,y,z; char first_initial;

  7. C Language Elements Data types • int: integer between -32767 to 32767 • double: for real numbers 3.14, 0.34 1.23e5, 1.23E5= 1.23 * 10 to the power 5 0.34e-4 = 0.000034 • char : for showing individual character ‘A’, ‘a’, ‘ ’,...

  8. /* Convert the weight from pounds to kilograms */ comment #include <stdio.h> standard header file preprocessor #define KILOS_PER_POUND 0.45359 constant macro directive int reserved word main(void) { double pounds, variable kilos; printf(" Enter the weight in pounds"); standard identifier scanf(“%lf”, &pounds); special symbol kilos = pounds * KILOS_PER_POUND; printf(" That equals %f kilos. \n”, kilos); punctuation return (0); } special symbol

  9. Executable Statements Data can be stored/changed in memory cells by • Assignment statement variable = expression; x= x + z / 6.9; x= -9; • Input operation that required #include <stdio.h>

  10. Output Operation - printf printf( format string, printlist) • printf( “ Hi %c - your age is %d\n”, na,age); • printf (“It is 1th line\n”); printf (“\n and 2th line); • printf(“ Enter the weight in pounds”); scanf( “%lf” , &pounds); printf(“ %f pounds was entered”, pounds);

  11. Input Operation-scanf scanf (format string, input list) • scanf (“%c%d”, &initial, &age); • & is using for each int, double and char variables (it means address of memory) • The order of placeholders correspond to the order of variables in the input list • For numbers the characters are scans until non-digits, blank or cr. For characters only first character before cr is considered

  12. /* This program calculates ? */ #include <stdio.h> #define PI 3.14159 int main(void) { double r,a,c; scanf(“%lf”, r); a= PI * r * r; c= 2 * PI * r; printf( “? is %f cm^2. \n”, a); printf( “? is %f cm. \n”, c); return (0); }

  13. Formating Values of Type int • printf ( “Result is: %3d meters”,… Examples: Value format display 234 %6d 234 234 %1d 234 -234 %6d -234 -234 %1d -234 234 %d 234

  14. Formating Values of Type double • printf ( “Result is: %6.2f meters”,… Examples: Value format display -99.42 %6.1f -99.4 99.999 %6.2f 100.00 -.006 %8.5f -0.00600 -.003 %.3f -0.006 -3.15 %.1f -3.2 99.67 %f 99.67

  15. #include <stdio.h> int main(void) { double x= 123.456; int y=12345; printf( “ %f %.3f %.1f \n”,x,x,x ); printf( “ %3d %5d %8d \n\n”,y,y,y ); return (0); } • 123.456 123.456 123.5 • 12345 12345 12345

  16. Batch Mode Example: weightconvert <data >output include <stdio.h> #define KILOS_PER_POUND 0.45359 int main(void) { double pounds, kilos; scanf(“%lf”, &pounds); printf(“Weight in pounds is %.2f. \n“, pounds); kilos = pounds * KILOS_PER_POUND; printf(" That equals %f kilos. \n”, kilos); return (0); }

  17. Program-controlled Input and Output files • File pointer variable include <stdio.h> #define KILOS_PER_POUND 0.45359 int main(void) { double pounds, kilos; FILE *inp, /* pointer to input file */ *outp; /* pointer to output file */

  18. Os preparing the files for access /* open the input and output files */ inp = fopen (“a:weight.txt”, “r”); outp= fopen (“a:distance.out”, “w”); /* get the input from file */ fscanf(inp,“%lf”, &pounds); fprintf(outp, “Weight in pounds is %.2f. \n“, pounds);

  19. kilos = pounds * KILOS_PER_POUND; /* Display the result in output file */ fprintf(output, “That equals %f kilos. \n”, kilos); /* Close files */ fclose(inp); fclose(outp); return (0); }

  20. Common programming Errors Syntax Error Missing ; or variable definiation • double pounds instead of double pounds, kilos; Last comment is not closed • /* Close files instead of /* Close files */ Correct the errors in declaration part first

  21. Runtime Errors scanf (“%lf” , &radius); scanf (“%c%c%c”, &a, &b, &c); instead of scanf (“%c%c%c”, &a, &b, &c); scanf (“%lf” , &radius); Input 50 ABC  a b c radius \n A B 50

  22. Logic Errors • scanf( “%d%d” , a, b) => incorrect results • Deskchecking • Debugging

More Related