<div style='background-color: none transparent;'></div>

Pages

Home » , » Cexercises! read as; SexErsises! Samba! Pit senyor!

Cexercises! read as; SexErsises! Samba! Pit senyor!

lab exercise

1 Getting Started & Data Types Before we can explore the C programming language you need a lesson in how to use the tool itself.
// list the include file
#include <direct.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <dos.h>
void main()
            {
            printf("Hello World\n\r");
            getch();
            }
We will start with the above programme (http://www.dictlabexercises101.c) which will print a message onto your screen, and wait for you to press any key on the keyboard.
Use the menu bar command ‘file’ to open and save files. The screen is a full screen editor. Use the ‘compile’ command to compile the programme, and the ‘run’ command to run it.
Note also the Help facility – which is incredibly useful as it describes all the major C operations and provides example code that can be cut & pasted into your own programmes.
Once you have understood how to use these basic tools, move on to using C variables.
There are a number of C variable types. char int and long are signed 8-bit, 16-bit and 32-bit integers. Unsigned varieties are also available and are declared as unsigned char, unsigned int and unsigned long.
Try the following programme http://www.dictlabexercises101.c, which also introduces the concept of a formatted print.
void main()
            {
            unsigned char a; // create a signed char variable
            unsigned int b; // create a signed int variable
            unsigned long c ; // create a signed long variable
            printf("My Test Programme\n\r");
            a = 0 ;
            while (!kbhit()) // stay in this loop until a key is pressed
                        {
                        printf("%d ",a); // print variable a as a decimal number followed by a space, %d means decimal
                        a++; // increment variable a delay(100); // wait 100ms
                        }
            getch();
            }
When you run this programme you will see the current value of the variable a displayed on the screen. It will have a numeric range from 0 to 255. When it reaches 255 it will go back to zero – WHY?

Now repeat this for the unsigned integer. http://www.dictlabexercises101.c
void main()
            {
            unsigned char a; // create a signed char variable
            unsigned int b; // create a signed int variable
            unsigned long c ; // create a signed long variable
            b= 0 ;
            while (!kbhit()) // stay in this loop until a key is pressed
                        {
                        printf("%d ",b); // print variable b as a decimal number followed by a space, %d means decimal
                        b += 256; // add 256 to variable b
                        delay(100); // wait 100ms
                        }
            getch();
            }
Note that the variable wraps around when it is greater than 65535.
Finally repeat this for the unsigned long variant.  http://www.dictlabexercises101.c

void main()
            {
            unsigned char a; // create a signed char variable
            unsigned int b; // create a signed int variable
            unsigned long c ; // create a signed long variable
            c= 0 ;
            while (!kbhit()) // stay in this loop until a key is pressed
                        {
                        printf("%lu ",c); // print variable c as a decimal number followed by a space, %lu means long unsigned decimal
                                c += 8192; // add 8192 to variable 2
                               delay(20); // wait 20ms
                                }
            getch();
               }
Now repeat all these experiments using signed variables and note the differences.
Now we introduce the floating point types, these are float and double. void main()
            {
            float e ; // declare a floating point variable
            while (!kbhit())
                        {
                        printf("%f ",u);
                        u += 1000.05 ; // add 1000.05 to e
                        delay(50); // wait 50ms
                        }
            getch();
            }
This programme adds 1000.05 to e each cycle, but note that it very quickly loses resolution at the lower end. This is because a floating-point mantissa only has 24 bits, and it simply cannot cope with numbers that have a wide dynamic range. To prove that it can cope with 0.5 change the above programme to add only 0.5 to variable e.
Now replace the float declaration with a double declaration.
http://www.dictlabexercises101.c
void main()
            {
            double e ; // declare a double precision floating-point variable
            while (!kbhit())
                        {
                        printf("%lf ",u);
                        u += 1000.05 ; // add 1000.05 to e
                        delay(50); // wait 50ms
                        }
            getch();
            }
Using double precision improves resolution, but is slower and requires more memory.
What happens if we try to mix up our data types? If you try to force an int variable into a char variable it will simply be truncated to 8-bits. You should not mix up data types, and some compilers will warn you if you try to do it.
Try the following
http://www.dictlabexercises101.c
void main()
            {
            unsigned char a ;
            unsigned int b ;
            b = 1025 ; // load 1025 into the unsigned integer b
            a = b ; // put in into a
            printf("%d ",a); // print it out
            getch(); // wait for a key stroke
            }

 TRY THIS >>

Simple Program that display!!

#include<stdio.h>
#include<conio.h>               // you can eliminate this!
main()                                 // even you use void main() or int main(). the program will still run
{
   printf("This sentence will display when you run it to turbo C");    //ahmmmmm....
getch();
}

Simple Program with Next Line or \n Sample 1!!

#include<stdio.h>
#include<conio.h>               // you can eliminate this!
main()                                 // even you use void main() or int main(). the program will still run
{
   printf("1st Sentences\n");    //observe of the position \n
   printf("2nd Sentences\n"); 
   printf("\n")
   printf("3rd Sentences"); 
   printf("\n4th Sentences"); 
getch();
}

Simple Program with \t or Tab Space!!

#include<stdio.h>
#include<conio.h>               // you can eliminate this!
main()                                 // even you use void main() or int main(). the program will still run
{
   printf("\t1 tab Space\t\t2 tab space \t\t\t3tab space");    //observe of the position and numbers of \t
   
getch();
}


Simple Program with a gotoxy!!



#include<stdio.h>
#include<conio.h>               // declaration for gotoxy
main()                                 // even you use void main() or int main(). the program will still run
{
   gotoxy (34,1);                // gotoxy(x,y)
   printf("NO TAB SPACE BUT ITs look like it has.");   //observe of the position of this sentence w.out a \t
   gotoxy(34,10);
   printf("NO NEXT LINE BUT ITs look like it has.");  //observe of the position of this sentence w.out a \n
getch();
}
Share this article :

No comments :

Post a Comment

 
Copyright © 2011. datasavvy . All Rights Reserved
Company Info | Contact Us | Privacy policy | Term of use | Widget | Advertise with Us | Site map
Template Modify by Creating Website . Inpire by Darkmatter Rockettheme Proudly powered by Blogger