320 likes | 425 Vues
Dive into the fundamentals of C programming with this comprehensive overview. Chapter 2 introduces the iconic Hello World program, explains crucial language elements such as variable declarations, data types, and executable statements. Learn about reserved words, standard identifiers, and how to use printf and scanf for input/output operations. The chapter also covers arithmetic expressions and number formatting, providing essential knowledge for beginners. Start your programming journey with C and build a strong foundation in coding concepts!
E N D
KK10103 – Computer Programming Chapter 2 : Overview of C By Suraya Alias
2.0 The Hello World Program /*The classic HelloWorld */ #include <stdio.h> int main(void) { printf(“Hello World!!"); return 0; }
2.1 C Language Element • /*The classic HelloWorld */ • These are comments, starts with /* ends with */, // is used to comment out per line. • #include <stdio.h> • Lines begin with # is the preprocessing directives which communicates with the processor. • #include <stdio.h> means to include standard header file that has definitions such as printf and scanf. • The other preprocessor directive is #define, such as #define HOUR 60 , which means that the constant macro HOUR has the value of 60. • Syntax : #define NAME value
2.1 C Language Element • int main (void) • The main function is where the execution begins. • The braces { } enclose the main function body, which contains declaration and executable statements. • The keyword int indicates that the main function returns an integer value 0 after the execution ends. • The keyword void means the main function takes no argument • The function body has two parts : declaration and executable statements. • Every declarations and statements ends with semicolon ; in C programming
2.1 C Language Element • Reserved words • a word that has special meaning in C • Such as int, void, double and return • Standard Identifiers • also has special meaning such as printf and scanf • User Defined Identifiers • user can define their own identifiers such as HOUR, KM_PER_MILE • An identifier must consist only letters, digit and underscore • An identifier cannot begin with a digit • A C reserved word cannot be used as an identifier • Lowercase and uppercase identifiers does make a different in C. The names Rate and rate and RATE is considered as different identifiers.
2.2 Variable Declaration and Data Types • Variables • a memory cell used to store value and can be changed as the programs executes • Variable Declaration • statement that tells the compiler the variable name and the value stored in the variable. • The Variable declarations starts with an identifier followed by the variable name such as : int count; double x, y, x; char w; • Data types • a set of values and operations that can be performed on those values • Data type int – to represent integers in C, such as 77 • Data type double – a real number that is separated by decimal point, such as 1.5674 and 0.09 • Data type char – represents an individual character value such as a letter, digit or special symbol. Such as ‘A’, ‘7’, ‘:’
Figure 2.1 C Language Elements in Miles-to-Kilometers Conversion Program
Figure 2.2 Memory(a) Before and (b) After Execution of a Program 2.3 Executable Statements
Input / Output Operations • The printf function • Used for printing formatted output and prompting message • printf(“Please enter a number>”) • printf(“That equals %f kilometers.\n ”, kms); • That equals 16.090000 kilometers • %f is the placeholder which is replaced by the value from kms • A placeholder always begins with the symbol % • The scanf function • Used to read formatted input • scanf(“%lf”, &miles); • The format “%lf” is the placeholder that tells scanf what kind of data to copy to variable miles • The name of each variable is preceded by & character
Placeholder in Format Strings • Multiple placeholder • printf (“Hi %c%c%c – you will be %d years old today \n”, letter_1, letter_2, letter_3, age); • Will display as • Hi BOB – you will be 32 years old today
2.5 Arithmetic Expression • Rules for evaluating Expression • Parentheses rules • Solve or evaluate expression in bracket first • Operator Precedence Rules • Unary + - • * / % • Binary + - • Associativity Rules • For same level, left to right
Figure 2.10 Evaluation Tree and Evaluation forv = (p2 - p1) / (t2 - t1);
Figure 2.11 Evaluation Tree and Evaluation for z - (a + b / 2) + w * -y
Figure 2.13 Batch Version of Miles-to-Kilometers Conversion Program
2.6 Formatting Numbers in Program Output • Formatting values of Type int • By using field width • printf(“Results: %3d meters = %4d ft. %2d in. \n”, meters, feet, inches); • Results : 21 meters= 68 ft. 11 in. • Formatting values of Type double • To display the value of x to an accuracy of 2 decimal place we can use the placeholder %6.2f • Example : x is -25.554 will be displayed as -25.55
2.7 Interactive Mode, Batch Mode and Data Files • Interactive mode • A mode where user responds to prompt by entering data • Batch mode • Programs scans data from data file
Figure 2.13Batch Version of Miles-to-Kilometers Conversion Program
Program controlled Input and output files Using the FILE * command FILE *inp, /* pointer to input file */ *outp; /* pointer to output file */ /* Open the input and output files. */ inp = fopen("D:\\distance.txt", "r"); outp = fopen("D:\\distanceout.txt", "w"); /* Get and echo the distance in miles. */ fscanf(inp, "%lf", &miles); fprintf(outp, "The distance in miles is %.2f.\n", miles); /* Display the distance in kilometers. */ fprintf(outp, "That equals %.2f kilometers.\n", kms); /* Close files. */ fclose(inp); fclose(outp);
Figure 2.14 Miles-to-Kilometers Conversion Program with Named Files
Figure 2.15 Compiler Listing of a Program with Syntax Errors
Figure 2.17 Revised Start of main Function for Supermarket Coin Value Program
Figure 2.18 A Program That Produces Incorrect Results Due to & Omission