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

Unit 2

Uploaded by

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

Unit 2

Uploaded by

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

Basics of C

UNIT-2 Programming
SYNTAX
The basic syntax of C programming involves a set of rules and
structures that must be followed when writing C code.

Example:
#include <stdio.h>

int main() {
printf("Hello World!");
return 0;
}
Example explained:

Line 1: #include <stdio.h> is a header file library that lets us work with
input and output
functions, such as printf() (used in line 4). Header files add functionality to
C programs.

Line 2: A blank line. C ignores white space. But we use it to make the code
more readable.

Line 3: Another thing that always appear in a C program is main(). This is


called a function.
Any code inside its curly brackets {} will be executed.

Line 4: printf() is a function used to output/print text to the screen. In our


example, it will output "Hello World!".

Line 5: return 0 ends the main() function.

Line 6: Do not forget to add the closing curly bracket } to actually end the
main function.
Statements
A computer program is a list of "instructions" to be "executed" by
a computer.
In a programming language, these programming instructions are
called statements.
The following statement "instructs" the compiler to print the text
"Hello World" to the screen:

It is important that you end the statement with a semicolon ;


If you forget the semicolon (;), an error will occur and the program
will not run:
SEMANTICS
In C programming, semantics refers to the meaning of the code, in
contrast to syntax, which describes the structure or grammar of the
language.
While syntax dictates how to write valid C statements (e.g., proper
use of semicolons, correct variable declarations), semantics
determines what those statements actually do and whether they
make logical sense within the program's context.
STRUCTURE OF C
PROGRAMS
• Documentation
This section consists of the description of the program, the name
of the program, and the creation date and time of the program.
• Preprocessor Section
All the header files of the program will be declared in the
preprocessor section of the program.
Header files help us to access other's improved code into our
code.
• Definition
Preprocessors are the programs that process our source code
before the process of compilation.
There are multiple steps which are involved in the writing and
execution of the program. Preprocessor directives start with the
'#' symbol.
Global Declaration
The global declaration section contains global variables, function
declaration, and static variables.
Variables and functions which are declared in this scope can be
used anywhere in the program.
Main() Function
Every C program must have a main function. The main() function of
the program is written in this section.
Operations like declaration and execution are performed inside the
curly braces of the main program.
Sub Programs
User-defined functions are called in this section of the program.
The control of the program is shifted to the called function
whenever they are called from the main or outside the main()
function.
COMMENTS
HEADER FILES
DATA TYPES IN C
 Primary data types
Primary data types are built in data types. They are directly
supported by machine. They are also known as fundamental data
types.
• integer Data Type:
The integer datatype in C is used to store the integer numbers (any
number including positive, negative and zero without decimal part).
Octal values, hexadecimal values, and decimal values can also be
stored in int data type in C.
Range: -2,147,483,648 to 2,147,483,647
Size: 4 bytes
Format Specifier: %d
• Character Data Type:
char data type can store single character of alphabet or digit or
special symbol. Each character is assigned some integer value
which is known as ASCII values.
Range: (-128 to 127) or (0 to 255)
Size: 1 byte
Format Specifier: %c
• Float Data Type:
float data type can store floating point number which represents a
real number with decimal point and fractional part.
Range: 1.2E-38 to 3.4E+38
Size: 4 bytes
Format Specifier: %f
• Double Data Type
The double data type in C is used to store decimal numbers
(numbers with floating point values) with double precision. It can
easily accommodate about 16 to 17 digits after or before a decimal
point.
Range: 1.7E-308 to 1.7E+308
Size: 8 bytes
Format Specifier: %lf
 Secondary data types
Secondary data types are not directly supported by the machine. It is
combination of
primary data types to handle real life data in more convenient way. It
can be further
divided in two categories:
a) Derived data type
Derived data type is extension of primary data type. It is built-in
system and its structure cannot be changed. Examples: Array,
Pointer, etc…
b) User defined data types
User defined data type can be created by programmer using
combination of primary data type and/or derived data type. User can
design it as per special requirements.
Examples: Structure, Union, enum, etc…
VARIABLES
Variables are memory location in computer's memory to store data.
To indicate the memory location, each variable should be given a
unique name called identifier. Identifiers are used for naming
variables, functions and arrays.
Variable names are just the symbolic representation of a memory
location.
Examples of variable name: sum, car_no, count etc.
int num;
float a, b;
char a;
Rules for Naming Variables in C
• We can assign any name to a C variable as long as it follows the
following rules:
• A variable name must only contain letters, digits,
and underscores.
• It must start with an alphabet or an underscore only. It
cannot start with a digit.
• No white space is allowed within the variable name.
• A variable name must not be any reserved word or keyword.
• The name must be unique in the program.
KEYWORDS
OPERATORS
1.ARITHMETIC OPERATORS
Arithmetic operators are
used for mathematical
calculation.
C supports following
arithmetic operators:
2. RELATIONAL OPERATORS
Relational operators are used to compare two numbers and taking
decisions based on their relation.
Relational expressions are used in decision statements such as if,
for, while, etc...
3. LOGICAL OPERATORS
Logical operators are used to test more than one condition and
make decisions
4. ASSIGNMENT OPERATORS
Assignment operators are used to assign the result of an expression
to a variable.
C also supports shorthand assignment operators which simplify
operation with assignment.
5. INCREMENT AND DECREMENT
OPERATORS
These are special operators in C which are generally not found in
other languages.
6. CONDITIONAL OPERATOR
Ternary operator is known as Conditional Operator.
A ternary operator pair " ? :" is available in c to construct
conditional expressions of the form:
exp1?exp2:exp3
Where exp1, exp2,exp3 are expression.
The operator ?:works as follows: expl is evaluated first. if it is true,
then the expression exp2 evaluated and becomes the value of the
expression. If exp1 false, exp3 is evaluated and its value becomes
the value of the expression.
7. BITWISE OPERATORS
Bitwise operators are used to perform operation bit by bit. Bitwise
operators may not be applied to float or double.
8. SPECIAL OPERATORS
C supports some special operators of interest such as comma
operator sizeof operator, pointer operator (& and *) (discuss in unit
6 pointer) and member selection operators (. and - >).
TYPE CONVERSION OR TYPE
CASTING
When an operator has operands of different types, they are
converted to a common type, this is known as type casting or type
conversion.
Typecasting is making a variable of one data type to act like
another data type such as an int to float.
There are two types of type casting:
1. Implicit Type Casting
When complier automatically convert data type of the data then it
is called as a implicit type conversion.
2. Explicit Type Casting
When type casting forcefully converts the value of one type into
another type then it is called as a explicit type conversion.
PRECEDENCE AND
ASSOCIATIVITY
Operator precedence and associativity are rules that decide the
order in which parts of an expression are calculated.
Precedence tells us which operators should be evaluated first, while
associativity determines the direction (left to right or right to left) in
which operators with the same precedence are evaluated.
Precedence of an operator is its priority in an expression for
evaluation.
Operator precedence is why the expression 5 + 3 * 2 is calculated as
5+(3*2), giving 11, and not as (5+3) * 2, giving 16.
We say that the multiplication operator (*) has higher "precedence"
or "priority" than the addition operator (+), so the multiplication
must be performed first.
Associativity is the left-to-right or right-to-left order for grouping
operands to operators that have the same precedence.
Following table provides a complete list of operator, their
precedence level, and their rule of association.

You might also like