Programming_workshop.pdf
Document Details
Uploaded by DiligentComplex
Polygon University
Full Transcript
Programming workshop Mouna Bouzazi Polygon University October 25, 2024 Professor October 25, 2024 1 / 20 Plan 1. Generalities about Programming Languages 2. General Structure of a C Program 3. Examples of...
Programming workshop Mouna Bouzazi Polygon University October 25, 2024 Professor October 25, 2024 1 / 20 Plan 1. Generalities about Programming Languages 2. General Structure of a C Program 3. Examples of C Programs 4. The main() Function in C 5. Variables in C Professor October 25, 2024 2 / 20 Generalities about Programming Languages Generalities about Programming Languages What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. The main reason for its popularity is because it is a fundamental language in the field of computer science. C is strongly associated with UNIX, as it was developed to write the UNIX operating system. Professor October 25, 2024 3 / 20 Generalities about Programming Languages Generalities about Programming Languages Why Learn C? It is one of the most popular programming languages in the world If you know C, you will have no problem learning other popular programming languages such as Java, Python, C++, C, etc, as the syntax is similar C is very fast, compared to other programming languages, like Java and Python C is very versatile; it can be used in both applications and technologies Professor October 25, 2024 3 / 20 Generalities about Programming Languages Generalities about Programming Languages There are two types of translators: The interpreter, which translates programs instruction by instruction in the context of continuous interaction with the user. Thus, the program is translated at each execution. The compiler, which translates the programs as a whole: the entire program must be provided as a block to the compiler for translation. It is translated only once. Professor October 25, 2024 3 / 20 General Structure of a C Program General Structure of a C Program Overview In general, a C program consists of constructing individual blocks called functions that can invoke each other. Each function performs a specific task. The main Function To be executable, a C program must contain a special function called main, which will be the entry point of the execution (i.e., the first function invoked at the start of execution). All other functions are essentially subroutines. Variable Declarations A C program also includes the declaration of variables, which correspond to memory locations. These locations store values that can be used or modified by one or more functions. Professor October 25, 2024 4 / 20 General Structure of a C Program The Classic ”Hello World” Program C Program Example # include < stdio.h > // Preprocessor directive void main () { printf ( " Hello world ! " ) ; } Professor October 25, 2024 5 / 20 General Structure of a C Program A Complete Example with All C Program Components C Program Example # include < stdio.h > // Preprocessor directive # define TVA 15 // VAT rate in Luxembourg float price_TTC ; // External variable declaration float add_TVA ( float price_HT ) { return price_HT * (1 + ( TVA /100) ) ; } void main () { float HT ; // Internal variable declaration puts ( " Enter the pre - tax price : " ) ; // Function call scanf ( " % f " , & HT ) ; // Defined in stdio. h price_TTC = add_TVA ( HT ) ; // Call our function printf ( " Total price ( incl. VAT ) : %.2 f \ n " , price_TTC ) ; } Professor October 25, 2024 6 / 20 General Structure of a C Program Different Components of a C Program A simple program is composed of several parts: Preprocessor directives One or more functions, one of which must be called main(), which constitutes the main program and consists of two parts: The declaration of all variables and functions used Instructions Multi-line Comments Comments begin with , and they can span multiple lines. Professor October 25, 2024 7 / 20 General Structure of a C Program Preprocessor Directives Preprocessor directives in C start with the symbol #. Command Description #include Allows the use of the functions printf() and scanf() #include Allows the use of mathematical functions #define PI 3.14159 Defines the constant PI #undef PI From this point, the constant PI is no longer defined #ifdef PI If PI is defined, compile instructions 1, otherwise, instructions 2 #else The alternative instructions to compile if PI is not defined #endif Ends the conditional compilation block Professor October 25, 2024 8 / 20 General Structure of a C Program Mandatory Preprocessor Directive Among these directives, only one is mandatory for the correct functioning of a program: #include Without this directive, you cannot use useful functions for screen output (printf()) and reading data from the keyboard (scanf()). The usage of these functions will be explained later. Professor October 25, 2024 9 / 20 The main() Function in C The main() Function Structure The main() function starts with an opening curly brace { and ends with a closing curly brace }. Inside, each instruction ends with a semicolon ;. All variables must be declared before being used. General Structure of main() main () { int i ; instruction_1 ; instruction_2 ;... } Professor October 25, 2024 10 / 20 The main() Function in C Example of a Simple Program Simple C Program Example # include < stdio.h > main () { printf ( " Hello world \ n \ n " ) ; } Professor October 25, 2024 11 / 20 The main() Function in C Constants in C In C, there are different types of constants: Integer constants: 1, 2, 3,... Character constants: ’a’, ’A’,... String constants: "Hello" No Logical Constants There are no logical constants in C. Instead, integers are used for boolean tests: 0 is equivalent to false Any non-zero value is considered true Professor October 25, 2024 12 / 20 Variables in C Variable Names in C C distinguishes between uppercase and lowercase letters. To avoid confusion, variable names are written in lowercase, while uppercase is reserved for symbolic constants defined by #define. Variable names must start with a letter. They should not contain spaces. The only special character allowed is the underscore. Additionally, you cannot use reserved keywords like while, if, case, etc., as variable names. Function names should also not be used for variables. Professor October 25, 2024 13 / 20 Variables in C Variable Declaration in C To declare a variable, prefix its name with its type. There are 6 basic types of variables in C: Type Description Min Value Max Value char Character (1 byte, 8 bits) −27 27 − 1 7 short Integer (1 byte) −2 27 − 1 31 int Integer (4 bytes) −2 231 − 1 63 long Integer (8 bytes) −2 263 − 1 38 float Real number (4 bytes) ∼ −10 ∼ 1038 308 double Real number (8 bytes) ∼ −10 ∼ 10308 Each type can be prefixed with unsigned, which forces the variable to take only positive values. Professor October 25, 2024 14 / 20 Variables in C Examples of Variable Declarations in C Here are some examples of variable declarations in C and their meanings: Declaration Meaning int a; a is an integer int z = 4; z is an integer and its value is 4 unsigned int x; x is a positive (unsigned) integer float zx, zy; zx and zy are of type float (real numbers) float zx = 15.15; zx is a float with a value of 15.15 double z; z is a double precision real number char zz; zz is a character variable char zz = ’a’; zz has the value ’a’ Note: There is no complex number type in C. Professor October 25, 2024 15 / 20 Variables in C Basic Operators in C The first operator to know is the assignment operator =. Example: a = b + 1; This operator assigns the value of the right-hand side to the variable on the left. The right-hand side is first evaluated, and then its value is assigned to the left-hand variable. For example, i = i + 1 makes sense. Natural operations are performed using the following operators: + : Addition - : Subtraction * : Multiplication / : Division % : Modulo (remainder of division) % is the modulo operation: 5 % 2 gives the remainder of dividing 5 by 2, which is 1. Professor October 25, 2024 16 / 20 Variables in C Advanced Operators in C Special operators in C, which should be used with caution: ++ : Increments the variable by 1. -- : Decrements the variable by 1. These two operators are not used with floating-point numbers. Here are some examples: i ++; i - -; Their use can become tricky when combined with other operators. For example: int i = 1 , j ; j = i ++; j = ++ i ; Additionally, operations like ’a’ + 1 are valid in C, yielding the next character in the ASCII sequence after a. Professor October 25, 2024 17 / 20 Variables in C Increment and Decrement Operators When the ++ operator is placed before a variable, the increment operation is performed first. When ++ is placed after the variable, the increment is done after the current value is used. The same behavior applies to the -- operator for decrementing. Example 1: int i = 1 , j ; j = ++ i ; Example 2: j = i - -; Professor October 25, 2024 18 / 20 Variables in C Additional Assignment Operators C provides several shorthand operators to perform operations and assignments in a single step: i += 5; : equivalent to i = i + 5; i -= 3; : equivalent to i = i - 3; i *= 4; : equivalent to i = i * 4; i /= 2; : equivalent to i = i / 2; i %= 3; : equivalent to i = i % 3; These operators simplify code when performing an operation and updating the variable in one step. Professor October 25, 2024 19 / 20 Variables in C Comparison and Logical Operators The operators used to compare two variables in C are as follows: == : Equal to != : Not equal to < : Less than : Greater than >= : Greater than or equal to Logical operators are also available for combining conditions: : Logical AND || : Logical OR Important: Do not confuse the assignment operator = with the comparison operator ==. Professor October 25, 2024 20 / 20