What is the Correct Way to Initialize a Character in C Programming?

In summary, The conversation involves a program that reads input data and displays them all at once. However, there is an issue with the scanf function that causes the program to close immediately after the first input. The issue is resolved by using a string instead of a single character variable.
  • #1
nard
16
0
the foolowing program is to read inputed data and display them all at once but when i run it it asks for the first information and then runs the total program without asking for the input of other information.after that it doesn't respond and closes immediatelly.
can someone help me and tells me what's wrong with my program?
thanks for your help.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct database {
char name;
int phone_number,id_numb;
};
main()
{
struct database ident;
printf("enter a name\n");
scanf("%c",&ident.name);
printf("enter a phn numb\n");/* enter a phone number*/
scanf("%d",&ident.phone_number);
printf("enter the id_numb\n");/*enter an id number*/
scanf("%d",&ident.id_numb);
printf("the identity of %s is:\n",ident.name);/*displays the name*/
printf("phone:%d id:%d \n",ident.phone_number,ident.id_numb);/*displays the phone number and the id number*/
system("PAUSE");
}
 
Technology news on Phys.org
  • #2
nard said:
scanf("%c",&ident.name);
the scanf("%c",...) is the issue. This reads a single character into the single character variable name. If you enter more than one character, scanf will attempt to handle those with the following scanf()'s.

Perhaps you'd want to use a string, which will solve the problem:

char name[128];

scanf("%s", ident.name);

printf( "... %s ...", ident.name);
 
  • #3
I saw where i was making an error.
i corrected and while initialising the character i defined as an array and that way in the printf and scanf functions instead of using %c i used %s.after that the program is compiled and run without complications
 

1. What are structures in c programming?

Structures in c programming are user-defined data types that allow you to combine different data types into a single unit. They are also known as "structs" and are used to group related data together for easier organization and access.

2. How do you declare a structure in c programming?

To declare a structure in c programming, you use the struct keyword followed by the name of the structure and a set of curly braces. Inside the curly braces, you define the members of the structure using their corresponding data types.

3. How do you access members of a structure in c programming?

To access members of a structure in c programming, you use the dot operator (.) followed by the name of the structure and the name of the member. For example, if you have a structure called student with a member name, you would access it using student.name.

4. Can a structure contain another structure as a member?

Yes, a structure in c programming can contain another structure as a member. This is known as nested structures and allows for more complex data organization and manipulation.

5. How do you pass a structure to a function in c programming?

To pass a structure to a function in c programming, you can either pass it by value or by reference. When passing by value, the function receives a copy of the structure, while passing by reference allows the function to directly modify the original structure. To pass by reference, you use a pointer to the structure as the argument in the function call.

Similar threads

  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
2
Views
933
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
2
Replies
47
Views
4K
  • Programming and Computer Science
Replies
30
Views
2K
  • Programming and Computer Science
Replies
6
Views
5K
  • Programming and Computer Science
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Programming and Computer Science
Replies
3
Views
998
Back
Top