C Lecture-2-Parts-Of-C
C Lecture-2-Parts-Of-C
C++ programs has parts and components that serve specific purposes.
#include <iostream>
using namespace std;
int main ()
{
cout << " Programming is Fun !!! ";
return 0;
}
• ignored by compiler
• notes to human reader
• /*...*/
• mark the beginning and end of a comment with
multiple lines
• Declares that a program will be accessing entities whose names are part
of the namespace called std
• e.g. every name created by the iostream file is part of the namespace std
int
• Short for integer – it indicates that the function main will send and integer
value back to Operating System ( OS ) when it is completed execution.
main()
• Start of function main . Function is a group of one or more programming
statement. named main
• The starting point of the program
• Newline: \n or endl
• Horizontal tab : \t causes the cursor to skip over to the next tab stop.
• Single quote: \' - causes a single quotation mark to be printed
• Double quote: \" – causes a double quotation mark to be printed.
• Double backslash: \\ - causes a single backslash mark to be printed
• These can occur in strings:
o "hello\nthere"
o "she said \" boo \" very quietly"
cout << "The best selling book on Amazon \n is \" The Help \" ";
Literals
Identifiers
• Rules:
a) May not be a keyword (see p. 41 for complete list)
b) The first character must be a letter or underscore
c) Following characters must be letters, numbers or underscores
only.
Variables
o int someNumber;
o char firstLetter;
What about the following : int int , int Int , int _main , int include# ,
int namespaces.
Variable Assignment
Variable Initialization
• To initialize a variable means to assign it a value when it is defined:
• You can define and initialize multiple variables at once (and change them
later) :
#include <iostream>
using namespace std; What is the output of this program ??
int main()
{
int number;
number = 100;
cout << "The value of the number is " << number << endl;
number = 50;
cout << "The value of the number is " << number << endl;
cout << "The value of the number is " << " number " << endl;
return 0;
}
Data Types
Data Types
*some compiler use 10 bytes for long double : the range is +/- 3.4E-
4932 and
+/- 1.1E4832
• Floating-point literals can be represented in
Note : there are no unsigned floating point data types. On all machines,
variables of the float , double, and long double data types can store positive or
negative numbers.
int i; float f;
f = 8.9;
i = 8.9; // stores 8 in i ( truncates, does not round )
i = 8;
f = 8; // stores 8.0 in f
f = 7.9;
i = f; // stores 7 in i
• Defined as bool
• Literals: the values are true or false
bool boolValue;
boolValue = true;
cout << boolValue << endl;
boolValue = false;
cout << boolValue << endl;
Output:
1
0
• char
• Literals: All the keyboard and printable symbols such as 'A' '3' '!' '\n'
'n'.
• Numeric value of character from the ASCII character set is stored in memory:
char letter;
letter = 'A'; // 65 is stored in memory
cout << letter << endl;
letter = '!';
cout << letter << endl;
Output:
A
!
char letter;
letter = 65;
cout << letter << endl;
letter = 66;
cout << letter << endl;
Output:
A
B
firstName = "George";
lastName = "Washington";
Named Constants
Same literal may be used throughout a program, but may want to change it
later.
General Form:
For Example
• Initialization required
Scopes of a Variable
Example :-
#include <iostream>
using namespace std;
int main () {
value = 150; //error, use of value before it is
defined
int value;
cout << value; }
sizeof
• sizeof function returns size of a data type in bytes in any system.
• The result is system-dependent.
• The argument may be a data type:
sizeof(int) // result is 4 on most systems
• The argument may be a variable:
double salary;
cout << sizeof(salary); // result is 8 on most systems
cout << "The size of an integer is " << sizeof(int) << " bytes.\n";
#include<iostream>
using namespaces std;
int mian(){
integer a;
a,b,s int;
d float;
cin <<"The end of the program";
retun 0;
}