c programming practice examples

Thread Starter

Gajyamadake

Joined Oct 9, 2019
144
I am confused by some keywords in C language .I am trying to understand their use in coding

Take a look at this code I am declaring variable x and assigning value
Code:
int x = 8
main()
{
   .......
}
I am declaring variable with extern keyword
Code:
 extern int x = 8
main()
{
   .......
}
anyone can help me understand. What does this exerern keyword do in the code ?
 

Yaakov

Joined Jan 27, 2019
1,604
extern makes a variable global. That is, it makes the variable‘s scope such that it is available everywhere in the program and not just within a function.
 

Analog Ground

Joined Apr 24, 2019
169
Adding "extern" to a variable when it is declared inside a function tells the compiler not to reserve storage for the variable locally in the function. Rather, it tells the compiler the variable is outside the function somewhere and the compiler needs to find it. This can get a little confusing because the use of "extern" within a function is not always required. If the variable is declared in the same source code file, "extern" can be omitted. IMHO, It is good practice to always include it. For example, what if the source code is restructured and the variable moves to somewhere else. It also tells anyone the nature of the variable. "extern" is a keyword used when defining the "scope" of a variable.
 

Ian Rogers

Joined Dec 12, 2012
666
extern is normally used to tell the compiler that it is declared elsewhere... If you include an external file that has no header and the declarations are specifically for that file, then to use any element from the external file within your main file, declare the variable ( or indeed function) as extern and the compiler will find it..
 

philbowles2012

Joined Mar 28, 2013
42
"extern makes a variable global. " are you sure about that? As I understand it, it has to BE global already, extern just tells the compiler it is defined in a different compilation unit, i.e. while it may not be defined HERE, I still want to use it as if it were..
 

Yaakov

Joined Jan 27, 2019
1,604
"extern makes a variable global. " are you sure about that? As I understand it, it has to BE global already, extern just tells the compiler it is defined in a different compilation unit, i.e. while it may not be defined HERE, I still want to use it as if it were..
No I am not certain, I was following documentation I use, and C is not my first language, so to speak. So, I will withdraw my comment as I didn't test it, and it could be wrong.

EDIT:

Apparently, standard C allows the extern keyword inside the same file, and it is effectively redundant, but if the variable is defined in an #include, the explicit use of extern indicates this.

That's what confused me.

Oh well, I suppose I should learn more C...
 
Last edited:

Thread Starter

Gajyamadake

Joined Oct 9, 2019
144
typedef keyword

typedef is a keyword used in C language to assign alternative names to existing datatypes

I wrote my own code to understand use of typedef keyword
Code:
#include<stdio.h>

/*typedef <data-type> <variable_name>*/

typedef int x = 20 ;

void main( )
{  
    x n1 = 40;
   
    printf ("%d", n1);
 
}
error:
typedef 'x' is initialized (use __typeof__ instead)
typedef int x = 20 ;

What is this error ? What's the correct use of typedef keyword in c code
 

philbowles2012

Joined Mar 28, 2013
42
typdef is used to define what are effectively you own keywords. You can't assign a value to them. You use them as shothand for complicated type definitions to save typing and create clearer code.

e.g.
typedef struct {
int scruples;
int drams;
int grains;
} WEIGHT;
The structure WEIGHT can then be used in the following declarations:

WEIGHT chicken, cow, horse, whale;

PS Google is very good at answering these one liners...I got that from the first hit after searching "typedef example" :)
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
144
typdef is used to define what are effectively you own keywords. You can't assign a value to them. You use them as shothand for complicated type definitions to save typing and create clearer code.
I was confused but I learned by making mistake
Code:
#include<stdio.h>

/*typedef <data-type> <variable_name>*/

 typedef int x ;
 
void main( )
{   
    x n1;
    n1 = 20;
    
    printf ("%d", n1);
    
}
 

MrChips

Joined Oct 2, 2009
19,406
extern says that the variable is defined somewhere else, i.e. it is not defined here.

This does not make the variable global. Inherently, for extern to work the variable must be global.
Hence the variable must exist somewhere else and defined globally.

In the example given, extern is redundant because x is globally defined.
Code:
extern int x = 8;
main( )
{
   .......
}
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
144
extern says that the variable is defined somewhere else, i.e. it is not defined here.
register keyword
I have declare two different variable with int type
Code:
#include <stdio.h>
int main() {
 
   register int a = 10;
   int b = 8;
   printf("The value of register variable a: %d \n",a);
   printf("The value of variable b: %d \n",b);
 
   return 0;
}
I can't identify the difference between auto declaration and register declaration
 

MrChips

Joined Oct 2, 2009
19,406
Because, as far as the result is concerned, there is no difference.

When you define a variable, e.g. int a;
the variable is allocated space in memory.

When you specify register, you are asking the compiler to use an internal CPU register, not computer memory.
 

Analog Ground

Joined Apr 24, 2019
169
"register" is a bit odd. It suggests an optimization to the compiler to put the variable, if possible, in one of the registers within the processing unit instead of on the "stack" in memory. It is only for local or "automatic" variables. It suggests the variable is used a lot and keeping it in a register, instead of memory, will execute the program faster. The compiler does not have to use a register. It is a suggestion. I look to see if the compiler is using a register by looking at the assembly code. Another way to get this and more optimizations is to turn on optimizations in the compiler.

I am not sure if "register" is used much these days. Memory is faster and turning on more extensive compiler optimizations may be more common. The usefulness is very much dependent on the processor and architecture of the computer. I would not spend much time learning about this one. Better to learn about compiler optimizations which can make a large difference in execution speed.
 

Analog Ground

Joined Apr 24, 2019
169
I was informed ( somewhere on this site ) that the "register" specifier is superfluous on embedded systems anyway!
Not sure I would go that far. Can you elaborate why it might be superfluous? Any possibility of finding a link? All I can think of is modern compilers make it unnecessary. In which case, it is superfluous on all systems with modern compilers.
 
Last edited:

Ian Rogers

Joined Dec 12, 2012
666
Not sure I would go that far. Can you elaborate why it might be superfluous? Any possibility of finding a link? All I can think of is modern compilers make it unnecessary. In which case, it is superfluous on all systems with modern compilers.
Here is a quote from the XC compiler documentation.
datasheet said:
4.5.6 Variables in Registers
With MPLAB XC8, there is no direct control of placement of variables in registers. The
register keyword (which can only be used with auto variables) is silently ignored
and has no effect on the allocation of variables.
Some arguments are passed to functions in the W register rather than in a memory
location; however, these values will typically be stored back to memory by code inside
the function so that W can be used by code associated with that function. See
Section 4.8.6 “Function Parameters” for more information as to which parameter variables can use registers.
It may just be Pic and Avr… But I can see the logic behind it
 

djsfantasi

Joined Apr 11, 2010
5,704
Type “C volatile” into your browser.
Several pages of results are returned.
I suggest for each keyword you’d like to learn about, that you search for “C keyword” first.

If you still have specific questions, follow up here.
 

MrChips

Joined Oct 2, 2009
19,406
This is not the way to learn C keywords.
Read what the keyword does. Look at examples and explanations of what it does.

Like register, you are not going to see the difference with volatile.
And if you don't see a difference, don't use it until you fully understand its purpose.
 
Top