🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Chapter 1 Overview of C.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Transcript

Unit I : Chapter 1 Overview of C Faculty In charge : Achsah Susan Mathew MCA, MBA, KSET We need language to communicate with computers We need English-like language(with syntax i.e C ) to work with computers(0 or 1) History of C C Programming language was devel...

Unit I : Chapter 1 Overview of C Faculty In charge : Achsah Susan Mathew MCA, MBA, KSET We need language to communicate with computers We need English-like language(with syntax i.e C ) to work with computers(0 or 1) History of C C Programming language was developed by Dennis Ritchie (Father of C) It was originally created for UNIX Operating System AT & T Bell Lab in USA, 1972 Why named C ? because many ideas and principles derived from B language B C ALGOL (short for Algorithmic Language) Root of all Modern language BCPL ("Basic Combined Programming Language") Features of C (Characteristics of C) 1.C is known as Middle level language, since it combines the features of high level programming as well as low level Programming 2.It is robust language(It has built-in functions, operators to write complex program) 3.It is highly portable (Program written for one computer can run on another computer) 4.C is a structured language (Programs are subdivided to modules) Each modules performs specific task 5. It is a Free-form language, it does not have restrictions while writing the program(can write multiple statements on the same line) 6. It is case sensitive language(uppercase and lowercase is different) Character set In the C programming language, the character set refers to a set of all the valid characters that we can use in the source program for forming words, expressions, and numbers. All the Character set of the c language is based on ASCII (American Standard Code For Information Interchange ) Alphabets Alphabets is a set of letter from A to Z or a to z Each alphabets have its own ASCII value Capital Alphabets and Small alphabets have different ASCII value We can use Actual Alphabets or its ASCII value. both are same work in our program Digits It is used to represent numbers 0 to 9 Each Digit's have its own ASCII value We can use Actual Digit's or its ASCII value. both are same work in our program Symbols/Special Characters Special Symbols used in the programs such as +,&,*,% ,{,] Each Symbol's have its own ASCII value We can use Actual Symbol's or its ASCII value. both are same work in our program White Space Characters It occupies an area on a page such as \t (tab) \n (go to new line or next line) Character Set C Tokens A token is the smallest unit used in a C program. Each and every punctuation and word that you come across in a C program is token. Each tokens specify with the particular meaning We have 6 Types of C Tokens Keywords It is a Vocabulary of a C Language It is also know are reserved words Fixed meaning and cannot be changed C language has 32 Keywords(written in small letters Identifiers When ever we write instruction it should be written in names for our identification It is given by the user therefore it is called as user-defined names It is used for naming variables, arrays, constant, functions Pick from the character set and give a name Rules for making a Identifiers 1. First character of the identifier should start with the alphabet’s or underscore ( _ ) 2. Digit can be used after the first character Marks1, Marks2,... ( Right ) 1Roll, 2Roll,... ( Wrong ) 3.Space is not allowed between the Identifier names 4. Underscore sign can be used for the Space Max_Marks, Min_Marks,... ( Right ) Max Marks, Min Marks,... ( Wrong 5. Keywords cannot be used as a Identifier int, float, char,.... ( Wrong ) int, float, char are keyword Int, Float, Char,..... ( Right ) Int is not a keyword so can be used 6. No Special Symbol are allowed ,*,{,& ( Wrong ) underscore ( _ )is allowed ( Right ) 7.Word limit : Only first 31 character are considered 8.Case sensitive (Uppercase and Lowercase is different) Achsah ACHSAH achsah Difference between Keywords and Identifiers Keywords Identifiers It is reserved It is a user defined name given for Variable, Functions Considers only Considers letter, letters underscore and digits Use only lowercase Lower case and Upper case are allowed No Special Symbol or Punctuation is No Punctuation or used Special Symbol used except Underscore Starts with Can Start with lowercase uppercase or lowercase or underscore Datatypes Data types Each variable in C has an associated data type Each data type requires different amount of memory It shows the type of the data that will be stored in the memory It is used to declare the variables or Identifiers Classification of Datatypes Primary Datatype Primary Data type is also called as Built-in data types It can be called as predefined data types/Fundamental data types/Primitive data type/Basic Datatype. This operates directly on the computers Memory What is Bit and Byte Bit means Binary value (0/1) 1 Byte = 8 bits Character(char) A Character is a single ASCII Character. Any symbol enclosed with two single quotes (‘ ’)is a character. %c as the format specifiers Syntax: datatype Variable_name; datatype Variable_name=character/value; char pen = ‘A’; or char try = 65; It takes 1 byte of Memory We can represent +ve numbers and –ve numbers Char Memory Representation Different types of Char Syntax for signed & unsigned signed char ch=-1; unsigned char b=‘a’; or char b=‘a’; Range of Values in Char Calculate the Range we use formula Total Value we can store is 256 Integer type (int) A number without fractional part (without decimal point) is called integer %d is the format specifiers Integer can be +ve number and –ve number It takes 2 byte old Machine/4 byte of Memory new machine Syntax: datatype Variable_name ; datatype Variable_name=value ; Eg: int a; int a=10; It is important to declare the datatype for a variable Correct way of writing(declaring) Int Memory Representation Range of Values in integer 16 bit Machine = 2 bytes Range b/w -32768 to +32767 Any number beyond this range is illegal/Error Range of Values in integer 32/64 bit Machine = 4 bytes Range b/w -2,147,483,648 to + 2,147,483,647 Any number beyond this range is illegal/Error Float type (float) A number with fractional part is called floating point Float in C is used to store decimal and exponential values %f is the format specifiers It can store both positive and negative decimal point values It occupies 4 bytes in Memory Eg: float a; float a=13.2; float with 6 digit of precision(Single Precisions) The Range of Values (Minimum or Maximum values) 3.4 * 10^-38 to 3.4 * 10^38 double type (double) It is used to hold large floating point numbers. When higher precision is required we use double %lf format specifier for double It occupies 8 bytes in Memory With 14 digit precisions (double precisions) The Range of Values (Minimum or Maximum values) 1.7 * 10^-308 to 1.7 * 10^308 Qualifiers/Modifiers These are Keywords, which is used to alter the basic data types based on size and sign Modifiers that are applied to the basic data types are Sign: Size: signed short unsigned long Syntax: Modifier Basic datatype variable name; Eg: short int a, b, c; unsigned int d=4; Qualifiers for char data type Char datatype can have signed and unsigned Qualifier long and short is not used with char data type Eg: signed char m; //range -128 to 127 unsigned char m; //range 0 to 255 Qualifiers for int data type All Qualifers i.e signed, unsigned, short and long can be used with int i)signed int and normal int is similar signed int a; Same as int a; //can hold both positive, negative number and Zero ii) unsigned int can store only positive number (basically no sign means positive number) Eg: unsigned int a; // only +ve number can be stored in a iii) short int required 2 bytes memory, In some machines short int have half the size of int Eg: short int a; // gets 2 byte iv) long int (Store large integer value) In 16 bit Machine int is 2 byte long int is 4 byte In 32 bit Machine int and long int are same i.e 4 byte long long int 8 byte (for larger int value Eg: long long int a; // gives 8 byte Qualifiers for float and double data type Change the size and sign of floating point numbers float has 4 bytes, if not sufficient then c offers double which is 8 bytes If that also is not sufficient c offers long double which is 10 bytes, In few complier 12 or 16 bytes of memory is offered Eg: long double d=100090;// 10 bytes or 12 or 16 bytes depended on the Machine Variables Is a named memory location Used to hold value that can be modified by the program All variables must be declared before they are can be used Syntax: data type variable-list; int i, j, sum; i ,j, and sum are called variables of type int All the variable declaration ends with semicolon C also uses to initialize variables int i = 10 , j = 20; sum; All the variables declared are stored in the memory Inside the memory Memory Address 325660 Value of the Variable 10 Name of a variable i Variable declaration will do two things 1.It tells the type(int, char, float, double) of the value the variable will hold 2.It tells the Compiler the name of the variable Rules for Variables It has the same rules like Identifiers Start with Alphabets not digits No Space,(_) underscore can be used No Special Symbol No Keywords Case sensitive Variables declared inside the function are called local variables Variables declared outside of all the functions are called global variables Types of Variables 1. Integer Variables 2. Float Variables 3. Double Variables 4. Character Variables 5. String Variables Integer Variables A number without fractional part +ve or –ve or 0 numbers Syntax: int variables1,variables2….variables n; Eg: int a,b,c; Float Variables A number with fractional part (eg 34.89) 6 digit precision Syntax: float variables1,variables2….variables n; Eg: float percentage,marks; Double Variables A larger point number with fractional part +ve or –ve decimal point values Gives upto 14 digit precision Syntax: double variables1,variables2….variables n; Eg: double pi,task; Character Variables Given in a single quote eg : ‘A’ Syntax: char variables1,variables2….variables n; Eg: char see, a; String Variables Given in a double quote “Hello” Syntax: char variables1[size],variables[….variables n; Eg: char name ; Constant Constant means something that do not change Once defined , later cannot be modified during the execution Syntax: const datatype variable=value; const float pi=3.14; Defining variable as a constant: Value cannot be changed later in the program const int x=10; ……. ……. ……. x=20; //wrong Error const int x; x=10;// Error We need to remember that constant variable can be initialized only at the time of declaration Constants Types of Constants Refers to the fixed values that do not change during the execution of a program C Constants Primary Secondary Array, Structure, Pointer Numeric Character Primary: Numeric Constants Numbers are referred to as numeric constants Two Types of numeric constant Integer constants Real constants (Fractional/Floating point and exponential value) Integer Constants It is a sequence of digits Any whole number value is an integer. No decimal point It can be +ve , -ve number No sign, then it is positive An integer constant refers to a sequence of digits without a decimal point. They can be represented as: Decimal Octal Hexadecimal Decimal Integer constant (base 10) It consists of any combinations of digits taken from the set 0 through 9, preceded by an optional – or + sign. The first digit must be other than 0. Embedded spaces, commas, and non-digit characters are not permitted between digits Valid: 32767 -9999 -23 Invalid: 12,245 - Illegal character (,) 10 20 30 - Illegal character (blank space) Octal Integer Constant (base 8) It consists of any combinations of digits taken from the set 0 through 7. with leading 0 If a constant contains two or more digits, the first digit must be 0. Valid: 037 0435 00 Invalid:0786 - Illegal digit 8 123 - Does not begin with zero 01.2 - Illegal character (.) Hexadecimal integer constant (base 16) Combination of 0 to 9 10-A/a 11-B/b 12-C/c 13-D/d 14-E/e 15-F/f Preceded by 0x or 0X Eg: 0x5, 0xa10 (valid) Ox5.5, 0FX, (Invalid) Real Constants Is often called as floating point constant Written in two forms  Fractional  Exponential forms Rules for Fractional part 1.Must have at least one digit(0 to 9) 2.Must have decimal point 3.Can be Positive or negative, Default sign is Positive 4.Space not allowed, Special symbol Rules for exponential form 1.Used to represent value which is either too small or too large, can have +ve , -ve value for exponent Syntax: mantissa e exponent Eg: 0.65 e 4 Mantissa and exponential part is separated by letter ‘e’ Mantissa part is either +ve or –ve, Default sign +ve Exponent must have at least one digit Blank Space is not allowed, no special symbol 0.65e4 //Valid 12e-2 // Valid 1,25.30 // Invalid 5*6e38 //Invalid Character Constants There are two character constants 1.Single Character They are character enclosed within a pair of single quote marks Eg: ‘5’ ‘x’ ‘;’ 2.String Character A sequence of character enclosed in double quotes The character may be letters, numbers and blank space Eg: “Hello” “3667” “x” Symbols In C programming language, generally, the special symbols have some special meaning and they cannot be used for other purposes. [] () {}, ; * = # Operators C - Operators, An operator is a symbol that tells the compiler to perform specific mathematical or logical function addition (+), subtraction (-), multiplication (*), division (/), Logical NOT (!) Logical OR (||) Logical AND (&&) Symbolic Constants It is Unique feature called Preprocessor directive (also Symbolic Constants) Preprocessor directive : Processes the source code before it passes through compiler Preprocessor has rules: 1. Start with # 2. No semicolon at the end Eg: #define #include Macro Substitution directives Macro is a defined name having a replacement text Syntax: #define Identifier Replacement _text Eg: #define two 3 #define X 100 #define X 100 main() main() { { int a,b,c; int a,b,c; a=10; a=10; b=20; b=20; c=X; c=100; a=a+X; a=a+100; b=X+b; b=100+b; } } Rules to be followed while defining the symbolic constants They are defined at the beginning No space between # and define # define two 3 //error #define should not end with semicolon #define two 3; //error Symbolic names or identifier is same as variable names (user defined names) Re-assigning of values once declared is not allowed #define two 3 two=7; //not allowed Additional Reference 1.General Structure of C Program 2.Creating and Executing C Program(Lab Content) Basic Structure of C Program Documentation Section: The documentation section contains a set of comment including the name of the program other necessary details. Comments are ignored by compiler and are used to provide documentation to people who reads that code Comments are be giving in C programming in two different ways: Single Line Comment -// This is single line comment Multi Line Comment- Link Section: The link section consists of header files Which link to the system library. The statement begins with # #include stdio.h is a header file which is included using the preprocessing directive #include. stdio.h means standard input and output (.) extension and h is header file Definition Section: The definition section defines all symbolic constants. A symbolic constant is a constant value given to a name which can't be changed in program. #define PI 3.14 #define MONTHS 02 In above code, the PI,MONTHS is a constant and its value is 3.14 Global Declaration There are some variables and those variables are declared in this section that is outside of all functions. A global variable is a variable that is declared outside all functions. A global variable can be used in all functions. A local variable is a variable that is declared inside a function. A local variable can only be used in the function where it is declared. #include int a, int b; // Global variables(declaration main() { int answer; // Local variable x = 5; y = 7; } A global function is a function that is declared outside all functions. Not mandatory but a good practice to mention at the begin to the compiler #include int a, int b; // Global variables int sum();// Global Function main() { int answer; // Local variable x = 5; y = 7; sum(); } int sum() { ……………… } main() Function Section The main () function section is the most important section of any C program. The compiler start executing C program from main() function. The main() function is mandatory in C Declaration Part - All the variables that are later used in the executable part are declared in this part. Executable Part - This part contains the statements that are to be executed by the compiler. main() { int a=10,b=20; // Declaration Part printf(“Hello User \n”); // Executable Part } Subprogram Section The subprogram section contains all the user defined functions. These function are called from the main function They are placed after main function MyFunction() { printf(“Hello”); } #include Link Section #include #define MONTHS 12 #define AMOUNT 1000 Definition Section myfunction(); Global function int a=10, b=20; Global Variables Global Declaration Section void main() main function section(execution begins) { int x=30,y,z; Declaration Section printf("%d",a); y=sqrt(MONTHS -2) ; z=AMOUNT + 300; myfunction(); } myfunction() User defined function { printf("%d %d", a,b); } Check your Learning 1.Identify the sections Which Section is this #include #include 2.Any statement that starts with # is called? 3. Abbreviate stdio.h 4.Identify the sections Which Section is this #define PI 3.14 5.Identify the sections Which Section is this 6.Identify the comment line // Welcome to C 7.Identify which section #include void main() { int p, q, monkey; int man; printf(“Learning”); printf(“is fun”); } 8.Where does the execution begin? 9.Identify which section #include void main() { int p, q, monkey; int man; } 10.Identify which section #include int a,b,c; void main() { int p, q, monkey; int man; } 11.Identify which section mysection() { int pen, q, donkey; } 12.Identify which section #include int a,b,c; mysection() void main() { int p, q, monkey; int man; mysection(); } mysection() { ----- } 13.Which section is ignored by the compiler ? 14.Identify local variables #include int a,b,c; mysection() void main() { int p, q, monkey; int man; mysection(); } mysection() { ----- } 15.Identify global variables #include int a,b,c; mysection() void main() { int p, q, monkey; int man; mysection(); } mysection() { ----- } 16. User defined function should be always called from? Executing a c Program How to Create C Program Using Turbo C To Save the program: Press “F2” To Compile the program: Press “Alt F9” To Execute the program: Press “Ctrl F9” Execution on Dev C++ Type your Programs here Programs not saved Select C.C file.exe file Compile (F9) No Errors Run (F10) Sample C Program #include void main() { printf(“Hello User \n”); } o/p: Hello User Addition of two number #include int main() { int a, b, c; printf("Enter two numbers to add\n"); scanf("%d%d", &a, &b); c = a + b; printf("Sum of the numbers = %d\n", c); return 0; } Thank you

Tags

C programming computer science programming languages
Use Quizgecko on...
Browser
Browser