Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8120419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:09:28+00:00 2026-06-06T05:09:28+00:00

In this c program a=8; main() { printf(%d, a); } variable a has been

  • 0

In this c program

a=8;
main()
{
    printf("%d", a);
}   

variable a has been declared without any data type and still this program compiles successfully and gives the desired output.
output ::

8  

see it on ideone.
but, when i declared the same variable inside main, it gives compilation error.

main()
{
     a=8;
    printf("%d", a);
}  

output ::

prog.c:2: warning: return type defaults to ‘int’
prog.c: In function ‘main’:
prog.c:3: error: ‘a’ undeclared (first use in this function)
prog.c:3: error: (Each undeclared identifier is reported only once
prog.c:3: error: for each function it appears in.)
prog.c:4: warning: implicit declaration of function ‘printf’
prog.c:4: warning: incompatible implicit declaration of built-in function ‘printf’  

see here.

How the first program is working but the second?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T05:09:30+00:00Added an answer on June 6, 2026 at 5:09 am

    What you see here is the “Implicit Int Rule” at work. Simply put the rule says:

    “A variable declared without an explicit type name is assumed to be of type int.”

    Note that this rule was revoked in the c99 Standard[Ref 1].However, depending on your compiler and its settings, the first example might compile with a warning, or will fail to compile(with strict compilation settings)

    If you compile your first example with strict settings adhering to c99 Standard the compiler will tell you the root cause.

    check here.

    cc1: warnings being treated as errors
    prog.c:1: error: data definition has no type or storage class
    prog.c:1: error: type defaults to ‘int’ in declaration of ‘a’
    prog.c:3: error: return type defaults to ‘int’
    prog.c: In function ‘main’:
    prog.c:4: error: implicit declaration of function ‘printf’
    prog.c:4: error: incompatible implicit declaration of built-in function ‘printf’

    EDIT:

    why does the first example work but second does not?

    Note the emphasis on the words “variable declared” in the rule.

    In first example, since the statement is at the global scope it is treated as an Implicit declaration, and the Implicit Int Rule gets applied to it.

    In Second example, the statement acts as an Assignment and not a Declaration. Since there is no declaration the Implicit int rule does not apply here. In the absence of any type the compiler cannot determine what is the type of a and hence reports the error.


    [Ref 1]

    C99 Standard: Foreword
    Para 5:

    This edition replaces the previous edition, ISO/IEC 9899:1990, as amended and corrected
    by ISO/IEC 9899/COR1:1994, ISO/IEC 9899/COR2:1995, and ISO/IEC 9899/AMD1:1995.
    Major changes from the previous edition include:
    …..
    …..
    — remove implicit int
    …..
    …..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can any one give me the dry output for this program? #include <stdio.h> main()
int main() { int i=3; (++i)++; printf(%d,i); } This programs works with g++ compiler
I use the Frama-C tool to generate the dependence graph of this program(main.c). #include<stdio.h>
Consider this program int main() { float f = 11.22; double d = 44.55;
I created this program: #include <iostream> #include <fstream> using namespace std; int main ()
I make this program :: #include<stdio.h> char *raw_input(char *msg); main() { char *s; *s
First look at this code: class Program { static void Main(string[] args) { var
I have an existing C++ win32 console app. This application contains a main program
I wrote this small C++ program and built it(Release) #include<iostream> int main(){ std::cout<<Hello World;
For context - read this . Problem: class Program { static void Main() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.