简体   繁体   中英

C program in ubuntu using visual studio code

The code I wrote:

 #include<stdio.h>

 int main()
 {
 int i,m,maths=0,english=0,e,science=0,s,all=0;
 for(i=1;i<=5;i++)
 {
    printf("enter the marks of english:\n");
    scanf("%d",&e);
    printf("enter the marks of maths:\n");
    scanf("%d",&m);
    printf("enter the  marks of science:\n");
    scanf("%d",&s);
    if(m>=95 && s>=95 && e>=95)
     all++;
    if(m>=90)
    maths++;
    if(e>=90)
    english++;
    if(s>=90)
    science++;

 }
  printf("95% or above in all subject : %d \n",all);
   printf("90% or above in maths :%d\n",maths);
  printf("90% or above in english: %d\n",english);
  printf("90% or above in science: %d\n",science);
 }
 

I have to input the marks of 5 students through keyboard and I am getting the following error that I don't understand. Why am I getting this kind of error?

 gcc studentMarks.c -o studentMarks.out 
studentMarks.c: In function ‘main’:
   studentMarks.c:24:17: warning: ' ' flag used with ‘%o’ gnu_printf format [-Wformat=]
   printf("95% or above in all subject : %d \n",all);
             ^
  studentMarks.c:24:44: warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat=]
   printf("95% or above in all subject : %d \n",all);
                                       ~^
 studentMarks.c:25:17: warning: ' ' flag used with ‘%o’ gnu_printf format [-Wformat=]
 printf("90% or above in maths :%d\n",maths);
             ^
 studentMarks.c:25:37: warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat=]
 printf("90% or above in maths :%d\n",maths);
                                ~^
 studentMarks.c:26:17: warning: ' ' flag used with ‘%o’ gnu_printf format [-Wformat=]
 printf("90% or above in english: %d\n",english);
             ^
studentMarks.c:26:39: warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat=]
 printf("90% or above in english: %d\n",english);
                                  ~^
 studentMarks.c:27:17: warning: ' ' flag used with ‘%o’ gnu_printf format [-Wformat=]
 printf("90% or above in science: %d\n",science);
             ^
 studentMarks.c:27:39: warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat=]
 printf("90% or above in science: %d\n",science);
                                  ~^ 

在此处输入图片说明

In C programming %o represent octal values. So when you are writing 95% or its reading it as %o . Since there is a space between % and o, C compiler doesn't recognize it as octal or percentage and that's why its giving warnings. But, if you replace 95% with 95%% then c compiler reads it as 95 percent.

#include<stdio.h>

 int main()
 {
 int i,m,maths=0,english=0,e,science=0,s,all=0;
 for(i=1;i<=5;i++)
 {
    printf("enter the marks of english:\n");
    scanf("%d",&e);
    printf("enter the marks of maths:\n");
    scanf("%d",&m);
    printf("enter the  marks of science:\n");
    scanf("%d",&s);
    if(m>=95 && s>=95 && e>=95)
     all++;
    if(m>=90)
    maths++;
    if(e>=90)
    english++;
    if(s>=90)
    science++;

 }
  printf("95%% or above in all subject : %d \n",all);
   printf("90%% or above in maths :%d\n",maths);
  printf("90%% or above in english: %d\n",english);
  printf("90%% or above in science: %d\n",science);
 }

This should work now.

For each line, from lines 24 through 27, the compiler is emitting two warnings (not errors as you indicate) to help you out.

The first is caused when your compiler encounters % o from the longer substring 95% or .

The compiler and the printf function interprets % o as %o with a blank flag to allow for a - to be emitted if the signed numeric type is negative.

The second warning is caused because of the first interpretation and the fact that you included only one argument after the format string. If you had accidentally provided two signed integer arguments after the format string, then you would not have seen any warnings.

If you try to execute the code as is in spite of the warnings, then you'd get undefined behavior.

Any time you want to include a % in your format string and you want to emit it literally, then you have to encode it as %% .

As an aside, unrelated to your question, the value you provided to the gcc -o option is unusual. One would typically call the output executable studentMarks or studentmarks , not studentMarks.out .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM