1 / 50

INTRODUCTION TO C PRGRAMMING

INTRODUCTION TO C PRGRAMMING. About C. Developed in 1972 By Dennis Ritchie at AT & T Bell Lab in USA Many ideas took from BCPL and B languages so gave name C. Structured programming is possible by using Functions Extension is easily possible by introducing new libraries

sezja
Download Presentation

INTRODUCTION TO C PRGRAMMING

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. INTRODUCTION TO C PRGRAMMING

  2. About C • Developed in 1972 By Dennis Ritchie at AT & T Bell Lab in USA • Many ideas took from BCPL and B languages so gave name C. • Structured programming is possible by using Functions • Extension is easily possible by introducing new libraries • In 1989, C is accepted by ANSI (American national standardization institute)

  3. Why C if we have C++,C# and Java • Very difficult to learn C++,C# and Java without knowing C. • Major parts of OS like Windows,UNIX, Linux and Device drivers etc. are still in C because performance wise C is better than other. • C programs are comparatively time and memory efficient that’s why programs related to Mobile Devices, microwave ovens, washing machine etc. written in C. • Professional 3D games also written in C because of speed.

  4. First C Program /* First C Program */

  5. Original C Compiler IDE

  6. Console Screen or Result screen

  7. Preprocessor Directive • The text inside /* and */ is called comment or documentation. • The statement starting with # (hash sign) is called pre-processor statement. • The #include is a "preprocessor" directive that tells the compiler to put code from the header called stdio.h into our program before actually creating the executable. • By including header files, you can gain access to many different functions--both the printf and scanf functions are included in stdio.h. • getch has its prototype in conio.h header file. • stdio.h is a header file which has: • Prototype or declaration only of the library functions • Predefined constants Note: Header file does not contain the code of library functions. It only contains the header or prototype.

  8. main Function • A program may contain many functions, but it essentially contains a main function. • The return type of main function is kept int type. A program should return value. • 0 (zero) in case of normal termination.Non-zero in case of abnormal termination, i.e. termination due to some error.

  9. Steps of Program Execution • Pre-processing: • Pre-processing statements are processed first • All the statements starting with #(hash) sign are preprocessing statements • eg. #include and #define • Compilation: The syntactical validity of the complete program is checked • If there is no error the compiler generates the object code (.obj) • Linking: Symbolic Links are resolved in this phase • A function call is called symbolic link • A function can be user defined or ready made function in the library • The linker substitutes the name of the function with the address of the first instruction of the function • Execution: The Instruction of object code are executed one by one.

  10. Steps of Program Execution • Pre-processing • Compilation • Linking • Loading • Execution Alt + F9 Ctrl+ F9

  11. Types of Errors • Syntax error: When there is the violation of the grammatical ( Syntax) rule. Detected at the compilation time. • Semicolon not placed • Standard Construct not in proper format • Identifier not declared • Linker Error: When definition or code of a program is not found. • Path of header not properly defined • Library file not found • The function name is misspelled

  12. Types of Errors Cont... • Runtime error: It is generated during execution phase. Due to mathematical or some operation generated at the run time. • Division by zero • Square root of negative number • File not found • Trying to write a read only file • Logical Error: Produced due to wrong logic of program. Tough to identify and even tougher to remove. • Variables not initialized • Boundary case errors in loops • Misunderstanding of priority and associativity of operators

  13. Types of Statements in C Prog. • Preprocessing Statements: Also called compiler directives. The purpose depends on the type of commands. • # include statement inserts a specified file in our program • They don't exist after compilation • Declarative Statements: Used to declare user identifier i.e. to declare data types for example: int a, b; • These statements do not exist in the object code.

  14. Types of Statements in C Prog. Cont... • Executable Statements: The statements for which the executable or binary code is generated. For • Input/Output Statements like printf(), scanf() • Assignment Statements. The syntax is lvalue=rvalue • Conditional Statements like if(a>b) max =a; else max=b; • Looping Statements. Also called Iterative statements or repetitive statements. • For • while • do while • Function Call like y=sin(x);

  15. Types of Statements in C Prog. Cont... • Special Statements: There are four special statements • break • continue • return • exit

  16. Keywords & Identifiers • Every C word is classified as either a keyword or an identifier. • Keywords: The meaning of some words is reserved in a language which the programmer can use in predefined manner. Theses are called keywords or reserve words. For example: do, while, for, if, break, etc… • Identifiers refers to the names of variables , function, structure and array. • Examples: main, printf, average, sum etc.

  17. 32 -Reserved Words (Keywords) in C • 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

  18. Rules of Making Identifier or variable Rule1. Identifier name can be the combination of alphabets (a – z and A - Z), digit (0 -9) or underscore (_). E.g. sum50, avgUpto100 etc. Rule 2. First character must be either alphabet or underscore E.g. _sum, class_strength,height are valid 123sum, 25th_var are invalid Rule 3. Size of identifier may vary from 1 to 31 characters but some compiler supports bigger size variable name also. Rule-4. No space and No other special symbols(!,@,%,$,*,(,),-,+,= etc) except underscore are allowed in identifier name. • Valid name:  _calulate, _5,a_, __ etc. • Invalid name: 5@, sum function, Hello@123 etc. Rule 5. Variable name should not be a keyword or reserve word Invalid name: interrupt, float, asm, enum etc.

  19. Rule 6: Name of identifier cannot be exactly same as of name of function or the name of other variable within the scope of the function. • What will be output of following program? • #include<stdio.h> int sum(); int main(){   int sum; sum=sum();   printf("%d",sum);     return 0; } int sum(){ int a=4,b=6,c; c=a+b; return c;} Output: Compiler error

  20. What Are Variables in C? • Variables are named memory location. It may be used for storing a data value. A variable may take different values at different time during execution. Example intavg,length etc. • Naming Convention for Variables: • C programmers generally agree on the following conventions for naming variables. 1. Use meaningful identifiers eg for storing sum use variable name sum 2. Separate “words” within identifiers with underscores or mixed upper and lower case. Examples: surfaceArea, surface_Areasurface_area etc

  21. Case Sensitivity • C is case sensitive • It matters whether an identifier, such as a variable name, is uppercase or lowercase. • Example: area Area AREA ArEa are all seen as different variables by the compiler.

  22. Identify valid variable name Valid Valid Valid Invalid- no white space allowed Valid Invalid- no special symbol allowed other than _ Invalid- no keyword allowed Invalid- no special symbol allowed other than _ Invalid- numeral cant be 1st character Invalid- . Is special symbol Invalid- no keyword allowed • John • X1 • _ • Group one • Int_type • Price$ • char • (area) • 1ac • i.j • if

  23. The C Character Set • A character denotes any alphabet, digit or special symbol used to represent information. • Figure 1 shows the valid alphabets,numbers and special symbols allowed in C.

  24. Constants and Variables • The alphabets, numbers and special symbols when properly • combined form constants, variables and keywords. • A constant is an entity that doesn’t change whereas a variable is an entity that may change. • In any program we typically do lots of calculations. The results of these calculations are stored in computers memory. Like human memory the computer memory also consists of millions of cells. The calculated values are stored in these memory cells. To make the retrieval and usage of these values easy these memory cells (also called memory locations) are given names. Since the value stored in each location may change the names given to these locations are called variable names.

  25. Example of Variable and Constant Here 3 is stored in a memory location and a name x is given to it.Then we are assigning a new value 5 to the same memory location x. This would overwrite the earlier value 3, since a memorylocation can hold only one value at a time. Since the location whose name is x can hold different values at different times x is known as a variable. As against this, 3 or 5 donot change, hence are known as constants.

  26. Types of C Constants • C constants can be divided into two major categories: • (a) Primary Constants • (b) Secondary Constants • These constants are further categorized as shown in Figure

  27. Integer Constants • Rules for Constructing Integer Constants • An integer constant must have at least one digit. • It must not have a decimal point. • It can be either positive or negative. • If no sign precedes an integer constant it is assumed to be • positive. • No commas or blanks are allowed within an integer constant. • The allowable range for integer constants is -32768 to 32767. • Ex. 426 • +782 • -8000 • -7605 • 25,000, 67 000 are invalid integers

  28. Real or Floating Point Constants • Rules for Constructing Real Constants • Real constants are often called Floating Point constants. The real constants could be written in two forms—Fractional form and Exponential form. • Following rules must be observed while constructing real constants expressed in fractional form: • A real constant must have at least one digit. • It must have a decimal point. • It could be either positive or negative. • Default sign is positive. • No commas or blanks are allowed within a real constant. • Ex.: +325.34, 426. , -32.76 , -48.5792, +.5

  29. Exponential Form of Real constants • Used if the value of the constant is either too small or too large. • In exponential form of representation, the real constant is • represented in two parts: • The part appearing before ‘e’ is called mantissa, whereas the part following ‘e’ is called exponent. • Rules: • The mantissa part and the exponential part should be separated by a letter e. • The mantissa part may have a positive or negative sign. • Default sign of mantissa part is positive. • The exponent must have at least one digit, which must be a • positive or negative integer. Default sign is positive. • Range of real constants expressed in exponential form is -3.4e38 to 3.4e38. • Ex.: +3.2e-5, 4.1e8, -0.2e+3, -3.2e-5 • 7500000000 will be 7.5E9 or 75E8

  30. Character Constant • Rules for Constructing Character Constants 1. A character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas. 2. Both the inverted commas should point to the left. For example, ’A’ is a valid character constant whereas ‘A’ is not. 3. The maximum length of a character constant can be 1 character. • Ex.: 'A‘, 'I‘, '5‘, '='

  31. Declaring Variables • Before using a variable, you must give the compiler some information about the variable; i.e., you must declare it. • The declaration statement has following format • <Data Type> <Variable name> • Data type will indicate the type of variable just like variable may integer type so data type int will be used. • Examples of variable declarations: • int length ; • float area ; • char ch;

  32. Declaring Variables (con’t) • When we declare a variable • Space is reserved in memory to hold a value of the specified data type • That space is associated with the variable name. • That space is associated with a unique address. • If we are not initializing variable than space has no known value(garbage value). • Visualization of the declaration • int a ; a 3718 2000 int

  33. Declaring Variables (con’t) meatballs garbage FE07 int

  34. Notes About Variables • You must not use a variable until you somehow give it a value. • You can not assume that the variable will have a value before you give it one. • Some compilers do, others do not! This is the source of many errors that are difficult to find. • Assume your compiler does not give it an initial value!

  35. Using Variables: Initialization • Variables may be be given initial values, or initialized, when declared. Examples: int length = 7 ; float diameter = 5.9 ; char initial = ‘A’ ; length 7 diameter 5.9 initial ‘A’

  36. Data Types • To deal with some data, we have to mention its type (i.e. whether the data is integral, real, character or string etc.) So data types are used to tell the types of data. • Data Types Categories

  37. Fundamental or Primitive Data Types

  38. Data Types Range

  39. Data Types Format Specifier

  40. Sample C Program a #include<stdio.h> #include<conio.h> void main() { int a; char b; float c; a=10; b=‘A’; c=50; printf(“b=%c ,a=%d,c= %f”,b,a,c); getch(); } Output:

  41. Sample C Program a #include<stdio.h> #include<conio.h> void main() { int a; char b; float c; a=10; b=‘A’; c=50; printf(“b=%c ,a=%d,c= %f”,b,a,c); getch(); } Output:

  42. Sample C Program(Modified-1) a #include<stdio.h> #include<conio.h> void main() { int a; char b; float c; a=10; b=‘AB’; c=50; printf(“b=%c ,a=%d,c= %f”,b,a,c); getch(); } Output:

  43. Sample C Program(modified-2) #include<stdio.h> #include<conio.h> void main() { int a; char b; float c; a=10; b=‘ABC’; c=50; printf(“b=%c ,a=%d,c= %f”,b,a,c); getch(); } Output:

  44. Sample C Program(modified-2) #include<stdio.h> #include<conio.h> void main() { int 5a; char b; float c; 5a=10; b=‘A’; c=50; printf(“b=%c ,a=%d,c= %f”,b,5a,c); getch(); } Output:

  45. Sample C Program(Modified-3) a #include<stdio.h> #include<conio.h> void main() { int a; char b; float c; a=32769; b=‘A’; c=50; printf(“b=%c ,a=%d,c= %f”,b,a,c); getch(); } Output:

  46. Range of Unsigned int

  47. Overflow in Unsigned int • If number is X where X is greater than 65535 then New value = X % 65536 If number is Y where Y is less than 0 then New value = 65536– (Y% 65536) (Take Y without sign) • Example: • unsigned int X=67777; • unsigned int Y= -10; • X=2241 • Y=65526

  48. Rang of int

  49. Range of int If number is X where X is greater than 32767 then p = X % 65536 if p <=32767 then New value = p else New value = p - 65536 If number is Y where Y is less than -32768 then p = Y % 65536 (Take Y without sign) If p <= 32767 then New value = -p else New value = 65536 -p • Example: int X=32768; =-32768 int Y= -65537; = -1

More Related