C Programming Notes.pdf

June 28, 2018 | Author: Sambit Patra | Category: C (Programming Language), Data Type, Integer (Computer Science), Byte, Variable (Computer Science)
Report this link


Description

1BASICS OF C LANGUAGE Introduction The C language was developed in early 1970s at AT&T labs by Brian Kernighan and Dennis Ritchie. The language was originally developed to write the UNIX operating system. Since that time, literally thousands of applications have been written in C C is a small language with relatively few commands. In order to make programming in C easier, a number of libraries have been written. These libraries allow you to use higher level commands or functions which make it easier to write a program. Character Set of C C programs are written using a combination of alphanumeric characters, special symbols and white space characters. The following characters are used in C language. Alphabets Numbers Special Symbols White Space : : : : a to z, A to Z 0,1 to 9 +–*/\&|% !?[]{} ><= ._:;^~'" space, horizontal tab, formfeed, newline, and carriage return Following table gives the list of symbols used in C Symbol Name Symbol Name + * / \ | ^ . ~ _ % $ # < plus minus asterisk slash back slash vertical bar caret period tilde underscore percentage dollar sign number sign less than or opening angle bracket : ; ‘ “ ? ! & { } [ ] ( ) > colon semicolon apostrophe quotation mark question mark exclamation mark ampersand left brace right brace left bright right bracket left parenthesis right parenthesis more than or closing angle bracket Escape Sequences The output of a C program may also use a combination of characters such as \b, \n, \t, etc. These combinations are known as escape sequences. The escape sequence represents a single character, even though it is written as a two or more character. The following table gives the escape sequence used in C 1 Sequence Meaning Sequence Meaning \a \b \f \f \r \t alert sound backspace formfeed newline carriage return asterisk \v \’ \” \? \\ \0 vertical tab single quote double quote question mark backslash null Keywords of C The C programming language keeps a small set of keywords for its own use. These keywords cannot be used as identifiers in the program. The C language specifies 32 keywords. Here is the list of keywords used in Standard C; you will notice that none of them use upper-case letters. auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while Following rules must be kept in mind when using keywords.   Keywords are case sensitive. For example, return is a keyword and it must be used as is. So it cannot be used as Return, or RETURN. The keywords have special meaning in the language and cannot be used for any other purpose such as constant name or variable name. Identifiers in C An identifier is a word used in the program for any variable, function, data definition, etc. In the programming language C, an identifier is a combination of alphanumeric characters Following rules must be kept in mind when naming identifiers.  The first character must be a letter of the alphabet or an underline, and the remaining being any letter of the alphabet, any numeric digit, or the underline. Both upper and lower case letters can be used. Identifiers are case sensitive. Thus using "salary" for a variable is not the same as using "SALARY" and neither of them is same as using "saLAry" for a variable. All three refer to different variables. An identifier can use an underscore sign. For example the identifier " student_name " contains the underscore.   2  In old C up to eight characters can be used for identifiers. If more than eight are used, they may be ignored by the compiler. In Standard C this limit has changed to 31 characters. In practice the actual limit will depend on the C compiler. Variable Names in C A variable is a identifier used to represent a numerical value or a character. The value of the variable can change during the execution of the program. Following rules must be kept in mind when using variables:       Keywords of C language cannot be used as variable names. A variable name must start with an alphabet or the underscore (_) character. The digits 0 – 9 cannot be used to start the variable name. The first character can be followed by a sequence of letters and /or digits. A variable name can be a maximum of 8 characters in length. No special characters such as comma, space period, semicolon (;) are permitted in variable names. A variable name is case sensitive. Thus BOY, boy and Boy are regarded as three different variables in C. Some examples of valid variable names in C language are: area, radius, HEIGHT, due_date, Marks The following are invalid variable names. The reason why each name is invalid is also given. Variable break #salary due date 2008marks new.value Reason why it is invalid keyword must begin with an alphabetic character contains a space begins with a numeric character contains a period Basic Data Types in C Data in the C programming language are of two different types, namely numbers and characters. Each type of data requires different amount of memory. Numbers: These are further divided into two types: integer and real. A number without a decimal point is known as an integer (or a whole number). A number with decimal point is known as a real number. C defines two basic types of integers: int and long.   int: This requires 2 bytes of memory and can store 65536 different numbers. The range of these numbers are from – 32768 to +32767 long: This requires 4 bytes of memory and can store 4294967295 different numbers. Thus it can store larger numbers. The range of these numbers are from –2147483648 to +2147483647 3 C defines two basic types of real numbers: float and double.   float: This requires 4 bytes of memory. It is used to store numbers. In the range 3.4 × 10–38 to +3.4 × 10+38 double: This requires 8 bytes of memory. It is used to store numbers in the range 1.7× 10–308 to 1.7× 10+308 . Thus it can store larger numbers. To represent individual characters, C uses the char type. This requires only one byte of memory. Each represented character will have a corresponding integer value in the range 0 – 255. Thus a char is a special type of integer. Most machines today use the ASCII character set, in which the letter A is represented by the integer code 65, the digit 1 is represented by integer code 49, the space character is represented by the integer code 32, etc. The following table gives the memory requirement and range of various data types in C. Data type int long float double char Memory Used 2 bytes 4 bytes 4 bytes 8 bytes 1 byte Range – 32768 to +32767 –2147483648 to +2147483647 3.4 × 10–38 to +3.4 × 10+38 1.7× 10–308 to 1.7× 10+308 value range 0 – 255 Basic Constants in C There are four basic types of constant in C. They are: integer constant, floating-point constant, character constant and string constant. The first two represent numbers and are hence also known as the numeric constants. The following rules apply to numeric constants:     A negative constant is preceded by a minus sign. For positive constant the plus sign is optional. blank space and comma is not permitted within a constant. the value of constant must be within the specified range. If the number has two or more digits, the first digit must not be 0. 4 Integer Constant: A constant number without a decimal point is an integer constant. The following are examples of invalid numeric constant. The reason why each is invalid is also given. Integer Constant 12,89 0982 32_24 3.14 47 9 Reason why it is invalid contains a comma must not begin with zero must not contain underscore (_) integer constant cannot have a period(.) contains a blank space C allows a number to be written in three different number systems.   Decimal: This is the base 10 number system. This system uses the digits from 0 to 9. Octal : This is the base 8 number system. This system uses the digits 0 to 7. The first digit must be zero (0) in order to identify it as an octal number. Following are examples of valid octal integer constants. 012  038 04 015 0122 Hexadecimal: This is the base 16 number system. This system uses the digits 0 to 9 and A, B, C, D, E, F. The letters A through F represent the decimal quantities 10 through 15 respectively. A hexadecimal integer constant must begin with a 0x or 0X. It can then be followed by sequence of valid digits. Following are examples of valid hexadecimal integer constants. 0x12 0x3f9 0X4 0xABC 0xFFFF Floating Point Constant : A constant number with decimal is a floating point number. Such numbers may also be expressed in exponential (scientific) form. For example the value 356.3 can be written as 3.563e2 where e2 means multiply by 102. The mantissa must be real or integer. The exponent must be an integer a minus or optional plus sign. Following are examples of valid floating point constants. 0.33e-3 12.001 -123456 25.4E-8 1.232e+3 Character Constant : Character constant is a single character enclosed in apostrophes i.e. single quotation marks. The following are examples of valid character constants. ‘A’ ‘5’ ‘#’ ‘m’ ‘ ’ In above the last constant has a blank space enclosed in apostrophes. String Constant : String constant is a combination of valid character enclosed with quotation marks i.e. double quotes. The following are examples of valid string constants. “Arvi” “ARVI” “ReD” “12345” “3+x” A string constant can contain the newline character sequence \n. For example ‘I\n am\n happy’ is a valid string constant. 5 Test Yourself: 1. Identify the following as valid or invalid octal integer constants? 12 0319 0.34 045 02 2. Which of the following are invalid hexadecimal integer constants? 12 0x319 0x3.4 0xCDG 0xabcd 3. Why are the following numbers invalid floating point constants? $314 3,19.2 2e+1.4 2e 24 56E+1,4 Variable Declaration In general a C program will make use of variables to store data. All variables used in a program must be declared with specific data type. The syntax to declare a variable is: datatype v1,v2,... , vn ; where v1, v2,... vn are name of variables. The variables are separated by comma. A declaration statement must end with a semicolon The following lines declares four variables: a of type int , x of type float, m of type double and r of type char. int a; float x; double m; char r; Two or more variables of the same type can be declared in one line. int a,b,c; float x,y,weight; Assignment A variable can be assigned value after declaration. The example shows three variables declared of type integer. The variables are assigned values later in the lines that follow. int a,b,pi; .. .. .. .. .. .. a = 10; b = 20; pi = 3.1412; We can also assign values to variables at the same time when a variable is declared as follows: int age = 35; char code = ‘A’; 6 In these lines: age is an integer variable given initial value 35, and code is a char type variable given the initial value ‘A’ C also supports multiple assignments. For multiple assignments the following syntax is used: identifier 1 = identifier 2 = . . . . = expression; For example the value 55 is assigned to variables a, b and c as follows: a=b=c=55 ; Arithmetic Expressions In C, arithmetic expression is a combination of constants, variables and operators arranged as per the syntax of the language. Some examples of algebraic expression and C expression is given below: Algebraic Expression p+qr-35 5x2+5y-7 a ab bc+ c +3d b x(a+c+d) x=3.2x-y 1 1 y=x+x2 C Expression p+q*r-35 5*x*x+5*y-7 a/(b*c)+a*b/c+3*d a*(a+b/c+d) x=3.2*x-y y=1/x+1/(x*x) Evaluation of Arithmetic Expressions: The basic evaluation procedure is from the left to the right. An arithmetic expression within parentheses is evaluated left-to-right using the rules of precedence of operators. There are two priority levels for arithmetic operators in C.   High priority : * / % Low priority : + – The evaluation is done in two left-to-right passes. During the first pass the high priority operators are applied. During the second pass the low priority operators are applied. Consider the expression: x = a + b/4 – c*4 + 3 where a = 3, b=8, c =1. The expression for given values is : x = 3 + 8/4 – 1*4 + 3 Evaluation is as follows: First Pass Second Pass : x = 3 + 2 – 1*4 + 3 x = 3 + 2 – 4 + 3 : x = 5 – 4 + 3 (8/4 evaluated) (1*4 evaluated) (3+2 evaluated) 7 x = 1 + 3 x = 4 (5-4 evaluated) (1+3 evaluated) The order of evaluation can be changed by using parenthesis. When an expression is written in parenthesis it gets highest priority. For example, in the expression : 5+6/3– 1*(4+3), the expression 4+3 is evaluated first. When parentheses are nested, the innermost parentheses have highest priority. For example : 15-(16/(2+2)*2)+3 15-((16/2)+2)*2)+3 The first expression evaluates to 10, whereas the second expression evaluates to –2. Parentheses can also be used to improve the readability of the program. Expressions Expression is a single value or a combination of values and variables which are connected by operators. Expression can also be a logical condition that is true or false. In C the true and false condition are represented by integer value 1 and 0 respectively. Following are examples of expression : a+b-c ; x=a ; x>6 ; The first expression uses the arithmetic operators + and –, the second expression uses the assignment operator =, the third expression uses the relational operator >. These operators are discussed later. Questions: 1. State four rules for choosing a variable name. 2. What are escape sequences? Give three examples. 8 Some Simple C programs Program 1: A C program consists of one or more functions. In the example given below main() is such a function. All C programs must have a main() function. main() { printf("hello, world"); } Programs written in C language have to be compiled and then executed. There is no single compiler of C. These compilers differ from one another. Even on the same computer there may be several compilers. Execution of the above program begins at the first statement of main. The main function will usually invoke other functions to perform its job, some coming from the same program, and others from libraries. printf is a library function which will format and print output on the terminal (unless some other destination is specified). In this case it prints the message ‘hello, world’. The output is shown below. hello, world Program 2: Here's a bigger program that adds three integers and prints their sum. main () { int a, b, c, sum; a = 1; b = 2; c = 3; sum = a + b + c; printf("sum is %d", sum); } The output is shown below. sum is 6 Program 3: Here's a similar program that adds three floating point numbers and prints their sum. # include <stdio.h> main () { float a, b, c, sum ; a = 1.0 ; b = 2.0 ; c = 3.0; sum = a + b + c ; printf ("sum is %f", sum) ; } 9 The output is shown below. sum is 6.000000 10


Comments

Copyright © 2024 UPDOCS Inc.