C Language Word
C Language Word
/* start here */
libraries.
Almost all c programs use the “stdio” or standard input/output library. Many also
To use a library, include the header file (I.e., “stdio.h”) at the top of the file.
For most special purpose libraries (I.e., math) you need to include the library on
C VARIABLE TYPES
The most common types are: char, int, float, and double.
myVal = 2; /* This would be an error, because myVal was not yet declared */
LOGICAL OPERATORS
C defines these logical operators: <, >, <=, >= and == (the equivalence operator)
You can compare any variable. Characters are compared based on their ASCII
values.
You can extend the logic with && (and), ~ (not) and || (or).
THE IF STATEMENT
If the expression is true (not zero), the statement is executed. If the expression is
if (expression) {
statement 1;
statement 2;
statement 3;
will be.
2
THE FOR LOOP
The for loop will first perform the initialization. Then, as long is test is TRUE, it
Counter = 0;
Counter = 1;
Counter = 2;
3
C is a procedural programming language.Computer program are set of instructions that
tells the computer how to carry out some computations and perform some other
functions. The instructions that constitute a program have to be expressed in one of the
language is one of the widely use programming languages at the present time.
1. Header Files Inclusion: The first and foremost component is the inclusion of the
#include <(header_file_name).h>
4
2. Main Method Declaration: The next part of a C program is to declare the main ()
int main()
{}
3. Variable Declaration: The next part of any C program is the variable declaration. It
etc.
5. Return Statement: The last part in any C program is the return statement. The
Functions
- The main function indicates to the system the point to start program execution at run
time
○ COMMENT
- Comments are inserted in a C- program for users and not for the underlying machine
5
- Format is:
/* comment */
● ADVANTANGES OF C
- It offers the flexibility of a high level language i.e. simpler for human operator to
programming
○ JUSTIFICATIONS
● CHARACTERISTICS OF C – LANGUAGE
6
- It is strong Typing
- It is case sensitive
o SYMBOLS IN C
- Just like natural language have symbols used for forming words and sentences, C
ALPHABETS:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
NUMERAL: 0 1 2 3 4 5 6 7 8 9
SPECIAL CHARACTERS :
SAMPLE C PROGRAM I
#include <stdio.h>
int main()
7
Printf("Goodbye, World!");
return 0;
Data types
C has several types of variables, but there are a few basic types:
Defined using char, int, short, long or long long.
using unsigned char, unsigned int, unsigned short, unsigned long or unsigned lo
ng long.
using float and double.
SAMPLE C PROGRAM
#include <stdio.h>
int main() {
int a = 3;
float b = 4.5;
double c = 5.25;
float sum;
sum = a + b + c;
8
printf("The sum of a, b, and c is %f.", sum);
return 0;
SAMPLE C PROGRAM
void main()
scanf(“%f”,&base); //the value will be stored in base. & indicates d address of the data item
scanf(“%f”,&height);
printf(“Area=%f\n”,area);
computer to execute the first statement and then moves to the next statement until
9
- A set of statements for effecting different sequencing than the normal is called
If Sytax
if (condition)
statement;
int foo =1;
int bar =2;
IF ELSE Syntax
IF else statement are use for statement that contains one condition and two
possible action.condition may be true or false
IF(Condition)
{
Statament1;
Statement2;
}
Else
{
Statement1;
10
Statement2;
}
We can use the else keyword to execute code when our expression evaluates to false.
int foo = 1;
int bar = 2;
Syntax
If (condition)
if(condition);
Else
Statement2;
Statement3;
if(peanuts_in_jar>80){
if(peanuts_eaten<max_peanut_limit){
printf("Take as many peanuts as you want!\n");
}
11
}else{
if(peanuts_eaten>peanuts_in_jar){
printf("You can't have anymore peanuts!\n");
}
else{
printf("Alright, just one more peanut.\n");
}
}
FOR LOOPS
For loops in C are straightforward. They supply the ability to create a loop - a
code block that runs multiple times. For loops require an iterator variable, usually notated
as i.
FORSyntax
For(exp1;exp2;exp3)
Statement;
inti;
for(i=0;i<10;i++){
printf("%d\n",i);
}
This block will print the numbers 0 through 9 (10 numbers in total).
12
For loops can iterate on array values. For example, if we would want to sum all the
intarray[10]={1,2,3,4,5,6,7,8,9,10};
int sum =0;
inti;
for(i=0;i<10;i++){
sum += array[i];
}
NESTING OF LOOP
When a loop written inside the body of another loop then it is know as nesting
of loopp.Any type of loop can be nested in any type such as while’do while,for
Void main()
{
Inti,j;
For(i=0;i<2;i++)
For(j=0;j<5;i++)
Printf(“%d%d”,i,j);
}
Output;
J=01234
i=1
j=01234
Break statement is used inside loop and switch statement. It causes immediate exit
from that loop in which it appears .and it is written with condition. It is written with the
keyword break
Example:
13
Voic main()
Int j=0;
For(j=0;j<6;j++)
If(j==4)
Break;
Output
0123
CONTINUE STATEMENT
Continue statement is used for continuing next iteration of loop after skipping
some statement of loop. When it encountered control automatically passes through the
beginning of the loop. It is usually associated with the if statement. It is useful when we
want to continue the program without executing any part of the program.
The difference between break and continue is, when the break encountered loop is
terminated and it transfer to the next statement and when continue is encounter control
Example:-
void main()
int n;
if(n==4)
14
continue;
printf(“%d”, n);
Printf(“out of loop”);
WHILE LOOPS
While loops are similar to for loops, but have less functionality. A while loop
continues executing the while block as long as the condition in the while remains true.
For example, the following code will execute exactly ten times:
int n =0;
while(n <10){
n++;
}
While loops can also execute infinitely if a condition is given which always evaluates as
true (non-zero):
while(1){
/* do something */
}
Program to display number in ascending order using while loop
#include <stdio.h>
int main() {
while (number<=100){
15
printf(“The number is %d\n”,number);
return 0;
Multiplication table of 12
#include <stdio.h>
int main() {
int n,i=1
scanf(“%d’’,&n);
while(i<=10)
Printf(“%d*%d=%d\n”,n,I,n*i)
i=i+1;
ARRAYS
Arrays are special variables which can hold more than one value under the same
variable name, organized with an index. Arrays are defined using a very straightforward
syntax:
Accessing a number from the array is done using the same syntax. Notice that arrays in C
are zero-based, which means that if we defined an array of size 10, then the array cells 0
Example 1
intnumbers[10];
16
numbers[0]=10;
numbers[1]=20;
numbers[2]=30;
numbers[3]=40;
numbers[4]=50;
numbers[5]=60;
numbers[6]=70;
/* print the 7th number from the array, which has an index of 6 */
printf("The 7th number in the array is %d", numbers[6]);
Example 2
# include <stdio.h>
int main(){
int array[]={1,2,3,4,5,6,7,8,9,10};
int factorial=1;
int i=0;
for(i=0;i<10;i++){
factorial*= array[i];
# include <stdio.h>
int main() {
Int array[]={1,2,3,4,5,6,7,8,9,10};
17
Int Sum=0;
Int i;
Sum+=array[i];
return 0;
# include<stdio.h>
Int main() {
Int i=0;
Int array[]={1,7,4,5,9,3,5,11,6,3,4};
While(i<10){
Printf(“%d\n,array[i]);
18