Thread: what is the output

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    33

    what is the output

    Code:
    a[]={0,1,2,3,4};
    a[i]=i;
    but i is not declared so what it return why?


    thank you..!!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If i is not declared the program won't compile so it won't return anything. If i is declared but not initialized then it is undefined what value i will contain.

    Jim

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    ideone says
    cc1: warnings being treated as errors
    prog.c:1: error: data definition has no type or storage class
    prog.c:1: error: type defaults to ‘int’ in declaration of ‘a’
    prog.c:2: error: ‘i’ undeclared here (not in a function)
    prog.c:2: error: data definition has no type or storage class
    prog.c:2: error: type defaults to ‘int’ in declaration of ‘a’
    You can see the output for yourself

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by jimblumberg View Post
    If i is declared but not initialized then it is undefined what value i will contain.

    Jim
    In other words What is the default value of integer in c

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by qny View Post
    ideone says
    You can see the output for yourself
    Well that's ok,because then the compiler says what's wrong...but if the code looks like this
    Code:
    int main(void)
    {
       int i;
       int a[]={0,1,2,3,4};
       a[i]=i;
       return 0;
    }
    then the compiler will tell nothing,but i is uninitialized so actually we have a logic error here,which of course is more difficult to detect(in large programs) than the syntactic ones

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    then the compiler will tell nothing,but i is uninitialized
    Actually my compiler tells me about using uninitialized variables.

    main.c||In function ‘main’:|
    main.c|4|warning: variable ‘a’ set but not used [-Wunused-but-set-variable]|
    main.c|5|warning: ‘i’ is used uninitialized in this function [-Wuninitialized]|
    ||=== Build finished: 0 errors, 2 warnings ===|
    Jim

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by jimblumberg View Post
    Actually my compiler tells me about using uninitialized variables.
    Jim
    I suppose then it is up to the compilers and the versions of it.I compile it in a Linux with an old i would say compiler

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If you are using Linux you are probably using gcc which is what I used. What compiler switches are you using? Are you at least using -Wall -Wextra?

    Jim

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by jimblumberg View Post
    If you are using Linux you are probably using gcc which is what I used. What compiler switches are you using? Are you at least using -Wall -Wextra?
    Jim
    No flags at all. gcc -o programName fileName.c .
    I used what you did (h.c was the file i created and compiler said nothing again.) I am via putty(is this relevant ? )
    Code:
    linux11:/home/users/std10093>pico h.c
    linux11:/home/users/std10093>gcc -o  -Wall -Wextra h h.c
    gcc: h: No such file or directory
    linux11:/home/users/std10093>gcc -Wall -Wextra -o h h.c
    linux11:/home/users/std10093>h
    Segmentation fault
    linux11:/home/users/std10093>gcc -o h h.c
    linux11:/home/users/std10093>h
    Segmentation fault
    linux11:/home/users/std10093>

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Here is a complete list of the warning options I use:
    -Wshadow"
    -Winit-self"
    -Wredundant-decls"
    -Wcast-align"
    -Wundef"
    -Wfloat-equal"
    -Winline"
    -Wunreachable-code"
    -Wmissing-declarations"
    -Wmissing-include-dirs"
    -Wswitch-enum"
    -Wswitch-default"
    -Wmain"
    -pedantic-errors"
    -pedantic"
    -Wextra"
    -Wall"
    -g"
    -std=c11"
    You cant try add them one at a time, I recommend using at least the -Wall -Wextra -pedantic every time you compile.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ overlapping output and adding extensions to output files
    By lordmorgul in forum Linux Programming
    Replies: 9
    Last Post: 05-11-2010, 08:26 AM
  2. How to edit output in struct and call for the output
    By andrewkho in forum C Programming
    Replies: 4
    Last Post: 03-16-2010, 10:28 PM
  3. terminal output not showing output properly
    By stanlvw in forum C Programming
    Replies: 13
    Last Post: 11-19-2007, 10:46 PM
  4. output a string to a standard output
    By sh4k3 in forum C Programming
    Replies: 3
    Last Post: 06-15-2007, 05:59 AM
  5. Replies: 3
    Last Post: 02-19-2003, 08:34 PM

Tags for this Thread