Questions tagged as 'c'

3
answers

How does serial communication work and how to do it using C / C ++?

I use Debian 7.1 and I need to do serial communication between a computer and a microcontroller using C / C ++ . I have already done a lot of research on the subject, but the articles and examples I found are very confusing, unclear and not v...
asked by 20.03.2014 / 17:05
2
answers

strcpy is merging numeric format with other chars

I do not know if I could make myself understood in the title, but when I use strcpy() to copy a char* to another when I put a format so "teste" it works normally, but when I put a string format 3 letters (digits in case),...
asked by 02.06.2018 / 23:47
3
answers

How to store any string in C?

How do I store a string ) without first knowing the size of this string , for example: #include<stdio.h> int main (){ char nome[]; /* a array não tem tamanho determinado porque o input ainda não foi dado */ printf...
asked by 14.01.2017 / 14:30
2
answers

How to read the command line?

Let's say I want to do a routine to automate the process of compiling a program. In general (regardless of language), you call the compiler from the command line. For example:    javac myProgram.class or    gcc myProgram.c -o myProgram...
asked by 20.07.2016 / 17:45
1
answer

___ ___ erkimt Is there a difference between using "return" or "exit ()" to end the "main ()"? ______ qstntxt ___

%code% terminates running the application immediately. The %code% command exits the function, but when it is inside the %code% it will exit the application, including the value used in it will be returned to the one who called the application as an error code (zero is ok), as well as the function output.

Of course there is a difference in other functions, where %code% terminates application immediately and the command will return the execution stream to the calling function.

If both do the same thing in the described situation, does either use one or the other?

    
______ azszpr174313 ___

There is an important semantic difference in C ++. The %code% will close the scope and call all pending destructors. If the application is closed, rarely will the call of the destructors produce a different result, but technically it is possible for one of them to do something that is important to the end of the application, or to print relevant information for the user. >

Even though the action looks the same, the %code% function causes a premature exiting of the application. Then execution is terminated almost immediately. %code% finalizes static objects, but not destructors. The %code% and %code% neither does this and it closes on time.

There's a difference even in C if you have functions registered with %code% . These functions will always run no matter how the application is shutting down. But there will be undefined behavior if one of the linked functions in %code% has reference to some data in stack . Something like %code% and %code% . This is especially important in C. documentation

Note that the %code% function is not special and can be called within the application. In this case there will be an important difference since a %code% will not terminate the application, although in the context of the question this would not happen. But understand that if what you wrote will not be called in the operating system and this %code% is part of a module that will be loaded and used in another application, your module will terminate the application and not only your code if you use %code% , %code% will then be the most appropriate in most situations.

A call to %code% can be recursive, although it should not. Of course there is difference in this case, but it falls into what I said in the previous paragraph.

That's why you always have to choose the most semantically correct way . If you want to terminate the application immediately use %code% , otherwise use %code% . Many people say that if %code% is terminating the application, %code% is the most appropriate. Not everyone agrees, especially in C ++. That's why it's good to understand the workings, the implications of each and do not blindly follow rules.

I do not know if it counts as a difference, because it is a function, to use it, you need to use a %code% so that it is available, and if the code unit it contains would not enter the application, the executable will be a bit bigger. The command is always available.

    
___

exit() terminates running the application immediately. The return command exits the function, but when it is inside the main() it will exit the application, including the value used in it will be returned to the one who ca...
asked by 30.12.2016 / 13:15
1
answer

What is the use of * in the expression "Foo * foo = new Foo" in C ++? [duplicate]

I was analyzing this question made in SOEN. There you are teaching to instantiate a particular class. I was able to understand more or less how it works, because when I use my example, it is giving error when compiling. class Pessoa {...
asked by 05.01.2016 / 15:50
2
answers

How to limit decimals?

I have a question, can you limit the number of decimal places in C ++? float x = 2.958; Instead of rounding up or down using floorf , roundf , can you just pick up the two numbers after the comma? That would look more or less...
asked by 26.10.2015 / 12:27
2
answers

Calculate Body Mass Index in C

I'm doing a simple C program to calculate body mass index. However, it is returning a different (wrong) value than a regular calculator. #include <stdio.h> int main() { float height, weight, imc; printf("Hello World!\nWhat's yo...
asked by 02.10.2015 / 17:10
4
answers

Problem in checking prime numbers

I have to create a vector of random numbers in a range of 0 to 250, and show which cousins are. This is my code: #include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { int i,j,n[10]; srand(time(NU...
asked by 19.12.2014 / 01:01
2
answers

Computer Buffer

I have read in several places about buffers and there they say that it is a place in memory to store temporary values so they give an example like this in C: char exemplo[10]; and they say that this is a buffer , but the exemplo v...
asked by 07.04.2015 / 02:32