×
Don’t watch the clock; do what it does. Keep going.
--Your friends at LectureNotes
Close

Data Structure using C

by Prabu Selvam
Type: NoteViews: 17Uploaded: 17 days agoAdd to Favourite

Share it with your friends

Suggested Materials

Leave your Comments

Contributors

Prabu Selvam
Prabu Selvam
K. RAMAKRISHNAN GROUP OF INSTITUTIONS, TRICHY Course code – Course Name Year & Semester _____________________________ UNIT 1 - C PROGRAMMING BASICS 1.1 Structure of a C program C program is consists of 6 main sections: Documentation Section • Consists of comment lines which include the name of programmer, the author and other details like time and date of writing the program. • Documentation section helps anyone to get an overview of the program. • Eg: //Hello World Link Section • Consists of the header files of the functions. • It provides instructions to the compiler to link functions from the system library. • Eg: #include<stdio.h> Definition Section • All the symbolic constants are written in definition section. • Macros are known as symbolic constants. • Eg: #define PI 3.141 Global Declaration Section • The global variables that can be used anywhere in the program are declared in global declaration section. main() Function Section • It is necessary have one main() function section in every C program. • This section contains two parts, Declaration and Executable part. • The declaration part declares all the variables that are used in executable part. • These two parts must be written in between the opening and closing braces. • Each statement in the declaration and executable part must end with a semicolon (;). Subprogram Section • The subprogram section contains all the user defined functions that are used to perform a specific task. 1.2 Compilation and linking processes There are three basic phases occurred when we execute any C program. • Preprocessing • Compiling • Linking Unit 1 Page 1
Course code – Course Name Year & Semester : _____________________________ Preprocessing Phase C pre-processor is a program that accepts C code with preprocessing statements and produces a pure form of C code that contains no preprocessing statements (like #include). Compilation Phase The C compiler accepts a preprocessed output file from the preprocessor and produces a special file called an object file. Object file contains machine code generated from the program. Linking Phase The link phase is implemented by the linker. The linker is a process that accepts as input object files and libraries to produce the final executable program. 1.3 Constants/Literals • • • A constant is a value or an identifier whose value cannot be altered in a program. For example: 1, 2.5, "C programming is easy", etc. The different types of constants in C are: o Integer constants ▪ An integer constant is a numeric constant without any fractional or exponential part. ▪ ▪ ▪ Decimal constants: 0, -9, 22 etc Octal constants: 021, 077, 033 etc Hexadecimal constants: 0x7f, 0x2a, 0x521 etc o Floating-point constants ▪ A floating point constant is a numeric constant that has either a fractional form or an exponent form. ▪ For example: -2.0, 0.0000234, -0.22E-5 Unit 1 Page 2
Course code – Course Name Year & Semester : _____________________________ o Character constants ▪ A character constant is a constant which uses single quotation around characters. ▪ For example: 'a', 'l', 'm', 'F' o Escape Sequences ▪ Cannot be typed or has special meaning in C programming. Escape Sequences Character \b Backspace \n Newline \r Return \t Horizontal tab \v Vertical tab o String constants ▪ String constants are the constants which are enclosed in a pair of double-quote marks. ▪ For example: "good", "", " ", "x", "Earth is round\n" 1.4 Variables • • • • • • • In programming, a variable is a container (storage area) to hold data. To indicate the storage area, each variable should be given a unique name (identifier). For example: int apple = 20; Here, apple is a variable of integer type. The variable is assigned value: 20 The value of a variable can be changed, hence the name 'variable'. Rules for naming a variable in C o Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_). o An identifier cannot start with a digit. o Keywords cannot be used as identifiers. o No spaces are allowed o Example: ▪ _apple=20 #Valid ▪ if=20 #Invalid ▪ fruit_apple=20 #Valid ▪ fruit apple=20 #Invalid 1.5 Data Types in C • Unit 1 Fundamental Data Types o Integer types o Floating type o Character type Page 3
Course code – Course Name • • • • • • • • Unit 1 Year & Semester : _____________________________ Derived Data Types o Arrays o Pointers o Structures o Union User Defined data types o typedef o enum int - Integer data types: o Integers are whole numbers that can have both positive and negative values but no decimal values. o Example: 0, -5, 10 o In C programming, keyword int is used for declaring integer variable. o For example: int id; o Here, id is a variable of type integer. o You can declare multiple variables at once in C programming. o For example: int id, age; float - Floating types: o Floating type variables can hold real numbers such as: 2.34, -9.382, 5.0 etc. o We can declare a floating point variable in C by using either float or double keyword. o For example: float accountBalance; double bookPrice; o Here, both accountBalance and bookPrice are floating type variables. o In C, floating values can be represented in exponential form as well. o For example: float apple = 22.442e2; o The size of float is 4 bytes and the size of double is 8 bytes. char - Character types o Keyword char is used for declaring character type variables. o For example: char test = 'h'; o Here, test is a character variable. The value of test is 'h'. o The size of character variable is 1 byte. Arrays o An array is a collection of data that holds fixed number of values of same type. o For example: if you want to store marks of 100 students, you can create an array for it. o For example: float marks[100]; Pointers o In C, there is a special variable that stores just the address of another variable. It is called Pointer variable or, simply, a pointer. o For example: int* p; Structures o Structure is a collection of variables of different types under a single name. o Keyword struct is used for creating a structure. Page 4

Lecture Notes