I tried to include <windows.h> with also <stdio.h> but it shows: tcc: undefined symbol '_MessageBoxA#16' when i use my only Pocket Edition TinyCCompiler. does anybody know why?
#include <stdio.h>
#include <winapi/windows.h>
#include <winapi/winuser.h>
/* ooops fell of my chair */
int main()
{
char name[40];
char age[6];
printf("Enter name:");
scanf("%39s", name);
printf("\n");
printf("Enter age:");
scanf("%5s", age);
printf("\n");
printf("You: Hello my name is %s, and i'm %s years old.",name,age);
printf("\n----------------OTHER PROGRAM----------");
MessageBox(0, "Hello", "Hellodd", 1);
return 0;
}
You need to link with user32:
tcc -Wall code.c -luser32
Related
#include <cs50.h>
#include <stdio.h>
int main(void) {
string answer = get_string("what is your name? ");
printf("%s\n", answer);
}
I have a program with this code, that is running well, every other program that asks for input does not run rightly. I am on windows 10, I used MINGW to compile the program.
You could try it this way
#include<string.h>
#include <stdio.h>
int main(void) {
char answer[20];
printf("What is your name");
gets(name);
printf("%s\n", answer);
}
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.
Console in CLion won't print line after scaning input, but in iTerm after compiling it all works perfectly.
Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
char personName;
printf("Hello, what is your name?\n");
scanf("%s", personName);
printf("Hello, %s\n", personName);
return 0;
}
I just got this in CLion console:
Hello, what is your name?
Mike
Process finished with exit code 11
You need to use an array for personName. The code will be,
#include <stdio.h>
#include <stdlib.h>
int main() {
char personName[32];
printf("Hello, what is your name?\n");
if(scanf("%s", personName))
printf("Hello, %s\n", personName);
return 0;
}
The code is running in code blocks why its not runninng in visual studio 2015?
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
int main() {
char name[20];
char age[36];
char sentence[40];
puts("my name is");
gets(name);
puts("my age is");
gets(age);
strcat(sentence, "My name is ");
strcat(sentence, name);
strcat(sentence, " My age is ");
strcat(sentence, age);
puts(sentence);
_getch();
return 0;
}
From https://msdn.microsoft.com/en-us/library/bb531344.aspx:
gets and _getws
The gets and _getws functions have been removed. The gets function was
removed from the C Standard Library in C11 because it cannot be used
securely. The _getws function was a Microsoft extension that was
equivalent to gets but for wide strings. As alternatives to these
functions, consider use of fgets, fgetws, gets_s, and _getws_s.
And strcat wants a NUL-terminated string as destination, change to:
char sentence[40] = {0};
...
strcat(sentence, "My name is ");
I seek for an expert in c programming. Thanks in advance.
Example:
#include <stdio.h>
#include<stdlib.h>
#include<ctype.h>
void main()
{
char name[100][52],selection[2]="Y";
int x,nname=1;
float sales;
do
{
printf("Enter name: ");
fflush(stdin);
fgets(name[nname],51,stdin); // i need put a wrapper in here
printf("Enter sales: ");
scanf("%f",&sales);
if (sales<1000)
printf("%s\tgood\n",name[nname++]);
else
printf("%s\tvry good\n",name[nname++]);
printf("Enter another name?(Y/N)");
fflush(stdin);
fgets(selection,2,stdin);
*selection=toupper(*selection);
}while(nname<=100 && *selection=='Y');
for(x=1;x<nname;x++)
printf("%s\n",name[x]); // want print the result without(newline) /n
printf("END\n");
system("pause");
}
How do I print the names without being separated by new lines?
I compiled it with GCC 4.4.1 - MinGW and it works fine.
It launched only a warning. This is the result:
warning: return type of 'main' is not 'int'|
||=== Build finished: 0 errors, 1 warnings ===|
Now it works as you expect.
#include <stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include <string.h> // strlen()
void main() {
char name[100][52],selection[2]="Y";
int x,nname=1;
float sales;
do {
printf("Enter name: ");
fflush(stdin);
fgets(name[nname],51,stdin); // i need put a wrapper in here
for (x=0; x<strlen(name[nname]); x++){ // this will discarge the \n
if (name[nname][x] == '\n')
name[nname][x] = '\0';
}
printf("Enter sales: ");
scanf("%f",&sales);
if (sales<1000)
printf("%s\tgood\n",name[nname++]);
else
printf("%s\tvry good\n",name[nname++]);
printf("Enter another name?(Y/N)");
fflush(stdin);
fgets(selection,2,stdin);
*selection=toupper(*selection);
} while(nname<=100 && *selection=='Y');
for(x=1; x<nname; x++)
printf("%s ",name[x]); // want print the result without(newline) /n
printf("\nEND\n"); // inserted \n before END
system("pause");
}
Just use
printf("%s ", name[x]);
instead of
printf("%s\n", name[x]);
The \n character is creating the new lines.
Edit
fgets apparently reads newlines into the buffer - you can strip the newline with
name[nname][strlen(name[nname])-2] = '\0';