NMCP20 With 20 C
NMCP20 With 20 C
Learning Objectives of Subject: At the end of subject the students will able –
To use spreadsheet software for solving civil engineering problems.
To impart knowledge to analyze, solve, design and code numerical method
problems using C language.
To impart knowledge to analyze, solve, design and code civil engineering
problems using C language.
SECTION – A
SECTION-B
Introduction to C
Solving any problem in the computers needs software.
Software is the program written in language like FOTRON, C,
C++ etc. in the computer.
C is a powerful programming language. It can be used to
develop software like operating systems, databases, compilers,
etc.
For beginners C programming is an excellent language to learn
to program.
The C Programming language was developed by Dennis
Ritchie. C has been standardized by ANSI since 1989 (ANSI
C) and by the International Organization for
Standardization (ISO).
Keywords
Keywords are pre-defined, reserved words used in C
programming that have special meanings to the compiler.
Keywords are part of the syntax and they cannot be used
as an identifier. As C is a case sensitive language, all
keywords must be written in lowercase. Here is a list of all
keywords allowed in ANSI C.
A list of all keywords allowed in
ANSI C
Identifiers
Identifier is the name given to entities such as variables, functions,
structures etc. Identifiers must be distinctive. They are created to
give a unique name to an entity to identify it during the execution of
the program. Identifier names should be different from
keywords. Rules for naming identifiers
A valid identifier can have letters (both uppercase and lowercase
letters), digits and underscores.
The first letter of an identifier should be either a letter or an
underscore.
There is no rule on how long an identifier can be. However, one may
run into problems in some compilers if the identifier is longer than
31 characters.
One can make any name as an identifier by using the above rule.
Variables
In C programming, a variable is a storage area to hold
data. To indicate the storage area, each variable should be
given a unique name (identifier). Variable names are just
the symbolic representation of a memory location. For
example:
int StudentMark = 75;
Here, StudentMark is a variable of int type. Here, the
variable is assigned an integer value 75. The value of a
variable can be changed, hence the name variable.
Rules for naming a variable
A variable name can only have letters (both uppercase and lowercase
letters), digits and underscore. The first letter of a variable should be
either a letter or an underscore. There is no rule on how long a
variable name (identifier) can be. However, you may run into
problems in some compilers if the variable name is longer than 31
characters.
C is a strongly typed language. This means that the variable type
cannot be changed once it is declared. For example:
int number = 5; // integer variable
number = 5.5; // error
double number; // error
Here, the type of number variable is int. You cannot assign a
floating-point (decimal) value 5.5 to this variable. Also, you cannot
redefine the data type of the variable to double. By the way, to store
the decimal values in C, you need to declare its type to
either double or float.
Literals
Literals are data used for representing fixed values. They
can be used directly in the code. For
example: 1, 2.5, 'c' etc. Here, 1, 2.5 and 'c' are literals.
Why? You cannot assign different values to these terms.
1. Integers
2. Floating-point Literals
3. Characters
4. String Literals
1. Integers
An integer is a numeric literal (associated with numbers)
without any decimal or exponential part. There are three types
of integer literals in C programming: 1) decimal (base 10), 2)
octal (base 8), hexadecimal (base 16).
Decimal integer constants can be any combination of digits.
For example: 0, -6, 24 etc
Octal integer constants can be any combination of digits from
the set 0 through 7. However, the first digit must be 0. For
example: 024, 076, 0765, etc
Hexadecimal integer constants should start with 0x. It can then
be any combination of digits taken from the set of 0 through
9and a through f: 0x2f, 0x7a, 0x541 etc
2. Floating-point Literals
A floating-point literal is a numeric literal that has either a
decimal form or an exponential part. For example: -28.0,
0.002684, -0.22E-6
Note: E-6 = 10-6
3. Characters
A character literal is created by enclosing a single
character inside single quotation marks. For
example: 'a', 'm', 'F', '2', '}' etc.
Addition to these constant, C recognizes special backslash
(Sometimes, it is necessary to use characters that 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 sequences are used. For
example: \n is used for start at newline. The
backslash \ causes escape from the normal way the
characters are handled by the compiler.
4. String Literals
A string literal is a sequence of alphanumeric characters
enclosed in double-quote marks. For example: “Radius”, “
My Name is Ram”
Constants
Constants are used to define a variable whose value
cannot be changed, one can use the const keyword. This
will create a constant. For example,
const double PI = 3.1416;
Notice, we have added keyword const. Here, PI is a
symbolic constant; its value cannot be changed.
Data Type
In C programming, data types are declarations for variables.
This determines the type and size of data associated with
variables. The different data types used in C, its size and
format specifier used in writing program are as follow.
C Input Output
C Output
Printf() is the main output function used in C. The main use
of this function is to send formatted output to the screen.
Ex:
C Input
In C programming, scanf() is one of the commonly used
function to take input from the user. The scanf() function
reads formatted input from the standard input such as
keyboards. Ex
C Programming Operators
Different programming operators are used in C language for
performing different operation. It mainly includes
Arithmetic operators, Assignment Operators, Comparison
& Logical operators, Bitwise Logical Operator and some
special operators.
C Arithmetic Operators
An arithmetic operator performs mathematical operations
such as addition, subtraction, multiplication, division etc
on numerical values (constants and variables).
C Assignment Operators
An assignment operator is used for assigning a value to
a variable. The most common assignment operator
is =. Table shows different Assignment operators used
in C.
C Comparison & Logical operators
Comparison and logical operators can be grouped in to two, mainly
relational operators and Logical operators. A relational operator
checks the relationship between two expressions. 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.
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.
C Bitwise Operators
During computation, mathematical operations like:
addition, subtraction, multiplication, division, etc are
converted to bit-level which makes processing faster and
saves power. Bitwise operators are used in C
programming to perform bit-level operations.
C special operators
case constant2:
// statements
break;
.
.
default:
// default statements
}
goto Statement