Rule 8.8

Moderators: misra-c, david ward

Post Reply
Akhil
Posts: 4
Joined: Mon Aug 12, 2013 10:12 am
Company: TCS

Rule 8.8

Post by Akhil » Thu Sep 04, 2014 1:14 pm

1. extern int s=10;
2. static int s;
3.
4. void foo()
5. {
6. /* Do Something*/
7. }


Is line 1 non compliant for Rule 8.8 in the above example. I'm confused since line 1 gives a definition and not just declaration.
Here I'm not able to make out whether the var 's' have external or internal linkage.

My compiler binds 's' var with extern linkage if a definition is given with extern.(is value is also assigned like the above case).

misra-c
Posts: 571
Joined: Thu Jan 05, 2006 1:11 pm

Re: Rule 8.8

Post by misra-c » Mon Sep 29, 2014 9:12 am

Code: Select all

1. extern int s=10;
2. static int s;
In the first line "s" is declared with external linkage, whereas in the second line "s" is declared with internal linkage. This is undefined behaviour [ C90: Undef 8, C99: Undef 7 ] and is a violation of rule 1.3

However, if the first two lines are switched

Code: Select all

1. static int s;
2. extern int s=10;
In this case "s" is declared with internal linkage in the first line. In the second line the declaration of "s" is not of external linkage, but instead takes the same linkage as the prior declaration which in this case is internal linkage. [See C99: Section 6.2.2 para 4 ]
Both declarations therefore have internal linkage. This is well-defined in C, but violates rule 8.8. Note: rule 8.8 applies to both definitions and declarations as stated in the Amplification.
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “8.8 Declarations and defnitions”