ALL SIR NOTES HAVE BEEN UPLOADED HERE.
Subject Teacher: Bhagirath Singh Chauhan # 9829275869
C was developed by a system programmer Dennis Ritchie in 1972, at American Telegraph & Telecommunication (AT & T) Bell Laboratories in New Jersey USA. The C language is often referred as middle level language because we can write high level as well as low level programs through C.
C is a general purpose programming language. You can generate games, business software, utilities, mathematical models, word processors, spreadsheets and other kinds of software.
C is a structured programming language. It uses structured statements such as while, for loops in place of goto statement which cause bugs (error) in the program.
System programming: C is used for system programming i.e. writing operating system. The UNIX operating system is also rewritten from C. Major parts of Windows, Linux, UNIX are still written in C because speed of execution is very fast in C compare to other languages.
Documentation Section
Link Section (#include section)
Definition Section
Global Declaration Section
Main Function Section
{
Declaration Part
Execution Part
}
Subprogram Section (User Defined Function)
Function 1
Function 2
….
Function n
A Preprocessor is a program that processes our program before it is passes to the compiler. Preprocessing is the first step of the language processing system. A preprocessor mainly performs three tasks on the High Level language code:
Removing comments
File inclusion
Macro expansion
Character set of a language is set of all the symbols used to write a program in that language. The characters in C are grouped into four categories:
1. Letters : A-Z or a-z
2. Digits : 0-9
3. Special symbols : ~, ‘! @#%^&*()_-+=\|{}[]:;” .?/
4. White spaces : blank space, tab space, carriage return, new-line, form-feed
Tokens are the smallest individual unit of a program. Each and every punctuation and word that you come across in a C program is token. C programs are written using these tokens and the syntax of the language. C has six types of tokens:
Keywords
Identifiers
Constants
Strings
Special symbols
Operators
Keywords are predefined, reserved words used in programming that have special meanings to the compiler. All keywords have fixed meanings and these meanings cannot be changed. Keywords serve as basic building blocks for program statements. All keywords must be written in lowercase. A list of keywords employed in C language:
auto double int struct break else long switch
case enum register typedef char extern return union
const float short unsigned continue for signed void
default goto sizeof volatile do if static while
Identifiers are the names given to variables, arrays, functions, pointers and structures. These are user-defined names and consist of a sequence of letters and digits, with a letter as a first character. Both uppercase and lowercase letters are permitted.
A variable is the name of a memory location that we use for storing data. We can change the value of a variable and we can also reuse it multiple times. Each variable has a name, data-type, size and the value it stores.
Rules for constructing variable name in C language are listed below:
Variable name may be a combination of alphabets, digits or underscores.
First character must be an alphabet or underscore.
No commas or blank spaces are allowed in a variable name.
No word, having a reserved meaning in C can be used for variable name.
Lower and upper case letters are significant.
A constant in C refer to fixed values that does not change during the execution of a program. There are four basic types of constants in C:
Character Constant
String Constant
Integer Constant
Real Constant
Character Constants: A character constant consist of a single character, single digit or a single special symbol enclosed within a pair of single inverted commas. All escape sequences are also considered as character constant.
Example:
const char ch = ‘a’;
Backslash Character Constant (Escape Sequence): Certain non-printable characters, which are used in the printf function are called as escape sequences. They always begin with backslash (\).
Escape sequence Meaning
\n new line
\t Horizontal tab
\b Back space
\r Carriage return
\a bell alert
\” to print double quotation
\0 null (point end of the string)
String Constant: A string constant is a sequence of one or more characters enclosed within a pair of double quotes (“ ”). If a single character is enclosed within a pair of double quotes, it will also be interpreted as a string constant and not a character constant.
Example:
“Bhagirath Chauhan”
Integer Constant: An integer constant refers to a sequence of digits and has a numeric value. There are three types of integers in C: decimal, octal and hexadecimal.
Decimal integers : 2, 45, -4 etc.
Octal integers : 020, -04 etc.
Hexadecimal integers : 0x4, -0x2B etc.
Real Constants: A number with a decimal point (fractional part) and an optional preceding sign represents a real constant. A floating point number can also be represented as exponential or scientific notation. For e.g. 0.0000987 can be written as 9.87*10-5 or 9.87e-05. Thus the general form is:
mantissa e exponent
So 9.87 is called mantissa and -05 is called as exponent.
Difference between Variable and Constant
S.No. Variables Constants
1
Stores data type value in a program.
It is similar to a variable but cannot be changed during program execution.
2
It can be changed after defining the variable in a program.
It is a fixed variable that cannot be changed after defining the variable in a program.
3
Typically, it uses int, float, char, double, etc. data types in a program.
It can be express in two ways: #define preprocessor and the const keyword.
4
Example: int a = 5; float pi = 3.14;
Example: cons tint size = 10; #define PI 3.14
PCE Programming for Problem Solving 1FY3-06
Subject Teacher: Bhagirath Singh Chauhan # 9829275869 Page No.: 5
The way a value stored in a variable interpreted is known as its data type. In other words, data type determines the type and size of data associated with variables. Every computer language has its own set of data types it supports. A data type signifies two important aspects:
Amount of space (size) to be reserved in the memory to store the data and
Nature (type) of data to be stored.
Data Types
Primitive/Primary/Basic Data Types Derived Data Types User Defined Data Types
- int - array - structure
- char - pointers - union
- float - typedef
- double - enum
- void
There are five primary data types in C language:
int data type
Integers are whole numbers with a range of values supported by a particular machine. The range of an int data type is compiler dependent (2 or 4 bytes). The highest bit (MSB) of an integer is used to store the sign of the integer. Format specifier for int data type is %d.
In order to provide some control over the range of numbers and storage space, C has three classes of integer storage, namely short int, int, and long int, in both signed and unsigned forms.
short and long (modifiers)
The short and long integers would usually occupy two and four bytes respectively. Format specifier for short int is %hd and long int is %ld.
signed and unsigned (modifiers)
Sometimes the program requires only positive values. In such cases the data type can be made unsigned. Thus the range of unsigned short int become 0 to 65535. Format specifier for unsigned is %u. (unsigned short - %hu).
char data type
A single character can be defined as a character (char) type data. A char data type occupy one byte memory. The qualifier signed or unsigned may be explicitly applied to char. A signed char has range from -128 to +127. An unsigned char has a range from 0 to 255. Format specifier for char data type is %c.
Formula to calculate the range of signed data types is 2n-1, and for unsigned data type is 2n where n is the number of bits occupied by the data type.
float, double and long double
A float occupies four bytes and can store from -3.4e38 to +3.4e38. If this is insufficient then C offers a double data type that occupies 8 bytes in memory and has a range from -1.7e308 to +1.7e308. If this is also insufficient then there is a long double that occupies 10 (12 in VS Code, 16 in Linux gcc) bytes and has a range from -1.7e4932 to +1.7e4932. Format specifier for float is %f, for double %lf and for long double is %Lf.
void type
The void type has no values. This is usually used to specify the type of functions. It can also play the role of a generic type, meaning that it can represent any of the other standard types.
typedef
C supports a feature known as ‘type definition’ that allows users to define an identifier that would represent an existing data type. The user-defined data type identifier can later be used to declare variables. It takes the general form:
typedef type identifier;
where type refers to an existing data type and ‘identifier’ refers to the ‘new’ name given to the data type.