Presentation is loading. Please wait.

Presentation is loading. Please wait.

A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive.

Similar presentations


Presentation on theme: "A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive."— Presentation transcript:

1 A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive Mode, Batch Mode and Program Redirection

2 A.Abhari CPS1252 C Language Elements Preprocessor Directives # (sign for preprocessor directive commands) #include Standard header file (.h) Library

3 A.Abhari CPS1253 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

4 A.Abhari CPS1254 C Language Elements Function main Every C program has main function Main function has heading and body {} A function body has declarations and executable statements int main(void) { printf("This is a C program\n"); return (0); } The control returns back to OS Program was executed without error

5 A.Abhari CPS1255 C Language Elements Reserved Words They have special meaning and can not be used for something else All are lowercase some of the reserved words are: void case switch return for signed else long if do int while

6 A.Abhari CPS1256 C Language Elements Identifiers Standard identifiers: For example scanf or printf are the names of defined operations User-defined identifiers (should begin with a letter and only can contain digit or _ ) Invalid Valid 1rate rate1 int Rate1 joe’s joe Uppercase & lowercase User-defined identifiers are considered different identifiers by C compiler Meaningful name should be used for User- defined identifiers It is a reserved word Different identifiers

7 A.Abhari CPS1257 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;

8 A.Abhari CPS1258 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’, ‘ ’,...

9 A.Abhari CPS1259 /* Convert the weight from pounds to kilograms */ comment #include 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); kilos = pounds * KILOS_PER_POUND; printf(" That equals %f kilos. \n”, kilos); punctuation return (0); } special symbol

10 A.Abhari CPS12510 Executable Statements Data can be stored/changed in memory cells by Assignment statement variable = expression; ( =) is not equal sign x= x + z / 6.9; x= -9; new_x = -x; Input operations such as scanf function that requires : #include

11 A.Abhari CPS12511 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);

12 A.Abhari CPS12512 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 When reading the numbers, scanf scans the digits and characters until it reaches non-digits, blank or cr. For characters only first character before cr is considered

13 A.Abhari CPS12513 /* This program calculates ? */ #include #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); }

14 A.Abhari CPS12514 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

15 A.Abhari CPS12515 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.003 -3.15 %.1f -3.2 99.67 %f 99.67

16 A.Abhari CPS12516 #include 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

17 A.Abhari CPS12517 Batch Mode In batch mode the program gets its input from a data file instead of the user and sends the result to a data file instead of the screen. It is also called redirection. For example the following program can be run by: weightconvert output /* weightconvert program */ include #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); }

18 A.Abhari CPS12518 Program-controlled Input and Output files File pointer variable include #define KILOS_PER_POUND 0.45359 int main(void) { double pounds, kilos; FILE *inp, /* pointer to input file */ *outp; /* pointer to output file */

19 A.Abhari CPS12519 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);

20 A.Abhari CPS12520 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); } We will discuss program-controlled input and output files in more details later.

21 A.Abhari CPS12521 Common programming Errors Syntax Error Missing ; or variable definition 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

22 A.Abhari CPS12522 Runtime Errors and Undetected Errors If we use scanf for numbers and then after that again we use scanf for the characters, the first character gets different value than what we entered. For example in a program scanf (“%lf”, &radius); scanf (“%c%c%c”, &a, &b, &c); is used instead of scanf (“%c%c%c”, &a, &b, &c); scanf (“%lf”, &radius); If we run this program with these inputs: I 50 ABC The values stored in the variables are:  a b c radius \n A B 50 One of the way for fixing this problem is placing space before first %C: scanf (“%lf”, &radius); scanf (“ %c%c%c”, &a, &b, &c);

23 A.Abhari CPS12523 Logic Errors Logic errors is shown by the wrong result of the program. For example forgetting & in the following statement: scanf( “%d%d”, a, b) produces => incorrect results Preventing logic errors is done by Deskchecking the algorithm and Debugging the program


Download ppt "A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive."

Similar presentations


Ads by Google