Unit2CPROGRAMMINGBASICSpdf 2024 09 09 13 51 30
Unit2CPROGRAMMINGBASICSpdf 2024 09 09 13 51 30
(01CE1101)
C Programming Basics
Compilation 010100011010
101011100000
and 101010101010
Linking 001110101010
processes
Creating program and save a file
Compiling the program
Steps of
Program Linking the program - with functions that
are needed from c library.
Execution
Executing the program
Output:
#include<stdio.h>
void main() Hello World..
{
printf(“Hello World..”);
Example
}
#include<stdio.h> Output:
void main()
Example { Hello World..How are you?
printf(“Hello World..”);
printf(“How are you?”);
}
Output:
#include<stdio.h>
void main()
Example {
Hello World..
How are you?
printf(“Hello World..”);
printf(“\nHow are you?”);
}
C does not contain any instruction to display output
on the screen.
All output to screen is achieved using readymade
printf( ) library functions - printf( ).
and
The general form of printf( ) function is,
its Purpose To display message
printf ( “Display strings”) ;
To print the value
printf ( "<format string>", <list of variables>) ;
The scanf() function is used for input. It reads the
input data from the console.
scanf("format Specifier”,&Variablename );
Note the use of ampersand (&) before the
scanf( ) variables in the scanf( ) function is a must.
and scanf(“%d”, &a);
its Purpose & is an ‘Address of’ operator. It gives the location
number (address) used by the variable in memory.
When we say &a, we are telling scanf( ) at which
memory location should it store the value supplied
by the user from the keyboard.
You need to use format specifiers whether you're printing
formatted output with printf() or accepting input with
scanf().
The format specifier is used during input and output.
It is a way to tell the compiler what type of data is in a
variable during taking input using scanf() or printing using
Format printf(). The following specifiers are used in C.
specifiers Character format specifier : %c
in C For Integer & Signed Integer format specifier : %d, %i
Floating-point format specifier : %f, %e or %E
String printing: %s
Double floating-point number : %lf
Unsigned integer: %u
Long long int: 2.%lld
Comments are used in a C program to clarify either
the purpose of the program or the purpose of some
statement in the program.
Comments It is a good practice to begin a program with a
in a comment indicating the purpose of the program, its
author and the date on which the program was
C Program written.
2 comment lines
- Single comment (//)
- Multiple line comment(/*…………*/)
Steps in
learning C
language
A character denotes any alphabet, digit or special
symbol used to represent information.
The C
Character Set
Increment ++
Increment decrement - -
and Both are used to change the value of an operand
decrement (constant or variable) by 1.
operators Increment ++ increases the value by 1.
decrement -- decreases the value by 1.
These two operators are unary operators,
meaning they only operate on a single operand.
The Bitwise operators is used to perform bit-level
operations on the operands.
The operators are first converted to bit-level and
Bitwise then the calculation is performed on the
Operators operands.
The mathematical operations such as addition,
subtraction, multiplication etc. can be performed
at bit-level for faster processing.
Bitwise
Operators
The conditional operator in C is also
called the ternary operator because it operates on
three operands.
The operands may be an expression, constants or
variables. It starts with a condition, hence it is
called a conditional operator.
Conditional Syntax:
operator expression1 ? expression2 : expression3;
or
condition ? true statement : false statement;
Example:
a=10; b=15;
X=(a>b)?a : b;
Special
Operators
Operator precedence determines the grouping of
terms in an expression and decides how an
expression is evaluated.
Certain operators have higher precedence than
Operator others.
Precedence For example, the multiplication operator has a
and higher precedence than the addition operator.
Associativity For example: Solve
10 + 20 * 30
10 + 20 * 30 is calculated as 10 + (20 * 30)
and not as (10 + 20) * 30
Operators Associativity is used when two
operators of same precedence appear in an
expression. Associativity can be
either Left to Right or Right to Left.
Operators with the highest precedence appear at
Operator the top of the table, those with the lowest appear
Precedence at the bottom. Within an expression, higher
and precedence operators will be evaluated first.
Associativity For example:
‘*’ and ‘/’ have same precedence and their
associativity is Left to Right, so the expression “100 /
10 * 10” is treated as “(100 / 10) * 10”.
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative */% Left to right
Additive +- Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Operator Equality == != Left to right
Precedence Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= Right to left
|=
Comma , Left to right
Type conversion :
In type conversion, a data type is automatically converted
into another data type by a compiler at the compiler time. In
type conversion, the destination data type cannot be smaller
than the source data type, that’s why it is also called widening
Type conversion. One more important thing is that it can only be
applied to compatible data types.
Conversion
and Type Type Conversion example
float y;
y=x; // y==30.000000.
#include <stdio.h>
int main ()
Type Casting - {
Example float x;
x = (float) 7/5;
printf(“%f”,x);
}
Type Casting
In typing casting, a data type is converted into another
data type by the programmer using the casting
operator during the program design.
The destination data type may be smaller than the
Type source data type when converting the data type to
another data type, that’s why it is also called narrowing
Conversion conversion.
and Type Syntax/Declaration:-
destination_datatype = (target_datatype)variable;
Casting
where
- (): is a casting operator.
- target_datatype: is a data type in which we want to
convert the source data type.
Converting an expression of a given type into another type
is known as type casting. typecasting is more use in c
language programming. Here, It is best practice to convert
lower data type to higher data type to avoid data loss.
Data will be truncated when the higher data type is
Type Casting – converted to lower. For example, if a float is converted to
Types int, data which is present after the decimal point will be
lost.
There are two types of type casting in c language.
✓Implicit Conversion
✓Explicit Conversion
IMPLICIT CONVERSION
Implicit conversions do not require any operator for converted. They
are automatically performed when a value is copied to a compatible
type in the program.Here, the value of a has been promoted from int
to double and we have not had to specify any type-casting operator.
This is known as a standard conversion.
Example :
#include<stdio.h>
Type Casting – #include<conio.h>
Types void main()
{
int i=20;
double p;
clrscr();
p=i; // implicit conversion
printf(“implicit value is %d”,p);
getch();
}
EXPLICIT CONVERSION
In C language, Many conversions, especially those that imply a
different interpretation of the value, require an explicit
conversion. We have already seen two notations for explicit
type conversion.
Ex:
#include<stdio.h>
Type Casting – #include<conio.h>
Types void main()
{
int i=20;
short p;
clrscr();
p = (short) i; // Explicit conversion
printf(“Explicit value is %d”,p);
getch();
}
S.No TYPE CASTING TYPE CONVERSION
In type casting, a data type is
Whereas in type conversion, a data
converted into another data
1. type is converted into another data
type by a programmer using
type by a compiler.
casting operator.
Type Type casting can be applied Whereas type conversion can only
Conversion 2. to compatible data types as be applied to compatible
well as incompatible data types. datatypes.
and Type In type casting, casting
Casting 3.
operator is needed in order to Whereas in type conversion, there
cast the a data type to another is no need for a casting operator.
data type.
In typing casting, the
destination data type may be Whereas in type conversion, the
4. smaller than the source data destination data type can’t be
type, when converting the data smaller than source data type.
type to another data type.
S.NO TYPE CASTING TYPE CONVERSION
Type casting takes place during
Whereas type conversion is done
5. the program design by
at the compile time.
programmer.
Type
Conversion Type casting is also called Whereas type conversion is also
narrowing conversion because in called widening conversion
and Type 6. this, the destination data type because in this, the destination
Casting may be smaller than the source data type can not be smaller than
data type. the source data type.