×
Study like there’s no tomorrow because if you keep putting off your studies for tomorrow, you’ll probably be too late.
--Your friends at LectureNotes
Close

Note for Programming in C - C By smiley sindhu

  • Programming in C - C
  • Note
  • 14 Views
  • Uploaded 9 months ago
0 User(s)
Download PDFOrder Printed Copy

Share it with your friends

Leave your Comments

Text from page-1

SASTRA University I Btech Programming in C IV. C Language Components The four main components of C language are 1) The Character Set. 2) Tokens 3) Variables 4) Data Types 1) The Character Set : Character set is a set of valid characters that a language cab recognize. A character represents any letter, digit or any other sign.  Letters - A,B,C……Z or a,b,c …………z.  Digits - 0,1…………9  Special Symbols - ~!@#$%^&.........  White Spaces - Blank space, horizontal tab, carriage return, new line, form feed. 2) Tokens: The smallest individual unit in a program is known as a token. C has five tokens i. Keywords ii. Identifiers iii. Constants iv. Punctuations v. Operators 2.1. Keywords: Keywords are reserved word in C. They have predefined meaning cannot changed. All keywords must be written in lowercase. Eg:- auto,long,char,short etc. 2.2. Identifiers: - Identifiers refer to the names of variable, functions and arrays. These are user-defined names. An identifier in C can be made up of letters, digits and underscore. Identifiers may start with either alphabets or underscore. The underscore is used to make the identifiers easy to read and mark functions or library members. 2.3. Constants: - Constants in C refers to fixed values that do not change during the execution of a program. C support several types of constants. 2.3.1. Numerical Constants i. Integer Constant 1. Decimal Constant 2. Octal Constant 3. Hexadecimal Constant ii. Float Constant 2.3.2. Character Constants i. Single Character Constant ii. String Constant i) Integer Constant: - An integer constant is a whole number without any fractional part. C has three types of integer constants. Decimal Constant: - Decimal integers consists of digits from 0 through 9 Eg.: 34,900,3457,-978

Text from page-2

Octal Constant: - An Octal integer constant can be any combination of digits from 0 through 7. In C the first digit of an octal number must be a zero(0) so as to differentiate it from a decimal number. Eg.: 06,034,07564 Hexadecimal Constant: Hexadecimal integer constants can be any combination of digits 0 through 9 and alphabets from „a‟ through „f‟ or „A‟ through „F‟. In C, a hexadecimal constant must begin with 0x or 0X (zero x) so as to differentiate it from a decimal number. Eg:- 0x50,0XAC2 etc ii) Floating Constants (Real): Real or floating point numbers can contain both an integer part and a fractional part in the number. Floating point numbers may be represented in two forms, either in the fractional form or in the exponent form. A float point number in fractional form consists of signed or unsigned digits including decimal point between digits. E.g:- 18.5, .18 etc. Very large and very small numbers are represented using the exponent form. The exponent notation use the „E‟ or „e‟ symbol in the representation of the number. The number before the „E‟ is called as mantissa and the number after forms the exponent. Eg.:-5.3E-5,-6.79E3,78e05 i) Single Character Constant: - A character constant in usually a single character or any symbol enclosed by apostrophes or single quotes. Eg.: ch=‟a‟ ii) String Constant: - A sequence of character enclosed between double quotes is called string constant. Eg.: ―Hello Good Morning‖ 2.4) Punctuations: - 23 characters are used as punctuations in C. eg: + _ / ; : > ! etc 2.5) Operators: - An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation on data stored in variables. The variables are operated as operands. C operator can be classified into 8 types. i. Arithmetic Operators : + - * / % , These are known as binary arithmetic operators, since they operate on two values (or terms or operands) ii. Assignment Operators : = iii. Relational Operators: < > <= >= == != iv. Logical Operators:! && || v. Conditional Operators: ! : vi. Increment & Decrement Operator : ++ -vii. Bitwise Operator:! & | ~ ^ << >> viii. Special Operator : sizeof ,(comma) 3) Variables: A variable is an object or element that may take on any value or a specified type. Variable are nothing but identifiers, which are used to identify variables programming elements. Eg: name, sum, stu_name, acc_no etc. 4) Data types: Data types indicate the types of data a variable can have. A data types usually define a set of values, which can be stored in the variable along with the operations that may be performed on those values. C includes two types of data. Simple or Fundamental data type Derived data type 4.1 Simple Data Types: There are four simple data types in C.  int  char float  double int:- This means that the variable is an integer are stored in 2 bytes, may range from -32768 to 32767. char:- This means that the variable is a character type, char objects are stored in one byte. If unsigned, the values may be in the range 0 to 255.

Text from page-3

Float:- This means that the variable is a real number stored in 4 bytes or 32 bits. The range of floating point values are between 3.4E-38 to 3.4E38 or 6 significant digits after the decimal point. Double: This means that the variable is a double precision float type. In most cases the systems allocates 8 bytes or 64 bits space, between 1.7E-308 to 1.7E308. 4.2 Derived Data Types: Derived data types are constructed from the simple data types and or other derived data types. Derived data include arrays, functions, pointers, references, constants, structures, unions and enumerations. Variable Type Character Unsigned character Integer Short integer Long integer Keyword char unsigned char int short int long int Bytes Required 1 1 2 2 4 Unsigned integer Unsigned short integer unsigned int unsigned short int 2 2 long integer Float Double unsigned long int float double 4 4 8 Long Double long double 10 Range -128 to 127 0 to 255 -32768 to +32767 -32768 to 32767 -2147483648 2147483647 0 to 65535 0 to 65535 to 0 to 4294967295 3.4E +/- 38 (7 digits) 1.7E +/- 308 (15 digits) 3.4E-4932 to 1.1E+4932 Ex: The Boolean Data Type _Bool A _Bool variable is defined in the language to be large enough to store just the values 0 and 1.The precise amount of memory that is used is unspecified. _Bool variables are used in programs that need to indicate a Boolean condition. For example, a variable of this type might be used to indicate whether all data has been read from a file. By convention, 0 is used to indicate a false value, and 1 indicates a true value. When assigning a

Text from page-4

value to a _Bool variable, a value of 0 is stored as 0 inside the variable, whereas any nonzero value is stored as 1. To make it easier to work with _Bool variables in your program, the standard header file <stdbool.h> defines the values bool, true, and false. Ex: _Bool boolVar = 0; printf ("boolVar = %i\n", boolVar); Types _Complex and _Imaginary //Chapter number 4,Pg.39 _Complex and _Imaginary data types enable complex and imaginary numbers to be declared and manipulated, with functions in the library for supporting arithmetic on these types. Normally, you should include the file <complex.h> in your program for working with complex and imaginary numbers. Support for _Complex and _Imaginary types is optional for a compiler. float _Complex - Complex number. double _Complex - Extended accuracy complex number. long double _Complex - Extra extended accuracy complex number For example, a double_Complex variable c1 can be declared and initialized to the value 5 + 10.5i with a statement such as: double _Complex c1 = 5 + 10.5 * I; Library routines such as creal and cimag can then be used to extract the real and imaginary part of c1, respectively. An implementation is not required to support types _Complex and _Imaginary, or can optionally support one but not the other. II Operators and Expressions Precedence of Operators: Precedence is defined as the order in which the operators in a complex evaluation are evaluated. Associativity: When two or more operators have the same precedence then the concept of associativity comes into discussion. Precedence and Associativity of various operators Operator Description [] . () ++ -access array element access object member invoke a method post-increment post-decrement ++ -- + - ! ~ pre-increment pre-decrement unary plus unary minus logical NOT bitwise NOT () new cast object creation Level 1 Associativity left to right 2 right to left 3 right to left */% +-+ multiplicative additive string concatenation 4 5 left to right left to right << >> >>> < <= > >= == != shift relational type comparison 6 7 left to right left to right equality 8 left to right & ^ | && bitwise AND bitwise XOR bitwise OR conditional AND 9 10 11 12 left to right left to right left to right left to right

Lecture Notes