Getting Started Flashcards
Four Important aspects of any language:
- Way it stores data
- Way it operates upon it.
- how it accomplishes input and output.
- how it lets you control the sequence of execution of instructions in a program.
Why is C used in some parts of Operating systems?
Speed of execution.
Device drivers are written in C.
Steps of learning C:
- Alphabets; Digits; Special Symbols.
- Constants; Variables; Keywords
- Instructions
- Program
The C character Set
All alphabets, Capital and small.
All digits 0-9
Special Symbols o keyboard [31].
[ ~ is but ` is not valid]
Constant, Variable and Keywords:
These formed by the combination of the C character set.
Constants: Entity with a fixed value.
Variables; Entity with changing values.
Variable names theory:
The memory locations are given names and since the value stored in these memory keeps changing hence called variable names.
This allows us to retrieve and use the value stored in the memory location.
Types of C constant:
- Primary
Integer constant; Real Constant; Character Const. - Secondary constants
Array; Structure; Pointer; Union; Enum,..etc
Rules Of Integer Constant:
- Must have at least 1 digit.
- Must not have a decimal point.
- Can be +ve or -ve.
- No sign implies +ve.
- No commas and blanks allowed.
- Range: -32768 to 32767 [ depends on Compiler, this
is for a 16-bit compiler.]
Rules for constructing Real Constants:
aka Floating Point Constants. expressed in 2 forms: 1. Fractional Form: a. at least 1 digit. b. Decimal point Must. c. Can be +ve or -ve, default +ve d. No commas, blanks allowed.
- Exponential Form: [For too small or too big value]
a. Has mantissa and exponent[separated by e]
b. Mantissa can be +ve or -ve, Default +ve.
c. Exponent must have at least 1 digit again maybe +ve or -ve. Dafault is +ve.
d. Range: -3.4e38 to 3.4e38
Rules for Constructing Character Constants:
Character constant has max length of 1 character.
Enclosed in inverted commas, both single inverted, towards left.
like : ‘A’
‘1’
‘@’
In a language, the type of variables supported depends on ?
the type of constant supported.
Rules for constructing Variable Names:
- length 31 (some compilers: 247)
- First char must be alphabet or underscore
- No commas or blanks are allowed.
- ONly special symbol allowed is an underscore.
These rules apply for all types of primary and secondary variables.
To differentiate between variables what is done?
Type declaration statements.
int varname;
float varname;
char varname;
C keywords:
keywords cant be used as var names.
they are already explained to the compiler.
AKA reserved words
There are 32.
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
Compiler Specific keywords:
ANSI said they should be preceded by 2 underscores.
like __near, __far, __asm
C program
how do instructions or statements make them?
Each instruction is written as a separate statement.
Therefore, a C program compromises of a series of statements.
They must appear in the order in which we expect them to execute.
To increase readability u can insert blacnk spaces btw words but not btw varnames, constant, keyword.
Statements are entered in small case letters.
Statement terminator:
;
Every statement ends with a ;
Comments:
Comment enclosed in /* */
can be anywhere, even btw the statement.
To explain code or provide information.
Compiler Completely Ignores it.
Can comments be multiline?
Yes
Can comments be nested?
NO
C is also called as free form cuz:
No specific rules for position at which a statement should be written.
Declare a variable:
and whats initialisation?
int n; int or float or char Declare before using. Assigning value to it is intialising. int i = 10; /* declared and initialised */
function printf()
printf( “”,);
format string may contains format specifiers : %f to print real value %d for int %c for char %s - char. %p - address. %u - unsigned Integer.
list of variables can contain Expressions.
NewLine
Takes cursor to next line.
\n
One of the Escape Sequences.
Compilation and Execution:
To type your C program you need an EDITOR.
once written it is necessary to convert it to machine lang(0s, 1s) so machine can execute it. Compiler performs this conversion.
Integrated Development Environment: Provides editor and compiler.
scanf() function:
scanf(“%d%f%c”,&int,&real,&char);
ampersand [&] : Address of operator. &int : We are telling at which memory location should it store the value supplied by user. User must use blank,tab or newline(enterkey) to separate the inputs.
Types of instructions:
- Type Declaration
- Arithmetic
- Control Instruction
Is it valid?
int a = b = c = d = 10 ;
order of assigning
Its invalid as u are assigning b to a before defining it.
but
int a,b,c,d; a = b=c = 10; will work.
float a = 1.5, b = a; is fine but
float b = a+ 1.1, a = 1.5; is not.
as we are trying to use before defining it.
Operands:
Variables and constants on RHS of = [assignment operator], as these are operated by the arithmetic operators [+,-,*,/].
Arithmetic Instructions :
way of assignment
Value on the right of = is assigned to left.
RHS is evaluated using constants and numerical values stored in variable names. This value is assigned on the LHS.
One variable is allowed on LHS
Types Of Arithmetic Instructions:
- Just Integer constants and variables.
- Real mode
- Mixed mode [real and integer operands]
% operator:
Modulus operator returns remainder, not applied on floats.
Sign of numerator is assigned to the remainder.
5% -2 = 1
-5 % 2 = -1
Arithmetic Instruction on char:
1. To store constants in char variable. char a; a = 'F'; a will store F's ASCII value i.e 70. int z; z = a + 'G'; Addition is performed on ASCII values of chars. z = 70+71
The one missed operator in C?
Exponentian. but we can use: #include main() { /* 3 raise to 2 */ print("%d",pow(3,2) ); }
Integer to float conversion:
Implicit:
Operation on real and integer yields a real, the integer is converted to real and the operation is performed.
float a,b,c ;
int s;
s = a * b * c /100 + 32/4 - 3 * 1.1;
discuss conversions:
Type Conversion In Assignments.
during evaluation in RHS everything is promoted to real if it’s not but since s is an int it can’t store real so evaluated value gets demoted to integer and is stored in s.
Hierarchy of Operations:
- , / , % > + , - > =
Associativity of Operators:
Its of 2 types:
1. Right to left [ = ]
2. Left to right [ +,/,*]
Used to break tie when same priority operators come together, like / , * .
In left to right : Left operand must be unambiguous, not involved in any sub expression. Then its performed early.
a = 3/2*5
How will compiler proceed?
/,* have L to R associativity.
but / has its left operand unambiguous hence it is performed earlier then *.
Control Instructions:
- Sequence Control.
- Selection or Decision Control.
- Repetition or Loop control Instruction.
- Case Control.
These instructions enable us to specify in which order must instructions be executed.
Determines ‘ Flow Of Control’.