Unit 1
Unit 1
Objectives
❏ To understand the structure of a C-language program.
❏ To write your first C program.
❏ To be able to create good identifiers for objects in a program.
❏ C basic data types.
❏ To be able to create and use variables.
❏ To understand input and output concepts.
1
Background
2
C Programs
3
Structure of a C Program
4
The Greeting Program
5
Examples of Block Comments
6
Examples of Line Comments
7
My first C program!
8
#include <stdio.h>
// program prints hello world
int main() {
printf ("Hello world!");
return 0;
}
9
Note
C is a case-sensitive language.
10
Variables
11
Variables Declaration
12
‘B’
Variable Initialization
13
Example 1
14
#include <stdio.h>
// program prints a number of type int
int main() {
int number = 4;
printf (“Number is %d”, number);
return 0;
}
Output: Number is 4
Example 2
15
#include <stdio.h>
// program reads and prints the same thing
int main() {
int number ;
printf (“ Enter a Number: ”);
scanf (“%d”, &number);
printf (“Number is %d\n”, number);
return 0;
}
#include <stdio.h>
int main() {
/* this program adds
two numbers */
int a = 4; //first number
int b = 5; //second number
int answer = 0; //result
answer = a + b;
}
Data Types
25
Note
sizeof (float) ≤ sizeof (double) ≤ sizeof (long double)
26
Increment Operators
27
int a=10,b=20;
float f=20.8;
char c='A';
There are many types of variables in
c:
1.local variable
2.global variable
3.static variable
Local Variable
32
1. #include <stdio.h>
2. void main(){
3. int a;
4. int b;
5. int sum;
6. }
int x = 5;
int y = 3;
printf("%d", x > y);
Computer Science: A Structured Programming Approach Using C 48
Logical Operators
• ON / OFF
• TRUE / FALSE
Ex-2
printf("%d", 10 == 10);
printf("%d", 10 == 15);
printf("%d", 5 == 55);
2 Right-to-Left
(type) Cast Operator
* Dereference Operator
6 Left-to-Right