Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x

C

22K

Solutions

13K

Contributors

C is a general-purpose, imperative computer programming language, supporting structured programming, lexical variable scope and recursion, while a static type system prevents many unintended operations. By design, C provides constructs that map efficiently to typical machine instructions, so it has found lasting use in applications that had formerly been coded in assembly language, including operating systems as well as various application software for computers ranging from supercomputers to embedded systems. It is distinct from C++ (which has its roots in C) and C#, and many later languages have borrowed directly or indirectly from C.

Share tech news, updates, or what's on your mind.

Sign up to Post

Hi all,
I am comletely new in c programing,
I have a server client program in c , that I use thread in it,
Client send a sentence and server convert it to upper case and send to client,
My question is:
How can in server find which client connect to server, for example :
If a request receive from client 1 ,server send hello client 1 , and for client 2, it send hello client 2, and etc,

Very thanks for your helps,

My code is
Thanks dear,
My code in server is :
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>

int main(){
  int welcomeSocket, newSocket, portNum, clientLen, nBytes;
  char buffer[1024];
  struct sockaddr_in serverAddr;
  struct sockaddr_storage serverStorage;
  socklen_t addr_size;
  int i;

  welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);

  portNum = 7891;
  
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_port = htons(portNum);
  serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  

  bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));

  if(listen(welcomeSocket,5)==0)
    printf("Listening\n");
  else
    printf("Error\n");

  addr_size = sizeof serverStorage;

  /*loop to keep accepting new connections*/
  while(1){
    newSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);
    /*fork a child process to handle the new connection*/
    if(!fork()){
      nBytes =

Open in new window

0
What does it mean to be "Always On"?
LVL 5
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Dear all,

I have an open code of MFC made in 2002. Could someone please tell me approximately which is the version of this script?

I imagine it is Microsoft C or Microsoft C++, but which version if was compiled in 2002?

Thanks

mfc.jpg
0
If i want to create a database of students Information using structure and store in FILE,how to count how many student's information are there in that FILE using C language?
0
My Code:

#include <stdio.h>

int main(int argc, char *argv[]){
  char line1[128];
  char line2[128];
  char line3[128];
  char rem_text[128];
  FILE *f;

  f = fopen((argv[1]), "r");
  if (!f) {
     printf("error");
  } else {
      fscanf(f, "%127[^\n]\n%127[^\n]\n%127[^\n]\n%127[^\n] ", line1, line2,line3, rem_text);
      printf("1:%s\n", line1);
      printf("2:%s\n", line2);
      printf("3:%s\n", line3);
      printf("4:%s\n", rem_text);
      fclose(f);
  }

  return 0;
}

Open in new window

The Program works almost as expected, except for one detail. What it is supposed to do is, take the first three lines of a file and store them in individual variables. (which works) but then I would like to store the entire remaining bit of the file into its own variable. How do I do that? Thanks in advance

If for example my test.txt file contains:
`Kapitel 1`
`chapter_21.txt`
`chapter_42.txt`
`'Would you tell me, please, which way I ought to go from here?'
'That depends a good deal on where you want to get to,' said the Cat.
'I don't much care where -' said Alice.
'Then it doesn't matter which way you go,' said the Cat.
'- so long as I get SOMEWHERE,' Alice added as an explanation.
'Oh, you're sure to do that,' said the Cat, 'if you only walk long enough.'`

I would like to store` Kapitel 1` as `Title`

`chapter_21.txt` as `chapter_a`

`chapter_42.txt` as `chapter_b`

and

`'Would you tell me, please, which way I ought to go from here?'
'That depends a good deal on where you want to get to,' said the Cat.
'I don't much care where -' said Alice.
'Then it doesn't matter which way you go,' said the Cat.
'- so long as I get SOMEWHERE,' Alice added as an explanation.
'Oh, you're sure to do that,' said the Cat, 'if you only walk long enough.'` as `rem_text`
0
Hi,

I am developing an application for "Bus Driver" where one of important feature is to calculate Odometer inside Android app itself.

Problem Statement : My odometer calculation is working on Google Nexus 10" Tablet and but it is not working on Acer Iconia 10" Tablet.

I have used different method to calculate distance between two different (Latitude, Longitude)

Method 1 : Haveshine Formula
double dlon = Math.toRadians((vCurrent_lon - vLast_lon));
                        double dlat = Math.toRadians(vCurrent_lat - vLast_lat);

                        Log.i("CalcDistance ", " 2 ");

                        double a = Math.pow(Math.sin(dlat / 2), 2) + Math.cos(Math.toRadians(vCurrent_lat)) * Math.cos(Math.toRadians(vLast_lat)) * Math.pow((Math.sin(dlon / 2)), 2);
                        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

                        double vRADIUS_OF_EARTH = 3961;

                        //vDistanceBetweenTwoLocation = 3961 * c;
                        vDistanceBetweenTwoLocation = vRADIUS_OF_EARTH * c;

                        //================= Rounding Odometer upto 02 digits ==================================
                        Lib_Yukti_Common_Util vLib_Common = new Lib_Yukti_Common_Util();
                        vDistanceBetweenTwoLocation = vLib_Common.Round(vDistanceBetweenTwoLocation);
                        //====================================================================================
Method 2  : distanceTo

public static final float calculateDistanceTo(Location fromLocation, Location toLocation) {
    return fromLocation.distanceTo(toLocation);
}



Thanks in Advance
0
HI!
I was going to work with sounds in C. I just wanted to know if this is possible to write my own "library" with the standart libraries of C
as i don`t want to obey any rules of sound libraries out there like OpenAl, PortAudio, BASS etc. I really want to do some work by myself which handles just microphone of my laptop, record  voice, keep it in a "buffer" and play it, that`s all. Nothing else.
Can you guys PLEASE help, where and how to start sharing your experience with me?
Any suggestions are very much apprecieted!!!
0
So i have a program which filters a file containing "emails" in a single line, i link you to the exercise (it is a pdf) https://training.olinfo.it/#/task/ioit_spam/statement so my problem is that it doesn't accomplish all of the 4 tasks but from my tests (i've created a file with 1000 emails) it should correctly and pass all of its cases. i don't know if it's a problem of the corrector on the site (you need to register to test the program) but i link here the scoring (it should score 100 in theory) it might be a problem of the corrector? https://imgur.com/a/QmeY5 and the source file is this https://pastebin.com/iVPXUjxb
Thanks to everyone answering i hope i've made the question clear, the program works in my machine, it can even scan 10000 emails if i'd want to, infact i've tried filtering a 8mb file containing 1000 emails with 1000 words each.
this is the link for the 1000 emails input file https://drive.google.com/file/d/1f9OWkzZ_i9T8s6BcNIq5hXqBogCR2Dcj/view?usp=sharing
0
I have a C program and when I press ctrl +d right after the program starts, it ends in an infinite loop. Should I press ctrl +d in the course of the program it still gives me an output.

Is there a way to make the program exit without any output when CTRL +D is pressed

This is my code:

         ...
    
       {
            int repeats = 0, counter = 0;
            //Amount of triangles
            repeats = checkInput0();
            int i;
            
            
            // scanf("%d", &repeats);  //num is the user input
            for(i=0; i<repeats; i++)
            {
                //printf("enter first number");
                a_arr[i] = checkInput1();
                //printf("enter first number");
                b_arr[i] = checkInput2();
                //printf("enter first number");
                c_arr[i] = checkInput3();
            }
            
            
            float a_sqrd;
            float b_sqrd;
            float c_sqrd;
            
            
            //     checking if given triangle is valid
            do {
                a = a_arr[counter];
                b = b_arr[counter];
                c = c_arr[counter];
                a_sqrd = a*a;
                b_sqrd = b*b;
                c_sqrd = c*c;
                
                if((a + b > c) && (a + c > b) && (b + c > a))
                {
                    // Checking for special cases of triangle
                    
                    if(a==b && b==c)

Open in new window

0
how can i produce sound or play a music in codeblocks using c language ?
0
I will be given a set of X/Y points and their corresponding Z values. Z = f(X, Y), but I do not know the function f. The points will be clustered together and I don't expect pathological conditions. The local maximums and minimums will have extra sample points to help make the interpolation better. The goal is to come up with a smooth surface that represents the function f as close as possible.

The analyst providing me with the set of points of known XYZ points will also give me an XY rectangular grid; and at all the intersection points of horizontal and vertical lines, I need to provide a z-value.

One thought I had was to consider a point and look at 4 quadrants, and take the closest point in each quadrant, and using those 4 points or more to try to interpolate. Prefer to be able to use a curved surface to get the results rather than linear planes. One rule is that the final results will match the given XYZ points . The given points are known as anchor points or knots.

I am just looking for something functional. I am not interested in speed.

 I am not allowed to use binaries, so I will have to build from source code in Linux or Windows.

Thanks in advance.
Paul
0
Industry Leaders: We Want Your Opinion!
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

I am writing a program in C to mimic a simple shell interface however I have a small bug in my code which I cannot figure out. When I run the program in background it starts to execute but when I press enter (instead of prompting me to enter a new command), it goes in an infinite loop which I dont understand why. Can someone please tell me what I am doing wrong here?

Replit link for the program https://repl.it/MrPH/3

/*
Program to mimic linux shell interface
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>

#define MAX_LINE 80 /* The maximum length command */

int main(void){
  char *args[MAX_LINE/2 + 1]; /* command line arguments */
  int should_run = 1; /* flag to determine when to exit program */
  char user_input[MAX_LINE]; 
  int i = 0;
  int background = 0;
  while(should_run){
    printf("OSH> ");
    fflush(stdout);
    scanf ("%[^\n]%*c", user_input);

    i = 0;
    args[i] = strtok(user_input," ");
    while (args[i] != NULL){
      i++;
      args[i] = strtok(NULL, " ");
    }

    if(strcmp(args[0], "exit") == 0) /* exit command */
      break;

    if(strcmp(args[i-1], "&") == 0){ /* if & is included process running in background*/ 
        args[i-1] = NULL;
        background = 1;
    } 

    pid_t pid; 
    pid = fork(); /* fork child process */
    if(pid < 0){  //if fork failed due to some reason 
        return 1;
    }
    
    if (pid == 0){
        execvp(args[0],args); /* execute the 

Open in new window

0
When a user performs a system call such as ...  long enc_file(const char *filename, const char *data, long len)

How do you get those arguments inside kernel space?  Basically I have to creat(filename) with *data for as long as len
0
The <Time.h> for MSP430 lack asctime_c() function, is there way to transfer asctime into char data[] instead?    

[code]char date[20];
zprintf("%s\n", asctime(time_info));    

How to put asctime into char data[]

Thanks.
0
How to pass the augument within zprintINFO(.....) to zprintf(.....)

This is for MSP430 and PIC24 code. zpintf is custom printf code to limits code space. I have repeating text INFO


//==================================================================
//==================================================================zprintf
// Purpose  :
// Input    :
// Output   :
// Note     :
//================================================================
void zprintfINFO(char *format, ...)
{
    zprintf(format,...);
}

//==================================================================
//==================================================================zprintf
// Purpose  :
// Input    :
// Output   :
// Note     :
//================================================================
void zprintf(char *format, ...)
{

Open in new window

0
How to insert image or picture in turbo c
Plz me
0
api-ms-win-crt-runtime-l1-1-0.dll
cairo-vs14.dll
croco-0.6-vs14.dll
gdk_pixbuf-2-vs14.dll
gio-2-vs14.dll
glib-2-vs14.dll
gmodule-2-vs14.dll
gobject-2-vs14.dll
libxml2.dll
pango-1-vs14.dll
pangocairo-1-vs14.dll
pangowin32-1-vs14.dll
rsvg-2.0-vs14.dll
vcruntime140.dll
zlib1.dll

I want to merge these dll's into single dll, Please help me
0
Hello ,


Any body knows a sample for re-calculate checksums and drop packets on netfilter module ?

Thank you
0
Hello ,

We are building an IDS/IPS/Waf system that reads the packets from PF_Ring and apply rules to IPTables / Netfilter / Ntuple .
That software needs to access to userspace for iptables and kernelspace for netfilter. So we already pipe the both side.
But our main concern is about the performace issue. Time is our biggest enemy and need to solve the problem in mikro / mili seconds.
So where is the best place working area for our  main core of software for performance ? kernel area or user area ? and why .

Thanks for information
0
Hello ,

I want to know if there is a possible way to apply BPF filter to drop packets on netfilter  


Thank you



#define __KERNEL__
#define MODULE
#include <linux/ip.h>             
#include <linux/kernel.h> 
#include <linux/module.h> 
#include <linux/netdevice.h>      
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h> 
#include <linux/skbuff.h>         
#include <linux/udp.h>      

#include <linux/ip.h>


              
static struct nf_hook_ops netfilter_ops;                        
static unsigned char *ip_address = "\x5D\xBB\xCD\x73"; 
static char *interface = "p1p2";                          
unsigned char *port = "\x00\x17";                       
struct sk_buff *sock_buff;                              
struct udphdr *udp_header;                              
unsigned int main_hook(unsigned int hooknum,
                  struct sk_buff **skb,
                  const struct net_device *in,
                  const struct net_device *out,
                  int (*okfn)(struct sk_buff*))
{
  if(strcmp(in->name,interface) == 0){ return NF_DROP; }  
  struct iphdr* iph = ip_hdr(skb);
  if(iph->saddr == *(unsigned int*)ip_address){ return NF_DROP; }   
        
        
        
	/**sock_buff = *skb;
	if(!sock_buff){ return NF_ACCEPT; }                   
	if(!(iph)){ return NF_ACCEPT; }              
	if(iph->protocol != 17){ return NF_ACCEPT; }                 
	udp_header = (struct udphdr *)(sock_buff->data + (iph->ihl *4)); 
	

Open in new window

0
Concerto Cloud for Software Providers & ISVs
LVL 5
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Hello

Is there any body knows ready to use sub routine when we send an array it will draw sth. like that :

value |-------------------------------------------------- count
    0 |                                                     32167
    1 |                                                      9999
    2 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  1723569
    4 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@                        973472
    8 |@@@                                                 111055
   16 |                                                     14777
   32 |                                                       376
   64 |                                                       585
  128 |                                                        19
  256 |                                                         0
  512 |                                                         0

Open in new window

0
//************************** tracks indexs*******************
/*
each track saved in sd card has index , so to play track number we will using it's index 
for example to play track number 1 we will using "00003"  index which will send to play track function 
all the same for the rest of all tracks 
*/ 
char buffer15[6]={"00001"};  // track 15 index 
char buffer16[6]={"00002"};  // track 16 index
char buffer1[6]={"00003"};   // track 1 index
char buffer2[6]={"00004"};   // track 2 index
char buffer3[6]={"00005"};   // track 3 index
char buffer4[6]={"00006"};   // track 4 index
char buffer5[6]={"00007"};   // track 5 index
char buffer6[6]={"00008"};   // track 6 index
char buffer7[6]={"00009"};   // track 7 index
char buffer8[6]={"00010"};   // track 8 index
char buffer9[6]={"00011"};   // track 9 index
char buffer10[6]={"00012"};  // track 10 index
char buffer14[6]={"00013"};  // track 14 index

//*************************

Open in new window


How can I access these mp3files via filename not index
0
Hi, new member here and first post. My goal is to create a basic tab control using the Win32 API that contains a canvas for rendering OpenGL. Please see attachment for my source code. I created a tab control with two tabs; the first tab contains a button and the second tab contains the rendering context. The first tab, when active, successfully displays a button. The second tab, when active, does not display the OpenGL canvas. The only way I can get the canvas to appear in the GUI is to exclude the tab control (comment out the CREATE_TAB_PANE macro in my example to do this). I am rendering the OpenGL context within a static control. Thanks in advance for your help.
OpenGlTabWin32.cpp
0
what is c language?what is it's necessity.?
0
Hi,

This is Ravi...

So far I read everything mentioned above...so I request you to provide one end to end example to do the same... because I am completely new to it...
0
I am in the process of moving legacy codes in c to Intel 64 (ksh) from Unix Tru64 (ksh).  
 I created a basic hello world application in c so I could setup the debugger.   INTEL has instructed me to use the gdb-ia debugger.   I don't know where any manuals are to read this for myself.   I have gotten a few errors along the way that have lead me to add the following lines to my ksh users .profile.  
 export LD_LIBRARY_PATH=/opt/intel/debugger_2017/libipt/intel64/lib
 export PYTHONHOME=/usr
 Currently I am getting an error:  $>gdb-ia ImportError: No module named site
 With every question I ask INTEL, it takes me a business day to get an answer.  So, I've had great luck with Experts Exchange in the past, so I'm back to get some direction.  
 My last correspondence with INTEL was:  
      "Looks like you forgot to source the environment."
        "#source /opt/intel/bin/compilervars.sh intel64"

 Where am I going to add this line of code?   The "#source" makes me think it would go into an include file somewhere.   Can anyone see through this and give me some advice???
 I also have another post out there for mapping Tru64 compiler options to INTEL.  

 Thanks everyone for your help!
0

C

22K

Solutions

13K

Contributors

C is a general-purpose, imperative computer programming language, supporting structured programming, lexical variable scope and recursion, while a static type system prevents many unintended operations. By design, C provides constructs that map efficiently to typical machine instructions, so it has found lasting use in applications that had formerly been coded in assembly language, including operating systems as well as various application software for computers ranging from supercomputers to embedded systems. It is distinct from C++ (which has its roots in C) and C#, and many later languages have borrowed directly or indirectly from C.