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

Chapter 1 - BASIC OF C - VARIABLE

This document provides an overview of basic C/C++ concepts including C programs, variables and data types, and operators. It discusses the typical structure of a C program including preprocessor commands, main functions, statements, and comments. It also describes different variable data types like integers, floating-point, Boolean, and characters. Finally, it covers various operators in C/C++ like arithmetic, increment/decrement, and assignment operators.
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)
33 views

Chapter 1 - BASIC OF C - VARIABLE

This document provides an overview of basic C/C++ concepts including C programs, variables and data types, and operators. It discusses the typical structure of a C program including preprocessor commands, main functions, statements, and comments. It also describes different variable data types like integers, floating-point, Boolean, and characters. Finally, it covers various operators in C/C++ like arithmetic, increment/decrement, and assignment operators.
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/ 31

Chapter 1: BASIC OF C/C++

1.1 C program
1.2 Variables & Data types
1.3 Operator

1
1.1 C Program

Programs are a sequence of instructions or statements.


These statements form the structure of a C++ program. C
program basically consists of the following parts:
• Pre-processor commands.
• main function.
• Statements & expressions.
• Comments
#include “stdio.h”
void main()
{
/* my first program in C */
printf("Hello, World! \n");
getch();
}

2
1.1 C Program
• Line 1: #include “stdio.h”
– pre-processor command, which tells a C compiler to
include stdio.h file before going to actual compilation .
• Line 2: void main()
– the main function where the program execution begins..
• Line 4: /* my first program in C */
– this is comments and will be ignored by the compiler.
• Line 5,6: printf("Hello, World! \n");
– This line is a C++ statement. A statement is an expression
that can actually produce some effect.
• Line 3,7: { }
– The open brace ({) at line 5 indicates the beginning
of main's function, and the closing brace (}) at line 7,
indicates its end.
3
1.1 C Program

Build & Debug: after entered your code, you can build
and run it within Visual Studio, and then run it as a stand-
alone app from the command line.

• 1. To build your project, choose Build Solution from


the Build menu. The Output window shows the results of
the build process.

• 2. To run the code, on the menu bar, choose Debug, Start


without debugging.

4
1.1 C Program

Fix the errors: There are two tabbed windows in the


results window below the editor: the Output window, which
contains the raw compiler output (including error messages);
and the Error List window, which provides a sortable and
filterable list of all errors and warnings.

•1. When build succeeds, you see results like this in


the Output window:

5
1.1 C Program

•2. Walk through the error list and address all the errors in
your code:
•3. Click on the error line in the Error List window to
jump to the line the error occurs in.

•1. When build succeeds, you see results like this in


the Output window:

01/2009 6
1.1 C Program

Create your program with Visual Studio Software:

01/2009 7
1.2 Variables and data types:

1.2.1 Variables:
 In C, variable is a portion of memory to store a value:
 You need define variable before using it.
 To declare a variable in C:
Type Name;

• Each variable needs a name that identifies it and distinguishes it


from the others.
• A valid name of variable is a sequence of one or more letters,
digits, or underscore characters (_).
• Spaces, punctuation marks, and symbols cannot be part of an
identifier
• Name must always begin with a letter or underscore characters .

01/2009 8
1.2 Variables and data types:

1.2.2 Data types:


 In C, data types represent the basic storage units need to
store a value.
 There are 4 fundamental data types in C:
• Integer: They can store a whole number value, and can either
be signed or unsigned.
• Floating-point types: They can represent real values, such
as 3.14 or 0.01, with different levels of precision.
• Boolean type: The boolean type, known in C++ as bool, can only
represent one of two states, true or false
• Character types: They can represent a single character, such
as 'A' or '$'.

01/2009 9
1.2 Variables and data types:

1.2.1 Data types:


 Integer variable.
Data types Size
char 8 bits.
short 16 bits.
int 32 bits
long 64 bits.

Unique representable
Size Notes
values
8-bit 256 = 28
16-bit 65 536 = 216
32-bit 4 294 967 296 = 232 (~4 billion)
64-bit 18 446 744 073 709 551 616 = 264 (~18 billion billion)

01/2009 10
1.2 Variables and data types:

1.2.1 Data types:


 Floating-point variable.
Data types Size
float 32 bits.
double 64 bits.

 Boolean variable
Data types Value
bool true or false.

 Character variable
Data types Value
char 8 bit - ASCII.

01/2009 11
1.2 Variables and data types:

1.2.3 Declaration of Variables:


 C requires every variable to be declared with its type
before its first use.
 This informs the compiler the size to reserve in memory
for the variable and how to interpret its value.
 The syntax to declare a new variable in C++ simply write
the type followed by the variable name

01/2009 12
1.2 Variables and data types:

1.2.3 Declaration of Variables:


 If declaring more than one variable of the same type,
they can all be declared in a single statement by
separating their identifiers with commas. For example:

 This declares three variables (a, b and c), all of them of


type int, and has exactly the same meaning as:

01/2009 13
1.2 Variables and data types:

1.2.3 Declaration of Variables:


 When the variables are declared, they have an
undetermined value until they are assigned a value for
the first time.
 To initialize variables, we can write:

01/2009 14
1.3 Operator:
 In C, an operator is a symbol that tells the compiler to
perform specific mathematical or logical functions.
 In other words we can say that an operator operates the
operands.
 For example:

• Here, ‘+’ is the operator known as addition operator and ‘a’ and
‘b’ are operands.
• The addition operator tells the compiler to add both of the
operands ‘a’ and ‘b’.
• C is the result of this calculation.

01/2009 15
1.3 Operator:

1.3.1 Arithmetic Operators :

 Arithmetic operators used to perform arithmetic/


mathematical operations on operands.
 There are 7 arithmetic operators in C:

01/2009 16
1.3 Operator:

1.3.1 Arithmetic Operators :

01/2009 17
1.3 Operator:

1.3.1 Arithmetic Operators :

- Note:
• Division:
• In normal calculation, 9/4 = 2.25. However, the output is 2 in the
program
• It is because both variables a and b are integers. Hence, the
output is also an integer. The compiler neglects the term after
decimal point and shows answer 2 instead of 2.25.
• But, 9/4.0 = 2.25.

01/2009 18
1.3 Operator:

1.3.1 Arithmetic Operators :

- Note:
• Modulus:
• The modulus operator % computes the remainder. When a = 9 is
divided by b = 4, the remainder is 1
• The % operator can only be used with integers

01/2009 19
2.3 Operator:

2.3.1 Arithmetic Operators :

- Increment / Decrement:
• Increment ++ increases the value by 1 whereas decrement --
decreases the value by 1.
• These two operators are unary operators, meaning they only
operate on a single operand.
• The operators ++ and -- are used as pre-fix or post-fix.

01/2009 20
1.3 Operator:

1.3.1 Arithmetic Operators :

- Pre-fix vs post-fix:

• Suppose you use ++ operator as prefix like: ++var. The value


of var is incremented by 1 then, it returns the value.
• Suppose you use ++ operator as postfix like: var++. The original
value of var is returned first then, var is incremented by 1.

01/2009 21
1.3 Operator:

1.3.1 Arithmetic Operators :

- Increment / Decrement:
• Increment ++ increases the value by 1 whereas decrement --
decreases the value by 1.
• These two operators are unary operators, meaning they only
operate on a single operand.
• The operators ++ and -- are used as pre-fix or post-fix.

01/2009 22
1.3 Operator:

1.3.2 Assignment Operators:

 Assignment operators are used to assign value to a


variable.
 The left side operand of the assignment operator is a
variable and right side operand of the assignment
operator is a value.
 The value on the right side must be of the same data-
type of variable on the left side otherwise the compiler
will raise an error.

01/2009 23
1.3 Operator:

1.3.2 Assignment Operators:

01/2009 24
1.3 Operator:

1.3.2 Assignment Operators:

- Note:
• “+=”: This operator is combination of ‘+’ and ‘=’ operators. This
operator first adds the current value of the variable on left to the
value on right and then assigns the result to the variable on the
left.

• If initially value stored in a is 5. Then (a += 6) = 11

01/2009 25
1.3 Operator:

1.3.3 Relational Operators:

 Relational operators are used for comparison of the


values of two operands..
 If the relation is true, it returns 1; if the relation is false, it
returns value 0.
 The result of relational calculation always is a boolean
value.

01/2009 26
1.3 Operator:

1.3.3 Relational Operators:

01/2009 27
1.3 Operator:

1.3.3 Relational Operators:

01/2009 28
1.3 Operator:

1.3.4 Logical Operators:

 They are used to combine two or more conditions or to


complement the evaluation of the original condition in
consideration.
 An expression containing logical operator returns either
0 or 1 depending upon whether expression results true or
false.
 Logical operators are commonly used in decision making
in C programming.

01/2009 29
1.3 Operator:

1.3.4 Logical Operators:

01/2009 30
1.3 Operator:

1.3.4 Logical Operators:

01/2009 31

You might also like