If Statement

Conditionals are logical operations involving comparison of quantities using the given conditional operators:

Condition Operators,

< Smaller than

<= smaller than or equal to

== Equal to

!= not equal to

>= greater than or equal to

> Greater than

Boolean operators -

&& And

|| Or

! Not

Syntax -

if (conditional1)

{

Block of statements executed if conditional 1 is true

}

else if (conditional2)

{

Block of statements executed if conditional 2 is true

}

else

{

Block of statements executed otherwise

}

Example –

Do While Loop

do while loop -

In most computer programming languages, a do while loop, sometimes just called a do loop, is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition.

Syntax –

do {

statements;

} while (condition);

Example -

#include

int main(void)
{

int value, r_digit;

printf(“Enter a number to be reversed.\n”);
scanf(“%d”, &value);

do
{
r_digit = value % 10;
printf(“%d”, r_digit);
value = value / 10;
} while (value != 0);

printf(“\n”);

return 0;


}

Out Put –

Enter the number to be reversed.
123456 -
654321

While Loop

The while statement

The while provides a mechanism for repeating C statements wiles a condition is true. In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition.


Syntax -


while (condition){
program statement;

}

Example -

/* Sample program including the while loop */

#include

int main(void)
{

int loop = 0;

while (loop <=10)
{
printf(“%d”, loop);
++loop
}

return 0;

}

Output

0
1

10

C Programing Loops (for loop)

C uses two types of loops they are for and while. The for-loop has four distinct sections

Initialization

  • Condition,
  • Iteration
  • Body

The first three sections follow the for keyword and are enclosed in parentheses. The sections must be divided by semi-colons, even when one or more of the sections are empty. The body of the loop follows and is enclosed in braces - except when there is only one statement in the body, when the braces are optional.

FOR loop:

is executed repeatedly UNTIL the value of is FALSE.

* Before the first iteration of the loop, initializes variables for the loop.

* After each iteration of the loop, increments a loop counter. (Consequently, j++ is functionally the same as ++j.)

Syntax -

for ( [initialization] ; [condition] ; [increment] )

{

Statement;

}

Example -

#include

int main()

{

int count;

printf("nYou have until the count of ten...");

for(count=0; count <10;>

printf("%dn",count);

printf("Time is up!");

return(0);

}

C Data Type

For each variable consist of attach some data type.

The data type defines:

1. the amount of storage allocated to variables.

2. the values that they can accept.

3. the operations that can be performed on variables.


Data types and Related value Range -

Keyword Variable Type Range

char Character (or string) -128 to 127

int Integer -32,768 to 32,767

short Short integer -32,768 to 32,767

short int Short integer -32,768 to 32,767

long Long integer -2,147,483,648 to 2,147,483,647

unsigned char Unsigned character 0 to 255

unsigned int Unsigned integer 0 to 65,535

unsigned short Unsigned short integer 0 to 65,535

unsigned long Unsigned long integer 0 to 4,294,967,295

float Single-precision +/-3.4E10^38 to +/-3.4E10^38

floating-point
(accurate to 7 digits)
double Double-precision +/-1.7E10^308 to +/-1.7E10^308
floating-point
(accurate to 15 digits)


Signed Range Unsigned Range

char -128 to 127 unsigned char 0 to 255

int -32768 to 32,767 unsigned int 0 to 65,535

long -2,147,483,648 unsigned long 0 to 4,294,967,295 to 2,147,483,647

Get Maximum and Min value of Integer data type

Example 1.

#include
main(){
int i,j ;
i = 1;

while (i > 0) {
j = i;
i++;
}
printf ("Maximum value of integer is %d\n",j);
printf ("Min Value of integer is %d\n",i);
}

OUT PUT -

Maximum value of integer is - 2147483647
Min Value of integer is - 2147483648

Comments in a C program

Everything between those two points is ignored by the compiler.

Single line Comment

// This is how a single line comment looks in the C language

#include

main(){

int i,j,k;

i = 6;

j = 8;

k = i + j;

printf("sum of two numbers is %d \n",k);

// This is single line comment

}

Multiline Comments.

/* This is how a Multi line comment looks in the C language */

The beginning of the comment is marked by the slash and the asterisk: /*.

The end of the comment is marked by the asterisk and the slash: */.

Example 1.

#include

int main(){

/* This is the

comment.*/

printf("%15s","right\n");

printf("%-15s","left\n");

return(0);

}

Let's Start C "Hello World"

Let's start C Programing by saluting the world! Type the following program into your favorite editor:

#include <>

void main()

{

printf("\nHello World\n");

}

We are using the free Borland C compiler because it is powerful, flexible, and allows us to create software for both command line and GUI (Windows) applications.

Of course, the first thing that you will need to do is download the Borland C compiler (explained in the Free Programming Tools article, and install it following the instructions received during the download process.

Before we continue, we should point out that only the C language tutorial part of this free C tutorial is valid for MacOS users, since it is targeted towards the free Borland C compiler available for Windows and Linux.

Download Compiler Here

Compile your First Program

Save your program in a suitable text file (HelloWorld.c - note the .c extension), and turn it into a command line application by:

  1. Open a Command Prompt
  2. Navigate to the place you saved your HelloWorld.c file
  3. Type 'bcc32 HelloWorld.c'


Every C program contains a function called main. This is the start point of the program.

#include

Allows the program to interact with the screen, keyboard and filesystem of your computer. You will find it at the beginning of almost every C program.

main()

Declares the start of the function, while the two curly brackets show the start and finish of the function. Curly brackets in C are used to group statements together as in a function, or in the body of a loop. Such a grouping is known as a compound statement or a block.

printf("This is a C program\n");

Printf () function is use to prints the words on the screen. The text to be printed is enclosed in double quotes. The “ \n “ at the end of the text tells the program to print a newline as part of the output.

Try this out –

Example 1.

#include <>

void main()

{

printf("\n");

printf("Hello World");

printf("\n");

}


Example 2.

#include <>

void main()

{

printf("\nHello World\n");

}

C Language Introduction

C Language is a general-purpose, cross-platform, block structured, procedural, imperative computer programming language. It was developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system.

C is rather like Pascal or Fortran. Values are stored in variables. Programs are structured by defining and calling functions. Program flow is controlled using loops, if statements and function calls. Input and output can be directed to the terminal or to files. Related data can be stored together in arrays or structures.

C Programing primary use is for "system programming"

  • · Implementing operating systems and embedded system applications,
  • · Due to a combination of desirable characteristics such as code portability and efficiency
  • · Ability to access specific hardware addresses
  • · Ability to "pun" types to match externally imposed data access requirements
  • · Low runtime demand on system resources

C has also been widely used to implement end-user applications, although as applications became larger much of that development shifted to other, higher-level languages.

You can program anything in C, and the statement is well supported by my own experience with the language. Along with the resulting freedom however, you take on a great deal of responsibility because it is very easy to write a program that destroys itself due to the silly little errors that a good Pascal compiler will flag and call a fatal error. In C, you are very much on your own as you will soon find.