0% found this document useful (0 votes)
16 views

Unit 1

The document provides an introduction to the C programming language, covering its structure, basic data types, and how to write simple programs. It includes examples of variable declaration, initialization, and input/output operations, emphasizing the importance of identifiers and data types. Additionally, it discusses operators, including arithmetic, comparison, and logical operators, as well as the concept of local and global variables.

Uploaded by

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

Unit 1

The document provides an introduction to the C programming language, covering its structure, basic data types, and how to write simple programs. It includes examples of variable declaration, initialization, and input/output operations, emphasizing the importance of identifiers and data types. Additionally, it discusses operators, including arithmetic, comparison, and logical operators, as well as the concept of local and global variables.

Uploaded by

repaer072
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 54

Introduction to the C Language

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

C is a structured programming language. It is


considered a high-level language because it allows the
programmer to concentrate on the problem at hand
and not worry about the machine that the program
will be using. That is another reason why it is used by
software developers whose applications have to run on
many different hardware platforms.

2
C Programs

It's time to write your first C program.

Topics discussed in this section:


Structure of a C Program
Your First C Program
Comments
The Greeting Program

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;
}

Output: Hello world!


Examples of Valid and Invalid Names

9
Note
C is a case-sensitive language.

10
Variables

Variables are named memory locations that have a type,


such as integer or character, which is inherited from
their type. The type determines the values that a variable
may contain and the operations that may be used with
its values.

Topics discussed in this section:


Variable Declaration
Variable Initialization

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;
}

Output : Enter a number: 4


Number is 4
more and more
16

#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

Computer Science: A Structured Programming Approach Using C 17


Computer Science: A Structured Programming Approach Using C 18
Computer Science: A Structured Programming Approach Using C 19
Computer Science: A Structured Programming Approach Using C 20
Computer Science: A Structured Programming Approach Using C 21
Computer Science: A Structured Programming Approach Using C 22
Computer Science: A Structured Programming Approach Using C 23
Data Types
24
Note
sizeof (short) ≤ sizeof (int) ≤ sizeof (long) ≤ sizeof (long long)

25
Note
sizeof (float) ≤ sizeof (double) ≤ sizeof (long double)

26
Increment Operators
27

 Prefix Increment : ++a


 example:

int a=5;

b=++a; // value of b=6; a=6;

 Postfix Increment: a++


 example

int a=5;

b=a++; //value of b=5; a=6;
Contd…
28

 Comparision Operators: <, > , <=,


>= , !=, ==, !, &&,
|| .
 example:

int a=4, b=5;

a<b returns a true(non zero number) value.

 Bitwise Operators: <<, >>, ~, &, | ,^ .


 example

int a=8;

a= a>>1; // value of a becomes 4
Input / Output
29

 printf (); //used to print to


console(screen)
 scanf (); //used to take an input from
console(user).
 example: printf(“%c”, ’a’); scanf(“%d”, &a);
 More format specifiers
%c The character format specifier.
%d The integer format specifier.
%i The integer format specifier (same as %d).
%f The floating-point format specifier.
%o The unsigned octal format specifier.
%s The string format specifier.
%u The unsigned integer format specifier.
%x The unsigned hexadecimal format specifier.
%% Outputs a percent sign.
Variables
30

 A variable is a name of the memory


location. It is used to store data.
 It is a way to represent memory location
through symbol so that it can be easily
identified.
 Example:
int a;
float b;
char c;
Variables
31

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

 A variable that is declared inside the


function or block is called a local
variable.
void func()
{
int x=10;//local variable
}
Global Variable
33

 A variable that is declared outside the


function or block is called a global
variable.
int value=20;//global variable
void func()
{
int x=10;//local variable
}
Examples…

1. #include <stdio.h>
2. void main(){

3. int a;
4. int b;
5. int sum;
6. }

Computer Science: A Structured Programming Approach Using C 34


Example – 1
#include<stdio.h>
int a=50, b=40;
void main()
{
printf("a = %d and b=%d",a,b);
}
Example – 2
#include<stdio.h>
void main()
{
int x=50, y=40;
printf("x = %d and y=%d",x, y);
}

Computer Science: A Structured Programming Approach Using C 35


1. #include<stdio.h> void main()
2. // Global variables {
3. int a; int Ans1, Ans2, c=30;// Local variable
4. int b; a = 50;
b = 70;
5. int Add()
6. { Ans1 = Add();
7. return a + b; Ans2= Mul();
8. }
9. int Mul() printf("The addition result is: %d\n",Ans1);
10. { printf("The Multiplication result is: %d\
11. int c=10; // n",Ans2);
Local Variable printf("%d\n", c);
12. int d=20; //// }
Local Variable
13. return c*d;
14. }
15.

Computer Science: A Structured Programming Approach Using C 36


Output
The addition result is: 120
The Multiplication result is:
200
30

Computer Science: A Structured Programming Approach Using C 37


Examples

Computer Science: A Structured Programming Approach Using C 38


Output

Computer Science: A Structured Programming Approach Using C 39


Examples

Computer Science: A Structured Programming Approach Using C 40


Examples

Computer Science: A Structured Programming Approach Using C 41


Examples

Computer Science: A Structured Programming Approach Using C 42


Operators

Computer Science: A Structured Programming Approach Using C 43


Types

Computer Science: A Structured Programming Approach Using C 44


Computer Science: A Structured Programming Approach Using C 45
 int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400)

Computer Science: A Structured Programming Approach Using C 46


Assignment Operator : Assignment operators are used to assign values to variables.

Computer Science: A Structured Programming Approach Using C 47


Comparison Operators:
Comparison operators are used to compare two values (or
variables).

int x = 5;
int y = 3;
printf("%d", x > y);
Computer Science: A Structured Programming Approach Using C 48
Logical Operators

Computer Science: A Structured Programming Approach Using C 49


Increment/ Decrement

Computer Science: A Structured Programming Approach Using C 50


Booleans
 In programming, you will need a data
type that can only have one of two
values, like:
• YES / NO

• ON / OFF

• TRUE / FALSE

Booleans represent values that are


either true (1) or false (0).
Computer Science: A Structured Programming Approach Using C 51
Ex-1
int x = 10;
int y = 9;
printf("%d", x > y);

Ex-2
printf("%d", 10 == 10);
printf("%d", 10 == 15);
printf("%d", 5 == 55);

Computer Science: A Structured Programming Approach Using C 52


Operator
Precedence Description Associativity

() Parentheses (function call)

[] Array Subscript (Square Brackets)

1 . Dot Operator Left-to-Right

-> Structure Pointer Operator

++ , — Postfix increment, decrement

++ / — Prefix increment, decrement

+/– Unary plus, minus

!,~ Logical NOT, Bitwise complement

2 Right-to-Left
(type) Cast Operator

* Dereference Operator

& Addressof Operator

sizeof Determine size in bytes

3 *,/,% Multiplication, division, modulus Left-to-Right

4 +/- Addition, subtraction Left-to-Right

5 << , >> Bitwise shift left, Bitwise shift right Left-to-Right

< , <= Relational less than, less than or equal to

6 Left-to-Right

> , >= Relational greater than, greater than or equal to

7 == , != Relational is equal to, is not equal to Left-to-Right


Ex – 1
#include <stdio.h>
void main()
{
printf("value is = %d",
(10++));
}
Ex – 2
#include <stdio.h> Ex – 3
void main() #include <stdio.h>
{ int main()
int a=10,b=2,x=0; {
x=a+b*a+10/2*a; int x,y;
x=(100,200);
printf("value is =%d",x);
} y=100,200;
printf("x=%d,y=
%d",x,y);
return 0;
}

You might also like