C Hello World Example
A C program basically consists of the following parts:
-
Preprocessor Commands
-
Functions
-
Variables
-
Statements & Expressions
-
Comments
Let us look at a simple code that would print the words "Hello World":
#include
int main()
{
/* my first program in C */
printf("Hello, World! \n");
return 0;
}
Let us look various parts of the above program:
-
The first line of the program #include is a preprocessor command, which tells a C compiler to include stdio.h file before going to actual compilation.
-
The next line int main() is the main function where program execution begins.
-
The next line /*...*/ will be ignored by the compiler and it has been put to add additional comments in the program. So such lines are called comments in the program.
-
The next line printf(...) is another function available in C which causes the message "Hello, World!" to be displayed on the screen.
-
The next line return 0; terminates main()function and returns the value 0.
Tokens in C
A C program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol. For example, the following C statement consists of five tokens:
printf("Hello, World! \n");
The individual tokens are:
printf
(
"Hello, World! \n"
)
;
Semicolons ;
In C program, the semicolon is a statement terminator. That is, each individual statement must be ended with a semicolon. It indicates the end of one logical entity.
For example, following are two different statements:
printf("Hello, World! \n");
return 0;
Comments are like helping text in your C program and they are ignored by the compiler. They start with /* and terminates with the characters */ as shown below:
/* my first program in C */
You cannot have comments within comments and they do not occur within a string or character literals
Integer Types
Following table gives you details about standard integer types with its storage sizes and value ranges:
Type
|
Storage size
|
Value range
|
char
|
1 byte
|
-128 to 127 or 0 to 255
|
unsigned char
|
1 byte
|
0 to 255
|
signed char
|
1 byte
|
-128 to 127
|
int
|
2 or 4 bytes
|
-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
|
unsigned int
|
2 or 4 bytes
|
0 to 65,535 or 0 to 4,294,967,295
|
short
|
2 bytes
|
-32,768 to 32,767
|
unsigned short
|
2 bytes
|
0 to 65,535
|
long
|
4 bytes
|
-2,147,483,648 to 2,147,483,647
|
unsigned long
|
4 bytes
|
0 to 4,294,967,295
|
To get the exact size of a type or a variable on a particular platform, you can use the sizeof operator. The expressions sizeof(type) yields the storage size of the object or type in bytes. Following is an example to get the size of int type on any machine:
#include
#include
int main()
{
printf("Storage size for int : %d \n", sizeof(int));
return 0;
}
When you compile and execute the above program it produces the following result on Linux:
Storage size for int : 4
Floating-Point Types
Following table gives you details about standard floating-point types with storage sizes and value ranges and their precision:
Type
|
Storage size
|
Value range
|
Precision
|
float
|
4 byte
|
1.2E-38 to 3.4E+38
|
6 decimal places
|
double
|
8 byte
|
2.3E-308 to 1.7E+308
|
15 decimal places
|
long double
|
10 byte
|
3.4E-4932 to 1.1E+4932
|
19 decimal places
|
The header file float.h defines macros that allow you to use these values and other details about the binary representation of real numbers in your programs. Following example will print storage space taken by a float type and its range values:
#include
#include
int main()
{
printf("Storage size for float : %d \n", sizeof(float));
printf("Minimum float positive value: %E\n", FLT_MIN );
printf("Maximum float positive value: %E\n", FLT_MAX );
printf("Precision value: %d\n", FLT_DIG );
return 0;
}
When you compile and execute the above program, it produces the following result on Linux:
Storage size for float : 4
Minimum float positive value: 1.175494E-38
Maximum float positive value: 3.402823E+38
Precision value: 6
Variable Definition in C:
A variable definition means to tell the compiler where and how much to create the storage for the variable. A variable definition specifies a data type and contains a list of one or more variables of that type as follows:
type variable_list;
Here, type must be a valid C data type including char, w_char, int, float, double, bool or any user-defined object, etc., and variable_list may consist of one or more identifier names separated by commas. Some valid declarations are shown here:
int i, j, k;
char c, ch;
float f, salary;
double d;
#include
int main ()
{
/* variable definition: */
int a, b;
int c;
float f;
/* actual initialization */
a = 10;
b = 20;
c = a + b;
printf("value of c : %d \n", c);
f = 70.0/3.0;
printf("value of f : %f \n", f);
return 0;
}
When the above code is compiled and executed, it produces the following result:
value of c : 30
value of f : 23.333334
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C language is rich in built-in operators and provides the following types of operators:
-
Arithmetic Operators
-
Relational Operators
-
Logical Operators
-
Bitwise Operators
-
Assignment Operators
-
Misc Operators
This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.
Arithmetic Operators
Following table shows all the arithmetic operators supported by C language. Assume variable A holds 10 and variable B holds 20 then:
Show Examples
Operator
|
Description
|
Example
|
+
|
Adds two operands
|
A + B will give 30
|
-
|
Subtracts second operand from the first
|
A - B will give -10
|
*
|
Multiplies both operands
|
A * B will give 200
|
/
|
Divides numerator by de-numerator
|
B / A will give 2
|
%
|
Modulus Operator and remainder of after an integer division
|
B % A will give 0
|
++
|
Increments operator increases integer value by one
|
A++ will give 11
|
--
|
Decrements operator decreases integer value by one
|
A-- will give 9
|
Relational Operators
Following table shows all the relational operators supported by C language. Assume variable A holds 10 and variable B holds 20, then:
Show Examples
Operator
|
Description
|
Example
|
==
|
Checks if the values of two operands are equal or not, if yes then condition becomes true.
|
(A == B) is not true.
|
!=
|
Checks if the values of two operands are equal or not, if values are not equal then condition becomes true.
|
(A != B) is true.
|
>
|
Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.
|
(A > B) is not true.
|
<
|
Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.
|
(A < B) is true.
|
>=
|
Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.
|
(A >= B) is not true.
|
<=
|
Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.
|
(A <= B) is true.
|
Logical Operators
Following table shows all the logical operators supported by C language. Assume variable A holds 1 and variable B holds 0, then:
Show Examples
Operator
|
Description
|
Example
|
&&
|
Called Logical AND operator. If both the operands are non-zero, then condition becomes true.
|
(A && B) is false.
|
||
|
Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true.
|
(A || B) is true.
|
!
|
Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.
|
!(A && B) is true.
|
Bitwise Operators
Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and ^ are as follows:
p
|
q
|
p & q
|
p | q
|
p ^ q
|
0
|
0
|
0
|
0
|
0
|
0
|
1
|
0
|
1
|
1
|
1
|
1
|
1
|
1
|
0
|
1
|
0
|
0
|
1
|
1
|
The Bitwise operators supported by C language are listed in the following table. Assume variable A holds 60 and variable B holds 13, then:
Show Examples
Operator
|
Description
|
Example
|
&
|
Binary AND Operator copies a bit to the result if it exists in both operands.
|
(A & B) will give 12, which is 0000 1100
|
|
|
Binary OR Operator copies a bit if it exists in either operand.
|
(A | B) will give 61, which is 0011 1101
|
^
|
Binary XOR Operator copies the bit if it is set in one operand but not both.
|
(A ^ B) will give 49, which is 0011 0001
|
~
|
Binary Ones Complement Operator is unary and has the effect of 'flipping' bits.
|
(~A ) will give -61, which is 1100 0011 in 2's complement form.
|
<<
|
Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.
|
A << 2 will give 240 which is 1111 0000
|
>>
|
Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.
|
A >> 2 will give 15 which is 0000 1111
|
Assignment Operators
There are following assignment operators supported by C language:
Show Examples
Operator
|
Description
|
Example
|
=
|
Simple assignment operator, Assigns values from right side operands to left side operand
|
C = A + B will assign value of A + B into C
|
+=
|
Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand
|
C += A is equivalent to C = C + A
|
-=
|
Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand
|
C -= A is equivalent to C = C - A
|
*=
|
Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand
|
C *= A is equivalent to C = C * A
|
/=
|
Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand
|
C /= A is equivalent to C = C / A
|
%=
|
Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand
|
C %= A is equivalent to C = C % A
|
<<=
|
Left shift AND assignment operator
|
C <<= 2 is same as C = C << 2
|
>>=
|
Right shift AND assignment operator
|
C >>= 2 is same as C = C >> 2
|
&=
|
Bitwise AND assignment operator
|
C &= 2 is same as C = C & 2
|
^=
|
bitwise exclusive OR and assignment operator
|
C ^= 2 is same as C = C ^ 2
|
|=
|
bitwise inclusive OR and assignment operator
|
C |= 2 is same as C = C | 2
|
More explaination of C.
A C program basically has the following form:
-
Preprocessor Commands
-
Functions
-
Variables
-
Statements & Expressions
Basic structure:
#include
int main()
{
/* My first program */
printf("Hello, World! \n");
return 0;
}
Preprocessor Commands:
These commands tells the compiler to do preprocessing before doing actual compilation.
Like #include
Functions:
are main building blocks of any C Program. Every C Program will have one or more functions and there is one mandatory function which is called main() function. This can be of any type i.e. integer, float or character depending on the return value.
Variables: are used to hold numbers, strings and complex data for manipulation.
Some examples on C Variables………………………………
Above program for (minus, multiply,, division)
Statement:
Example:
X=a + b;
Flow control:
-
For loop (With any Example)
-
If Statement (With any Example)
Share with your friends: |