Lecture Title: The Basics of C Course: General Computer Programming (CPE 224) Course Lecturer: Mr. Abdulazeez Salami
The document provides an overview of the basics of the C programming language. It discusses what C is, how it was developed, its standardization under ANSI, and key elements of a C program like functions, constants, variables, compiling, linking, and program structure. The main points are that C is a popular general purpose language, it was first developed in 1972, the ANSI standard enhanced portability, and a basic C program requires functions like main(), constants/variables, and compiling/linking to create an executable.
Download as PPT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
44 views
Lecture Title: The Basics of C Course: General Computer Programming (CPE 224) Course Lecturer: Mr. Abdulazeez Salami
The document provides an overview of the basics of the C programming language. It discusses what C is, how it was developed, its standardization under ANSI, and key elements of a C program like functions, constants, variables, compiling, linking, and program structure. The main points are that C is a popular general purpose language, it was first developed in 1972, the ANSI standard enhanced portability, and a basic C program requires functions like main(), constants/variables, and compiling/linking to create an executable.
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 33
Lecture Title: The Basics of C
Course: General Computer
Programming (CPE 224) Course Lecturer: Mr. Abdulazeez Salami
Computer Engineering Department,
Federal University of Technology, Minna. What is C? • C is a programming language. • The C language was first developed in 1972 by Dennis Ritchie at AT&T Bell Labs. • Ritchie called his newly developed language C simply because there was a B programming language already. (As a matter of fact, the B language led to the development of C.) • C is a high-level programming language. • In fact, C is one of the most popular general purpose programming languages. • In the computer world, the further a programming language is from the computer architecture, the higher the language’s level. • You can imagine that the lowest-level languages are machine languages that computers understand and execute directly. • The high-level programming languages, on the other hand, are closer to our human languages What is C? • High-level programming languages, including C, have the following advantages: • Readability: Programs are easy to read. • Maintainability: Programs are easy to maintain. • Portability: Programs are easy to port across different computer platforms. • The C language’s readability and maintainability benefit directly from its relative closeness to human languages, especially English. • Each high-level language needs a compiler or an interpreter to translate instructions written in the high-level programming language into a machine language that a computer can understand and execute. • Different machines may need different compilers or interpreters for the same programming language. The ANSI C Standard • For many years, the de facto standard for the C programming language was the book The C Programming Language, written by Brian Kernighan and Dennis Ritchie in 1978. • However, the book was written as a tutorial introduction to C, not as a comprehensive or official standard for the language. • As different vendors offered varying implementations of the C language, differences between those implementations began to appear. • Fearing that C might lose its portability, a group of compiler vendors and software developers petitioned the American National Standards Institute (ANSI) to build a standard for the C language in 1983. • ANSI approved the application and formed the X3J11 Technical Committee to work on the C standard. • By the end of 1989, the committee approved the ANSI standard for the C programming language. • The ANSI standard for C enhances the original K&R standard and defines a group of commonly used C functions that known as the ANSI C standard library. Writing Your First C Program • A Simple C Program • /* 02L01.c: This is my first C program */ • #include <stdio.h> • main() • { • printf (“Howdy, neighbor! This is my first C program.\n”); • return 0; • } • This is a very simple C program, which is saved in a file called 02L01.c. • Note that the name of a C program file must have an extension of .c. Comments • The first line contains a comment: /* 02L01.C: This is my first C program */ • You notice that this line starts with a combination of slash and asterisk, /*, and ends with */. • In C, /* is called the opening comment mark, and */ is the closing comment mark. • The C compiler ignores everything between the opening comment mark and closing comment mark. • That means the comment in the first line of code, 02L01.C: This is my first C program, is completely ignored by the compiler. • The only purpose of including comments in your C program is to help you document what the program or some specific sections in the programs do. • Remember, comments are written for yourself and other programmers. • Adding comments into a C program does not increase the size of the binary code of the program (that is, the executable file), although the size of the program itself (that is, the source code) may become larger. • The performance speed of the executable file made from your C program is not affected in any way by the comments inside your C program. The #include Directive • Let’s now move to line 2 in the C program: #include <stdio.h> • You see that this line starts with a pound sign, #, which is followed by include. • In C, #include forms a preprocessor directive that tells the C preprocessor to look for a file and place the contents of that file in the location where the #include directive indicates. • The preprocessor is a program that does some preparations for the C compiler before your code is compiled. • Also in this line, you see that <stdio.h> follows #include. • This means the file the #include directive asks for is something called stdio.h. • Here, the #include directive does ask the C preprocessor to look for and place stdio.h at the location of the directive in the C program. • The name of the stdio.h file stands for standard input-output header file. • The stdio.h file contains numerous prototypes and macros to perform input or output (I/O) for C programs The #include Directive • Let’s now move to line 2 in the C program: #include <stdio.h> • You see that this line starts with a pound sign, #, which is followed by include. • In C, #include forms a preprocessor directive that tells the C preprocessor to look for a file and place the contents of that file in the location where the #include directive indicates. • The preprocessor is a program that does some preparations for the C compiler before your code is compiled. • Also in this line, you see that <stdio.h> follows #include. This means the file the #include directive asks for is something called stdio.h. • Here, the #include directive does ask the C preprocessor to look for and place stdio.h at the location of the directive in the C program. • The name of the stdio.h file stands for standard input-output header file. • The stdio.h file contains numerous prototypes and macros to perform input or output (I/O) for C programs. • The files that are included by the #include directive, like stdio.h, are called header files because the #include directives are almost always placed at the start, or head of C programs. The main() Function • In line 4 of the code, you see this function: main () • This is a very special function in C. Every C program must have a main() function, and every C program can only have one main() function. • The execution of your program always starts with the main() function. • If you create other functions in your program, main() will always execute first, even if it is at the bottom of your program file. • In the code, the main() function body starts in line 4 and ends in line 8. • Because this is a very simple program, the main() function is the only function defined in the program. • Within the main() function body, a C library function, printf(), is called in order to print out a greeting message (see line 6). • One more important thing about main() is that the execution of every C program ends with main(). • A program ends when all the statements within the main() function have been executed. The main() Function • In the printf() function, is the newline character, \n. • It tells the computer to move the cursor to the beginning of the next line so that anything printed out after the message will start on the next line on the screen. • The main() function itself returns an integer value. • Therefore, in line 7 of the code, there is a statement, return 0;, that indicates that 0 is returned from the main() function and the program is terminated normally. • There are cases when you must end your program due to an error condition. • When that happens, you can return values other than 0 to tell the operating system (or the program that ran your program) that there was an error. Compiling and Linking • There are three steps needed to create an executable file. First, a program file written in C, called source code, is made. • The name of the source code file ends with the extension .c. Then the source code file is compiled by a C compiler, which creates a new file. • The new file is an object file. In the DOS and Windows operating systems, the extension is .obj. • You cannot execute the object file because there is some function code missing. • You have to finish the next step: linking. Linking is done by invoking a special program called a linker, which normally comes with the compiler package. • A linker is used to link together the object file, the ANSI standard C library, and other user-generated libraries to produce an executable file—the binary code. • The name of an executable file usually ends with the extension .exe in DOS and Windows. • In the computer world, program errors are also called bugs. In order to find those “hidden” errors in your program, you may need to use a debugger Structure of a C Program • A C program is made of basic elements, such as expressions, statements, statement blocks, and function blocks. • There are two smaller but important elements, constant and variable, which make up expressions. • As its name implies, a constant is a value that never changes. • A variable, on the other hand, can be used to present different values. • You can see many examples in which constants and variables are in the same statement. • For instance, consider the following: i = 1; • where the symbol 1 is a constant because it always has the same value (1), and the symbol i is assigned the constant 1. • In other words, i contains the value of 1 after the statement is executed. • Later, if there is another statement: i = 10; • after it is executed, i is assigned the value of 10. • Because i can contain different values, it’s called a variable in the C language. Structure of a C Program • An expression is a combination of constants, variables, and operators that are used to denote computations. • For instance, the following: • (2 + 3) * 10 • is an expression that adds 2 and 3 first, and then multiplies the result of the addition by 10. (The final result of the expression is 50.) • Here are some other examples of expressions: Structure of a C Program • An expression can contain symbols such as +, *, and /. • In the C language, these symbols are called arithmetic operators. • The table below lists all the arithmetic operators and their meanings: Structure of a C Program • You might already be familiar with all the arithmetic operators, except the remainder (%) operator. • % is used to obtain the remainder of the first operand divided by the second operand. • For instance, the expression: 6 % 4, yields a value of 2 because 4 goes into 6 once with a remainder of 2. • The remainder operator, %, is also called the modulus operator. • Other operators, which are used for syntax, include the comma and semicolon. • The semicolon is generally used to indicate the end of a statement, as you will see later. • The comma is used in certain instances where a statement is comprised of a list of expressions or declarations. Structure of a C Program • Along with numbers (such as the constant 7) and operators (such as the symbol +), expressions can also contain words that are called identifiers. • Function names (such as exit) and variable names (such as i), as well as reserved keywords, are all identifiers in C. • The following is the set of characters you can use to make a valid identifier. • Any characters or symbols that do not follow these rules are illegal to use in an identifier: • Characters A through Z and a through z. • Digit characters 0 through 9 (but these can not be used as the first character of an identifier). • The underscore character (_). • For instance, stop_sign, Loop3, and _pause are all valid identifiers. Structure of a C Program • The following are illegal characters; that is, they do not meet the above set of rules for identifiers: • C arithmetic signs (+, -, *, /). • Period, or dot character (.). • Apostrophes (‘) or quotes (“). • Any other special symbols such as *, @, #, ?, and so on. • Some invalid identifiers, for example, are 4flags, sum-result, method*4, and what_size?. Structure of a C Program • In the C language, a statement is a complete instruction, ending with a semicolon. • In many cases, you can turn an expression into a statement by simply adding a semicolon at the end of the expression. • For instance, the following: • i = 1; • is a statement. • A group of statements can form a statement block that starts with an opening brace ({) and ends with a closing brace (}). A statement block is treated as a single statement by the C compiler. • For instance, the following: • for(. . .) { • s3 = s1 + s2; • mul = s3 * c; • remainder = sum % c; • } Anatomy of a C Function • Functions are the building blocks of C programs. • As shown in the figure below, a function consists of six parts: the function type, the function name, arguments to the function, the opening brace, the function body, and the closing brace. Anatomy of a C Function • The function type is used to signify what type of value a function is going to return after its execution. • In C, int is used as the keyword for the integer data type. • Besides the int type, a function type can be one of the other types, such as the character type (keyword: char), the floating type (float), and so on. • A function name is normally given in such a way that it reflects what the function can do. • Since a function name is an identifier, when creating your own functions you must follow the rules for creating valid identifiers when naming your function. • In addition, you cannot use the names of standard C functions such as printf() or exit() to name your own functions. • They are already defined, and it is illegal to use the same function name in defining more than one function. Anatomy of a C Function • Pieces of information passed to functions are known as arguments. • As you’ve seen, the argument of a function is placed between the parentheses that immediately follow the function name. • The number of arguments to a function is determined by the declaration of the function, which in turn is determined by the task the function is going to perform. • If a function needs more than one argument, arguments passed to the function must be separated by commas; these arguments are considered an argument list. • If no information needs to be passed to a function, you just leave the argument field between the parentheses blank. • For instance, the main() function has no argument, so the field between the parentheses following the function name is empty. Anatomy of a C Function • As you might have already figured out, braces are used to mark the beginning and end of a function. • The opening brace ({) signifies the start of a function body, whereas the closing brace (}) marks the end of the function body. • The function body in a function is the place that contains variable declarations and other C statements. • The task of a function is accomplished by executing the statements inside • the function body one at a time. • It is important to remember that any variable declarations must be placed at the beginning of the function body. • It is illegal to put variable declarations anywhere other than the very beginning of a statement block. Understanding Data Types and Keywords • The C language reserves certain words that have special meanings to the language. • Those reserved words are sometimes called C keywords. • You should not use the C keywords for your own variable, constant, or function names in your programs. • The Table below lists the 32 reserved C keywords: Understanding Data Types and Keywords
Note that all C keywords are written
in lowercase letters. As I’ve mentioned, C is a case sensitive language. Therefore, int, as shown in the list here, is considered as a C keyword, but INT is not. The char Data Type • An object of the char data type represents a single character of the character set used by your computer. • For example, A is a character, and so is a. But 7 is a number. • However, a computer can only store numeric code. • Therefore, characters such as A, a, B, b, and so on all have a unique numeric code that is used by computers to represent the characters. • Usually, a character takes 8 bits (that is, 1 byte) to store its numeric code. • For many computers, the ASCII codes are the de facto standard codes to represent a character set. (ASCII, just for your information, stands for American Standard Code for Information Interchange.) • The original ASCII character set has only 128 characters because it uses the lower 7 bits that can represent 27 (that is, 128) characters. • On IBM-compatible PCs, however, the character set is extended to contain a total of 256 (that is, 28) characters. Character Variables • A variable that can represent different characters is called a character variable. • You can set the data type of a variable to char by using the following declaration format: • char variablename; • where variablename is the name you provide in which to store values of this type. • If you have more than one variable to declare, you can use the following format: • char variablename1, variablename2, variablename3; • For example, the following statement declares MyCharacter and sets it to ‘A’: • char MyCharacter = ‘A’; Character Constants • A character enclosed in single quotes (‘) is called a character constant. • For instance, ‘A’, ‘a’, ‘B’, and ‘b’, are all character constants that have their unique numeric values in a given character set. • For instance, you may see the unique numeric values from the ASCII character set. • It is important to remember that character constants are always surrounded by single quote characters (‘) while a string of more than one character uses the double quote (“). • From the ASCII character set, you will find that the unique numeric (decimal) values of ‘A’, ‘a’, ‘B’, and ‘b’ are 65, 97, 66, and 98, respectively. • Therefore, given x as a character variable, and given the ASCII character set, for instance, the following two assignment statements are equivalent: • x = ‘A’; • x = 65; • So are the following two statements: • x = ‘a’; • x = 97; The Escape Character (\) • Actually, you have seen the escape character (\) when you learned to use the newline character (\n) to break a message into two pieces. • Therefore, the backslash (\) is called the escape character in the ASCII character set. • The escape character is used in the C language to tell the computer that a special character follows. • For instance, when the computer sees \ in the newline character \n, it knows that the next character, n, causes a sequence of a carriage return and a line feed. • Besides the newline character, some of the other special characters in the C language are as follows: The int Data Type • The int keyword is used to specify the type of a variable as an integer. • Integer numbers are also called whole numbers, which have no fractional part or decimal point. • Therefore, the result of an integer division is truncated, simply because any fraction part is ignored. • Depending on the operating system and the C compiler you’re using, the length of an integer varies. • On most UNIX workstations, for example, an integer is 32 bits long, which means that the range of an integer is from 2147483647 (that is, 231–1) to - 2147483648. The range of a 16-bit integer is from 32767 (that is, 215–1) to -32768. • Again, this can vary among different systems, so you can check the reference materials for your compiler to be sure. Declaring Integer Variables • The following shows the basic declaration format: • int variablename; • Similar to the character declaration, if you have more than one variable to declare, you • can euse the format like this: • int variablename1, variablename2, variablename3; • For example, the following statement declares MyInteger as an integer variable and • assigns it a value: • int MyInteger = 2314; • Similarly, the following statement declares A, a, B, and b as integer variables: • int A, a, B, b; • A = 37; • a = –37; • B = -2418; • b = 12 ; The float Data Type • The floating-point number is another data type in the C language. • Unlike an integer number, a floating-point number contains a decimal point. • For instance, 7.01 is a floating-point number; so are 5.71 and –3.14. • A floating-point number is also called a real number. • A floating-point number is specified by the float keyword in the C language. • Floating-pointer constants can be suffixed with f or F to specify float. • A floating-pointer number without a suffix is double by default. • Like an integer number, a floating-point number has a limited range. • The ANSI standard requires that the range be at least plus or minus 1.0 × 1037. • In most cases, a floating-point number is represented by taking 32 bits. • Therefore, a floating-point number in C is of at least six digits of precision. • That is, for a floating-point number, there are at least six digits (or decimal places) on the right side of the decimal point. Declaring Floating-Point Variables • The following shows the declaration format for a floating-point variable: • float variablename; • Similar to the character or integer declaration, if you have more than one variable to declare, you can use the format like this: • float variablename1, variablename2, variablename3; • For example, the following statement declares myFloat as a float variable and assigns it a value: • float myFloat = 3.14; • Similarly, the following statement declares a, b, and c as float variables: • float a, b, c; • a = 10.38; • b = –32.7; • c = 12.0f; The double Data Type • In the C language, a floating-point number can also be represented by another data type, called the double data type. • In other words, you can specify a variable by the double keyword, and assign the variable a floating-point number. • The difference between a double data type and a float data type is that the former uses twice as many bits as the latter. • Therefore, a double floating-point number is of at least 10 digits of precision, although the ANSI standard does not specify it for the double data type.