Chapter 1 - BASIC OF C - VARIABLE
Chapter 1 - BASIC OF C - VARIABLE
1.1 C program
1.2 Variables & Data types
1.3 Operator
1
1.1 C Program
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.
4
1.1 C Program
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.
01/2009 6
1.1 C Program
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;
01/2009 8
1.2 Variables and data types:
01/2009 9
1.2 Variables and data types:
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:
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:
01/2009 12
1.2 Variables and data types:
01/2009 13
1.2 Variables and data types:
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:
01/2009 16
1.3 Operator:
01/2009 17
1.3 Operator:
- 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:
- 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:
- 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:
- Pre-fix vs post-fix:
01/2009 21
1.3 Operator:
- 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:
01/2009 23
1.3 Operator:
01/2009 24
1.3 Operator:
- 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.
01/2009 25
1.3 Operator:
01/2009 26
1.3 Operator:
01/2009 27
1.3 Operator:
01/2009 28
1.3 Operator:
01/2009 29
1.3 Operator:
01/2009 30
1.3 Operator:
01/2009 31