0% found this document useful (0 votes)
9 views14 pages

12TH Computer July Notes

This document provides an overview of programming languages, focusing on the C language. It covers the types of programming languages, language processors, the structure of a C program, and common programming errors. Additionally, it discusses identifiers, keywords, and variables in C, including rules for naming and declaring variables.

Uploaded by

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

12TH Computer July Notes

This document provides an overview of programming languages, focusing on the C language. It covers the types of programming languages, language processors, the structure of a C program, and common programming errors. Additionally, it discusses identifiers, keywords, and variables in C, including rules for naming and declaring variables.

Uploaded by

ammaraaqeel3157
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CHAPTER 08

GETTING
STARTED WITH C
Programming Languages
Program:
A well defined set of instructions given to the computer A person who
Examples of Programs: develops program.
Microsoft Word , Adobe Photoshop, Skype
Programming Language: Programmer
 A set of words and symbols used to write a computer program
 Provides way of communication between user and computer
 A computer program is written in a programming language Syntax
Types of Programming Language:
1. Low Level Language Set of Rules For Writing a
Program
Near to computer hardware and far from human languages

Machine Language Assembly Language

• Instructions are written in binary • One step higher than machine


form or code language
• Directly understood by the • English like words(mneomonics)
Programming
computer are used instead of binary code
Languages
• Native language of computer • Easier than machine language
• Fast program execution • Mostly used to write system
• No need of translator software
• Machine dependent • program written in assembly Low-Level High-Level
• Difficult to understand and learn language cannot be executed
• Very time consuming for writing directly on the computer
and modifying program • Requires a translator named
assembler
Machine Language Assembly Language
High Level Language Characteristics of High Level Language

Programming languages whose


instructions resemble the English
language are called high level These are English like languages, hence are close to
Programs written in high level languages are Easy
languages. Every high level 1 human languages and far from the machine 4 To Modify and Debug, and More Readable.
language defines a set of rules for language and are very Easy to Learn.
writing programs called syntax of
the language. Every instruction in
the high level language must The Standardized syntax of high-level languages
No Deep Hardware Knowledge Required for
confirm to its syntax. If there is a
syntax error in the program, it is 2 writing efficient programs, so programmers can 5 ensures consistent program writing. Organizations
focus on solving problems instead of architecture. like ANSI establish these standards.
reported by the language
translator (compiler or
interpreter). The program does not
translate into machine language Machine Independence: It enable programs to run As it’s easy to learn, attracting More Programmers.
unless the error is removed. 3 on various computers with minor modifications, 6 They also result in Shorter Programs, as one high-
Examples: C/C++, Java, Pascal, like C programs on Intel® and Motorola processors level instruction equals many low-level
FORTAN, BASIC, COBOL instructions.
High-Level Language VS Low-Level Language

Aspect High-Level Language Low-Level Language


Abstraction Level High, closer to human languages Low, closer to machine code

Ease of Learning Easier to learn and use Harder to learn and use

Syntax Similar to English Machine-specific instructions

Hardware Knowledge Requires minimal hardware knowledge Requires detailed hardware knowledge

Portability Highly portable, machine-independent Less portable, machine-dependent

Development Speed Faster development due to simplified syntax Slower development due to complex syntax

Error Detection Easier error detection and debugging Harder error detection and debugging

Examples Python, Java, C++ Assembly, Machine Code


Language Processors OR Translator Source Code Object Code
A Type of system software that converts high level language or assembly language instructions into
machine language is known as Language Processor or Translator. Translator
1. Compiler
 Program that converts the instruction of a high-level language into machine language as a whole
 Converts source code or program into machine or object code or program if it is free of errors
 Object program can be executed many times without translating it again Source Code Object Code
 A source code or program containing an error cannot be compiled
 The compiler generates error message to describe the cause of error • High Level Language • Machine Language
Instructions Instructions
 All errors must be removed to successfully compile a source program • Also called source • Also called object
Examples: program program or machine
C & C++ Compilers • Computer cannot code
understand source • Computer can
2. Interpreter code understand it
 Translate one statement of source code / program and executes it and then next statement is • Cannot be executed directly
translated and this goes on until end of the program directly
 If there is an error in the program statement, the interpreter stops working and displays the error
message
 Advantage over compiler is that an error is found immediately

Types of Translators
 Does not execute the statement containing error
Compiler
 Not very efficient as It converts the instructions into machine program/code each time it is executed
Examples:
Interpreter
GWBASIC uses interpreter
3. Assembler Assembler
 Program that translates the instructions of assembly language into machine language
History of C Language Characteristics of C Language
 C is a popular general purpose
language
 Developed by Dennis Ritchie These are English like languages, hence are close to
Programs written in C language are Easy To Modify
at AT & T Bell Labs in 1972 1 human languages and far from the machine 4 and Debug, and More Readable.
 C language was derived from language and are very Easy to Learn.
earlier programming
language called B developed
by Ken Thomson in 1969-70 No Deep Hardware Knowledge Required for The Standardized syntax of high-level languages
which provided the basis for 2 writing efficient programs, so programmers can 5 ensures consistent program writing. Organizations
focus on solving problems instead of architecture. like ANSI establish these standards.
the development of C
 C language was originally
designed to write system
Machine Independence: It enable programs to run As it’s easy to learn, attracting More Programmers.
program under Unix OS They also result in Shorter Programs, as one high-
 Earlier version of C was known
3 on various computers with minor modifications, 6
like C programs on Intel® and Motorola processors level instruction equals many low-level
as K & R (Kernighan and instructions.
Ritchie)
 American National Standard Reasons To Write Program in C Language Than Machine Language:
Institute (ANSI) developed a 1. Easier To Write:
standard version of C C is a high level language, Instructions of C are similar to English language that are easy to understand
language in late 1980s → BUT Program written in machine language consist of 0s and 1s, Very difficult to write, modify and manage.
ANSI C
2. Machine Independence:
 New version provided many
Program written in C language can be executed on different types of computer
features that were not
BUT Program written in machine language is machine-dependent, It can only work on the computer it was designed for
available in the older Version
Turbo C++ and Necessary steps for the execution of a Program

 Compiler for C: Turbo C++


 IDE: TC editor for creating, editing, saving, compiling, Source Code Compiler Object Code Linker Executable code User Screen
and running C programs ALT+F9 CTRL+F9 ALT+F5

 Debugger: Detects and removes errors


 Starting IDE: Type "TC" in DOS prompt and press sample.c sample.obj sample.exe
Enter
 IDE Menu Bar: Allows creation, editing, compilation,
List of Errors
execution, and debugging
Header Files
 Menu Access: Use mouse or ALT key + highlighted
character (e.g., ALT+F for File menu)

1. Creating and Editing a C Program: 3. Compiling a C Program:


Open Edit Window: File > New in Turbo C++ IDE. Compile: Converts source code to .obj. Select Compile > Compile or press
Edit Window: Has a double-lined border with a blinking cursor as the Alt + F9.
starting point. Error Messages: Displayed if there are issues in the source code.
Expand Window: Use the arrow in the upper-right corner or Windows >
Zoom.
4. Linking a C Program:
Navigation: Use scroll bars or arrow keys. Linking: Combines the object file with library files. Select Compile > Link.
Executable: Generates a .exe file.
2. Saving a C Program:
Save: Select File > Save or press F2. Name the file and press Enter.
5. Executing a C Program:
Run: Load the program into memory using Run > Run or Ctrl + F9.
File Extensions:
View Output: Select Window > User Screen or press Alt + F5.
 .cpp for C++ (additional features).
 .c for standard C (ANSI C features). 6. Setting Output and Source Directories:
 Custom Location: Specify path for saving. Default Location: Files are saved in the BIN folder.
Custom Location: Set Option > Directories and specify output directory.
Basic Structure of a C Program A name replaced by a constant
value before the program is
C Program Structure: compiled.
Flexibility: The structure of a C program is flexible, providing power and
control. Constant Macro
Structured Language: C is a structured programming language with a well- 2. Main Function:
defined way of writing programs. Definition: Every C program must have a main function where execution
Linking: A C program is linked with multiple files before execution. The linker begins.
does this job, but you must specify the files to be linked. Syntax: void main(void)
Parts of the Program: Entry Point: The main function is the entry point of the C program. Execution
1. Preprocessor Directives: starts here.
Purpose: Preprocessor directives provide instructions to the C preprocessor, 3. Body of Program:
which modifies the source program before compilation. The body of the program contains the program logic and is enclosed within
Syntax: Begins with # (e.g., #include). braces {}.
Example: #include<stdio.h> gives access to standard input/output functions It includes:
like printf. Statements: Inside the braces, statements like printf are executed. Each
 #include Directive: statement ends with a semicolon ;.
Syntax: #include<standard header file>
Examples:
Example: Hello World Program
#include<stdio.h> for input/output functions.
//Preprocessor Directive

Parts of Program
#include<math.h> for mathematical functions. Preprocessor
#include<stdio.h>
Purpose: Tells the compiler where to find the meanings of standard identifiers Directives
//Main Function
like printf from standard header files (e.g., stdio.h or math.h).
void main() Main Funation
 #define Directive:
//Body of a Program
Purpose: Defines constant macros to be used throughout the program.
{ Body of Program
Syntax: #define Macro_Name expression
printf("Hello World!");
Example: #define SECTOR_PER_HOUR 3600
}
Constant Macro: A name replaced by a constant value before the program is
compiled.
Common Programming Errors Unstructured Languages Structured Languages
1. Syntax Errors:
 Occur when the program violates one or more  Entire logic is implemented in a single module  Logic is divided into smaller modules, each
grammar rules of the C language. (function). handling a specific task.
 The compiler detects these errors as it attempts  Difficult to debug due to lack of organization.  Easier to debug due to modular structure.
to translate the program.
Causes:
 Missing statement terminators (semicolons).
 Using a variable without declaration.
Debugging Features of Turbo C++
 Missing delimiters. Syntax
 If there is a syntax error, the program cannot be
1. Single Stepping:
Executes one line at a time to find errors.
executed. Error Types Runtime Procedure: Select Run > Trace Into or press F7.
2. Runtime Errors:
 Occur when the program directs the computer to
2. Watches:
perform an illegal operation (e.g., dividing by Logical Monitors variable values during program
execution.
zero).
Procedure: Place cursor on the variable, select
 These errors are detected and displayed during
Debug > Watches, then Add Watch to monitor its
the execution of the program.
value.
 When a runtime error occurs, the program stops, Bug & Debugging
and a diagnostic message is displayed. 3. Breakpoints:
3. Logical Errors: Stops execution at specific points to check
In programming, errors are variables.
 Occur when a program follows a faulty
commonly referred to as Procedure: Place cursor on the line and select
algorithm.
Debug > Toggle Breakpoint or press CTRL+F8.
 These errors are not detected by the compiler, so “bugs,” and the process of
no error message is shown. finding and removing these 4. Evaluate/Modify Window:
 Logical errors do not cause the program to crash, Changes variable values during execution.
bugs is called debugging. Procedure: Select Debug > Evaluate/Modify, enter
making them hard to detect.
 They can only be identified by observing variable name and new value.
incorrect program output and thorough testing.
CHAPTER 09
ELEMENTS OF C
Identifier, Keywords & Variables
Identifier: Keywords:
An identifier is the name given to the variable, Variable: Keyword is a word in C language that has
constant, function or label in the program.  A variable is a named memory location or memory cell a predefined meaning and purpose.
 This Feature present in all computer  Store program's input data and its computational results during execution  Also known as reserved words
languages  The value of variable can change during program execution  Can not be used for another purpose
 A good identifier name should be descriptive  The variables are created in RAM  Written in lowercase letters
but short  If a new value is stored in the variable, it replaces the previous value  The total number of keywords is 32
 An identifier in C language may consist of 31
character Declaring a Variable in C:
Name of Variable
Rules for Identifier: C is a strongly typed language, meaning all
 The first character must be an alphabet or
underscore (_) such as _large, age, marks
Address of Variable 10 Contents of Variable variables must be declared before use.
Syntax: Data_Type Variable_Name;
etc. Computer Memory Data_Type specifies the type of data the variable
 The remaining can be alphabetic characters, will store (e.g., int, double).
digits or underscores such as Number1, Variable_Name is the name of the variable.
total_marks etc. Example:
 The reserved word cannot be used as
Rules For Naming Variables: int marks;
identifier name such as int, double, break etc. 1. Allowed Characters: Variable names can include letters, digits, and
double length;
Valid identifiers: letter1, inches, KM_PER_MILE, underscores (_).
_large 2. First Character: The first character must be a letter or an underscore (_); Initializing a Variable in C:
Invalid identifiers: 1letter, Happy*trout, return using an underscore is discouraged. Subsequent characters can include Initializing a variable means assigning a value at
digits. the time of its declaration. If not initialized, a
Types of Identifiers: 3. Invalid Names: Names like 9winner, #home, and 2kgms are invalid.
1. Standard Identifiers variable may contain meaningless data
4. Case Sensitivity: C is case-sensitive, so count, Count, and COUNT are (garbage), leading to unexpected results. To avoid
A type of identifier that has special meaning in C is considered different variables.
known as standard identifier. this, always declare and initialize variables before
5. Keywords: C keywords (e.g., int, void, while) cannot be used as variable use.
Examples: printf, scanf names.
2. User-defined Identifiers Example:
6. Length: Variable names can be up to 31 characters long (though int count = 125;
 A type of identifier that is defined by the technically up to 255, only the first 31 are significant for most compilers).
programmer is known as user-defined // Declaration and initialization
7. Spaces: Spaces are not allowed in variable names (e.g., problem solving is char ch = 'z';
identifier. invalid).
 The user-defined identifiers are used to store // Declaration, definition, and initialization
8. Data Type: A variable can only be declared with one data type. float weight = 75.8;
data and program results
Examples: my_name, marks // Declaration and initialization
Constants, Data Types & Comments
Constants in C:
A constant in C is a value that cannot be changed Data Types in C: Comments in C:
during the program's execution. Constants are useful In C, data types define the type and operations of data. Before designing a program, Comments enhance program
for representing fixed values that should not be data types are specified for each value. Variables are associated with data types to readability by adding notes for
modified.Constants improve code readability, store different types of data. debugging, modification, and
maintainability, and prevent accidental modification Types of Data Types: explanation.
of critical values. 1.Pre-Defined Data Types: These are built-in types like int, double, char, float. Types of Comments:
Types of Constants in C: 2.User-Defined Data Types: These are custom types created using typedef, struct, 1. Single-Line Comments:
1. Integer Constants: enum, or union. Start with // and are used for brief
These are whole numbers, positive or negative, Integer Data Types (int, short, long): explanations.
without a decimal point.  int stores whole numbers (e.g., -1010, 32432). Example: // This program calculates
Example: 10, -5, 1000.  short int and long int handle smaller and larger ranges of integers, respectively. the factorial of a number.
 signed or unsigned modifiers affect whether the integer can hold negative values. 2. Multi-Line Comments: Enclosed
2. Floating-point Constants:
Memory:
These represent real numbers (numbers with between /* and */ for longer notes
• int: 2 bytes, range: -32768 to 32767 (signed), 0 to 65535 (unsigned).
decimals). spanning multiple lines.
• long: 4 bytes, range: -2147483648 to 2147483647.
Example: 3.14, -0.001, 2.5e3 (scientific notation). Example:
3. Character Constants:
Floating-Point Data Types (float, double, long double): /* This program performs
These represent single characters enclosed in single  Used for numbers with fractional parts (e.g., 12.35874, -8.64). multiple operations
quotes.  Stored in scientific notation (e.g., 2.45634e5). on the input data. */
Example: 'A', 'z', '1'. Precision: Key Point: Comments are ignored by
4. String Constants: • float: 4 bytes, 6-7 digits, range: 10^-38 to 10^38. the compiler and do not affect the
A sequence of characters enclosed in double quotes. • double: 8 bytes, 15 digits, range: 10^-308 to 10^308. program's execution.
Example: "Hello", "World". • long double: 10 bytes, 19 digits, range: 10^-4932 to 10^4932.
5. Enumeration Constants:  Potential issues include overflow and underflow when handling very large or small
These are constants defined using enum to represent numbers.
a set of named integer constants. Character Data Type (char):
Example: enum day { Sunday, Monday, Tuesday };  Represents a single character (e.g., 'a', '5', '#').
 Occupies 1 byte of memory. Characters are manipulated differently than integers.
6. Macro Constants (#define):
Constants defined using #define preprocessor
directive.
Example: #define PI 3.14
Operators in C Language Operator VS
1. Arithmetic Operators: Operands
These operators perform basic arithmetic operations. They are: 4. The assignment operator (=)
It is used to assign a value to a variable. It takes the expression
Operator Description Example Code Result
on the right side, evaluates it, and assigns the result to the
+ Addition int sum = a + b; sum = 11 variable on the left side.
- Subtraction int diff = a - b; diff = 5 Example:
* Multiplication int product = a * b; product = 24 int height = 10, width = 5, area;
/ Division int quotient = a / b; quotient = 2 General Form: variable = expression;
% Modulus int remainder = a % b; remainder = 2 The expression on the right side is evaluated first, and its value
is assigned to the variable on the left side.
Unary Operators
2. Relational Operators: Important Note: If the variable appears on the right side and the  Operate on one operand.
Relational operators are used to compare two values. They expression on the left side, a syntax error will occur.  Examples: ++, --, !.
return either true (non-zero) or false (0) depending on whether 5. Increment and Decrement Operators: Binary Operators
the comparison is true or not. The relational operators in C are: These operators modify the value of a variable by increasing or  Operate on two operands.
Operator Description Example Code Result decreasing it by 1.  Examples: +, -, *, ==.
== Equal to (a == b); true ++ (Increment): Increases the value of the operand by 1.
!= Not equal to (a != b); true -- (Decrement): Decreases the value of the operand by 1.
Prefix and Postfix: 6. Compound Assignment:
< Less than (a < b); false These operators combine arithmetic
> Greater than (a > b); true Prefix Increment (++a): First increments the operand, then uses
its value. operations with assignment. They modify
<= Less than or equal to (a <= b); false the value of a variable by performing an
Postfix Increment (a++): First uses the operand, then
>= Greater than or equal to (a >= b); true operation and then assigning the result
increments its value.
back to that variable.
3. Logical Operators: +=: Adds a value to the variable.
Logical operators are used to combine relational expressions to Operator Description Example Code Result
-=: Subtracts a value from the variable.
form complex conditions. These operators allow you to make ++ Increment (Prefix/Postfix) int i = ++j; i = 11 *=: Multiplies the variable by a value.
decisions based on multiple conditions.
/=: Divides the variable by a value.
The logical operators in C are: -- Decrement (Prefix/Postfix) int i = j--; i = 10
 && (Logical AND): Returns true if both operands are true.
 || (Logical OR): Returns true if at least one operand is true.
 ! (Logical NOT): Reverses the truth value of its operand.
Operator Precedence & Expression
Operator Precedence:
Operator precedence determines the order in which operators are evaluated in an expression. Expressions in C
The higher the precedence, the earlier the operator is evaluated. An expression is a combination of operands and
operators that evaluates to a value.
Example: Types of Expressions:
int a = 10, b = 5, result; Arithmetic Expressions
result = a + b * 2; // result = 10 + (5 * 2) = 20 Involve arithmetic operators (+, -, *, /, %).
Here, multiplication (*) has higher precedence than addition (+), so 5 * 2 is evaluated first. Example: int result = a + b;
Logical Expressions
Precedence Operator(s) Description Combine relational expressions using logical operators
(&&, ||, !).
1st !, ++, --, ~, sizeof Unary operators, logical NOT, increment, decrement Example: if (a > b && b < c)
Relational Expressions
2nd *, /, % Arithmetic multiplication, division, modulus Compare two values using relational operators (==, !=,
<, >, <=, >=).
3rd +, - Arithmetic addition and subtraction Example: if (a == b)
Mixed-Type Expressions
Combine different data types. Result type is determined
4th <, <=, >, >=, ==, != Relational operators (comparison) by the larger type.
Example: double result = a + b; (int + double gives
5th && Logical AND
double)
7th = Assignment operator
8th +=, -=, *=, /=, %= Compound assignment operators

You might also like