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

C Language Elements Preprocessor Directives

Jan 01, 2016

Download

Documents

aretha-massey

C Language Elements Preprocessor Directives. # (sign for preprocessor directive commands) #include #include #include Standard header file (.h) Library. C Language Elements Preprocessor Directives. Constant Macros - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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

Page 2: C Language Elements   Preprocessor Directives

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

Page 3: C Language Elements   Preprocessor Directives

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 OSProgram was executed without error

Page 4: C Language Elements   Preprocessor Directives

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 switchreturn for signed else long If do int while

Page 5: C Language Elements   Preprocessor Directives

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

Page 6: C Language Elements   Preprocessor Directives

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;

Page 7: C Language Elements   Preprocessor Directives

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’, ‘ ’,...

Page 8: C Language Elements   Preprocessor Directives

/* 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

Page 9: C Language Elements   Preprocessor Directives

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>

Page 10: C Language Elements   Preprocessor Directives

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);

Page 11: C Language Elements   Preprocessor Directives

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

Page 12: C Language Elements   Preprocessor Directives

/* This program calculates ? */#include <stdio.h>#define PI 3.14159int 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);}

Page 13: C Language Elements   Preprocessor Directives

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

Page 14: C Language Elements   Preprocessor Directives

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

Page 15: C Language Elements   Preprocessor Directives

#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

Page 16: C Language Elements   Preprocessor Directives

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); }

Page 17: C Language Elements   Preprocessor Directives

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 */

Page 18: C Language Elements   Preprocessor Directives

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);

Page 19: C Language Elements   Preprocessor Directives

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);

}

Page 20: C Language Elements   Preprocessor Directives

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

Page 21: C Language Elements   Preprocessor Directives

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

Page 22: C Language Elements   Preprocessor Directives

Logic Errors

• scanf( “%d%d” , a, b)

=> incorrect results

• Deskchecking

• Debugging