Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » C programming language

C FAQ - Can we initialize structure members within structure definition?



C programming FAQ: Can we initialize structure members within structure definition? Here, we will also learn how we can initialize the members of structure?
Submitted by IncludeHelp, on May 18, 2018

Can we initialize structure members within structure definition in C?

Que: Can we initialize structure members within structure definition?

No!
We cannot initialize a structure members with its declaration, consider the given code (that is incorrect and compiler generates error).

struct numbers{
	int a=10;
	int b=20;
};

Here, we are initializing a with 10 and b with 20 that will not be executed by the compiler and compiler will return an error.

Let’s see the example and error returns by the compiler

#include <stdio.h>

struct numbers{
	int a=10;
	int b=20;
};

int main()
{
	//structure variable declaration
	struct numbers num;
	
	//printing the structure members 
	printf("a=%d, b=%d\n",num.a, num.b);
	
	return 0;
}


Output

prog.c:4:7: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token
  int a=10;
       ^
prog.c: In function ‘main’:
prog.c:14:27: error: ‘struct numbers’ has no member named ‘a’
  printf("a=%d, b=%d\n",num.a, num.b);
                           ^
prog.c:14:34: error: ‘struct numbers’ has no member named ‘b’
  printf("a=%d, b=%d\n",num.a, num.b);
                                  ^
prog.c:11:17: warning: variable ‘num’ set but not used [-Wunused-but-set-variable]
  struct numbers num;
                 ^~~

How to initialize structure members?

While declaring structure’s variable (object to the structure), we can assign the values of the members.

Syntax:

struct structure_name structure_variable_name ={value1, value2};

Example:

struct numbers num ={10,20};

Correct program:

#include <stdio.h>

struct numbers{
	int a;
	int b;
};

int main()
{
	//structure variable declaration
	struct numbers num ={10,20};
	
	//printing the structure members 
	printf("a=%d, b=%d\n",num.a, num.b);
	
	return 0;
}

Output

    a=10, b=20





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.