0% found this document useful (0 votes)
42 views43 pages

C Language Lec 2. High Level Language & Beginning of C: Constant, Identifier, Keyword, Data Types, Variables & Etc

Uploaded by

Tech Bandits
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
42 views43 pages

C Language Lec 2. High Level Language & Beginning of C: Constant, Identifier, Keyword, Data Types, Variables & Etc

Uploaded by

Tech Bandits
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 43
Computer Programming Using C Lecture - 2 High Level Language, Beginning of C: Constant, Identifier, Keyword, data types, variables, etc. Contents High Level Language Introduction to C Basic Structure of C Program Programming Elements Header File Library functions, printf and scanf Identifier Constant Keyword Data type Variable * Errors High-level Languages * Enables a programmer to write programs that are independent of a Certs) computer. E.g. C, C+4, Java, etc. High Level Programming CC) * Such languages are considered high- Mewitsttcdoelcn level because they are closer to Pesve r human languages and far from Low Level machine language. Programming Fee Hardware * It provides strong abstraction from the details of the computer. Wey High-level Languages sale_price = 1.66 . Tra Machine Level Code High Level Code (C program) Introduction to C Itis high level programming language and invented by Denis Ritche in 1972 at Bell Laboratory (AT & T) in USA. No full form of C. It is invented after B, so it is called C. It has replaced the ALGOL, PL/I, etc. Advantages: * It is used in designing software's, such as Windows, MS Office, Oracle, Unix, Linux, Visual Basic, SQ| Server, Compiler, etc. * C provides foundation for learning other high level programming languages. Learning a Language + Learning English: Alphabets Word Sentence (ello am Good (abred, (Hello, Good, ete.) (Hello | am Good.) I want to learn C.) * Learning C: Character Set Keyword, Instruction or Program (a, b.6 dso tes Identifier, etc. Statement (int a=10; 1, %,, >, <, ete.) int, main, 10, ete, Int b=a420;) Programming Elements * Character Set: Symbols used to express thoughts. + Alphabets: A, B,C, D, .....Z.a, b, Cw. Z. * Digits: 0, 1, 2,.3, 4, 5, 6, 7,8, 9. * Special Symbols: !, ~, 2, $, & @,4,5% /5=<>4% “sinb)blohl~ * Keywords: keywords are the special words whose meaning is already explained to the compiler. They are reserved for special purpose and t cannot be used for other purpose. * Chas 32 keywords: int, float, double, char, short, signed, unsigned, long, if, else, switch, case, default, goto, for, while, do, break, continue, void, sizeof, static, auto, extern, register, volatile, return, typedef, const, struct, enum, union. Programming Elements + Identifiers: Identifiers are the name of program elements, such as variable name, function name, array name, etc. + Example: int x =10, here x is a identifier. * Naming Rules of Identifiers: * First character should not be a digit * Spaces and comas, are not allowed * Special symbols are not allowed except underscore (_). * The length cannot be more than a particular number of characters or depends on the compiler. + Example: * Turbo C: 8 character + ANSI C: 15 character * VC++ : 30 character id C++ : 80 character. Dr. Vijaypal Rathors eee Programming Elements * Constant: Constant means fixed value which cannot be changed during execution. Constant Integer Real (float) Single String (10, -4, 1, 0) 2.5, ~4.3, 0.12, 5.0 (m8, 9,7) “Bennett”, “Hi”, “A”, ““ * Note: all types of constants are always called a R-values. Means, we cannot apply any type of constant at the left side of assignment operator. ® A’ =¥ +10; + Example: Y+10= ws Programming Elements: Variable * Variable: It is a named location in computer memory whose value can be changed during program execution. * Syntax for Declaration of Variable: + data type identifier; ch Note: Always use a variable name which reflects its purpose. Example: 3 100 char ch; int x; ___—————> | Garbage * Declaration and initialization: 200 + data type identifier= value; Example: char c int x= 10; ch} 65 100 Structured programming * Only the following code structures are used to write programs: 1.Sequence of sequentially executed statements. 2.Conditional execution of statements (i.e., "if" statements). 3.Looping. 4.Structured SubRoutine calls (e.g., 'gosub' but not 'goto’). ome eece esi eka) Structured Programming contd.. + Structured Programming is generally a non-issue when doing ModularProgramming or ObjectOrientedProgramming as it's assumed that individual methods are structured. ome eece esi eka) Types of Variable + C language defines two types of variables basis of their visibility in the program. * Global Variable * Local Variable * Global Variable: * These variables are declared or defined outside all the function body and visible throughout the program. * The value of these variables can be utilized by any instruction and function in the program. b * Local Variable: * These variables are defined and declared inside the function body and visible only to that function area. * The value of these variables can be utilized by only the instructions written inside! that function. PADDY) acl ireesrien= ia) Programming Elements * Data Types: The stored information is called data and type of data is called data type. + Example: The student informatidh Information Name Roll Number Marks cae (amit) 302 Character or Strin Data * All these information stored in computer memory. The size of memory is measured in terms of bit (b), Byte (B), Kilo-Byte (KB), Mega-Byte (MB), Giga-Byte (GB), etc. * Bit is a smallest unit of memory that is equivalent to 1 or 0. Dr. Vijaypal Rathor's screen Dr. Vijaypal Rathor's screen Units of Computer Memory Measurements 1 Bit = Binary Digit 8 Bits = 1 Byte 1024 Bytes =1KB [Kilo Byte] 1024 KB = 1 MIB [Mega Byte] 1024 MB = 1GB [Giga Byte] 1 TB [Terra Byte] = 1PB [Peta Byte] = 1 EB [Exa Byte] =12B [Zetta Byte] = 1 YB [Yotta Byte] = 1Bronto Byte 1024 Brontobyte = 1 Geop Byte Geop Byte is the Highest Memory. Programming Elements: Data Types in C — ., Primary Secondary (Primitive(Basic/Fundamental) Derived/User defined + Character: char + Array + Integer: int + Pointer + Float: float, double + string + Structure + Enum + Union Dr. Vijaypal Rathor's screen Programming Elements: Data Types in C Pentre tied ere oe char, Lyte -128 to 127 int 2byte -32,768 to 32,767 %d 1.2E-38 to 3.4E+38 a ie (Precision: 6 decimal places) xt Sean ptr 2.3€-308 to 1.76+308 vst (Pre jon: 15 decimal places) Note: The size of the data type varies with different compilers. + The memory space require by the data type to store a value, is known as its size. The size also decides the range of the data type. The range for int/char data types can be represented as -2"* to 2"-1, where n is number of bits, Data Types: Specifiers and Modifiers Primitive data types are used for basic & small calculations of the numbers but there is need to store large and specific numbers. C provides two key elements for this: 1. Specifier 2. Modifier Specifier: Specifier is the concept which defines the sign of the data value which is going to store in the memory. Types of Specifier: * signed: Anumber can be +ve or -ve * unsigned: A number always be positive Note: By default every data type is considered as signed. Data Types: Specifiers * Syntax: specifier data_type variable_name; > Example: unsinged char unsigned int x signed char ch; signed char 1 byte -128 to 127 unsigned char _—1 byte Oto 255 %e signed int 2byte -32,768 to 32,767 %d unsigned int 2byte 0 to 65,535 Sou Dr. Vijaypal Rathor's screen Data Types: Modifiers * Modifier: Modifier is the concept which defines the size of particular variable. It is used to increase the size of a data type. + Types of Specifier: + short: A number can be +ve or -ve + long: A number always be positive * Note: By default every data type is considered as short. Character can never be long. * Syntax: modifier data_type identifier; Example: short char ch; long int x; Dr. Vijaypal Rathor's screen Data Types: Modifiers short char 1 byte -128 to 127 %C short int 2 byte -32,768 to 32,767 %d longint 4 byte -2** to (2°*-1) %ld 3.4E-4932 to 1.1E+4932 %if long double 10 byte (19 decimal places) Dr. Vijaypal Rathor's screen i Combining Specifiers and Modifiers + A program can use modifier with specifier at the same time. * Syntax: specifier modifier data_type identifier; Example: unsinged Iyng int x a unsigned longint 4 bytes Oto (2-1) %lu * Note: We cannot use specifier and modifier simultaneously with other data types. Dr. Vijaypal Rathor's screen Cis Primary Data Types in C unsigned char int unsigned int long int unsigned long int float double 8 byte long double 10 byte -128 to 127 Oto 255 32,768 to 32,767 0 to 65,535 =2" to (2-1) Oto (2”-1) 1.2E-38 to 3.4E+38 (Precision: 6 decimal places) 2.3E-308 to 1.7E+308 (Precision: 15 decimal places) 3.4£-4932 to 1.1+4932 (Precision: 19 decimal places) %Id % lu MF if S6lf Use of Sizeof() * It is library function which returns the size of a variable/value in bytes. + Example: int a= 10; float f =3.2; double char cl * What will be the value of X: Answers: * Note: By default the real values 1. X= sizeof(a); eee are considered as double. 2. X= sizeof(10); 2.X=2 3. X= sizeof(f); 3. X=4 * Note: 4’ is interpreted as 65, 4. X = sizeof(3.2); 4.X=8 which integer. 5. X = sizeof(3.2f); 5. X=4 6. X=sizeof(ch); eaxeui 7. X= sizeof(‘A’); 7.X=2 8. X= sizeof(d); 8. X=8 Exercise on Data Types Ex. 1: unsigned int printf(“%u”, a); Output: ? Ex. 2: unsigned char ch = printf(“s6u", a); Output: ? Ex. 3: unsigned char eh = 256; printf(“%6u”, a); Output: ? Ex. 4: dint x = 65536; printf(“%u”, a); Output: ? Ex. 5: unsigned int a= printf("%d”, a); Output: ? Ex. 5: unsigned char ch = -1; printf("%d”, a); Output: ? Exercise on Data Types Ex. 1: unsigned int printf(“%u”, a); Output: 65535, Ex. 2: unsigned char ch = printf(“s6u", a); Output: 255 Ex. 3: unsigned char eh = 256; printf(“%6u”, a); ‘Output: 0 Ex. 4: d int x = 65536; printf(“%u", a); Output: 0 Ex. 5: unsigned int a= printf("%d”, a); Output: -1 Ex. 5: unsigned char ch = -1; printf("%d”, a); Output: 255 Al wes Exercise on Data Types er: m9: oma ono 7 8: ence oma =r int a= 62768; printf(“%d”, a); printf(“%u", a) Output: -62768 62768 Ex. 8: char a = 128; printf(“%d”, a); printf(“%u”, a) Output: -128 65408 Dr. Vijaypal Rathor's screen Exercise on Data Types Ex. 9: inta=-1; print#(“%d”, a); printi("s6u”, a); Output: -1 65535 Ex. 10: chara=‘A; printf(“%c”, a); printf(“%d", a); printf(“%u", a); Output: A 6s 65 Escape Sequences \n New Line \t Horizontal Tab \b BackSpace \r Carriage Return \a Audible bell Xv Printing single quotation Y printing double quotation ? Question Mark Sequence \ Back Slash \f Form Feed Ww Vertical Tab \o Null Value \nnn Print octal value \xhh Print Hexadecimal value ir Basic Structure of C Program Header Files: A file containing the declarations of functions that are used in source files or programs. A header file is normally included in a source file with an # include directive. : Standard input output header file. Provides functions for input/output operations. Mostly used Functions: + printf() + scanf()
; ; main() & ; ; ; ; ) iG } Input/output Functions Console Input/Output functions Formatted functions Unformatted functions Type | Input | Output Type | Input Output char | seanf() | printf) char [| getch() | puteh() getche() | putchar() getchar( ) int__[ scanft) | printf) int : - float | scanf() | printf() float - - string | scanf() | printf) string | gets() puts() Note: Formatted can take any type value, whereas unformatted character oriented and cannot work with escape sequences. Console based I/O * Unformatted Input: + getch() : Not display the entered character and does not wait for enter key. * getche() : Display the entered character and does not wait for enter key. * getchar(): Display the entered character and wait for enter key. Similar to scanf(). * gets(): Display the entered string and wait for enter key. * Unformatted Output: * putch(): It is used to display all alphanumeric characters + putchar(): It is used to print one character on the screen, and this may be any character from C character set (i.e., it may be printable or non-printable characters). * puts(): It is used to display the string. PADDY acl ireerien= is) Formatted I/O: Printf() and Scanf() printf(): It is a formatted function of stdio.h header file, which is used to print/display some value and/or message on the monitor screen. Syntax: To display message: _print{(’"Text String” To display value: printf(“format string”, variable_name); + Example: —printi("Hello Bennett”); —> Hello Bennett int x =5; char ch =‘A’; printf("X: %d, Ch: %c”, x, ch); —p> X:5, Ch: A scanf{): It is a formatted function of stdio.h header file, which is used to scan or take some input value in program from keyboard through monitor screen. Syntax: scanf(“format string”, &variable_name); __ Wait until the some value entered + Example: int x= 5; float f= 3.5; char ch ="A’; scanf(“%ed, %f, %ch”, &x, &f, &ch); —p 105.5 B el printf(“X: %d, Ch: %e”,%, ch); —p> X: 10, Ch: B First C Program #include main() { pripitf(“Hello Bennett”); printf(“I am Here”); #include main() { printf(“Hello Bennett”); printf(“\n | am Here”); Output: Hello Bennett! am Here Output: Hello Bennett lam Here Execution of C Program + Load C or Open the Editor * Write the program on Editor or IDE. * Save it with .C or CPP extension * Compile the Program (Alt + F9) + Run the program (Ctl + F9) + Analyse the result on output screen Dr. Vijaypal Rathor's screen Source Code Hello,c Compiler Lae Object Code Hello.obj Run Linker/Loaded Executable Code Hello.exe Errors in C Program * Syntax and Compile Time Error: Errors due to the incorrect syntax. Compiler tells about these errors int a; // this is the correct form * Example: Int a; // this is an incorrect form. * Find out the Error in following codes: #include #include int main() a main() = 10; t a=2; Brnet¢The value of a is : %d", a); -) // syntax error return 0; } printf("a is greater than 1"); return 0; i Dr. Vijaypal Rathor's screen Errors in C Program Output: #include int main() a= 10; printf("The value of a Is : %d", a); return 0; + Output: #include int main() - Complation failed due to folowing error(s) int a=2; x Rea if) // syntex error ey Pray printf("a is greater than 1"); return 0; + Cis Errors in C Program * Linking Error: Linker errors can be happened either due to the wrong function prototyping or usage of the wrong header file. + Example: 1.#include 2.int Main() 3. q 4. int a=78; 5 6 7 printf("The value of ais : %d", a); return 0; + Output: Errors in C Program * Logical and Run Time Error: The logical error is an error that leads to an undesired output. The errors exist during the execution-time even after the successful compilation known as run-time errors. + Example: What will be the output for the following examples ? #include int main() { #include int sum: ; // variable initialization int main() for(int I=1;1<=10;1++); // semicolon after loop {_ { iS int a=2; sum=sum+k; int b=2/0; k++; printf("The value of b Is : %d", b); return 0; printf("The value of sum is %d", sum); + return 0; } Errors in C Program * Errors are the problems or the faults that occur in the program, which makes the behavior of the program abnormal, and experienced developers can also make these faults. * The errors must be removed from the program for the successful execution of the program. * Programming errors are also known as the bugs or faults, and the process of removing these bugs is known as debugging. * Types of Errors: * Syntax or Compile Time Error * Linking Error * Logical or Run Time Error eee Leo Tears #include void (@) r v Ca TT na OQUTPU CODE REVIEW

You might also like