CHAPTER 2 Basics of C Programming
CHAPTER 2 Basics of C Programming
BASICS OF C PROGRAMMING
HISTORY OF C
Writing a C program in any other structure will hence lead to a Compilation Error.
Header Files
The first and foremost component is the inclusion of the Header files in a C program.
A header file is a file with extension .h
It contains C function declarations and macro definitions to be shared between
several source files.
There must to be starting point from where execution of compiled C program begins.
Syntax:
returntype main()
{
Return type may be int or void ( do not return any type of value)
Data Concepts:
Character set
Character set is a set of alphabets, letters and some special characters that are valid in C
language.
Alphabets
Uppercase: A B C ................................... X Y Z
Lowercase: a b c ...................................... x y z
Digits
0123456789
Special Characters
Blank space, newline, horizontal tab, carriage, return and form feed.
Special Characters in C Programming
, < > . _
( ) ; $ :
% [ ] # ?
^ ! * / |
- \ ~ +
C Keywords
Keywords are predefined, reserved words used in programming that have special meanings to the compiler.
Keywords are part of the syntax and they cannot be used as an identifier.
C Keywords
auto double int struct
break else long switch
case enum register typedef
char extern return union
continue for signed void
do if static while
default goto sizeof volatile
const float short unsigned
C Identifiers
Identifier refers to name given to entities such as variables, functions, structures etc.
Identifiers must be unique.
They are created to give a unique name to an entity to identify it during the execution of the program. For example:
1.int money;
2.double accountBalance;
Here, money and accountBalance are identifiers.
Also remember, identifier names must be different from keywords.
You cannot use int as an identifier because int is a keyword.
Here, the type of number variable is int. You cannot assign a floating-point (decimal) value 5.5 to this
variable. Also, you cannot redefine the data type of the variable to double. By the way, to store the
decimal values in C, you need to declare its type to either double or float.
Constants
If you want to define a variable whose value cannot be changed,
you can use the const keyword. This will create a constant. For
example,
const double PI = 3.14;
Notice, we have added keyword const.
Here, PI is a symbolic constant; its value cannot be changed.
const double PI = 3.14;
PI = 2.9; //Error
Data Types in C
Type Size (bytes) Format Specifier
int at least 2, usually 4 %d
char 1 %c
float 4 %f
double 8 %lf
short int 2 usually %hd
unsigned int at least 2, usually 4 %u
long int at least 4, usually 8 %li
long long int at least 8 %lli
unsigned long int at least 4 %lu
unsigned long
at least 8 %llu
long int
signed char 1 %c
unsigned char 1 %c
long double at least 10, usually 12 or 16 %Lf
int
Integers are whole numbers that can have both zero, positive and negative values but
no decimal values. For example, 0, -5, 10
We can use int for declaring an integer variable.
int id;
Here, id is a variable of type integer.
You can declare multiple variables at once in C programming. For example,
int id, age;
float and double
float and double are used to hold real numbers.
float salary;
double price;
In C, floating-point numbers can also be represented in exponential. For example,
float normalizationFactor = 22.442e2;
What's the difference between float and double?
The size of float (single precision float data type) is 4 bytes. And the size of double
(double precision float data type) is 8 bytes.
char
Keyword char is used for declaring character type variables. For example,
char test = 'h';
The size of the character variable is 1 byte.
* multiplication
/ division
remainder after division (modulo
%
division)
C Increment and Decrement Operators
C programming has two operators increment ++ and decrement -- to change the value of an
operand (constant or variable) by 1.
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.
1. #include <stdio.h>
2. int main()
3. { int a = 10, b = 100;
4. float c = 10.5, d = 100.5;
5. printf("++a = %d \n", ++a);
6. printf("--b = %d \n", --b);
7. printf("++c = %f \n", ++c);
8. printf("--d = %f \n", --d);
9. return 0;}
C Assignment Operators
An assignment operator is used for assigning a value to a variable.
The most common assignment operator is =
For example, x=int (7.5) means 7.5 is converted to integer by truncating it i.e. 7
b=(int) 22.7/(int) 5.3 means 22.7 will be converted to 22
When the above code is compiled and executed, it produces the following result −
Value of mean : 3.400000
#include <stdio.h>
main()
{
int i = 17;
char c = 'c'; /* ascii value is 99 */
int sum;
sum = i + c;
printf("Value of sum : %d\n", sum );
}
When the above code is compiled and executed, it produces the following result −
Value of sum : 116
Here, the value of sum is 116 because the compiler is doing integer promotion
Conditional or Ternary Operator
int x, y = 10;
x = (y < 10) ? 30 : 40;
cout << "value of x: " << x << endl;
getch();
}
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf("Output = %d", a&b);
return 0;
}
Output =8