First I code like this. but this get error.
#include <stdio.h>
int array[2][4]={1,2,3,4,5,8,9,0};
int (*p)[4]=NULL;
p=array;
int main(){
int j;
for(j=0;j<4;j++){
printf("%d",p[1][j]);
printf("\n");
}
return 0;
}
After that I modify my code like this.
I just put p=array; into int main()
#include <stdio.h>
int array[2][4]={1,2,3,4,5,8,9,0};
int (*p)[4]=NULL;
int main(){
p=array;
int j;
for(j=0;j<4;j++){
printf("%d",p[1][j]);
printf("\n");
}
return 0;
}
Why do they have different operation?
p=array; statement is an assignment statement and must be performed inside a function.
Here you declare and define global variable
int (*p)[4]=NULL;
Then you execute code (which is prohibited outside any function)
p=array;
Instead, you can do this
int (*p)[4]=array;
or explicitly declare and define like this
int (*p)[4];
int (*p)[4] = array;
p=array
is an assignment statement which cannot be performed outside an function.
As declaration should can be done anywhere inside a function (local declaration) or outside a function (global declaration) but assignment should be done inside a function only.
Though declaration and assignment at the same time can be done globally (like in your code):
int array[2][4]={1,2,3,4,5,8,9,0};
int (*p)[4]=NULL;
Related
I cannot print float variables when calling my functions. int variables print but floats won't print their proper value when passed to my function.
I tried to change the float to int and that worked
int main() {
int foo = 6;
call(foo);
}
void call(int bar) {
printf("%d", bar);
}
This worked and it does indeed print 6.
But, doing the same but with floats only prints out 0.00000:
int main() {
float foo = 6;
call(foo);
}
void call(float bar) {
printf("%f", bar);
}
How do I correctly call and then print float variables?
you need a forward declaration of call
void call(float integerrrr);
int main(){
float integerrrr=6;
call(integerrrr);
}
void call(float integerrrr){
printf("%f", integerrrr);
}
your compiler probably warned you about this
You could simply define call above main instead of below it. The compiler must have seen the declaration of functions when they are used, so a forward declaration like pm100 suggests is one way. Moving the whole definition above main is the another (that does not require a forward declaration).
#include <stdio.h>
void call(float integerrrr){
printf("%f", integerrrr);
}
int main(void){
float integerrrr = 6;
call(integerrrr); // now the compiler knows about this function
}
INT type variables i can print but float just does not
If your program actually compiles as-is it will use an old (obsolete) rule that makes an implicit declaration of undeclared functions when they are used. The implicit declaration would be int call(); - which does not match your actual function. The program (even the one that seems to be working) therefore had undefined behavior.
the compiler of c work from top to bottom so line by line,
so u will have to call the first function void call() then your main function:
void call(float integerrrr){
printf("%f", integerrrr);
}
int main(){
float integerrrr=6;
call(integerrrr);
}
I am trying to share a global array between two files as below:
main.c:
#include <stdio.h>
#include "another.h"
int n=10;
int a[n];
void main(){
printf("Enter value:\n");
scanf("%d",&a[0]);
display();
}
another.h:
#include <stdio.h>
extern int n;
extern int a[n];
void display(){
printf("%d",a[0]);
return;
}
However, I am getting the following error:
variable modified 'a' at file scope
I'm not able to understand why I'm getting this error. Does anyone have an explanation?
The following appears to be an attempt at creating a variable length array. But regardless what it is actually, the 2nd of the following two lines is not legal:
extern int n;
extern int a[n];
n is not yet defined at the time it is used to create a.
In case you were thinking to create a VLA...
by definition VLAs are only created with automatic storage duration on the stack. This makes them unusable for use as extern or for any type of globally scoped variable.
However you can declare the size variable as an extern in a header file so that it is project global for any source file that includes that file, and once it is defined it can be used to size any array within project scope:
<some.h>
extern size_t array_size;
extern int gArray[100];
<some.c>
#include "some.h"
...
size_t array_size = 20;
int gArray[100] = {0};//Not a VLA
...
int main(void)
{
int array[array_size] = {0};
for(int i=0;i<sizeof(gArray);i++
{
//populate each element of gArray here
}
...
<someother.c>
#include "some.h"
int otherFunc(void)
{
//gArray is visible here just as it is in main(), with exactly the same values.
int local array[array_size] = {gArray[0],gArray[1],gArray[2],...,gArray[19]};
...
I am reading a book on the C language ('Mastering C'), and found the topic on scope resolution operator (::) on page 203, on Google Books here.
But when I run the following code sample (copied from the book), the C compiler gives me an error. I searched on the internet but I am unable to find any reference to a scope resolution operator in C.
#include <stdio.h>
int a = 50;
int main(void)
{
int a =10;
printf("%d",a);
printf("%d\n", ::a);
return 0;
}
So if I want to access a global variable then how could I do that from within the main() function ?
No. C does not have a scope resolution operator. C++ has one (::). Perhaps you are (or your book is) confusing C with C++.
You asked how you could access the global variable a from within a function (here main) which has its own local variable a. You can't do this in C. It is lexically out of scope. Of course you could take the address of the variable somewhere else and pass that in as a pointer, but that's a different thing entirely. Just rename the variable, i.e. 'don't do that'
No, namespaces are a feature of C++.
It is, however, possible to refer to global a in your example.
You can achieve this by using the extern keyword:
#include <stdio.h>
int a = 50;
int main(void)
{
int a = 10;
printf("%d\n",a);
{ // Note the scope
extern int a; // Uses the global now
printf("%d\n", a);
}
return 0;
}
That's a bit tricky, though. It's bad style. Don't do that.
:: operator is available in C++ not C. If you wanted to access the global variable, use
#include <stdio.h>
int a = 50;
int main(void)
{
int a =10;
printf("%d",a); //prints 10
{
extern int a;
printf("%d", a); //prints 50
}
return 0;
}
Or you could use a pointer which holds the address of the global variable a and then dereference the pointer if you want to print the value of the global variable a.
No (the :: operator is C++ specific). In C, if you use the same identifier in different overlapping scopes (say, file scope a and block scope a), the block scope identifier shadows the file scope identifier and there is no way to refer to the shadowed identifier.
It is generally best programming practice to avoid shadowed variables. Many lint type programs can warn about this situation.
In plain C, there is no scope resolution. You have to name your variables differently.
That means that all variables a below are different ones:
#include <stdio.h>
int a = 50;
int main(void)
{
int a = 10;
{
int a = 20;
int i;
for (i = 0; i < 10; i++) {
int a = 30;
}
}
return 0;
}
You may use pointers to access and edit global variables in C.
#include <stdio.h>
#include <stdlib.h>
int a;
a=78;
int *ptr=&a; //pointer for global a
int main()
{
int a=0;
printf("%d",a); //Prints 0 as local variable
printf("%d",*ptr);
ptr=30; //changes the value of global variable through pointer
printf("%d",*ptr); //Now it prints 30
return 0;
}
It entirely depends on what kind of compiler you're using to execute your code.
#include <stdio.h>
int a = 50;
int main(void)
{
int a =10;
printf("%d\n",a);
printf("%d\n", ::a);
return 0;
}
Try running this code in Turbo C compiler, and you will get the results.
Now, regarding the C book that you're following, the examples codes present in the book must be in terms with Turbo C/C++ compiler and not the gcc compiler.
this is the first code
#include <stdio.h>
void a();
int i;
int main()
{
printf("%d\n",i);
a();
return 0;
}
int i=5;
void a()
{
printf("%d\n",i);
}
output
5
5
the second code is
#include <stdio.h>
void a();
extern int i;
int main()
{
printf("%d\n",i);
a();
return 0;
}
int i=5;
void a()
{
printf("%d\n",i);
}
ouput
5
5
what is the difference between the codes if their outputs are same...
and if they are same then what is the use of extern
extern is something like saying to the Linker that you have defined the variable in some other file( even global variables can be made extern inside other functions in same file) don't throw error now you will find the variable later then link it .
using extern your only declaring the variable not defining it(no memory allocated).
In Your second program for first declartion
extern int i;
No memory is allocated but later when you declare
int i = 5 ;
the memory is allocated for i. while linker searches for i in printf if i is not declared extern it throws a error since its extern linking takes place when it finds the defintion of i.
extern means that you are declaring the variable but not defining it (allocating memory for it). Have a look at this link for a good explanation of extern: http://www.geeksforgeeks.org/understanding-extern-keyword-in-c/
I'm learning C and doing the exercise of the function chapter. So i have written a small programm with 3 files and two small functions. Sincerly it does not work. I have no errors, the functions are simply not executed and i don't know why.
First of all this is my headerfile which only declares my functions.
//employee.h
int addEmployee(void);
int printEmployee(int i);
So the next file is for the definition of the functions.
//employee.c
#include "employee.h"
#include <stdio.h>
#include <ctype.h>
int numE;
int i;
int addEmployee(void)
{
printf("Please type in the number of your employees: ");
scanf_s("%d", &numE);
i = numE;
return i;
}
int printEmployee(int i)
{
printf("%d", i);
getchar();
getchar();
return i;
}
And the last file is used to execute the functions.
//lab6.c
#include "employee.h"
#include <stdio.h>
#include <ctype.h>
int main ()
{
int addEmployee();
int printEmployee();
return 0;
}
Using
int main ()
{
int addEmployee();
int printEmployee();
return 0;
}
you're basically declaring 2 new functions with 0 arguments.
You're not calling your "old" functions.
This should work, as others have pointed out:
int main ()
{
int emp = addEmployee();
printEmployee(emp);
return 0;
}
Because you're calling addEmployee(), storing it's result to emp and then printing emp using printEmployee. Since printEmployee is declared with one parameter, you just put emp in and it will work.
When you call a function you do not put the return type in front of the call. The call is simply the name of the function and any parameters you are calling it with. So your main function should look like this:
int main() {
addEmployee();
printEmployee(1);
return 0;
}
EDIT: So in your employee.c file, you are trying to use addEmployee() to take a number of employees from the command line and store it in the variable i right? And you want printEmployee() to tell you how many employees were entered? Here's how you would do that.
//employee.c
#include "employee.h"
#include <stdio.h>
#include <ctype.h>
int i;
int addEmployee(void)
{
int numE;
printf("Please type in the number of your employees: ");
scanf_s("%d", &numE);
i = numE;
}
int printEmployee()
{
printf("%d", i);
getchar();
getchar();
}
Here's what I did.
First, I made numE a variable local to the addEmployee function that uses it. Generally you should keep variable scope as small as possible. That means keep them down to the lowest level they are used. In this case, numE is only needed by addEmployee() so that's its scope.
Second, I removed the parameter from int printEmployee(int i). It was overriding your i variable at the file level. So you were storing the number read into numE in i but then when you entered printEmployee() you were creating a new, empty i that hid it. When you called printEmployee(1) from main, you were passing the value 1 into i in printEmployee(int i). By removing the parameter, you stop hiding employee.c's i.
Finally, I removed the returns. A function doesn't have to return anything in C. And if you are not going to use the return, then it's just an extra line of code to include it.
There's one more change you'll have to make to make this work, in your lab6.c file. Remove the parameter from the call to printEmployee()
//lab6.c
#include "employee.h"
#include <stdio.h>
#include <ctype.h>
int main ()
{
addEmployee();
printEmployee();
return 0;
}
Should work the way you expect it now.
you want:
int main ()
{
addEmployee();
printEmployee(1);
return 0;
}
Change this:
int main ()
{
int addEmployee();
int printEmployee();
return 0;
}
To this:
int main ()
{
addEmployee();
printEmployee();
return 0;
}
You're re-declaring the functions instead of calling them.
You'll also have to change your printEmployee function to not accept an integer argument; it seems like it should just be using the same global variable as addEmployee. This is a bad idea though; global variables are generally to be avoided. addEmployee should probably return the employee ID, which you could store and then pass into printEmployee.