Cprogram
Cprogram
PROGRAMMING
Basics about C
program
Topics covered
- Structure of C Program
- Life Cycle of Program from Source code to
Executable
- Compiling and Executing C Code
- Keywords
- Identifiers
- Primitive Data types in C
- Variables
- Constants
- Input/Output statements in C
- Operators
- Type conversion and type casting
Structure of a C program
A C program is divided into different sections. There are
six main sections to a basic c program :
1. Documentation/ Comments Section
2. Preprocessor Directives/ Link Section
3. Definition Section
4. Global Declaration Section
5. Main Function
6. Sub-program Section
1. Document/ Comments Section
• The documentation section is the part of the program where the
programmer gives the details associated with the program.
Example:
/* Comments Section
Structure of C Program
Author: XXXX
Date : 28/07/20
*/
// My first C Program
2. Link Section
• This part of the code is used to declare all the header files that
will be used in the program.
• This leads to the compiler being told to link the header files to
the system libraries.
• Example:
• #include<stdio.h>
• These are also called preprocessor directives.
• Preprocessor Directives are always preceded with ‘#’ sign .
• These are the first statement to be checked by the compiler.
• There are many preprocessor directives but most important is
#include <stdio.h>
2. Link Section (cont…)
#include<stdio.h>
• Tells the compiler to include the file stdio.h during
compilation
• ‘stdio.h’ stands for standard input output header
file and contains function prototype for input
output operations e.g. printf() for output and
scanf() for input, etc.
2. Link Section (cont…)
Other popular header files:
• #define PI 3.14
4. Global Declaration Section
• The global variables that can be used anywhere in
the program are declared in global declaration
section.
Example:
int add(int a, int b)
{
return a+b;
}
Example of a C Program
//File Name: areaofcircle.c
// Author:XXX
// date: 01/10/2020
//description: a program to calculate area of circle
#include<stdio.h>//link section
#define pi 3.14;//definition section
float area(float r);//global declaration
int main()//main function
{
float r;
printf(" Enter the radius:n");
scanf("%f",&r);
printf("the area is: %f",area(r));
return 0;
}
float area(float r)
{
return pi * r * r;//sub program
}
Life Cycle of a C Program
When a variable is declared without short or long keyword, the default is short-
signed int
Difference between signed and unsigned integers
float double
• int emp_num;
• float salary;
• char grade;
• double balance_amount;
• unsigned short int velocity;
Initializing Variables
• int emp_num = 7;
• float salary = 1100.50;
• char grade = ‘A’;
• double balance_amount = 5000;
Constants
• A constant is an entity whose value does not
change during the execution of the program
Constants
• Declaration
• \n , \t , \r , \a etc.
printf()
• printf("This is a message \n");
#include <stdio.h>
int main()
{ int i ;
float x;
scanf(“%d%f”, &i, &x);
return 0;
}
Example
#include <stdio.h>
int main()
{
printf(“ \n Result: %d %c%f”, 12, ‘a’, 2.3);
return 0;
}
Result: 12a2.3
More I/O statements in C
Input: gets()
Output: puts()
main(){
char s[20]; //string is array of char
puts("Enter a string: "); gets(s);
puts("Here is your string"); puts(s);
}
C operators
Output:
|| (Logical OR)
• The || operator is used to determine whether either of the condition is true.
• For example:
• a=50,b=9
• if (a== 10 || b == 9) // either of the condition should be true for printing hello!
printf(“Hello!");
If both of the two conditions are false, then only the printf command is bypassed.
Ex2: if(0||9)
printf(“Correct !”);
else
printf(“Incorrect !”);
Caution: If the first operand of the || operator evaluates to true, the second operand will
not be evaluated. This could be a source of bugs if you are not careful.
• For instance, in the following code fragment:
• if (9 || X++) {
• print(“X=%d\n“,X); } variable X will not be incremented because first condition is evaluates
to true.
|| (Logical OR)
Ex2: if(!(0||9))
printf(“Correct !”);
else
printf(“Incorrect !”);
Bitwise Operators
•In the C programming language, operations can be performed on a bit
level using bitwise operators.
•Following are the bitwise Operators
Bitwise AND
•& operation may be used to determine whether a particular bit is set (1)
or clear (0). For example,
•given a bit pattern 0011 (decimal 3), to determine whether the second bit
is set; we use a bitwise AND with a bit pattern containing 1 only in the
second bit:
• 0011 (decimal 3)
• & 0010 (decimal 2)
• 0010 (decimal 2)
Bitwise OR(|)
Left Shift Operator (<<):The left shift operator will shift the
bits towards left for the given number of times.
If you left shift like 2<<2, then it will give the result as 8.
The right shift operator will shift the bits towards right for the
given number of times
main() {
int a , b,c;
a = 10;
b = (a == 1) ? 20: 30;
printf( "Value of b is %d\n", b );
c = (a == 10) ? 20: 30;
printf( "Value of c is %d\n", c );
}
WAP to check even or odd using
conditional operator
#include<stdio.h>
main() {
int n;
printf("Enter number n");
scanf("%d",&n);
(n%2 == 0) ? printf("%d is even",n): printf("%d is odd",n);
}
Operators available in C can be classified in
following ways:
Example 1:
a==b!=c
(a==b)!=c
Example of Associativity of Operators
Example 2:
4 / 2 / 3 (4 / 2) / 3 2 / 3 = 0
4 / 2 / 3 4 / (2 / 3) 4 / 0 = undefined
What will be the value of i
i=2*3/4+4/4+8-2+5/8
i=6/4+4/4+8-2+5/8 operation: *
i=1+4/4+8-2+5/8 operation: /
i = 1 + 1+ 8 - 2 + 5 / 8 operation: /
i=1+1+8-2+0 operation: /
i=2+8-2+0 operation: +
i = 10 - 2 + 0 operation: +
i=8+0 operation : -
i=8 operation: +
Arithmetic expressions vs C expressions
•Although Arithmetic instructions look simple to use one often
commits mistakes in writing them.
Output:
x = 3.000000
Type Conversion Example
#include <stdio.h>
main()
{ int number = 1;
char character = 'k';
int sum;
sum = number + character;
printf("Value of sum : %d\n", sum );
} OUTPUT
Value of sum :
10 8
Important regarding Type Conversion
• Type conversion is also called as implicit or standard type
conversion.
res = (int) a + b;