Topic 2 C Overview C Language Elements Variable

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 A. Abhari CPS 125 1

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

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 programn"); The control returns back to OS return (0); } Program was executed without error A. Abhari CPS 125 4

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 A. Abhari CPS 125 5

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 1 rate 1 int Rate 1 Different identifiers joe’s It is a reserved word joe • Uppercase & lowercase User-defined identifiers are considered different identifiers by C compiler • Meaningful name should be used for Userdefined identifiers A. Abhari CPS 125 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; A. Abhari CPS 125 7

C Language Elements Data types • int: integer between -32767 to 32767 • double: for real numbers 3. 14, 0. 34 1. 23 e 5, 1. 23 E 5= 1. 23 * 10 to the power 5 0. 34 e-4 = 0. 000034 • char : for showing individual character ‘A’, ‘a’, ‘ ’, . . . A. Abhari CPS 125 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”, £s); kilos = pounds * KILOS_PER_POUND; printf(" That equals %f kilos. n”, kilos); punctuation return (0); } special symbol A. Abhari CPS 125 9

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 <stdio. h> A. Abhari CPS 125 10

Output Operation - printf( format string, printlist) • printf( “ Hi %c - your age is %dn”, na, age); • printf (“It is 1 th linen”); printf (“n and 2 th line); • printf(“ Enter the weight in pounds”); scanf( “%lf” , £s); printf(“ %f pounds was entered”, pounds); A. Abhari CPS 125 11

Input Operation-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. A. Abhari character before. CPS 125 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; c= 2 * PI * r; printf( “? is %f cm^2. n”, a); printf( “? is %f cm. n”, c); return (0); } A. Abhari CPS 125 13

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

Formating Values of Type double • printf ( “Result is: %6. 2 f meters”, … Examples: Value format display -99. 42 %6. 1 f -99. 4 99. 999 %6. 2 f 100. 00 -. 006 %8. 5 f -0. 00600 -. 003 %. 3 f -0. 003 -3. 15 %. 1 f -3. 2 99. 67 %f 99. 67 A. Abhari CPS 125 15

#include <stdio. h> int main(void) { double x= 123. 456; int y=12345; printf( “ %f %. 3 f %. 1 f n”, x, x, x ); printf( “ %3 d %5 d %8 d nn”, y, y, y ); return (0); } Þ 123. 456 123. 5 Þ 12345 A. Abhari 12345 CPS 125 16

Batch Mode • In batch mode the program gets 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 <data >output /* weightconvert program */ include <stdio. h> #define KILOS_PER_POUND 0. 45359 int main(void) { double pounds, kilos; scanf(“%lf”, £s); printf(“Weight in pounds is %. 2 f. n“, pounds); kilos = pounds * KILOS_PER_POUND; printf(" That equals %f kilos. n”, kilos); return (0); } A. Abhari CPS 125 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 */ A. Abhari CPS 125 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”, £s); fprintf(outp, “Weight in pounds is %. 2 f. n“, pounds); A. Abhari CPS 125 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); } We will discuss program-controlled input and output files in more details later. A. Abhari CPS 125 20

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 A. Abhari CPS 125 21

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); A. Abhari CPS 125 22

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 A. Abhari CPS 125 23
- Slides: 23