Like Tree5Likes
  • 1 Post By std10093
  • 1 Post By anduril462
  • 1 Post By anduril462
  • 1 Post By std10093
  • 1 Post By Matticus

Program come out with out proper execution

This is a discussion on Program come out with out proper execution within the C Programming forums, part of the General Programming Boards category; Hello everyone the following code doesn't execute properly ( After the second entry the program comes out)!! Code: #include<stdio.h> void ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    Pune, Maharashtra, India
    Posts
    33

    Program come out with out proper execution

    Hello everyone the following code doesn't execute properly
    ( After the second entry the program comes out)!!
    Code:
    #include<stdio.h>
    void linkfloat();
    int main(){
    	struct book {
    					char name;
    					float price;
    					int page;
    	};
    	struct book b[3];
    	int i;
    	for(i=0;i<3;i++){
    		printf("\nEnter name ,price and pages: ");
    		scanf("%c%f%d",&b[i].name,&b[i].price,&b[i].page);//seems some thing is wrong here
    	}
    	printf("\n");
    	for(i=0;i<3;i++){
    		
    		printf("%c%f%d\n",b[i].name,b[i].price,b[i].page);
    	}
    	
    	
    return 0;
    }
    void linkfloat(){
    					float a=0,*b;
    					b=&a;
    					a=*b;
    }
    Last edited by mohsen; 07-11-2013 at 02:23 PM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Is the problem relevant to the reading of a char with scanf? Try it out.
    mohsen likes this.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    1. Fix your indentation and put some spaces around your operators and between function parameters. If your code is hard to read, it's easy to make a mistake, and hard to find or fix it.

    2. Compile with warnings turned all the way up:
    Code:
    $ make foo
    gcc -Wall -ggdb3 -std=c99 -o foo foo.c -lm -lpthread
    foo.c: In function ‘main’:
    foo.c:18:5: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat]
    foo.c:18:5: warning: format ‘%f’ expects argument of type ‘double’, but argument 3 has type ‘float *’ [-Wformat]
    foo.c:18:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘int *’ [-Wformat]
    You're passing the wrong types to printf. printf and scanf behave differently, and expect different types, for each format specifier.

    3. In your prototype for linkfloat on line 2, put a void inside the parentheses as well. That tells the compiler that the function takes exactly zero arguments. Without the void, the compiler doesn't know how many or what type arguments it takes, so it can't check to make sure you're calling the function correctly. I.e., the compiler would accept linkfloat(); as well as linkfloat("asdf", 123, 456.789);.

    4. Generally it's a good idea to put your struct definition outside of main or any other function. If it's inside a function, only that function knows the struct type, and usually you will have more than one function that must deal with that struct type.
    mohsen likes this.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Location
    Pune, Maharashtra, India
    Posts
    33
    Std10093 Thank you

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You are welcome. It is a good thing to read anduril's post as well, since almost always he has something important to say. Moreover, we use to write
    Code:
    int main(void)
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    I should have been more specific about point #2. Mismatched format specifiers for printf can result in undefined behavior, which means anything can happen. Your program may crash, it may erase your hard drive. The behavior is undefined, so anything goes. In your case, passing the wrong data to the %c, %d and %f format specifiers is most likely only going to result in garbage output. However, with format specifiers like %s, which take a pointer to the data to display, passing incorrect data may very well result in a seg fault.
    Salem likes this.

  7. #7
    Registered User
    Join Date
    Jul 2011
    Location
    Pune, Maharashtra, India
    Posts
    33
    Thanks a lot anduril462
    But one more question Do you Know any book which explains completely undefined behavior station?
    I search all my c\c++ books , I couldn't find anything about that.
    Thanks again
    Last edited by mohsen; 07-12-2013 at 04:43 AM.

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Well, since it is undefined behavior, we are not sure what will happen You can have it in your mind like a severe logical error, you [U]must[U] avoid! As you gain experience, you are going to see a lot undefined behaviors coming into play
    mohsen likes this.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    You can read a version of a C standard draft, which contains a list of undefined behavior (section J2)
    (Originally from this link.)
    mohsen likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proper format/standard of C program?
    By zerokernel in forum C Programming
    Replies: 3
    Last Post: 11-17-2010, 01:15 AM
  2. can any one make this program run.. in proper output?
    By randyjhon143 in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2009, 09:22 AM
  3. program execution
    By kris.c in forum Tech Board
    Replies: 14
    Last Post: 05-16-2007, 02:54 PM
  4. execution of a C program
    By kris.c in forum C Programming
    Replies: 7
    Last Post: 07-27-2006, 12:14 PM
  5. Program Compiles but does not read proper file....
    By michigan353 in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2005, 08:19 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21