Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Paradigm can also be termed as method to solve some problem or do some task.
Programming paradigm is an approach to solve problem using some programming
language or also we can say it is a method to solve a problem using tools and
techniques that are available to us following some approach.
Advantage:
1. Very simple to implement
2. It contains loops, variables etc.
Disadvantage:
1. Complex problem cannot be solved
2. Less efficient and less productive
3. Parallel programming is not possible
Imperative programming is divided into three broad categories: Procedural, OOP and parallel
processing. These paradigms are as follows:
2.Procedural programming paradigm
This paradigm emphasizes on procedure in terms of under lying machine model.
There is no difference in between procedural and imperative approach.
It has the ability to reuse the code and it was boon at that time when it was in use because
of its reusability.
-main() function:
Every C program must have one main() function section. int main(void): This is the function
definition for main().Parenthesis followed to main is to tell the user again that main() is a
function. Int main(void) function return an integer.
void main(void):
This function takes no arguments and return nothing.
void main():
This function returns nothing and takes no arguments. The program contains statements that are
enclosed within the braces. The opening braces “{“ and closing braces “}”.
In these two braces main() function contains two parts,
declaration and executable part. It is user defined function.
The Opening braces sometimes called logical start and Closing braces known as logical end of
the program.
Data types:
Data types in c refer to an extensive system used for declaring variables or
functions of different types.
The type of a variable determines how much space it occupies in storage and how
the bit pattern stored is interpreted.
The types in C can be classified as follows −
Sr.No. Types & Description
1 Basic Types
They are arithmetic types and are further classified into: (a) integer types and (b)
floating-point types.
2 Enumerated types
They are again arithmetic types and they are used to define variables that can
only assign certain discrete integer values throughout the program.
4 Derived types
They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union
types and (e) Function types.
The array types and structure types are referred collectively as the aggregate types. The type of
a function specifies the type of the function's return value. We will see the basic types in the
following section, where as other types will be covered in the upcoming chapters.
Integer Types
The following table provides the details of standard integer types with their storage sizes and
value ranges −
Type Storage size Value range
int main() {
printf("Storage size for float : %d \n", sizeof(float));
printf("Minimum float positive value: %E\n", FLT_MIN );
printf("Maximum float positive value: %E\n", FLT_MAX );
printf("Precision value: %d\n", FLT_DIG );
return 0;
}
When you compile and execute the above program, it produces the following result on Linux −
Storage size for float : 4
Minimum float positive value: 1.175494E-38
Maximum float positive value: 3.402823E+38
Precision value: 6
Storage Class
A storage class defines the scope (visibility) and life-time of variables and/or functions
within a C Program. They precede the type that they modify. We have four different storage
classes in a C program −
auto
register
static
extern
The auto Storage Class
The auto storage class is the default storage class for all local variables.
{
int mount;
auto int month;
}
The example above defines two variables with in the same storage class. 'auto' can only be used
within functions, i.e., local variables.
The register Storage Class
The register storage class is used to define local variables that should be stored in a
register instead of RAM.
This means that the variable has a maximum size equal to the register size (usually one
word) and can't have the unary '&' operator applied to it (as it does not have a memory
location).
{
register int miles;
}
The register should only be used for variables that require quick access such as counters.
It should also be noted that defining 'register' does not mean that the variable will be
stored in a register.
It means that it MIGHT be stored in a register depending on hardware and
implementation restrictions.
The static Storage Class
The static storage class instructs the compiler to keep a local variable in existence
during the life-time of the program instead of creating and destroying it each time it
comes into and goes out of scope.
Therefore, making local variables static allows them to maintain their values between
function calls.
The static modifier may also be applied to global variables. When this is done, it causes
that variable's scope to be restricted to the file in which it is declared.
In C programming, when static is used on a global variable, it causes only one copy of that
member to be shared by all the objects of its class.
#include <stdio.h>
/* function declaration */
void func(void);
static int count = 5; /* global variable */
main()
{
while(count--) {
func();
}
return 0;
}
/* function definition */
void func( void ) {
static int i = 5; /* local static variable */
i++;
printf("i is %d and count is %d\n", i, count);
}
When the above code is compiled and executed, it produces the following result −
i is 6 and count is 4
i is 7 and count is 3
i is 8 and count is 2
i is 9 and count is 1
i is 10 and count is 0
The extern Storage Class
The extern storage class is used to give a reference of a global variable that is visible to
ALL the program files.
When you use 'extern', the variable cannot be initialized however, it points the variable
name at a storage location that has been previously defined.
When you have multiple files and you define a global variable or function, which will
also be used in other files, then extern will be used in another file to provide the
reference of defined variable or function.
Just for understanding, extern is used to declare a global variable or function in another
file.
The extern modifier is most commonly used when there are two or more files sharing the same
global variables or functions as explained below.
First File: main.c
#include <stdio.h>
int count ;
extern void write_extern();
main()
{
count = 5;
write_extern();
}
Second File: support.c
#include <stdio.h>
Variables
Here, playerScore is a variable of integer type. Here, the variable is assigned an integer value 95.
The value of a variable can be changed, hence the name variable.
char ch = 'a';
// some code
ch = 'l';
Rules for naming a variable
1. A variable name can have letters (both uppercase and lowercase letters), digits and
underscore only.
2. The first letter of a variable should be either a letter or an underscore.
3. There is no rule on how long a variable name (identifier) can be. However, you may run
into problems in some compilers if variable name is longer than 31 characters.
Note: You should always try to give meaningful names to variables. For example: firstNameis a
better variable name than fn.
C is a strongly typed language. This means, variable type cannot be changed once it is declared.
For example:
Constants/Literals
A constant is a value (or an identifier) whose value cannot be altered in a program. For
example: 1, 2.5, 'c' etc.
Here, 1, 2.5 and 'c'are literal constants. Why? You cannot assign different values to these terms.
You can also create non-modifiable variables in C programming. For example:
1. Integers
An integer is a numeric constant (associated with number) without any fractional or exponential
part. There are three types of integer constants in C programming:
decimal constant(base 10)
octal constant(base 8)
hexadecimal constant(base 16)
For example:
-2.0
0.0000234
-0.22E-5
3. Character constants
A character constant is created by enclosing a single character inside single quotation marks. For
example: 'a', 'm', 'F', '2', '}' etc;
4. Escape Sequences
Sometimes, it is necessary to use characters which cannot be typed or has special meaning in C
programming. For example: newline(enter), tab, question mark etc. In order to use these
characters, escape sequence is used.
For example: \n is used for newline. The backslash \ causes escape from the normal way the
characters are handled by the compiler.
Escape Sequences
\b Backspace
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\? Question mark
\0 Null character
5. String Literals
A string literal is a sequence of characters enclosed in double-quote marks. For example:
6. Enumerations
Keyword enum is used to define enumeration types. For example:
Here, color is a variable and yellow, green, black and white are the enumeration constants having
value 0, 1, 2 and 3 respectively.
Character set
Character set is a set of alphabets, letters and some special characters that are valid in C
language.
Alphabets
Uppercase: A B C ................................... X Y Z
Lowercase: a b c ...................................... x y z
C accepts both lowercase and uppercase alphabets as variables and functions.
Digits
0123456789
Special Characters
Special Characters in C Programming
, < > . _
( ) ; $ :
% [ ] # ?
^ ! * / |
- \ ~ +
White space Characters
blank space, new line, horizontal tab, carriage return and form feed
Keywords
Keywords are predefined, reserved words used in programming that have special meanings to the
compiler. Keywords are part of the syntax and they cannot be used as an identifier. For example:
int money;
do if static while
Identifier refers to name given to entities such as variables, functions, structures etc.
Identifier must be unique.
They are created to give unique name to a entity to identify it during the execution of the
program. For example:
int money;
double accountBalance;
Operato
Meaning of Operator
r
* multiplication
/ division
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
++a = 11
--b = 99
++c = 11.500000
++d = 99.500000
C Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common
assignment operator is =
= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
Example 3: Assignment Operators
c = a;
printf("c = %d \n", c);
c += a; // c = c+a
printf("c = %d \n", c);
c -= a; // c = c-a
printf("c = %d \n", c);
c *= a; // c = c*a
printf("c = %d \n", c);
c /= a; // c = c/a
printf("c = %d \n", c);
c %= a; // c = c%a
printf("c = %d \n", c);
return 0;
}
Output
c=5
c = 10
c=5
c = 25
c=5
c=0
C Relational Operators
A relational operator checks the relationship between two operands. If the relation is true, it
returns 1; if the relation is false, it returns value 0.
Relational operators are used in decision making and loops.
== Equal to 5 == 3 returns 0
return 0;
}
Output
5 == 5 = 1
5 == 10 = 0
5>5=0
5 > 10 = 0
5<5=0
5 < 10 = 1
5 != 5 = 0
5 != 10 = 1
5 >= 5 = 1
5 >= 10 = 0
5 <= 5 = 1
5 <= 10 = 1
C Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether
expression results true or false. Logical operators are commonly used in decision making in C
programming.
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
return 0;
}
Output
Bitwise Operators
During computation, mathematical operations like: addition, subtraction, addition and division
are converted to bit-level which makes processing faster and saves power.
Bitwise operators are used in C programming to perform bit-level operations.
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
int a, c = 5, d;
#include <stdio.h>
int main()
{
int a, e[10];
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
printf("Size of integer type array having 10 elements = %lu bytes\n", sizeof(e));
return 0;
}
Output
#include <stdio.h>
int main(){
char February;
int days;
printf("If this year is leap year, enter 1. If not enter any integer: ");
scanf("%c",&February);