i need help because of these constant nulls in C - c

this is my code and I have been getting constant nulls from it so I do need a help
#include<stdio.h>
#include<string.h>
int main () {
char jojo[100];
printf("name: ");
scanf("[^\n]*c", &jojo);
printf("Happy Birthday to %s.\n");
printf("\n");
return 0;
}

For starters in the call of scanf
scanf("[^\n]*c", &jojo);
you shall not use a pointer to the character array. Also you need to use the symbol '%' before the conversion specifiers.
Instead write
scanf("%[^\n]%*c", jojo);
In the call of printf you forgot to specify the argument that represents the array.
printf("Happy Birthday to %s.\n");
write
printf("Happy Birthday to %s.\n", jojo);
Here is a demonstrative program.
#include <stdio.h>
int main(void)
{
char jojo[100];
printf( "name: " );
scanf("%99[^\n]%*c", jojo );
printf( "Happy Birthday to %s.\n", jojo );
return 0;
}
The program output might look like
name: programmer Vaustin
Happy Birthday to programmer Vaustin

You probably want this:
#include<stdio.h>
#include<string.h>
int main() {
char jojo[100];
printf("name: ");
scanf("%s", jojo); // use the %s format specifier
// and remove the &
printf("Happy Birthday to %s.\n", jojo); // you forgot 'jojo' as second
// argument to printf
printf("\n");
return 0;
}
Explanations int the comments.

Related

Problem with the structures and loops in c programming

I wanted to create a program in c that reads the student name, roll number, marks of 3 subjects. when I run the program it is showing no errors, but the problem is whenever I try to input information it is taking only 2 inputs. Anyone please check the program and state the error in my program.
#include <stdio.h>
struct student
{
char sname[20];
int srollno;
int smarks[3];
};
int main ()
{
struct student e[3];
int i,j;
for (i=0;i<=2;i++)
{
scanf ("%s",e[i].sname);
scanf ("%d",e[i].srollno);
for (j=0;j<=2;j++)
{
scanf ("%d",e[i].smarks[j]);
}
}
}
it is taking only two inputs.
you have some problem in your scanf.
Try this:
scanf("%s",&e[i].sname);
I perform some little change on your code.
This is working version of code.
Code
#include <stdio.h>
struct student
{
char sname[20];
int srollno;
int smarks[3];
};
int main ()
{
struct student e[3];
int i,j;
for (i=0;i<=2;i++)
{
printf("Name: ");
scanf("%s", e[i].sname);
printf("roolNumber: ");
scanf("%d", &e[i].srollno);
for (j=0;j<=2;j++)
{
printf("Mark %d: ",j);
scanf("%d", &e[i].smarks[j]);
}
}
printf("\n\nprinting \n\n");
for (i=0;i<=2;i++)
{
printf("Name: %s\n", e[i].sname);
printf("roolNumber: %d\n", e[i].srollno);
for (j=0;j<=2;j++)
{
printf("Mark: %d\n", e[i].smarks[j]);
}
printf("\n");
}
}
Compile and Run
gcc -Wall source.c -o source
./source
I suggest to use printf() before try to scanf(), this makes User Interface better.
scanf() char array don't need & (address) operator. see this
char str[10];
scanf("%s", str);
printf("%s\n", str);
Work exactly like
char str[10];
scanf("%s", &str[0]);
printf("%s\n", str);
Because str is pointer to its first elementstr[0]. As stated in link we can write printf("%d\n", str==&str[0]); and always the result is one that show str and &str[0] are identical.

C: Any Alternative way to get each of string element without using conio.h?

I am trying to write a program to access the name from user and print each of element and it address but can't figure out what happen to my code. If Gcc compiler not support Conio.h library so what should i do? Can I write a program without using conio.h library?.
Please explain it
Here is my code:
//Write a program to find each of string element and it location
#include <stdio.h>
#include<conio.h>
int main(){
char name[10];
int i = 0;
printf("Please enter your name: ");
scanf("%s", name);
while(name[i] != '\0'){
printf("%c is located at %u", name[i], &name[i]);
i++;
}
getch();
return 0;
}
Output: No such file or directory
complination terminated
You should actually display a pointer using the %p format specifier. For more information Correct format specifier to print pointer or address?
#include <stdio.h>
int main(){
char name[10];
int i = 0;
printf("Please enter your name: ");
scanf("%s", name);
while(name[i] != '\0'){
printf("%c is located at %p", name[i], &name[i]);
i++;
}
return 0;
}
Edit: As mentioned in the comment by #David, you should check the return value of scanf also.
if (scanf ("%9s", name) != 1){
fputs ("error: name invalid or EOF\n", stderr);
return 1; }

Just first alphabet is showing in the output.[char type]

When I give the input then only first alphabet is showing.
I want to print the complete name which is I just entered.
#include <stdio.h>
int main()
{
char name;
char grades;
int i;
printf("Name of the Student:");
scanf("%c",&name);
printf("Name your Just entered is : %c",name);
return 0;
}
I agree with the others - but add some error checking and ensure no buffer overruns i.e
#include <stdio.h>
int main() {
char name[101];
printf("Name of the student:");
if (scanf("%100s", &name) == 1) {
printf("Name you just entered: %s\n", name);
return 0;
} else {
printf("Unable to read name of student\n";
return -1;
}
}
EDIT
As you have edited the question so that it does not have the same meaning as before I will leave my previous solution here.
But what you want is to use fgets - this allows for white space in the name
ie.
#include <stdio.h>
int main()
{
char name[100];
printf("Name of student:");
fflush(stdout);
fgets(name, 100, stdin);
printf("Students name is %s\n", name);
return 0;
}
Replace char name; with char name[100];. This will define name as array of chars, because you handled with it as single character.
For scanf replace it with scanf("%s",&name[0]);, and printf with printf("Name your Just entered is : %s",name);. %s means string, so it will scan whole string, not just single character. In scanf &name[0] points to beginning of array.
You need to scanf into an array, rather than into a single character:
#include <stdio.h>
int main() {
char name[100];
printf("Name of the student:");
scanf("%s", &name);
printf("Name you just entered: %s\n", name);
}
You are trying to store a array of characters(string) in a character. So only the first character is taken.To rectify this initialize the name as:
char name[40];
take input as :
scanf("%s",name);
and print as:
printf("name is %s",name);
name is a char and scanf will only catch one character when you use %c. You can use a char array to store the name instead :
char name[40];
/* edit the size for your need */
Also edit your scanf and printf to use a %s
You are reading (and printing) a single char using %c. If you want to handle stirngs, you should use a char[] and handle it with %s:
#include <stdio.h>
int main()
{
char name[100]; /* Assume a name is no longer than 100 chars */
char grades;
int i;
printf("Name of the Student: ");
scanf("%s",&name);
printf("Name your Just entered is : %s",name);
return 0;
}

Get text from user input using C

I am just learning C and making a basic "hello, NAME" program. I have got it working to read the user's input but it is output as numbers and not what they enter?
What am I doing wrong?
#include <stdio.h>
int main()
{
char name[20];
printf("Hello. What's your name?\n");
scanf("%d", &name);
printf("Hi there, %d", name);
getchar();
return 0;
}
You use the wrong format specifier %d- you should use %s. Better still use fgets - scanf is not buffer safe.
Go through the documentations it should not be that difficult:
scanf and fgets
Sample code:
#include <stdio.h>
int main(void)
{
char name[20];
printf("Hello. What's your name?\n");
//scanf("%s", &name); - deprecated
fgets(name,20,stdin);
printf("Hi there, %s", name);
return 0;
}
Input:
The Name is Stackoverflow
Output:
Hello. What's your name?
Hi there, The Name is Stackov
#include <stdio.h>
int main()
{
char name[20];
printf("Hello. What's your name?\n");
scanf("%s", name);
printf("Hi there, %s", name);
getchar();
return 0;
}
When we take the input as a string from the user, %s is used. And the address is given where the string to be stored.
scanf("%s",name);
printf("%s",name);
hear name give you the base address of array name. The value of name and &name would be equal but there is very much difference between them. name gives the base address of array and if you will calculate name+1 it will give you next address i.e. address of name[1] but if you perform &name+1, it will be next address to the whole array.
change your code to:
int main()
{
char name[20];
printf("Hello. What's your name?\n");
scanf("%s", &name);
printf("Hi there, %s", name);
getchar();
getch(); //To wait until you press a key and then exit the application
return 0;
}
This is because, %d is used for integer datatypes and %s and %c are used for string and character types

Writing 2 strings on the same line in C

So I want to make the hello.c program write both the first name and the last name on one line so in this form but when I run my program in this current form it gives me the error "expected â)â before string constant" I think I have the rest of the code down because I have removed that line and ran it and it works. So I just want to ask how to get 2 strings that I have already pointed to to go on the same line.
This is my code
#include <stdio.h>
int main()
{
char firstname[20];
char lastname[20];
printf("What is your firstname?");
scanf("%s", firstname);
printf("What is your lastname?");
scanf("%s", lastname);
printf("Hello %s\n", firstname "%s", lastname);
printf("Welcome to CMPUT 201!");
}
You want
printf("Hello %s %s\n", firstname, lastname);
instead of
printf("Hello %s\n", firstname "%s", lastname);
#include<stdio.h>
#include<string.h>
int main()
{
char first_name[20] = " ", last_name[20] = " ", full_name[40] = " ";
printf("What is your firstname?\n");
scanf("%s", first_name);
printf("What is your lastname?\n");
scanf("%s", last_name);
sprintf(full_name,"%s %s",first_name, last_name);
printf("name is %s\n",full_name);
return 0;
}
I have shown the same using sprintf.
1) also in your program you are not returning anything, if dont want to return anything make it as void function. When you write int function, always make it an habbit to return the integer.
2) Also when you write the printf function always make it habit to add \n(new line) so the output looks good
happy coding.

Resources