C Formatting Errors


 
Thread Tools Search this Thread
Top Forums Programming C Formatting Errors
# 1  
Old 11-23-2014
C Formatting Errors

I am compiling little codes and I am receiving formatting errors. Can anyone tell me the problems and how would I fix it?

1.
Code:
int main()
{
        int n;
        printf( "Enter an integer => " );
        scanf( "%d", n );
        printf( "%d\n", n );
}

bug1.c:7:2: warning: format â%dâ expects argument of type âint *â, but argument 2 has type âintâ [-Wformat]

Is this reading the input number as a string so it is giving an error?

2.
Code:
int main()
{
        int n = 1;
        double pi = M_PI;
        printf("Pi=%d, n=%f\n", pi, n);
}

bug2.c:8:2: warning: format â%dâ expects argument of type âintâ, but argument 2 has type âdoubleâ [-Wformat]
bug2.c:8:2: warning: format â%fâ expects argument of type âdoubleâ, but argument 3 has type âintâ [-Wformat]

3.
Code:
double avg(double a[], int n)
{
        int i;
        double sum;

        assert(n > 0);
        sum = 0.0;
        for (i=0;i<n;i++)
                sum += a[i];
        return sum/n;
}

int main()
{
        double a[5] = {1.0,2.0,3.0,4.0,5.0};
        int n;

        printf("Enter size:  ");
        scanf("%d",n);
        assert(n<5);
        printf("Avg. = %f\n",avg(a,n));
}

avg.c:22:2: warning: format â%dâ expects argument of type âint *â, but argument 2 has type âintâ [-Wformat]

Thank you for any help
# 2  
Old 11-23-2014
Did you read the man page for scanf?
# 3  
Old 11-23-2014
Quote:
Originally Posted by achenle
Did you read the man page for scanf?
So for the first one, I think I figured out the problem. It needs a pointer of the specified type.

But I am still stuck on the other two.
# 4  
Old 11-23-2014
The compiler is telling you exactly what's wrong in all of these cases.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with formatting

Hi, I am new to UNIX and need your help in formatting the below input command to the desire output Input: CREATE UNIQUE INDEX XPKTABLE1 ( COL1, COL2 ) ON TABLE_NM; Output: COMMENT ON TABLE DB_NM.TABLE_NM AS 'PK=,COL1,COL2; '; In... (14 Replies)
Discussion started by: varun2327
14 Replies

2. Shell Programming and Scripting

Formatting

Good day All, I have requirement where my input data looks like below ] Message5 Expecting Output as 04/MAR/2104 ||| 23:15:45 ||| servername ||| NOTIFICATION |||message1||||||userId|||||| Message5 I could not use space delimiter as in the messages I would be having them as... (2 Replies)
Discussion started by: Tomlight
2 Replies

3. Shell Programming and Scripting

Formatting Help

Hi Guys, i have report that runs every 10 min and send the report of failed jobs to my mail. Currently i am using a command like this to send mail. mailx -t -s "FAILURE JOBS IN HYDRA $temp_date" addressee@domain.com < temp_file5 But i am getting mail in this format ....... (4 Replies)
Discussion started by: gkrish
4 Replies

4. Shell Programming and Scripting

help formatting

I need to format a txt file and convert it in CSV. Any "future" column is separated by a newline. FROM: XS1 1.43294 0.0 XS2 1.21824 0.0 TO: XS1,XS2 (2 Replies)
Discussion started by: alfreale
2 Replies

5. UNIX for Dummies Questions & Answers

Major OS errors/Bash errors help!!!!

Hi all, dummy here.... I have major errors on entering the shell. On login I get: -bash: dircolors: command not found -bash: tr: command not found -bash: fgrep: command not found -bash: grep: command not found -bash: grep: command not found -bash: id: command not found -bash: [: =: unary... (12 Replies)
Discussion started by: wcmmlynn
12 Replies

6. AIX

Adapter Errors and Link Errors

$ errpt | more IDENTIFIER TIMESTAMP T C RESOURCE_NAME DESCRIPTION 3074FEB7 0802050205 T H fscsi1 ADAPTER ERROR B8113DD1 0802050205 T H fcs1 LINK ERROR B8113DD1 0802050205 T H fcs1 LINK ERROR 3074FEB7 0802050205 T H fscsi0 ADAPTER ERROR B8113DD1 ... (2 Replies)
Discussion started by: mcastill66
2 Replies

7. UNIX for Advanced & Expert Users

Adapter Errors and Link Errors

$ errpt | more IDENTIFIER TIMESTAMP T C RESOURCE_NAME DESCRIPTION 3074FEB7 0802050205 T H fscsi1 ADAPTER ERROR B8113DD1 0802050205 T H fcs1 LINK ERROR B8113DD1 0802050205 T H fcs1 LINK ERROR 3074FEB7 0802050205 T H fscsi0 ADAPTER ERROR B8113DD1 ... (0 Replies)
Discussion started by: mcastill66
0 Replies

8. UNIX for Dummies Questions & Answers

formatting

Hi Again Guys , Please i installed linux RH 6.1 on Toshiba , 10G , RAM=128 , 600 MHZ . After i installed linux i got many error messages , seems it was not installed correctly , also when i finished installation it did not ask me for the 2nd installation CD , and when i logged as root , i... (5 Replies)
Discussion started by: tamemi
5 Replies

9. UNIX for Dummies Questions & Answers

formatting

is it possible to format a powerbook g4 mac? like totally erase the HD then pop in the Mac OS cd and it will boot up an install like windows or any linux? (5 Replies)
Discussion started by: xeron
5 Replies

10. UNIX for Dummies Questions & Answers

formatting

I've been asking on IRC channels but no one answers me, I need to format my hard drive, normally it's just format c: but c doesn't exist, how do I format when I have linux mandrake installed. Please reply to this quickly, I'm kinda in a rush :( (1 Reply)
Discussion started by: darryll777
1 Replies
Login or Register to Ask a Question