LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-16-2023, 07:14 AM   #1
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Rep: Reputation: 4
Am unable to get the second output printed?


On Cygwin platform, on compiling C language program below, get the error, stated in comments, inside the program.
Code:
#include <stdio.h>
int main()
{
	int *pnum;
	char *pch;

	printf(" Size of integer pointer = %d\n\n", sizeof(pnum));
	printf("size of char pointer = %d", sizeof(pch));

	int x=10;
	int *ptr;
	ptr = &x;

	printf("\n \n ptr is: %i", x);

	int x1[6] = {1,2,3,4,5,6};
	
	ptr = x1;
        int i = 0;
	while (*ptr && i<=5) 
		printf("\nitem # %d ", i, "is :%d", *(ptr+i++));
                //Doubt: Only i is printed, while the value there is not printed on cygwin?
                //first tried with a similar expression:
                //printf("\n item # %d ", i, "is :%d", ptr[i++]);
                //that should yield the same result as above, as ptr[i++] is the same as *(ptr+i++)
}
The output is :
Code:
Size of integer pointer = 8

size of char pointer = 8

 ptr is: 10
 item # 1
 item # 2
 item # 3
 item # 4
 item # 5
 item # 6
But, it should be:

Code:
Size of integer pointer = 8

size of char pointer = 8

 ptr is: 10
 item # 1is :1
 item # 2is :2
 item # 3is :3
 item # 4is :4
 item # 5is :5
 item # 6is :6

Last edited by ajiten; 06-16-2023 at 07:16 AM.
 
Old 06-16-2023, 07:44 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,782

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Quote:
Originally Posted by ajiten View Post
Code:
		printf("\nitem # %d ", i, "is :%d", *(ptr+i++));
That's not how printf works, try

Code:
 printf("\nitem # %d is :%d", i, *(ptr + i++));
 
1 members found this post helpful.
Old 06-16-2023, 07:47 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
You cannot invent new ways how `printf` should work, you have to use it in the documented way.
Code:
    printf("\n item # %d is :%d", i, ptr[i++]);
Also your code has some problems, but lets take them one by one.
 
1 members found this post helpful.
Old 06-29-2023, 11:56 AM   #4
digitechroshni
LQ Newbie
 
Registered: Jun 2023
Location: Bhubaneswar
Posts: 2

Rep: Reputation: 1
There are two issues:

The format specifier %d expects an integer argument, but "is :%d" is a string argument. To fix this, you need to separate the two format specifiers and provide two corresponding arguments. One for %d and another for the string.

The order of the arguments is incorrect. The value to be printed should come after the format specifiers.

Here's the corrected printf statement:

printf("\nitem # %d is :%d", i, *(ptr+i++));

This will properly print the item number (i) and the corresponding value (*(ptr+i++)).

I hope it would be helpful for you!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] SELinux: command output printed on serial but not on ssh JuventusFC Linux - Embedded & Single-board computer 8 08-24-2021 04:44 AM
[SOLVED] GIMP: Why is my printed output not the expected size? haertig Linux - Software 4 02-03-2017 02:15 PM
CentOS 6.2, USB printer, CUPS says printed successfully but page is not printed stase Linux - Desktop 5 07-03-2012 06:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration