TRENDING NEWS

POPULAR NEWS

Char Name[20]; Printf

Write a C++ Program which will ask you for names and print them in alphabeticle order.?

To really do this is C++, it would seem more appropriate to use C++ data structures
like a Vector of Strings (rather than a 2-d array of chars). Oh well. Here's a simple
example:


#include
#include
#include

#define NAMELEN 16

char nameStrings[20][NAMELEN];
char *names[20];

void
readNames(int n)
{
  for (int i=0; i    names[i] = &nameStrings[i][0];
    fgets(names[i], NAMELEN, stdin);
  }
}

void
writeNames(const char* titleString, int n)
{
  printf("%s\n", titleString);
  for (int i=0; i    printf("%s", names[i]);
}

int
compareNames(const void*s1, const void*s2)
{
  char *p1 = *(char**)s1;
  char *p2 = *(char**)s2;
  return strncasecmp(p1, p2, NAMELEN);
}


int
main(int argc, char* argv[])
{
  char numBuffer[20];
  int numNames = 0;

  printf("How many names?");
  fgets(numBuffer, sizeof(numBuffer), stdin);
  numNames = atoi(numBuffer);
  if (numNames < 0 || numNames > 20)
    printf("Number of names must not exceed 20\n"), exit(-1);

  readNames(numNames);
  writeNames("Original list of names", numNames);
  qsort(names, numNames, sizeof(char*), compareNames);
  writeNames("\nSorted list of names", numNames);
  return 0;
}


And to show that it performs the comparison without respect to case:

$ g++ -g names.c
$ a.out
How many names?3
sTan
paul
Stan
Original list of names
sTan
paul
Stan

Sorted list of names
paul
sTan
Stan

#include #include struct student{                 char name[10];                 int rollno;                 float marks;                 }; main()  { struct student stdnt1= {"bikash",10,84}; struct student stdnt3; struct student stdnt2={"saheli",07,92}; stdnt3=stdnt2; printf(" stdnt1 : %s %d %f",stdnt1.name,stdnt1.rollno,stdnt1.marks); printf(" stdnt2 : %s %d %f",stdnt2.name,stdnt2.rollno,stdnt2.marks); printf(" stdnt3 : %s %d %f",stdnt3.name,stdnt3.rollno,stdnt3.marks);  }

Ensure that you have correctly installed and configured GCC compiler. Problem might occur due to the problem with GCC compiler .

C: what's wrong with "char name[20]="Seattle",(*ctr)[]=na... it wrong way to declare a string pointer?Why?

Please consider the following little program and clear the 2 confusions I encountered due to it thereafter.


#include

int main()
{

char name[20]="Seattle",(*ctr)[]=name; //Shows warning
//char name[20]="Seattle",(*ctr)[]=&name; //This works fine without warning
*ptr=name; //works fine as expected


printf("%s,%s",ptr,ctr);

}

OUTPUT: Seattle,Seattle

Confusions:

1)Is "char (*ctr)[]=&name;" also a correct way to declare a pointer to a string,other than the conventional way?I base it on my argument on the fact that we declare a pointer to integer array as "int (*ptr)[]"? If not, then why in the program it works fine as a string pointer WITHOUT a warning?PLZ shed some light on it.

2)Why does taking out the "&" above as "char (*ctr)[]=name;" instead of "char (*ctr)[]=&name;" shows a warning (even though output is same)?If there has to be warning, shouldn't it be for the case where we use "&" before a string name?I mean, why does "char (*ctr)[]=name" show warning while "char (*ctr)[]=&name" doesn't even though we are supposed to give only string names to string pointers?

3)Why does "char (*ctr)[]=&name;" work at all?That too without even a warning? Isn't it silly to use a "&" before a sting name in assigning address because the string name "name" itself is the base address and we assign string names to string pointers without using "&" as we do with variables?

PLEASE give your answers

How can I print my name in C language without using printf and scanf?

You can print in c language without using printf() function .include
int main(){
puts("XYZ"); //your name between quotation marks.
return 0;
}
here the function puts() is used for displaying standard outputOUTPUT: XYZ
A2A !

How to print the Spaces on Turbo C?

Most characters in between the quotes "sdfsdf sdfsdfsdf" are printed the same way to the screen.

so after the %s type a space.

I think I know your problem, though you didn't ask the question clearly enough.

Your scanf is NOT working like you expeted. scanf ("%s") only scans ONE word at a time.

If you typed in "John Smith", the scanf("%s") only scans "John". So your printf was not the problem.
there are other ways to scan in a complete line of text that you type.

The easiest function is gets
http://www.cplusplus.com/reference/cstdio/gets/

Good luck.

How to accept string with space in char array in C?

- Avoid using scanf with "%s".
- Avoid gets().

See these links for more information:
http://c-faq.com/stdio/getsvsfgets.html
http://c-faq.com/stdio/scanfprobs.html

#include

static int readline(char *s, size_t size)
{
        int c;

        size--;
        while ((c = getchar()) != EOF && c != '\n')
                if (size) {
                        *s++ = c;
                        size--;
                }
        *s = '\0';
        return c == EOF;
}

struct fileinfo {
        char file[20];
        char description[100];
};

int main(void)
{
        struct fileinfo fl[10];
        size_t i;
        size_t n;

        for (i = 0; i < sizeof fl / sizeof fl[0]; i++) {
                printf("enter file name\n");
                if (readline(fl[i].file, sizeof fl[0].file))
                        break;
                printf("enter description\n");
                if (readline(fl[i].description, sizeof fl[0].description))
                        break;
        }
        for (n = 0; n < i; n++) {
                puts(fl[n].file);
                puts(fl[n].description)...‡
        }
        return 0;
}

edit: Enrolling Indian students in courses on C is like releasing a swarm of moths into a room of burning candles. I don't see how concepts like the danger of gets() is difficult to grasp. You'd think that the fact that it was deprecated in C11 would lead to a reduced use of it, but Indians are clearly ignorant of C standards; they consider the behaviour of their beloved, outdated Turbo C to be the same across all implementations of C - from "sizeof (int)" to the availability of "conio.h".

If name N & number M are passed as input, what is the C program to print "N won M medals"?

Here is the program…#include
void main()
{
char N[100];
int M;
clrscr();
printf("enter the name\n");
scanf("%s",N);
printf("enter the number of medals\n");
scanf("%d",&M);
printf("%s won %d medals",N,M) `
}

How do I write a program in C to accept a name and display it during the execution of the programme?

Okay, first of all we will allocate the memory for storing the name we will store it in ‘name’ array which is of char type.#include

int main()
{
char name[20]; //I hope the user is not Russian :-P
//I've allocated 20 bits you can allocate as much as you want
printf("Enter your name: ");
//We will use fgets to store the input as gets is a dangerous function and can overload memory buffers
fgets(name,20,stdin);
printf("%s",name); //display the name
return 0;
}
So this is how you can input and display the name during runtime.Happy Coding!

How can we read the string ("firstname lastname") with spaces in C except using scanset and gets?

W can read a string with spaces using scanf("%[^\n]s ",name);                                    Here we can store both the first name and last name with space, of a person. Actually, using the above scanf statement function we can read any string without a newline in it. You can use anything at the place of x in the following scanf statementscanf("%s[^x]s",name);                                   For example if you use b at the place of x then it will read a string until it encounters b in the string, if present.

How can I write a c program which will print the input within a pre formed sentence, as if I enter my name as 'xyz', it must print 'hi xyz, nice to meet you'?

To print to the console in C programming language we use “printf()” statement.the prototype for printf function isint printf(const char *format, …);
format is the pointer to char (actually a string).“…” are called ellipses all the remaining arguments are captured.In the format string we specify the pre-formed sentence along with some format specifiers. The place of these format specifiers are filled with the corresponding data from the arguments passed to ellipses (first format specifier with second argument of printf, second format specifier with third argument of printf and so on). As the data stored in the computer is 1’s and 0’s, the format specifiers tell how to interpret those 1’s and 0’s.format specifier for character - %cformat specifier for integer - %dformat specifier for floating point - %fformat specifier for string(null(‘\0′) terminated character arrays) - %sRequired program is:#include
#define SIZE 100

void main() {
/* variable input to store the input from the user */
char input[SIZE];
printf("Enter a string: ");
/* scanning the input to the array input */
scanf("%s",input);
printf("\'hi %s, nice to meet you\'", input)
}
NOTE: take care the scanf takes space as delimiter, so only a single word can be scanned into input.

TRENDING NEWS