Page 1 of 1

Issue in printf statement

PostPosted: Wed Jul 10, 2013 1:21 pm
by Dora ji
Hi,

I am trying to call db2 in C on zos.Actually the program is fetching the data from the table.But it is not printing the output in the printf statement.
Hereby i have given the part of the code.

#include<stdio.h>
.
.
.
{
printf("*** Cursor Declaration  ***\n");

     EXEC SQL DECLARE C1 CURSOR FOR
    SELECT
    DEPTNUM
    FROM
    STDDATA
    WHERE
    STDID = 10
    ;

    EXEC SQL OPEN C1;

  printf("After OPEN Cursor C1 SQLERROR CODE(%d)\n",sqlcode);
  printf("Fetch Cursor");

  if(sqlca.sqlcode < 0) {return -1;}

      EXEC SQL FETCH C1 INTO
      :S_DEPTNUM;
      returncode = sqlcode;

  printf("After FETCH Cursor C1 SQLERROR CODE(%d)\n",sqlcode);

  printf("DEPTNUM IS:\n",S_DEPTNUM);     <-- error here

 printf("\n***   Successful   ***\n");
}


S_DEPTNUM is the host variable i declared in the program.
The printf statement which i bolded doesn't display a result.It displays only the statement which is under quotes.Do the printf statement is correct??.
I need that the fetched data should move into the host variables and it is to be displayed in spool.
Kindly please share your views to make the code successful.

Coded

Re: Issue in printf statement

PostPosted: Wed Jul 10, 2013 2:10 pm
by NicC
How about coding a format string for s_deptnum in your printf string?
How about using the code tags for presenting your code in a user friendly way?
Also, my understanding is that you should not use printf() unless there a minimum of 2 arguments - you shoud use puts(). This is because a printf() can be hacked - so I am told by "Deitel & Deitel"

Re: Issue in printf statement

PostPosted: Wed Jul 10, 2013 6:21 pm
by Dora ji
Thanks Nicc..

Its working.

Re: Issue in printf statement

PostPosted: Fri Apr 24, 2015 1:16 pm
by awesome
This is very nice and awesome post
I like it very much
Thanks alot...

Re: Issue in printf statement

PostPosted: Fri Apr 24, 2015 1:55 pm
by enrico-sorichetti
Also, my understanding is that you should not use printf() unless there a minimum of 2 arguments - you shoud use puts(). This is because a printf() can be hacked - so I am told by "Deitel & Deitel"


I humbly beg to differ

printf with one argument is unsafe only for a non literal format string


#include "stdio.h"
int main()
{
   int data =1;
   char *fmt  = "pointer based format  only\n";
   char *fmtd = "pointer based format  and data %d\n";

   printf(fmt);
   printf(fmtd, data);

   printf("constant      format  only\n");
   printf("constant      format  and data %d\n", data) ;

   return 0;

}


[enrico@enrico-imac tests]$cc z.c
z.c:12:9: warning: format string is not a string literal (potentially insecure) [-Wformat-security]
        printf(fmt);
               ^~~
1 warning generated.


[enrico@enrico-imac tests]$./a.out
pointer based format  only
pointer based format  and data 1
constant      format  only
constant      format  and data 1
[enrico@enrico-imac tests]$


same warning for c++

Re: Issue in printf statement

PostPosted: Fri Apr 24, 2015 7:19 pm
by NicC
Why didn't you say so at the time, Enrico?! This topic is almost a year old so it is that amount of time since I was reading the book - still not finished so still learning basics.

Re: Issue in printf statement

PostPosted: Fri Apr 24, 2015 7:56 pm
by enrico-sorichetti
:oops:
Hello Nic
the topic just came out when clicking on NEW POSTS
by awesome ยป Fri Apr 24, 2015 8:46 am

so I just replied without looking at the conversation flow

c /one year/two years/ ;)