Introduction to C-Language Programming
Structured programming
The programming that follows a top down approach, on which developers separate the overall program structure into different sub section, is called structured programming.
Advantages of structured programming
· Reduces complexity and easy to code.
· It takes less time to program and easy to debug.
· Reuse of modules and flow of control is clear.
C language
C Language is a high level structured programming language which is used to develop system software. Dennis Ritchie at Bell telephone laboratory developed C-language in early 1970s. C is called middle level language because it combines elements of high level language with some features of assembler.
Data types used in C
Data type are means to identify the type data and associated operation of handling it.
Data types used in C language
a) char b) int c) float d) double e) void
Features of C language
Limitations of C language
· There is no runtime checking. It has poor errors detection system.
· It does not support object oriented programming.
· C language has only 32 Keywords
· There is no strict type checking int data type to float variables.
Application of C Language
a) Operating Systemb) Language Compilers/Interface
c) Assemblers
d) Text Editors
e) DBMS
f) Utilities etc.
C Token
C tokens are the basic buildings blocks in C language which are constructed together to write a C program. Each and every smallest individual unit in a C program are known as C tokens
C tokens are of six types.
a) Keywords (eg: int, while),
b) Identifiers (eg: main, total),
c) Constants (eg: 10, 20),
d) Strings (eg: “total”, “hello”),
e) Special symbols (eg: (), {}),
f) Operators (eg: +, /,-,*)
C keywords (reserved word)
Keyword is a set of special words which are already defined for some tasks.
C has only a set of 32 keywords, which have their predefined meaning and cannot be used as a variable name.
E.g. 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 Character set
Character Set is a group of valid characters and symbols supported by a programming language. Alphabets, Digits and Special characters.
Identifiers
Identifiers are the names given to the entities such as variables, functions, arrays structures and unions.
E.g. int price; float total; Here, price and total are called identifiers.
Rules for naming Identifiers:
i) The Identifier must start with an alphabet or an under score (_).
ii) Identifier can be made from the combination of alphabets, digits and under score.
iii) No any C keyword can be used as an Identifier.
iv) Identifier must be unique and can be used for a single purpose only.
Format Specifier
The format specifier is used during input and output operation. It tells the compiler what type of data is stored in a variable during the input and output operation such as taking data from keyboard and display data on the screen. Some examples are %c, %d, %f, etc.
|
Data Type |
Format Specifier |
|
short int |
%hd |
|
unsigned short int |
%hu |
|
unsigned int |
%u |
|
int |
%d |
|
long int |
%ld |
|
unsigned long int |
%lu |
|
char |
%c |
|
float |
%f |
|
double |
%lf |
C Header Files
stdio.h - Used for standard input and output (I/O) operations. [printf( ), scanf( ), getchar( )] conio.h - Contains declaration for console I/O functions. [clrscr( ), exit( )]math.h - D e c l a r e s m a t h e m a t i c a l functions and macros. [pow(), squr(), cos(), tan(), sin(), log() ] string.h - Used for manipulating strings. [strlen(), strcpy(), strcmp(), strcat(), strlwr(), strupr(), strrev() ]
Escape Sequence
Escape Sequence is a pair of character that is used with printf() function to display non-printing character or a special
character on the screen.
Some Examples of Escape Sequence:
\n - new line
\t - tab
\b - backspace
\o - null character
\? - question mark
\\ - slash
\' - single quote
\” - double quote
Format Identifier
We need to include format identifier to tell the data type in the area of format string of printf() function.
|
Variable Type |
Format Identifier |
|
char |
%c |
|
int |
%d |
|
long int |
%ld |
|
float |
%f |
Variable
A variable is a location in the memory of a computer which holds data in a program and its value may change during the execution of program.
Constant
Constant are the values that do not change during the execution of a program.
String Constant
String constant is a message to be displayed along with the other values stored in variables. It is enclosed within double quotation (" ").
Output Function in C
Output function is used to show the calculated result or output on the screen. In C language, printf() is
one of the output function defined in header file.
printf() function
In C Language, printf() function is used to print the valued on the screen.
It is defined in header file. So, the header file must be added to use this function.
Input Function in C
Input function is used to ask data for processing.
In C language, scanf() is one of the input function defined in header file.
scanf() Function
scanf() is one of the most important functions of C Program.
This function is also defined in the header file and used to ask value from keyboard.
getch() function
getch() function is another input function of C language.
This function is defined in the header file .
This function is used to ask any one character from keyboard.
Arithmetic Calculations in C Program
There are basically four types of arithmetic operations:
i) Addition
ii) Subtraction
iii) Multiply
iv) Division
List of Arithmetic Operators in C
+ (Plus) - Addition
- (Minus) – Subtraction
* (Asterisk) – Multiplication
/ (Slash) – Division
% (Percentage Symbol) – Modulus Division
++ (Plus Plus) – Increment Operator
- - ( M i n u s
M i n u s ) – Decrement Operator
Operator
Operators are special symbols that are meant for specific tasks or operators.
The different types of C operators are:- Arithmetic operators, Assignment operators, Unary operators, Relational operators, Logical operators and Conditional
operators.
Arithmetic operators are the operators that are used for manipulating arithmetic data and performs arithmetic operation. The arithmetic
operators are:- +, -
, * , / and %
The operator that operate on one operand variable or constant is called urinary operator. E.g. ++, - -
C Expression
An expression consists of at least one operand with one or more operators. It is a legal combination of symbols that represents a value.
Logical Calculation in C
The calculation that is done based on one or more conditions is called logical calculations.
Several relational or comparison operators are used to check the condition which gives True or False as a calculated result.
Relational Operators in C
Relational Operator checks the relationship between two operands and returns either 1 (True) or 0 (False).
In C programming, relational operators can be used to take decisions and provide condition in looping statements.
List of Relational Operators in C
= = Equal to
> Greater than
< Less than
!= Not equal to
>= Greater than or equal
<= Less than or equal to
#include <stdio.h>
#include <conio.h>
int main()
{
int a, b, c;
float d;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
printf("Enter third number: ");
scanf("%d", &c);
d = (a + b + c) / 3.0;
printf("Average of three numbers = %.2f", d);
return 0;
#include <stdio.h>
#include <conio.h>
int main()
{
float r, h, v;
printf("Enter radius and height: ");
scanf("%f %f", &r, &h);
v = (22.0 / 7) * r * r * h;
printf("Volume of cylinder = %.2f", v);
return 0;
#include <stdio.h>
#include <conio.h>
int main()
{
float u, a, s;
int t;
printf("Enter initial velocity: ");
scanf("%f", &u);
printf("Enter acceleration: ");
scanf("%f", &a);
printf("Enter time: ");
scanf("%d", &t);
s = (u * t) + (a * t * t) / 2;
printf("Distance travelled by body = %.2f", s);
5. Write a C program to get radius
of circle and then print its area.
#include <stdio.h>
#include <conio.h>
int main()
{
int l, b, h, v;
printf("Enter length: ");
scanf("%d", &l);
printf("Enter breadth: ");
scanf("%d", &b);
printf("Enter height: ");
scanf("%d", &h);
v = l * b * h;
printf("Volume of box = %d", v);
return 0;
}
#include <stdio.h>
#include <conio.h>
int main()
float r, c;
printf("Enter radius: ");
scanf("%f", &r);
c = 2 * (22.0 / 7) * r;
printf("Circumference of circle = %.2f", c);
return 0;
}
#include <stdio.h>
#include <conio.h>
int main()
int l, b, h, a;
printf("Enter length: ");
scanf("%d", &l);
printf("Enter breadth: ");
scanf("%d", &b);
printf("Enter height: ");
scanf("%d", &h);
a = 2 * h * (l + b);
printf("Area of four walls = %d", a);
return 0;
}
#include <stdio.h>
#include <conio.h>
int main() {
int l, b, h, a;
printf("Enter length: ");
scanf("%d", &l);
printf("Enter breadth: ");
scanf("%d", &b);
printf("Enter height: ");
scanf("%d", &h);
a = 2 * ((l * b) + (b * h) + (h * l));
printf("Total surface area of box = %d", a);
return 0;
}
#include <stdio.h>
#include <conio.h>
int main()
{
float p, t, r, i;
printf("Enter principal: ");
scanf("%f", &p);
printf("Enter time: ");
scanf("%f", &t);
printf("Enter rate: ");
scanf("%f", &r);
i = (p * t * r) / 100;
printf("Simple Interest = %.2f", i);
#include <stdio.h>
#include <conio.h>
int main() {
float c, f;
printf("Enter temperature in Celsius: ");
scanf("%f", &c);
f = ((c * 9) / 5) + 32;
printf("%.2f Celsius = %.2f Fahrenheit", c, f);
}
#include <stdio.h>
#include <conio.h>
int main() {
int b, h;
float a;
printf("Enter base: ");
scanf("%d", &b);
printf("Enter height: ");
scanf("%d", &h);
a = (b * h) / 2.0;
printf("Area of triangle = %.2f", a);
return 0;
}
#include <stdio.h>
#include <conio.h>
int main()
{
float c, f;
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &f);
c = (f - 32) * 5 / 9;
printf("%.2f Fahrenheit = %.2f Celsius", f, c);
return 0;
}
Control Statement in C
Control statement defines the flow of control within the program.
Below are the statements:
- Simple if statement: It evaluates the
content only if expression is true.
Syntax
if (Condition) Statement; Or, if(Condition) { Statement; ....... }Example:
1. WAP to enter the marks of English and
print “Pass” if it is 40 or more.
# include<stdio.h>
int main ( )
{
float e;
printf
(“Enter English Marks \n”);
scanf (“%f”, &e);
if
(e >= 40)
{
Printf (“Pass”);
}
return 0;
}
2.
if-else
statement: It verifies the given condition and executes only one out of two
blocks of statements based on the condition result.
Syntax
if(Condition) Statement; else Statement; Or, if(Condition) { True block of statement; } else { False block of statement; } Example:
1. WAP to enter
the marks of English and print “Pass” if it is 40 or more, otherwise “Fail”.
int main ( )
{
float e;
printf (“Enter English Marks \n”);
scanf (“%f”, &e);
if (e >= 40)
{
printf (“Pass”);
}
else
{
printf (“Pass”);
}
return 0;
}
if-else-if statement: It is also known as if-else-if ladder. This statement is used when there are more than two possible actions based on different conditions.
Syntax
if(Condition1) { Statement1; } else if(Condition2) { Statement2; } ......... ......... else if(Condition N) { Statement N; } else { Default_Statement; } Example 1: WAP to
enter a number and print “Positive” if it is greater than 0, print “Negative”
if it is less than 0, otherwise print “Zero”.
# include<stdio.h>
int main ( )
{
int n;
printf("Enter number");
scanf("%d",&n);
if (n > 0 )
{
printf (“Positive”);
}
else if (n < 0 )
{
printf (“Negative”);
}
else
{
printf (“Zero”);
}
return 0;
}
- Switch
statement: It is a multi-way decision maker that tests the value of an
expression against a list of integers or character constants. When a match
is found, the statements associated with that constant are executed.
Syntax
switch(expression) { case constant1: statement1; break; case constant2: statement2; break; case constant3: statement3; break; Default: statement; }
#include
<stdio.h>
int
main()
{
int day;
printf("Enter a number (1-7) for a day
of the week: ");
scanf("%d", &day);
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Invalid input! Please
enter a number between 1 and 7.\n");
}
return 0;
}
#include<stdio.h>
int main()
{
int i,s;
for (i=1;i<=10;i++)
{
s=5*i;
printf("%d\n",s);
}
return 0;
}
#include<stdio.h>
int main()
{
int i,a=0,b=1,c;
printf(“%d%d””,a,b);
for (i=1;i<=10;i++)
{
c=a+b;
printf(“%d”,c);
a=b;
b=c;
}
return 0;
}
#include<stdio.h>
int main()
{
int i,a=1,g=1;
for (i=1;i<=10;i++)
{
printf(“%d”,a);
a=a+g;
g=g+1;
}
return 0;
}
#include<stdio.h>
int main()
{
int i,a=1,g=1;
for (i=1;i<=10;i++)
{
printf(“%d”,a);
a=a+g;
g=g+2;
}
return 0;
}
e) WAP to generate 3 12 27 48 ................10th term
#include<stdio.h>
int main()
{
int a,i;
for (i=1;i<=10;i++)
{
a=3*I*I;
printf(“%d”,a);
}
ooh
ردحذفIt's very helpful for USA thank you sir ☺️
ردحذفUs*
حذفإرسال تعليق