Issue in printf statement

Help for C/C++ for MVS, OS/390 C/C++, z/OS C/C++ and C/C++ Productivity Tools for OS/390
Dora ji
Posts: 12
Joined: Sun Jul 07, 2013 11:12 pm
Skillset: Fresher to mainframe
Referer: Webpage

Issue in printf statement

Postby Dora ji » Wed Jul 10, 2013 1:21 pm

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.

Code: Select all

#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

NicC
Global moderator
Posts: 3025
Joined: Sun Jul 04, 2010 12:13 am
Skillset: JCL, PL/1, Rexx, Utilities and to a lesser extent (i.e. I have programmed using them) COBOL,DB2,IMS
Referer: Google
Location: Pushing up the daisies (almost)

Re: Issue in printf statement

Postby NicC » Wed Jul 10, 2013 2:10 pm

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"
The problem I have is that people can explain things quickly but I can only comprehend slowly.
Regards
Nic

Dora ji
Posts: 12
Joined: Sun Jul 07, 2013 11:12 pm
Skillset: Fresher to mainframe
Referer: Webpage

Re: Issue in printf statement

Postby Dora ji » Wed Jul 10, 2013 6:21 pm

Thanks Nicc..

Its working.

awesome
Posts: 1
Joined: Fri Apr 24, 2015 1:14 pm
Skillset: tdhtdhet
Referer: google

Re: Issue in printf statement

Postby awesome » Fri Apr 24, 2015 1:16 pm

This is very nice and awesome post
I like it very much
Thanks alot...

enrico-sorichetti
Global moderator
Posts: 3006
Joined: Fri Apr 18, 2008 11:25 pm
Skillset: tso,rexx,assembler,pl/i,storage,mvs,os/390,z/os,
Referer: www.ibmmainframes.com

Re: Issue in printf statement

Postby enrico-sorichetti » Fri Apr 24, 2015 1:55 pm

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

Code: Select all


#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;

}


Code: Select all

[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.


Code: Select all

[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++
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort

NicC
Global moderator
Posts: 3025
Joined: Sun Jul 04, 2010 12:13 am
Skillset: JCL, PL/1, Rexx, Utilities and to a lesser extent (i.e. I have programmed using them) COBOL,DB2,IMS
Referer: Google
Location: Pushing up the daisies (almost)

Re: Issue in printf statement

Postby NicC » Fri Apr 24, 2015 7:19 pm

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.
The problem I have is that people can explain things quickly but I can only comprehend slowly.
Regards
Nic

enrico-sorichetti
Global moderator
Posts: 3006
Joined: Fri Apr 18, 2008 11:25 pm
Skillset: tso,rexx,assembler,pl/i,storage,mvs,os/390,z/os,
Referer: www.ibmmainframes.com

Re: Issue in printf statement

Postby enrico-sorichetti » Fri Apr 24, 2015 7:56 pm

: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/ ;)
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort


  • Similar Topics
    Replies
    Views
    Last post