CSE1201 Lecture1
CSE1201 Lecture1
CSE1201
Course Teacher: Jannatul Ferdous Ruma
2
Structure of C Program
A C program basically consists of the following parts:
• Preprocessor Commands
• Functions
• Variables
• Comments
Structure of C Program
Preprocessor
Commands
Main Function
Comment
Library
Function End of
Program
Preprocessor Commands
4
5
Functions
• Functions are main building blocks of any C Program.
• Every C Program will have one or more functions and there is one
mandatory function which is called main() function.
7
Structure of C Program
Preprocessor
Commands
Main Function
Comment
Library
Function End of
Program
8
C’s Basic Data Type
Type Keyword format Memory
Specifier Requirements
Character data char %c 1 Byte
Signed whole numbers int %d 2 or 4 Byte
Floating-point numbers float %f 4 Byte
Double-precision floating- double %lf 8 Byte
point number
valueless void ---
9
How to Declare Variables
• To declare a variable, use this general form:
type var-name;
• In C, a variable declaration is a statement and it must end in
a semicolon (;).
10
Variable
• Variables consist of letters and digits, in any order, except that the
first character must be a letter.
• Both upper-and lowercase letters are permitted, though common
usage favors the use of lowercase letters for most types of variables.
• Upper- and lowercase letters are not interchangeable (i.e., an
uppercase letter is not equivalent to the corresponding lowercase
letter.)
11
Variable (cont.)
• The underscore character (_) can also be included, and is considered
to be a letter.
• An underscore is often used in the middle of an variable.
• A variable may also begin with an underscore, though this is rarely
done in practice.
12
Variable (cont.)
• Case-sensitive
• COUNT and count are not same
13
Variable (Cont.)
Can not Use blank space
joty-5
apon
apon123 this_is_a_long_name
_sojeb_1 VaReNdRa
15
Is it Valid Variable Name?
4th The first character must be letter
17
Keywords
• There are certain reserved words, called Keywords, that have
standard, predefined meaning in C
18
Keywords
19
Expressions
• An expression is a combination of operators and operands.
• C expressions follow the rule of algebra
Expression Operator
Arithmetic Expression +, -, *, /, %
Logical Expression AND, OR, NOT
Relational ==, !=, <, >, <=, >=
20
Assign value to variable
• To assign a value to a variable, put its name to the left of an equal sign (=).
• Put the variable you want to give the variable to the right of the equal sign.
variable value
;
21
22
Input Numbers From Keyboard
• There are several methods
• The easiest is – scanf() function.
23
24
Escape Sequences
• There are certain characters in C when they are preceded by a
backslash (\) they will have special meaning.
• They are used to represent like newline (\n) or tab (\t).
25
Defining Constants
• There are two simple ways in C to define constants:
• Using #define preprocessor.
• Using const keyword.
26
Using #define preprocessor
• Following is the form to use #define preprocessor to define a
constant:
27
Using #define preprocessor
28
Symbolic Constants
29
30
Variables Types
• There are two types of variables in c program they are,
• Local variable
• Global variable
Global Variables
• These variables are declared /*
/* Compute
Compute Area
circle
circle */*/
Area and
and Perimeter
Perimeter of
of aa
void test()
{
printf("\n\nFrom test function");
printf("\nvalues: m=%d:n=%d:a=%d:b=%d",
m,n,a,b);
}
Type Conversion
• A type cast is basically a conversion from one type to another. There are two types
of type conversion:
• Implicit Type Conversion Also known as ‘automatic type conversion’.
• Done by the compiler on its own, without programmers intervention.
• All the data types of the variables are upgraded to the data type of the variable
with largest data type
Type Conversion (Example)
#include<stdio.h> #include <stdio.h>
int main() main ()
{ {
int x = 10; // integer x int a;
char y = 'a'; // character c a = 15/6;
x = x + y; // ‘a’ ascii is 97 printf("%d",a);
float z = x + 1.0; }
printf("x = %d, z = %f", x, z);
return 0;
}
Output: Output:
x = 107, z = 108.000000 2
Implicit Type Conversion
i (int) = 7, f (float)= 5.5, c (char) = ‘w’.
int main()
{
double x = 1.2;