doubt in static

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rampraveen
    New Member
    • Apr 2010
    • 37

    #1

    doubt in static

    #include"stdio. h"
    #include"conio. h"
    #include"cubesu bpr.c"
    void show();
    void main()
    {
    clrscr();
    printf("\nnumbe r square cubes");
    printf("\n---- ----- -----");
    for(a=1;a<=10;a ++)
    {
    b=a*a;
    c=a*a*a;
    printf(" \n %d \t%d \t%d ",a ,b,c);
    }
    printf("\n---------------\n");
    printf("\n press any key\n");
    getch();
    show();
    printf("\npress any key");
    getch();
    display();
    printf("\npress any key");
    getch();
    getch();
    }
    void show()
    {
    printf("\n Last value of a-->%d",a);
    printf("\n last value of b-->%d",b);
    printf("\n last value of c-->%d",c);
    }/*completd*/
    *************** *********
    *************** ********
    CUBESUBPR.c
    *************** ************
    static int a,b,c;
    void display()
    {
    printf("\n last value of a-->%d",a);
    printf("\n Last value of b-->%d",b);
    printf("\n last value of c-->%d",c);
    }/*completed*/
    *************** *************** *************** *
    OUTPUT
    *************** *************** ***************
    number square cubes
    ---- ----- -----
    1 1 1
    2 4 8
    3 9 27
    4 16 64
    5 25 125
    6 36 216
    7 49 343
    8 64 512
    9 81 729
    10 100 1000
    ------ ----- -------

    press any key

    Last value of a-->11
    last value of b-->100
    last value of c-->1000
    press any key
    last value of a-->11
    Last value of b-->100
    last value of c-->1000
    *************** *************** ***********
    my questions
    *************** *************** ***********
    in this program we are using static in tat second program.....
    1)i had read tat in one book the author said ,"where the static is used that type of variable can't be used in another program........ ............... ..............
    we are using static in tat second program . But if i execute first program, tat program executes without any error..if i change the static from this program to tat program at that time also it executes without any error...so what is the use of static in c.....this static variable can be shared in both program ..if anybody knows pls tell....
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Please use CODE tags -- they make your code easier to read and provide line numbers that we can refer to.

    "i had read tat in one book the author said ,'where the static is used that type of variable can't be used in another program'"
    Program is the wrong term ... static variables are not shared between translation units (see excerpt from the C99 Standard below).

    Originally posted by C99 Standard
    5.1.1.1 Program structure
    A C program need not all be translated at the same time. The text of the program is kept in units called source files, (or preprocessing files) in this International Standard. A source file together with all the headers and source files included via the preprocessing directive #include is known as a preprocessing translation unit. After preprocessing, a preprocessing translation unit is called a translation unit. Previously translated translation units may be preserved individually or in libraries. The separate translation units of a program communicate by (for example) calls to functions whose identifiers have external linkage, manipulation of objects whose identifiers have external linkage, or manipulation of data files. Translation units may be separately translated and then later linked to produce an executable program.
    You have two source files, but by #including cubesubpr.c into the first source file you end up with only one translation unit.

    The following threads may answer your questions about the static keyword:
    Static variables having block scope
    Global variables and static global variable
    External static

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by donbock
      You have two source files, but by #including cubesubpr.c into the first source file you end up with only one translation unit.
      You do not #include .c files. Every time you do you get a copy and this will cause the link to fail with multiple definitions.

      Instead, you ADD the second .c file to your make (project) and write a .h that can be #included in the other .c file.

      Only declarations can be included never definitions.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Let's be clear, it is not illegal to #include .c files, but it is very ill-advised.

        In 20 years of C programming, I can count on one hand the number of times it has been helpful #include .c files. Typically I'm weaving two separate programs together. For example, a unit-test framework:
        Code:
        test_foo.c:
           <stubs for external variables used by foo.c>
        
           #include  "foo.c"
           #include <stdio.h>
        
           int main(void) {
              <code that tests all features of foo.c>
           }
        
           <stubs for external functions used by foo.c>
        This arrangement gives the test code access to static functions and file-scope static variables inside foo.c. By stubbing out the external functions I have the opportunity to coerce their outputs in ways that force execution through all the nooks and crannies of the foo.c code. Finally, foo.c might be part of an embedded software system -- that doesn't prevent test_foo.c from running on the host computer.

        This is a very specialized exception. A good rule of thumb for a beginning C programmer is to plan on never #including .c files.

        Comment

        Working...