Summary

This document contains lecture notes on C programming basics, covering program structure, syntax, data types, and variables. The notes discuss concepts such as function calls, program organization, and variable declarations, providing an overview of fundamental C programming concepts.

Full Transcript

Lecture 2 C Basics 1. C program structure 2. Syntax 3. Data types 4. Variables 1 1. C program structure C source code program is organized as a sequence of functions. – A function contains a logic sequence of statements. – A statement may call another...

Lecture 2 C Basics 1. C program structure 2. Syntax 3. Data types 4. Variables 1 1. C program structure C source code program is organized as a sequence of functions. – A function contains a logic sequence of statements. – A statement may call another function, a function has to be declared or defined before it can be called. – Must contain main() function for an executable program. The execution of a C program starts from the main() function. Executable program is organized as a sequence of function blocks of machine code. 2 C program structure model [preprocessor directives] [global variables] [function declarations] main( arguments ) { [statements] } [function definitions] 3 #include // preprocessor directive include int a; // global variable declaration int add(int, int); // function declaration int minus(int, int); // function declaration // definition of main function, the start function int main() // main function header { // start of block function definition / function body a=1; // assign/set value 1 to global variable a int b=2; // declare local variable b and initialize/set value 2 printf("a+b=%d\n", add(a, b)); // function calls, output a+b=3 printf("a-b=%d\n", minus(a, b)); // function calls, output a-b=-1 return 0; } // end of block function definition // definition/implementation of function add(int, int) int add(int x, int y) // function header { return x+y; // function body } // definition/implementation of function minus(int, int) int minus(int x, int y) // function header { return x-y; // function body } 4 C program organization A large C program is decomposed into – function header files – header function implementation files – a main function file, called driver program file Refer to Lesson 1.2.4 5 2. Basic syntax C has 5 types of elements: symbol, keyword, expression, statement, function Basic symbols 6 C has 32 reserved words (keywords) (9) (4) (6) (11) (2) 7 Expressions – Use infix notation, consisting of constants, variables, operators, parenthesis. E.g., (1 + 2) * 3, 1==2, (1==1) && ( 2!=1) Statements – A C statement is a command/instruction to C compiler. – Statement types: declaration, assignment, condition, function call, flow control – Statements are organized to blocks (program block), a sequence of statements scoped by { }. 8 Function syntax 1. Function declaration/header syntax: returndatatype function_name(argument type list); 2. Function definition/implementation syntax: returndatatype function_name(argument type and name list) { // function block } 3 Function call syntax: function_name(parameter list) 9 3. Data types A data type (or simply type) defines 1. how a certain type of data values is represented in programming. 2. how many bytes and what bit pattern are used to represent a value in memory. 3. what and how operations are applied to the data values in programming and computers. Brief description: data type defines how a certain type of data values are represented and operated in programming and computers. 10 Primary and derived data types C provides primary data types (primitive, basic data types) – Defined by keywords: char, int, short, long, float, double, signed, unsigned – Arithmetic operations (+, -, *, /) are defined for primary types. Modular operation % is defined for non-floating primary data types. – Each of the primary data types has corresponding bit pattern in representation and operations. C provides methods to build secondary data types (derived, extended) using primary data types and keywords typedef, struct, union, enum. 11 Each data type has a size, i.e. the number of bytes to store the values of the type in memory. Each data type and has a defined valid value range. Example: the char type has size 1, e.g., one byte (8 bits), value range -128 to 127 The size of some data types is platform dependent. Example: the int type has 2 bytes in old 16 system, but 4 bytes in 32 and 64 bit system. We use 4 bytes as default size for the int type. 12 Size and range of primary data types SIZE IN DATA TYPE / Keyword RANGE BYTES char 1 -128 to 127 unsigned char 1 0 to 255 signed char 1 -128 to 127 int 4 (2) -231+1 to +231-1 (-32768 to 32767) unsigned int 4 (2) 0 to 232-1 (0 to 65535) signed short int 4 (2) -231+1 to +231-1 (-32768 to 32767) signed int 4 (2) -231+1 to +231-1 (-32768 to 32767) short int 2 (4) -32768 to 32767 (-231+1 to +231-1) unsigned short int 4 (2) 0 to 232-1 (0 to 65535) long int 4 (8) -231+1 to +231-1 (-263+1 to +263-1) unsigned long int 8 (4) 0 to 264-1 (0 to 4294967295) signed long int -263+1 to +263 (-2147483648 to 8 (4) 2147483647) float 4 3.4E-38 to 3.4E+38 double 8 1.7E-308 to 1.7E+308 long double (C99) 10 3.4E-4932 to 1.1E+4932 13 char type The char type is used to present characters by an integer defined by ASCII (American Standard Code for Information Interchange). Example: A is coded as 65, a as 97, 0 as 48. 14 How char type is stored in memory? Each addressable memory cell holds 8 bits (1 byte). The char type size is 1 byte, it uses one addressable memory cell. Example: the ASCII code of character A is 65. The binary representation of 65 is 100 0001 A is stored in memory cell as 0100 0001 How to do conversions of number representation in different bases? Refer to Lesson 1.3.2 15 int and unsigned int types int – Value range: from -2147483647= -231 +1 to 2147483647= 231-1 – Bit pattern: 4 bytes or 32 bits, left most bit represent sign, 0 for positive, 1 for negative, the rest 31 bits represent the absolute value in base 2 (binary format). Example Int values Binary 1 0000 0000 0000 0001 -1 1000 0000 0000 0001 -2147483647 1111 1111 1111 1111 unsigned int – Value range: from 0 to 4294967295 = 232-1 – Bit pattern: 4 bytes or 32 bits, 32 bits represent the value in base 2. Int values Binary 1 0000 0000 0000 0001 4294967295 1111 1111 1111 1111 16 How int type is stored in memory? When a data type size is bigger than 1, it needs a contiguous memory cells (called memory block) to store the value of the type. – Big-endian: store the most significant byte in the lowest address cell – Little-endian: store the least significant byte in the lowest address cell. little-endian is commonly used. int type size is 4, needs 4 memory cells. For example, 242713057310 = 1001 0000 1010 1011 0001 0010 1100 1101 2 = 9 0 A B 1 2 C D 16 Big-endian Little-endian Address Value Address Value 1003 CD 1003 90 1002 12 1002 AB 1001 AB 1001 12 1000 90 1000 CD 17 float and double types float type uses 4 bytes for single precision floating point numbers, bit pattern and operations are specified by IEEE 754 standard. https://en.wikipedia.org/wiki/Single-precision_floating-point_format double type uses 8 bytes for double precision floating point numbers, specified by IEEE 754 standard. https://en.wikipedia.org/wiki/Double-precision_floating-point_format 18 4. Variables Concepts of variables 1. A variable is a name identifier used in source code program to represent a data value of a certain type. 2. A variable is assigned a memory block with relative address by compiler, as well as instructions to set and get the values to the memory block. 3. A variable is instanced at runtime with absolute address of memory block. Brief description: a variable is an identifier of a data value in a program, it gets relative memory allocation at the compile time, and actual memory block at runtime. 19 C variables A variable must be declared with a type and name in a scope, and then used within the scope. 1. The variable declaration tells compiler to assign memory block with relative address. 2. A variable assignment statement tells compilers to generate instructions for writing values to the memory block. 3. Using the variable in an expression tells compilers to generate instructions to read values from the memory block. A variable should be initiated (assigned a value) before it is used in expressions. C variable names must start with a letter, followed by letters, underscores and numbers, and case sensitive. – C name convention: underscore_style, camelCaseStyle 20 Variable and scope 1. A variable has to be declared before it can be used. A variable has a scope, within which the variable is declared and used. 2. Scope can be nested, i.e. one scope is inside another scope. A variable declared before an inner scope can be used in the inner scope. Same variable name can be used to declare and use as a new variable in an inner scope. 3. Global variables are variable declared outside any scope, so can be used anywhere. 4. Local variables are variables declared in a scope block embraced by {}. e.g. in a function, so can only be used in the scope block. 21 Literals Literals refer those constant values assigned to variables in programming. Compiler recognizes the data types of a literal and convents to its bit pattern representations, bing used in generated instructions. Pre-processor #define can be used to define a literal string as macro, and then use macro in programming. During the pre- preprocessing steps, the macro will be replaced by its corresponding string. Example #define PI 3.1415926 float r = 4; float area = PI*r*r; float cf = 2*PI*r; float f = 2.4e-5; // 2.4e-5 = 0.000024 22 Examples of variable declaration and initialization Declaration char c; // let compiler assign 1 byte memory space for char variable c int a; // let compiler assign 4 bytes memory space for int variable a float f; // let compiler assign 4 bytes memory space for float variable f Assign values to variables a = 2; // compiler generates instructions that store 2 to memory of variable a at runtime c = ‘a’; // compiler generates instructions that store 9710 = 0111001 to memory of c f = 1.41; //compiler convert 1.41 to 32 bits single precision number, and generates instructions to store the number at memory of f. Declaration & initialization int a = 12; // or int a = 014; for Oct number 14, or int a = 0xC; for Hex number C char c = ‘a’; float f = 1.41; int result, x = 9, y = 3; // a list separated by comma if variables are of the same type. 23 sizeof sizeof is a unary operator used to get the sizes of data types, applying to all data types. Can also be used to determine the memory size of a variable. Example sizeof(char) returns 1, the size of a character data type int a = 10; sizeof(a) returns 4, the size of int type. 24 Constants Constants are fixed data values in a program. In C, constant variable (or read-only variable) is used to represent constants. Constant variables are declared and initialized by keyword const. Compiler does not allow to assign values to a constant variable after it is declared and initialized. Example const float pi = 3.1415926; float r = 4; float area = pi*r*r; float cf = 2*pi*r; pi = 3.14; // this is not allowed by compiler Read and test code example 1: Data type, variables and constants. 25

Use Quizgecko on...
Browser
Browser