C quiz

January 4, 2018 at 3:30 pm | Posted in Programming | Leave a comment

I was looking at an online C language quiz and came across this:

#include <stdio.h>

int main(void) {
 struct myStruct
  {
    int num = 10;
  }var;

  printf("%d\n", var.num
);

  return 0;
}

This will never compile. Guess why. Because You can’t initialize struct members in declaration. Here is what gcc 7.2.1 will throw:

[arnuld@arch64 programs]$ gcc -ansi -pedantic -Wall -Wextra t.c
t.c: In function ‘main’:
t.c:6:13: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token
int num = 10;
^
t.c:4:9: warning: struct has no members [-Wpedantic]
struct myStruct
^~~~~~~~
t.c:9:21: error: ‘struct myStruct’ has no member named ‘num’
printf(“%d\n”, var.num
^
t.c:7:4: warning: variable ‘var’ set but not used [-Wunused-but-set-variable]
}var;
^~~
[arnuld@arch64 programs]$

You can do this instead:

#include <stdio.h>

int main(void) {
 struct myStruct
  {
    int num;
  }var;

  var.num = 10;
  printf("%d\n", var.num);

  return 0;
}

Then you get this:

[arnuld@arch64 programs]$ gcc -ansi -pedantic -Wall -Wextra t.c
[arnuld@arch64 programs]$ ./a.out
10
[arnuld@arch64 programs]$

But this will not work:

#include <stdio.h>

struct myStruct
{
  int num;
}var;

int main(void)
{
  var x;
  x.num = -1;
  printf("%d\n", x.num);

  return 0;
}

[arnuld@arch64 programs]$ gcc -ansi -pedantic -Wall -Wextra struct.c
struct.c: In function ‘main’:
struct.c:11:3: warning: statement with no effect [-Wunused-value]
var x;
^~~
struct.c:11:7: error: expected ‘;’ before ‘x’
var x;
^
struct.c:12:3: error: ‘x’ undeclared (first use in this function)
x.num = -1;
^
struct.c:12:3: note: each undeclared identifier is reported only once for each function it appears in
[arnuld@arch64 programs]$

Can you guess why ?

Because /var/ is a myStruct variable, not a type. If you want it to be accessible inside main() then you have to make it a type using typedef, like this:

#include <stdio.h>

typedef struct myStruct
{
  int num;
}var;

int main(void)
{
  var x;
  x.num = -1;
  printf("%d\n", x.num);

  return 0;
}

[arnuld@arch64 programs]$ gcc -ansi -pedantic -Wall -Wextra struct.c
[arnuld@arch64 programs]$ ./a.out
-1
[arnuld@arch64 programs]$

C is a very small language. You can put entire C language in your head if you do few years of serious coding in C. For the standard libraries, you have have H&S5.

Copyright © 2018 Arnuld Uttre, Hyderabd, Telangana – 500017 (INDIA)
Licensed Under Attribution-NoDerivs 3.0 United States (CC BY-ND 3.0 US)

Advertisements

Leave a Comment »

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at WordPress.com.
Entries and comments feeds.

%d bloggers like this: