#include <stdio.h>
int main(){
printf("asd");
return 0;
}
The task is without modifying this program, overwriting or using define, in windows environment, this program have to write out : Yourname asd Yourname.
Any idea?
You can do it like this (edited to read Yourname from stdin):
#include <stdio.h>
#include <string.h>
int main(){
printf("asd");
return 0;
}
int printf(const char *__restrict __format, ...)
{
char str[106], scs[50];
scanf("%s", &scs);
strcpy(str,scs);
puts(strcat(strcat(strcat(strcat(str," "), __format)," "),scs));
return 1;
}
Here's a working demo.
You can do it in a simple way. Global objects are created on the static memory area, they are allocated and initialized before the execution of main, and are freed after the execution of main. Here's the simple answer to your problem:
#include<iostream>
struct MyName {
public:
MyName() { printf("%s", "GaborRuszcsak\n"); }
~MyName() { printf("%s", "\nGaborRuszcsak\n"); }
};
MyName isGaborRuszcsak;
int main(int argc, char** argv){
printf("asd");
return 0;
}
Hope that helps.
Related
im very new to programming, trying to learn C and cant figure out how to create/use a simple function.
Im trying to create a function called stringtest and then call it into the main and simply make the string strA print ABC.
void stringtest(char strA[20])
{
strA = "ABC";
}
int main()
{
char strA;
stringtest(strA[20]);
printf("This is strA", strA);
return 0;
}
You need to read up on pointers and the C syntax in general.
This is one way you could do it.
#include <stdio.h>
#include <string.h>
void stringtest(char *strA) {
strcpy(strA, "ABC");
}
int main(int argc, const char * argv[]) {
char strA[20];
stringtest(&strA[0]);
printf("This is strA -> %s \n", strA);
return 0;
}
Take care,
/Anders.
I don't think your code ran!!
There are a lot of bugs and errors in your code.
See the code given below to understand how to do this:
#include <stdio.h>
char strA[20];
void stringtest(){
strA[0]='A';
strA[1]='B';
strA[2]='C';
strA[3]='\0';
}
int main(){
stringtest();
printf("This is strA %s",strA);
}
Is there a way to make a function that stores a string, and then call that function in int main() that displays it on the screen? I have been searching a lot and haven't found a clear example. Here is my code. I would like to be able to call it without using the if statement
#include <stdio.h>
/* function declaration */
int StrPrint(char *str);
/* main() function */
int main()
{
char str[] = "The string i am returning n";
int (*ptr)(char *str);
ptr = StrPrint;
if (!(*ptr)(str))
printf("Done!\n");
return 0;
}
/* function definition */
int StrPrint(char *str)
{
printf("%s\n", str);
return 0;
}
The code you've posted is far more complicated than the simple task you're trying to accomplish.
Why not something like this:
#include <stdio.h>
void StrPrint(char* str);
int main(void)
{
char str[] = "The string i am returning n";
StrPrint(str);
return 0;
}
void StrPrint(char* str)
{
printf("%s\n", str);
}
This conflicts slightly with your requirement, in that the function doesn't store a string it just prints out the string passed to it as an argument. But according to the code you posted, this looks like what you're trying to accomplish.
As mentioned by you if you don't like to use pointers it can be done as shown below:
#include <stdio.h>
int StrPrint(char s[])
{
printf("%s\n", s);
return 0;
}
int main()
{
char str[] = "The string i am returning n";
if (!StrPrint(str))
printf("Done!\n");
return 0;
}
I want to parse a string into an array of tokens . '\n' and ';' are delimiters , for e.g. :
hello;hello
world
should be converted to an array containing: {"hello","hello","world"}.
I tried many different methods for doing this and always I fail (since it needs a dynamic array of char * I have trouble with implementing it).
Please note that I cannot use strtok or lexical analyzer.
How may I do this ? Any points ?
EDIT : here is one of methods I tried to use but I get segmentation fault (maybe a memory access issue somewhere in my code) :
#include <stdio.h>
#include <malloc.h>
#include <fcntl.h>
#include <string.h>
typedef struct {
int fd;
char *path;
int size;
char *mem;
struct stat st;
} file;
file *readfile(char *path) {
file *a=malloc(sizeof(file));
a->path=path;
a->fd=open(a->path,O_RDONLY);
if(a->fd<0) return 0;
fstat(a->fd,&a->st);
a->size=a->st.st_size;
a->mem=malloc(a->size);
read(a->fd,a->mem,a->size);
return a;
}
void releasefile(file *a) {
free(a->mem);
close(a->fd);
free(a);
}
char **parse(int *w,file *a) {
int i,j=0;
w=0;
for(i=0;i<=a->size;i++) {
if(a->mem[i]=='\n' || a->mem[i]==';') { a->mem[i]='\0'; j++; }
}
char **out=malloc(sizeof(char *)*j);
for(i=0;i<=a->size;i++) {
if(a->mem[i-1]!='\0') continue;
out[*w]=malloc(strlen(a->mem+i)+1);
memcpy(out[*w],a->mem+i,strlen(a->mem+i)+1);
w++;
return out;
}
int main(int argc,char **argv) {
file *a=readfile(argv[1]);
int *w=malloc(sizeof(int));
char **tokens=parse(w,a);
int i;
for(i=0;i<=*w;i++) {
puts(tokens[i]);
}
releasefile(a);
// ATM no need to check for mem leaks :)
}
Algorithm description : read file, put \0 where you see a delimiter, start and push tokens seprated by \0 into an array.
What has happened to computer science?
Anyway write a FSA - http://en.wikipedia.org/wiki/Finite-state_machine
Can do this using a table
I'm a beginner in C language. After reading the initial chapters of Ritchie's book, I wrote a program to generate random numbers and alphabets.
The program compiles fine with gcc. However on running it, it gives an error "Segmentation fault", which is incomprehensible to my limited knowledge. I'd be glad to understand what I've written wrong.
#include <stdio.h>
#include <stdlib.h>
#include "conio.h"
#include <time.h>
long int genrandom(int,int);
void randAlph(void);
char letterize(int);
int main (void) {
// char full[9];
// char part_non[4];
srand(time(0));
int i;
for (i=0;i<50;++i) {
randAlph();
};
}
long int genrandom(int mino,int maxo) {
int val=mino+rand()/(RAND_MAX/(maxo-mino)+1);
return val;
}
void randAlph (){
int val;
char text;
val=genrandom(0,26);
// return val;
text=letterize(val);
printf("%s ,",text);
}
char letterize(int num) {
char letter='A'+num;
return letter;
}
printf("%s ,",text); is wrong - it says that text is a nul-terminated array of chars. Use
printf("%c ,", text);
instead to print your single char.
#include <stdio.h>
#include <stdlib.h>
#include "conio.h"
#include <time.h>
int genrandom(int,int);
void randAlph(void);
char letterize(int);
int main (void) {
// char full[9];
// char part_non[4];
srand(time(0));
int i;
for (i=0;i<50;++i) {
randAlph();
};
}
int genrandom(int mino,int maxo) {//changed function return type to int
int val=mino+rand()/(RAND_MAX/(maxo-mino)+1); //Be careful when you are using '/' operator with integers
return val; //returning int here why set return type to long int?
}
void randAlph (){
int val;
char text;
val=genrandom(0,26);
// return val;
text=letterize(val);
printf("%c ,",text);//Replace %s with %c
}
char letterize(int num) { //No bound checking on num eh?
char letter='A'+num;
return letter;
}
That's all I had to say. :)
Why use %s when text is char. You dont need a string type in the function. Just a char would do. Change in the function : void randAlph ()
printf("%s ,",text);
to
printf("%c ,", text);
How to print the environment variables in a C program using environ.
extern char **environ
#include <unistd.h>
#include <stdio.h>
extern char **environ;
//...
int i = 0;
while(environ[i]) {
printf("%s\n", environ[i++]); // prints in form of "variable=value"
}
Do you mean
int main(int argc, char **argv, char **envp)
{
while(*envp!=null) {
printf("%s\n", *envp);
envp++;
}
return 0;
}